Testing webhooks | laravel-webhook-server | Spatie       

 SPATIE  

==

spatie.be/open-source

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

 Version   v4      

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

  Testing webhooks    
- [ Introduction ](https://spatie.be/docs/laravel-webhook-server/v4/introduction)
- [ Installation and setup ](https://spatie.be/docs/laravel-webhook-server/v4/installation-and-setup)
- [ Sending webhooks ](https://spatie.be/docs/laravel-webhook-server/v4/sending-webhooks)
- [ Signing requests ](https://spatie.be/docs/laravel-webhook-server/v4/signing-requests)
- [ Retrying failed webhooks ](https://spatie.be/docs/laravel-webhook-server/v4/retrying-failed-webhooks)
- [ Transport options ](https://spatie.be/docs/laravel-webhook-server/v4/transport-options)
- [ Events ](https://spatie.be/docs/laravel-webhook-server/v4/events)
- [ Testing webhooks ](https://spatie.be/docs/laravel-webhook-server/v4/testing-webhooks)
- [ Upgrading from v3 ](https://spatie.be/docs/laravel-webhook-server/v4/upgrading)

 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:

```
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
use Spatie\WebhookServer\WebhookCall;

it('sends the order webhook', function () {
    Http::preventStrayRequests();
    Http::fake([
        'https://other-app.com/webhooks' => Http::response('', 204),
    ]);

    WebhookCall::create()
        ->url('https://other-app.com/webhooks')
        ->payload(['order_id' => 123])
        ->useSecret('your-shared-secret')
        ->dispatchSync();

    Http::assertSent(fn (Request $request) => $request->url() === 'https://other-app.com/webhooks'
        && $request->method() === 'POST'
        && $request->body() === '{"order_id":123}'
        && $request->hasHeader('Signature', hash_hmac('sha256', '{"order_id":123}', 'your-shared-secret')));
});
```

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

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

Use `Bus::fake()` or `Queue::fake()` when you only need to assert that your application scheduled a webhook. This avoids sending requests:

```
use Illuminate\Support\Facades\Queue;
use Spatie\WebhookServer\CallWebhookJob;
use Spatie\WebhookServer\WebhookCall;

Queue::fake();

WebhookCall::create()
    ->url('https://other-app.com/webhooks')
    ->payload(['order_id' => 123])
    ->useSecret('your-shared-secret')
    ->dispatch();

Queue::assertPushed(CallWebhookJob::class);
```

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

Run `composer test`, `composer analyse`, and `composer format-test`. Static analysis uses Larastan and PHPStan level 10 without a baseline. Use `composer format` to apply the formatting rules.

 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)
