Hi,
I have multiple API versions and each version have a dedicated route file, whenever API is called with a client it provides a specific API version in headers which is being retrieved in RouteServiceProvider. The problem is that when I am performing a test the request doesn't have the custom headers which I am providing with the JSON call.
Below is the test case code.
$response = $this->postJson('/api/user/team/1/slideshow', [
'topic' => 'test topic',
'content' => 'test content',
'media' => 'test media',
], [
'Accept-Version' => 1.3,
'Authorization' => 'Bearer '.'asdfasdfasdf',
'Content-Type' => 'application/json',
'Accept' => 'application/json'
]);
$response->assertStatus(200);
and this is RouteServiceProvider code
Route::prefix('api')
->middleware('api')
->namespace($this->apiNamespace)
->group(function(){
$apiVersion = Request::header('Accept-version');
if($apiVersion){ // if the api version is set
$minApiVersion = config('app.min_api_verion_support');
if($minApiVersion > $apiVersion){ // check if api version is obsoleted
throw APIVersion::obsoleted($apiVersion);
}
Request::merge(['api_version' => $apiVersion]); // set api version to request object
$apiVersionNamespace = 'v' . str_replace('.','_',$apiVersion); // generate api version namespace
Route::namespace($apiVersionNamespace)->group(base_path("routes/api_v{$apiVersion}.php"));
}else{ // if api version is not set then use the default api version
Route::namespace('')->group(base_path("routes/api.php"));
}
});
I would like also add there, as there is no Accept-Version find in request headers so it reads the default api.php file. and there is a surpizing output when i write this code here.
dump(request()->headers);
Route::post('/user/team/1/slideshow', function(){
dump(request()->headers);
return 'request handled';
});
In the output when dump function is called without route it shows different headers and in Route callback there are different headers
Symfony\Component\HttpFoundation\HeaderBag^ {#347
#headers: array:5 [
"host" => array:1 [
0 => "demo.test"
]
"user-agent" => array:1 [
0 => "Symfony"
]
"accept" => array:1 [
0 => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
]
"accept-language" => array:1 [
0 => "en-us,en;q=0.5"
]
"accept-charset" => array:1 [
0 => "ISO-8859-1,utf-8;q=0.7,*;q=0.7"
]
]
#cacheControl: []
}
Symfony\Component\HttpFoundation\HeaderBag^ {#818
#headers: array:9 [
"host" => array:1 [
0 => "demo.test"
]
"user-agent" => array:1 [
0 => "Symfony"
]
"accept" => array:1 [
0 => "application/json"
]
"accept-language" => array:1 [
0 => "en-us,en;q=0.5"
]
"accept-charset" => array:1 [
0 => "ISO-8859-1,utf-8;q=0.7,*;q=0.7"
]
"content-length" => array:1 [
0 => 88
]
"content-type" => array:1 [
0 => "application/json"
]
"accept-version" => array:1 [
0 => 1.3
]
"authorization" => array:1 [
0 => "Bearer asdfasdfasdf"
]
]
#cacheControl: []
}