HTTP test helpers keep headers for every later request in the same test, including withToken()
- Subject
- composer · laravel/framework
- Affected versions
- ^11.0 || ^12.0 || ^13.0
- Category
- Undocumented Behaviour
- First seen
- 2026-10-13
- Repository
- laravel/framework
Observed behavior
In a feature test, withHeaders(), withHeader(), withToken() and withBasicAuth() do not apply to just the next request. They write to the test case's defaultHeaders property, and every later get()/post()/json() call in the same test merges those headers in until flushHeaders() or withoutToken() is called. A test that first makes one authorized request as a positive control, then asserts that a request with no bearer is refused, actually sends the earlier bearer on the "missing bearer" request: the refusal assertion receives 200 instead of 401 and the test can't fail for the reason it was written. The suite stayed green, and several reviews missed it, until a positive control was added ahead of the refusal cases.
What the documentation says
The HTTP tests docs ("Customizing Request Headers") show withHeaders() chained onto a single request, which reads as per-request. They don't say the headers persist across later requests in the test, and they don't mention flushHeaders().
Documentation URL
https://laravel.com/docs/13.x/http-tests#customizing-request-headersEvidence
Public source, v11.0.0 through v13.35.0 (latest): src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php declares `protected $defaultHeaders = [];`. withHeaders() does `$this->defaultHeaders = array_merge($this->defaultHeaders, $headers);`, withToken() calls withHeader('Authorization', ...), which sets `$this->defaultHeaders[$name] = $value;`, and transformHeadersToServerVars() merges defaultHeaders into every request. flushHeaders() resets `$this->defaultHeaders = [];`. Docs: laravel/docs http-tests.md (13.x), "Customizing Request Headers". Workaround: call flushHeaders() (or withoutToken()) after any authorized request in a test that also asserts refusals, or put the positive control in its own test.
Declarative probe
{
"ref": "v{version}",
"kind": "source_at_version",
"path": "src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php",
"assertion": {
"type": "contains",
"value": "$this->defaultHeaders = array_merge($this->defaultHeaders, $headers);"
},
"repository": "laravel/framework"
}References
Links
- No related links yet.
Related issues
- No related issues found yet.
Related pull requests
- No related pull requests found yet.
Confirmations
0
Published
2026-10-13
Last confirmed
Not yet confirmed
Freshness
Current
Activity
- No one has verified, debunked, or reproduced this yet.