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

Allow more status codes #334

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 2 additions & 2 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ private function assertStatusCodeIsInteger($statusCode)

private function assertStatusCodeRange(int $statusCode)
{
if ($statusCode < 100 || $statusCode >= 600) {
throw new \InvalidArgumentException('Status code must be an integer value between 1xx and 5xx.');
if ($statusCode < 100 || $statusCode > 999) {
throw new \InvalidArgumentException('Status code must be an integer value between 1xx and 9xx.');
}
}
}
46 changes: 35 additions & 11 deletions tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,32 @@ public function testDefaultConstructor()
self::assertSame('', (string) $r->getBody());
}

public function testCanConstructWithStatusCode()
/**
* @dataProvider statusCodeProvider
*/
public function testCanConstructWithStatusCode(int $statusCode, string $reasonPhrase = '')
{
$r = new Response(404);
self::assertSame(404, $r->getStatusCode());
self::assertSame('Not Found', $r->getReasonPhrase());
$r = new Response($statusCode);
self::assertSame($statusCode, $r->getStatusCode());
self::assertSame($reasonPhrase, $r->getReasonPhrase());
}

/**
* Although some of the status codes below seem invalid a status code can be above
*/
public function statusCodeProvider(): array
{
return [
[100, 'Continue'],
[200, 'OK'],
[201, 'Created'],
[401, 'Unauthorized'],
[404, 'Not Found'],
[500, 'Internal Server Error'],
[503, 'Service Unavailable'],
[600],
[999],
];
}

public function testConstructorDoesNotReadStreamBody()
Expand Down Expand Up @@ -110,11 +131,14 @@ public function testCanConstructWithProtocolVersion()
self::assertSame('1000', $r->getProtocolVersion());
}

public function testWithStatusCodeAndNoReason()
/**
* @dataProvider statusCodeProvider
*/
public function testWithStatusCodeAndNoReason(int $statusCode, string $reasonPhrase = '')
{
$r = (new Response())->withStatus(201);
self::assertSame(201, $r->getStatusCode());
self::assertSame('Created', $r->getReasonPhrase());
$r = (new Response())->withStatus($statusCode);
self::assertSame($statusCode, $r->getStatusCode());
self::assertSame($reasonPhrase, $r->getReasonPhrase());
}

public function testWithStatusCodeAndReason()
Expand Down Expand Up @@ -356,7 +380,7 @@ public function nonIntegerStatusCodeProvider()
public function testConstructResponseWithInvalidRangeStatusCode($invalidValues)
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Status code must be an integer value between 1xx and 5xx.');
$this->expectExceptionMessage('Status code must be an integer value between 1xx and 9xx.');
new Response($invalidValues);
}

Expand All @@ -369,14 +393,14 @@ public function testResponseChangeStatusCodeWithWithInvalidRange($invalidValues)
{
$response = new Response();
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Status code must be an integer value between 1xx and 5xx.');
$this->expectExceptionMessage('Status code must be an integer value between 1xx and 9xx.');
$response->withStatus($invalidValues);
}

public function invalidStatusCodeRangeProvider()
{
return [
[600],
[1],
[99],
];
}
Expand Down