Hi folks,
I am currently struggling with setting headers in my Laravel test cases. I am adding custom headers, but when debugging the request from within my application, they are not set at all:
<?php
namespace Tests\Feature\API;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class SearchTest extends TestCase
{
use RefreshDatabase;
/**
* Search for my custom value with app starting with version 2.1.0.
*
* @return void
*/
public function test_search_for_my_custom_value_with_app_from_v2_1_0()
{
$this->seed();
$user = User::factory()->create();
$params = [
'custom_array' => ['custom_value']
];
$headers = [
'X-App-Version' => '2.1.0 b1'
];
$response = $this->actingAs($user)->postJson('/api/user/search', $params, $headers);
}
}
When debugging either request()->headers or getallheaders() my custom header isn't included:
object(Symfony\Component\HttpFoundation\HeaderBag)#5205 (2) {
["headers":protected]=>
array(5) {
["host"]=>
array(1) {
[0]=>
string(17) "domain.com"
}
["user-agent"]=>
array(1) {
[0]=>
string(7) "Symfony"
}
["accept"]=>
array(1) {
[0]=>
string(63) "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
}
["accept-language"]=>
array(1) {
[0]=>
string(14) "en-us,en;q=0.5"
}
["accept-charset"]=>
array(1) {
[0]=>
string(30) "ISO-8859-1,utf-8;q=0.7,*;q=0.7"
}
}
["cacheControl":protected]=>
array(0) {
}
When calling the same endpoint from my mobile application, the headers are correct. Also, as you can see, the accept header is wrong. Somehow the request headers don't arrive at all.
As far as I can see there is no redirect involved or something. My REST debugger is advised to not follow redirects just to make sure I'm debugging correctly.
Does anyone have an idea what I am missing here?
edit: I also tried using withHeaders() instead of passing it as parameter to postJson().