Testing webhooks | laravel-webhook-server-go | Spatie       

 SPATIE  

laravel-webhook-server-go
=========================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-webhook-server-go](https://spatie.be/docs/laravel-webhook-server-go/v1)  Testing webhooks

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/laravel-webhook-server-go/v1) 

  Testing webhooks    
- [ Introduction ](https://spatie.be/docs/laravel-webhook-server-go/v1/introduction)
- [ Installation and setup ](https://spatie.be/docs/laravel-webhook-server-go/v1/installation-and-setup)
- [ Sending webhooks ](https://spatie.be/docs/laravel-webhook-server-go/v1/sending-webhooks)
- [ Signing requests ](https://spatie.be/docs/laravel-webhook-server-go/v1/signing-requests)
- [ Retrying failed webhooks ](https://spatie.be/docs/laravel-webhook-server-go/v1/retrying-failed-webhooks)
- [ Transport options ](https://spatie.be/docs/laravel-webhook-server-go/v1/transport-options)
- [ Events ](https://spatie.be/docs/laravel-webhook-server-go/v1/events)
- [ Testing webhooks ](https://spatie.be/docs/laravel-webhook-server-go/v1/testing-webhooks)
- [ Differences from PHP ](https://spatie.be/docs/laravel-webhook-server-go/v1/upgrading)
- [ Testing ](https://spatie.be/docs/laravel-webhook-server-go/v1/testing)
- [ Releasing ](https://spatie.be/docs/laravel-webhook-server-go/v1/releasing)
- [ Changelog ](https://spatie.be/docs/laravel-webhook-server-go/v1/changelog)
- [ Support us ](https://spatie.be/docs/laravel-webhook-server-go/v1/support-us)

 Testing webhooks
================

###  On this page 

1. [ Assert HTTP requests ](#content-assert-http-requests)
2. [ Assert dispatch without executing the job ](#content-assert-dispatch-without-executing-the-job)
3. [ Contributing to the package ](#content-contributing-to-the-package)

Assert HTTP requests
--------------------------------------------------------------------------------------------------------------------

Webhooks use Laravel's HTTP client. Fake the endpoint and dispatch synchronously to inspect a request without making a network call:

```
func TestSendsOrderWebhook(t *testing.T) {
    client := laraveltest.New(t, laravel.Options{
        Packages: []laravel.Package{webhookserver.New()},
    })
    endpoint := httpclienttest.New(client).PreventStrayRequests().
        Respond("other-app.com/webhooks", httpclienttest.Status(204))

    err := webhookserver.From(client.Context()).Call("https://other-app.com/webhooks").
        Payload(map[string]int{"order_id": 123}).Secret("your-shared-secret").
        DispatchSync(client.Context())
    if err != nil {
        t.Fatal(err)
    }

    endpoint.AssertSent(func(req *httpclienttest.Request) bool {
        return req.Method == "POST" && string(req.Body) == `{"order_id":123}` && req.Header.Get("Signature") != ""
    })
}
```

For queued dispatch, run the job or queue worker in the same application before asserting on the HTTP request. HTTP fakes are not shared with a separate worker process.

Assert dispatch without executing the job
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Use Laravel's queue fake when you only need to assert that your application scheduled a webhook:

```
queued := queuetest.New(client)
_, err := webhookserver.From(client.Context()).Call("https://other-app.com/webhooks").
    Payload(map[string]int{"order_id": 123}).Secret("your-shared-secret").Dispatch(client.Context())
if err != nil {
    t.Fatal(err)
}
queued.AssertPushed[webhookserver.CallWebhookJob]()
```

To test retry policy, choose a non-sync queue connection with `laraveltest.Queue("database")`, prepare a call and run `client.Attempt(webhookserver.CallWebhookJob{Delivery: delivery})`. Register the host's database driver and queue-table migration. The sync connection intentionally stops after one attempt.

Contributing to the package
-----------------------------------------------------------------------------------------------------------------------------------------

Run `go test -race ./...`, `go vet ./...`, `go mod tidy -diff` and `go mod verify`. Format code with `gofmt -w .`.

The package tests use isolated Laravel applications, real SQLite-backed queue workers, host routes, HTTP fakes and local HTTP/TLS servers. They cover retry boundaries, signed bytes, customization, timestamps, cancellation, raw bodies and queued listener serialization.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
