Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set status to 302 if Location header set #2345

Merged
merged 3 commits into from
Nov 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Slim/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,34 @@ public function getReasonPhrase()
return '';
}

/*******************************************************************************
* Headers
******************************************************************************/

/**
* Return an instance with the provided value replacing the specified header.
*
* If a Location header is set and the status code is 200, then set the status
* code to 302 to mimic what PHP does. See https://github.com/slimphp/Slim/issues/1730
*
* @param string $name Case-insensitive header field name.
* @param string|string[] $value Header value(s).
* @return static
* @throws \InvalidArgumentException for invalid header names or values.
*/
public function withHeader($name, $value)
{
$clone = clone $this;
$clone->headers->set($name, $value);

if ($clone->getStatusCode() === 200 && strtolower($name) === 'location') {
$clone = $clone->withStatus(302);
}

return $clone;
}


/*******************************************************************************
* Body
******************************************************************************/
Expand Down
16 changes: 16 additions & 0 deletions tests/Http/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,4 +337,20 @@ public function testWithInvalidJsonThrowsException()
// must have been caught earlier by the test framework
$this->assertFalse(true);
}

public function testStatusIsSetTo302IfLocationIsSetWhenStatusis200()
{
$response = new Response();
$response = $response->withHeader('Location', '/foo');

$this->assertSame(302, $response->getStatusCode());
}

public function testStatusIsNotSetTo302IfLocationIsSetWhenStatusisNot200()
{
$response = new Response();
$response = $response->withStatus(201)->withHeader('Location', '/foo');

$this->assertSame(201, $response->getStatusCode());
}
}