-1

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[*]');
    }
Naktibalda
  • 13,705
  • 5
  • 35
  • 51
Alladin
  • 1,010
  • 3
  • 27
  • 45
  • 2
    Don't get it why you called twice in the same test function with same route and request. – RBC Jun 01 '22 at 04:55
  • @RBC it's a long story actually.. but technically the first request will do something to the data, so if i pass the second request, i expect some changes and i wanted to assert those as well – Alladin Jun 01 '22 at 07:40
  • Have you tried putting a `sleep(1)` between the two request calls? – Donkarnash Jun 01 '22 at 09:56
  • @Alladin you NEVER test 2 things on the same test... if you want to "send the data and check if it was modified" you create a new test and setup the existing data and run the API call and then assert... check out my profile, you will have good examples in there! – matiaslauriti Jun 01 '22 at 13:40
  • @matiaslauriti thanks for your feedback.. however, is there any reason why would this fail? i already took a different approach but can't figure out why it's failing – Alladin Jun 01 '22 at 14:26
  • @Donkarnash unfortunately it didn't work.. but i took a difference approach and split the tests now – Alladin Jun 01 '22 at 14:27
  • @Alladin no idea why, you could try sharing your controller, route and middleware being executed – matiaslauriti Jun 01 '22 at 15:06

1 Answers1

1

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);
Naktibalda
  • 13,705
  • 5
  • 35
  • 51