Search code examples
codeception

php laravel api test always returns 404 in the second call after a first successful call


I am making an API test function that includes sending a post request twice in the same test.

the first request is successful and has no problem, and i can assert the response without any issue. however, in the second request, the route is not found and the response returns 404 error right away. even though i use the same request.

here is how the test function looks like:

public function tesAbc(\ApiTester $I)
    {
        $I->haveHttpHeader('Accept', 'application/json');
        $request = [
            'username' => 'abc',
            'password' => 'testABc',
            'data' => [
                'param1' => '123',
                'param2' => '456'
            ]
        ];

        /* the first request, response always 200 */
        $I->sendPOST('confirmation/slot', $request);

        $I->seeResponseCodeIs(200);
        $I->seeResponseIsJson();
       

        /* the second request, response always 404 */
        $I->sendPOST('confirmation/slot', $request);

        $I->seeResponseCodeIs(200);
        $I->seeResponseIsJson();
        $I->dontSeeResponseJsonMatchesJsonPath('$.data[*]');
    }

Solution

  • The problem is that you used relative URI confirmation/slot.

    The first request goes to /confirmation/slot then second request is relative to that and it goes to `/confirmation/confirmation/slot.

    Solution is simple, use leading / in URI:

    $I->sendPOST('/confirmation/slot', $request);