|
16 | 16 |
|
17 | 17 | class ResponseTest extends TestCase
|
18 | 18 | {
|
19 |
| - public function testResponseFactoryFromStringCreatesValidResponse() |
| 19 | + public function validHttpVersions() |
20 | 20 | {
|
21 |
| - $string = 'HTTP/1.0 200 OK' . "\r\n\r\n" . 'Foo Bar'; |
22 |
| - $response = Response::fromString($string); |
23 |
| - $this->assertEquals(200, $response->getStatusCode()); |
24 |
| - $this->assertEquals('Foo Bar', $response->getContent()); |
| 21 | + yield 'http/1.0' => ['1.0']; |
| 22 | + yield 'http/1.1' => ['1.1']; |
| 23 | + yield 'http/2' => ['2']; |
| 24 | + } |
| 25 | + |
| 26 | + public function validResponseHttpVersionProvider() |
| 27 | + { |
| 28 | + $responseTemplate = "HTTP/%s 200 OK\r\n\r\nFoo Bar"; |
| 29 | + foreach ($this->validHttpVersions() as $testCase => $data) { |
| 30 | + $version = array_shift($data); |
| 31 | + yield $testCase => [ |
| 32 | + 'response' => sprintf($responseTemplate, $version), |
| 33 | + 'expectedVersion' => $version, |
| 34 | + 'expectedStatus' => '200', |
| 35 | + 'expectedContent' => 'Foo Bar', |
| 36 | + ]; |
| 37 | + } |
| 38 | + } |
25 | 39 |
|
26 |
| - $string = 'HTTP/2 200 OK' . "\r\n\r\n" . 'Foo Bar'; |
| 40 | + /** |
| 41 | + * @dataProvider validResponseHttpVersionProvider |
| 42 | + * @param string $string Response string |
| 43 | + * @param string $expectedVersion |
| 44 | + * @param string $expectedStatus |
| 45 | + * @param string $expectedContent |
| 46 | + */ |
| 47 | + public function testResponseFactoryFromStringCreatesValidResponse( |
| 48 | + $string, |
| 49 | + $expectedVersion, |
| 50 | + $expectedStatus, |
| 51 | + $expectedContent |
| 52 | + ) { |
27 | 53 | $response = Response::fromString($string);
|
28 |
| - $this->assertEquals('2', $response->getVersion()); |
| 54 | + $this->assertEquals($expectedVersion, $response->getVersion()); |
| 55 | + $this->assertEquals($expectedStatus, $response->getStatusCode()); |
| 56 | + $this->assertEquals($expectedContent, $response->getContent()); |
29 | 57 | }
|
30 | 58 |
|
31 |
| - public function testResponseCanRenderStatusLine() |
| 59 | + /** |
| 60 | + * @dataProvider validHttpVersions |
| 61 | + * @param string $version |
| 62 | + */ |
| 63 | + public function testResponseCanRenderStatusLineUsingDefaultReasonPhrase($version) |
32 | 64 | {
|
| 65 | + $expected = sprintf('HTTP/%s 404 Not Found', $version); |
33 | 66 | $response = new Response();
|
34 |
| - $response->setVersion('1.1'); |
| 67 | + $response->setVersion($version); |
35 | 68 | $response->setStatusCode(Response::STATUS_CODE_404);
|
36 |
| - $this->assertEquals('HTTP/1.1 404 Not Found', $response->renderStatusLine()); |
| 69 | + $this->assertEquals($expected, $response->renderStatusLine()); |
| 70 | + } |
37 | 71 |
|
| 72 | + /** |
| 73 | + * @dataProvider validHttpVersions |
| 74 | + * @param string $version |
| 75 | + */ |
| 76 | + public function testResponseCanRenderStatusLineUsingCustomReasonPhrase($version) |
| 77 | + { |
| 78 | + $expected = sprintf('HTTP/%s 404 Foo Bar', $version); |
| 79 | + $response = new Response(); |
| 80 | + $response->setVersion($version); |
| 81 | + $response->setStatusCode(Response::STATUS_CODE_404); |
38 | 82 | $response->setReasonPhrase('Foo Bar');
|
39 |
| - $this->assertEquals('HTTP/1.1 404 Foo Bar', $response->renderStatusLine()); |
40 |
| - |
41 |
| - $response->setVersion('2'); |
42 |
| - $this->assertEquals('HTTP/2 404 Foo Bar', $response->renderStatusLine()); |
| 83 | + $this->assertEquals($expected, $response->renderStatusLine()); |
43 | 84 | }
|
44 | 85 |
|
45 |
| - public function testInvalidHTTPVersion() |
| 86 | + public function testInvalidHTTP2VersionString() |
46 | 87 | {
|
47 | 88 | $this->expectException(InvalidArgumentException::class);
|
48 | 89 | $this->expectExceptionMessage('A valid response status line was not found in the provided string');
|
|
0 commit comments