Welcome Guest, Not a member yet? Register   Sign In
Failed asserting that 302 is identical to 200
#1

(This post was last modified: 04-18-2022, 01:19 PM by Mobostar.)

Hello,
I am working on this personal project just to learn CI4 and Myth Auth.
I am getting the PHPUnit failure message you see in the topic line. "Failed asserting that 302 is identical to 200." when I run a PHPUnit test to test HTTP GET request.

This is the code that generates that:
Code:
      $response = $this->get('list');
      $response->assertStatus(200);

So, it seems the Myth Auth, which is in the middle authenticating and redirecting, is the reason this is happening. I bypassed Auth and the failure went away.

How does one go about testing HTTP requests in these situations? I mean without bypassing Auth every time.

Thanks,
Reply
#2

See https://codeigniter4.github.io/userguide...ion-values
Reply
#3

You can simulate a logged in user with the AuthTestTrait. It would be something like this, assuming you're using the Trait on the test class.:

PHP Code:
$user $this->createAuthUser();

$response $this->get('list');
$response->assertStatus(200); 

This will create a new user and log them in so when you attempt the get() call you would pass the checks.
Reply
#4

(04-18-2022, 08:07 PM)kilishan Wrote: You can simulate a logged in user with the AuthTestTrait. It would be something like this, assuming you're using the Trait on the test class.:

PHP Code:
$user $this->createAuthUser();

$response $this->get('list');
$response->assertStatus(200); 

This will create a new user and log them in so when you attempt the get() call you would pass the checks.

Yes, this worked.

kenjis' link looks like that could be another way of doing it. Good to know.

Thanks
Reply
#5

Excellent! Glad it worked for you.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB