Webhook Testing Toolkit for Laravel: A Signed-Request Helper and curl Recipes
A webhook endpoint that verifies signatures is easy to get half right: it accepts your own test requests and rejects the real ones, or the other way round. The fix is to test the unhappy paths on purpose. This cheat sheet gives you a small set of tools for that: a helper that signs requests inside your Laravel tests, and a shell script that sends valid, tampered, expired and duplicate requests to a running server.
What this is, and is not
These helpers go with the signature scheme from my guide on building a Laravel webhook receiver (a header of the form t=<timestamp>,v1=<hmac>, signed over <timestamp>.<raw body>). Real providers differ in header name, signed string and encoding, so adapt the signing function and keep everything else. I ran every command below against a Laravel 13 test application, and the status codes shown are the ones it returned. The built-in development server may handle requests one at a time, so a burst test there shows the duplicate path working. It does not prove behaviour under real concurrency.
1. A signed-request helper for Pest
Two functions: one builds the signature header, the other sends a request with an exact raw body. Passing the body as a string, not an array, is what makes the signature test honest.
function signature(string $body, string $secret = 'whsec_current', ?int $timestamp = null): string
{
$t = $timestamp ?? time();
return "t={$t},v1=".hash_hmac('sha256', "{$t}.{$body}", $secret);
}
function deliver(string $body, ?string $signature)
{
$server = ['CONTENT_TYPE' => 'application/json', 'HTTP_ACCEPT' => 'application/json'];
if ($signature !== null) {
$server['HTTP_ACME_SIGNATURE'] = $signature;
}
return test()->call('POST', '/api/webhooks/acme', [], [], [], $server, $body);
}
Use them like this:
$body = json_encode(['id' => 'evt_1', 'type' => 'invoice.paid']);
deliver($body, signature($body))->assertOk(); // valid
deliver($body, signature($body, 'whsec_wrong'))->assertUnauthorized(); // wrong secret
deliver($body, signature($body, 'whsec_current', time() - 3600))->assertUnauthorized(); // too old
deliver($body.' ', signature($body))->assertUnauthorized(); // body changed after signing
2. A shell script for a running server
This script signs with openssl and sends with curl, so you can test a local, staging or tunnelled endpoint without writing PHP. Set URL, SECRET and HEADER for your endpoint. Keep the secret out of your shell history by putting it in an environment variable you read from a file, not typing it into the command.
#!/usr/bin/env bash
# Usage: source webhook-test.sh; then call the functions below.
URL="${URL:-http://127.0.0.1:8002/api/webhooks/acme}"
SECRET="${SECRET:-whsec_lab_only_test}"
HEADER="${HEADER:-Acme-Signature}"
# sign <timestamp> <raw body> -> hex HMAC-SHA256 of "<timestamp>.<raw body>"
sign() { printf '%s' "$1.$2" | openssl dgst -sha256 -hmac "$SECRET" | awk '{print $2}'; }
# send <body> [timestamp-offset-seconds] [secret-override] -> prints the HTTP status
send() {
local body="$1" offset="${2:-0}" ts sig
ts=$(( $(date +%s) + offset ))
sig=$(SECRET="${3:-$SECRET}" sign "$ts" "$body")
curl -s -o /dev/null -w '%{http_code}\n' -X POST "$URL" \
-H 'Content-Type: application/json' -H 'Accept: application/json' \
-H "$HEADER: t=$ts,v1=$sig" --data-binary "$body"
}
valid() { send "$1"; } # 200 expected
expired() { send "$1" -4000; } # 401 expected (older than the tolerance)
future() { send "$1" 4000; } # 401 expected (too far ahead)
wrongkey() { send "$1" 0 whsec_wrong; } # 401 expected
tampered() { # 401 expected: sign one body, send another
local ts sig; ts=$(date +%s); sig=$(sign "$ts" "$1")
curl -s -o /dev/null -w '%{http_code}\n' -X POST "$URL" -H 'Content-Type: application/json' \
-H "$HEADER: t=$ts,v1=$sig" --data-binary "${1/invoice.paid/invoice.void}"
}
unsigned() { curl -s -o /dev/null -w '%{http_code}\n' -X POST "$URL" -H 'Content-Type: application/json' -d "$1"; } # 401 expected
# burst <body> <count> -> send the same delivery <count> times at once and count the status codes
burst() { export -f send sign; export URL SECRET HEADER BODY="$1"; seq 1 "$2" | xargs -P "$2" -I{} bash -c 'send "$BODY"' | sort | uniq -c; }
What each command should return
These are the results I got against the test application, using the commands above:
| Command | Meaning | Status returned |
|---|
valid '{"id":"evt_t1","type":"invoice.paid"}' | Correctly signed, new event | 200 |
| The same command again | A duplicate delivery | 200 (acknowledged, stored once) |
expired | Signature is valid but the timestamp is 4,000 s old | 401 |
future | Timestamp 4,000 s in the future | 401 |
wrongkey | Signed with a different secret | 401 |
tampered | Body changed after signing | 401 |
unsigned | No signature header at all | 401 |
burst '{"id":"evt_burst",...}' 10 | Ten identical deliveries at once | ten 200 responses, one stored row |
Check the database as well as the status codes. After this run, the valid event and the burst event each had exactly one row, and none of the rejected requests had stored anything.
Things the toolkit cannot tell you
- It cannot tell you your provider's real format. Test against the provider's own test or "send test event" feature too.
- A burst on the development server is not a concurrency test. The database's unique index is what makes simultaneous duplicates safe. Review it directly.
- It tests your endpoint, not your network path. Proxies and firewalls in front of production can change the body or drop headers, so run a real delivery through the full path once.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|
| Everything returns 401, including your valid request | Secret or header name mismatch, or the clock differs by more than the tolerance | Print the header you send, check the secret has no trailing newline, and check both clocks |
| Valid requests pass in Pest and fail from curl | The body bytes differ: shell quoting, a trailing newline or encoding | Use --data-binary, and sign the exact string you send |
tampered returns 200 | The endpoint is verifying a re-encoded copy of the body | Verify $request->getContent() |
| A burst creates several rows | No unique index on the provider and event id | Add the unique index and catch the duplicate |
| Signatures never match although the secret is right | The openssl output format differs between versions | Run the openssl command by itself, see which field holds the hash, and adjust the awk field in sign() |
Frequently asked questions
Why sign the raw string and not an array?
Because the endpoint verifies the exact bytes it received. Encoding an array in the test can produce different bytes from the ones you signed.
Can I use this against production?
Do not. Test against a local or staging endpoint with a test secret. A signed test event against production is a real event.
Does the script work with other languages' servers?
Yes. It only sends HTTP. Adjust the header name and signing string to match the server.
How do I test provider-specific signatures?
Change the sign() function and the header format. Some providers sign only the body and encode the signature as base64.
Where this fits
These helpers come from the same work as my guide on building a signed Laravel webhook receiver. If you are connecting your application to payments, a store or messaging and want the endpoints tested properly, see API Integration & Custom Automation and Laravel & PHP Development, or describe your integration here. More reference sheets are collected under Cheat Sheets.