Phillipp's avatar

Can't test 403 responses

Hello,

this is my test:

public function testVisitPollsOverviewWithoutToken(){
        $this->visit('/api/polls')
            ->seeStatusCode(403)
            ->seeJsonEquals([
                'error' => [
                    'message' => 'Missing or incorrect api token'
                ],
            ]);
    }

Get that when i run phpunit:

A request to [http://localhost/api/polls] failed. Received status code [403].
0 likes
11 replies
bobbybouwmann's avatar

So only the seeStatusCode doesn't work? The seeJsonEquals does work?

Phillipp's avatar

Only one of these function doesn't work too because he handles the 403 by his own. Why the fuck is he interested in the statuscode by his own ...

bobbybouwmann's avatar

I have no clue why the test would do this! Can you show your code that you try to test so we can rule out it's not your code that causes this behaviour?

Phillipp's avatar

The response comes from an Exception Handler:

if ($e instanceof ApiTokenMisMatchException) {
            return response()->view('errors.json.api', ['message' => 'Missing or incorrect api token'], 403);
        }
Phillipp's avatar
Phillipp
OP
Best Answer
Level 3

Got the solution in the Laravel Slack chat from 'cody'.

I should use the 'get()' method instead of 'visit()'.

4 likes
bobbybouwmann's avatar

Oke cool, I was thinking that as well.. but I thought visit was just a wrapper around get() and post()

Phillipp's avatar

Yes but visit comes with a check if the response have a 200 statuscode -.-

Please or to participate in this conversation.