Xairo Martínez Xairo Martínez - 13 days ago 8
PHP Question

Laravel 5.2 unit test set headers not working

I have the following assert:

$this->call('GET', '/api/v1/ubication/suggest', [
'term' => 'a'
], [], [], ['Accept' => 'application/json']);

$this->assertResponseStatus(422);


The call method definition is as follow:

public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null) {

}


I'm passing my header in the $server position but i still get 302 status code in the response(I'm testing a Request validation) instead of 422 that is what i expect.

How can i simulate that header in the test?

Answer

If you want to make a GET call with Headers, you can use:

$this->get($url, $headers);

Example:

$this->get('/oauth', [ 'HTTP_X_API_KEY' => 'xxxx' ]);

Since you are making a GET request, your parameters can be included in the $url.

Source