mardi 10 juillet 2018

Laravel http response after successful creation

After making a POST request to create a new resource, it's common to redirect the browser to a URL for viewing the created resource. Laravel handles this by returning redirect($location) from the controller action.

This is all well and good, except that it makes testing a little bit awkward. It would be great to be able to test these responses with something like:

$this->post(...)->assertSuccessful();

But that specifically tests for an HTTP status code between 200 and 299. Instead, it seems you need to use:

$this->post(...)->assertRedirect($location);

There isn't really a problem with that, but it's not ideal, since it's really testing two different things: that the POST request was successful and that the response redirected the user to the created resource.

Is there a better way to handle this? I thought about using a 201 response with a Location header, but browsers don't seem to follow those.

Aucun commentaire:

Enregistrer un commentaire