Xairo Martínez
Xairo Martínez

Reputation: 65

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?

Upvotes: 2

Views: 2574

Answers (2)

CEJ
CEJ

Reputation: 181

You can still use

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

The reason it did not work is that behind the scenes Laravel filters out any header that does not start with "HTTP_"

So all you need to do is replace 'Accept' with 'HTTP_Accept'

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

Upvotes: 1

Niraj Shah
Niraj Shah

Reputation: 15457

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

Upvotes: 2

Related Questions