Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Laravel testing method assertRedirect($uri) and assertLocation($uri)?

I was reading Laravel document HTTP tests and a question occurred. I can't tell the difference between assertLocation($uri) and assertRedirect($uri), since both are for redirecting to specific uri.

Anyone could help would be so much appreciated.

like image 272
Ray Avatar asked Sep 17 '25 13:09

Ray


1 Answers

If we look functionality of both assertLocation($uri) would assert that the current location header matches the given URI.
But assertRedirect($uri) would assert whether the response is redirecting to a given URI.

I agree with example given by @apokryfos,
only 3xx responses are considered to be redirect responses while a 201 is not a redirect so assertLocation will pass if the response is 201 with a specified location while assertRedirect will not pass for 201 responses.


If we look code wise,
The assertRedirect() function also calls assertLocation() internally but it also checks using PHPUnit::assertTrue() that the response is redirected, if not then it will send a message

'Response status code [201] is not a redirect status code.', where 201 specifies the status code of response.

Checkout the assertRedirect() from github repo of framework

like image 125
bhucho Avatar answered Sep 19 '25 03:09

bhucho