From 7840f8068190a2e63704ac6591021286a79c22c0 Mon Sep 17 00:00:00 2001 From: Stefan Brauner Date: Thu, 14 Feb 2019 21:40:47 +0100 Subject: [PATCH 1/9] according to the type hint version has to be a string --- test/ResponseTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/ResponseTest.php b/test/ResponseTest.php index 4df72fb148..c3e66ef2c3 100644 --- a/test/ResponseTest.php +++ b/test/ResponseTest.php @@ -27,7 +27,7 @@ public function testResponseFactoryFromStringCreatesValidResponse() public function testResponseCanRenderStatusLine() { $response = new Response(); - $response->setVersion(1.1); + $response->setVersion('1.1'); $response->setStatusCode(Response::STATUS_CODE_404); $this->assertEquals('HTTP/1.1 404 Not Found', $response->renderStatusLine()); From 101756867390c695cf159f9cc8ec9e3f450573e7 Mon Sep 17 00:00:00 2001 From: Stefan Brauner Date: Thu, 14 Feb 2019 21:48:03 +0100 Subject: [PATCH 2/9] replaced 'Request' with 'Response' --- docs/book/response.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/book/response.md b/docs/book/response.md index af9e2aac44..5e6d0e8de7 100644 --- a/docs/book/response.md +++ b/docs/book/response.md @@ -77,8 +77,8 @@ Method signature | Descrip `renderStatusLine() : string` | Render the status line header `setHeaders(Headers $headers) : self` | Provide an alternate Parameter Container implementation for headers in this object. (This is NOT the primary API for value setting; for that, see `getHeaders()`.) `getHeaders() : Headers` | Return the container responsible for storing HTTP headers. This container exposes the primary API for manipulating headers set in the HTTP response. See the section on [Headers](headers.md) for more information. -`setVersion(string $version) : self` | Set the HTTP version for this object, one of 1.0 or 1.1 (`Request::VERSION_10`, `Request::VERSION_11`). -`getVersion() : string` | Return the HTTP version for this request. +`setVersion(string $version) : self` | Set the HTTP version for this object, one of 1.0 or 1.1 (`Response::VERSION_10`, `Response::VERSION_11`). +`getVersion() : string` | Return the HTTP version for this response. `setStatusCode(int $code) : self` | Set HTTP status code. `getStatusCode() : int` | Retrieve HTTP status code. `setReasonPhrase(string $reasonPhrase) : self` | Set custom HTTP status message. From 5611b962deff4201f5aaaacdca1c98c9d62f6a1a Mon Sep 17 00:00:00 2001 From: Stefan Brauner Date: Thu, 14 Feb 2019 22:59:51 +0100 Subject: [PATCH 3/9] Support for HTTP/2 --- docs/book/client/intro.md | 2 +- docs/book/request.md | 2 +- docs/book/response.md | 2 +- src/AbstractMessage.php | 9 +++++---- src/Response.php | 2 +- test/ResponseTest.php | 15 +++++++++++++++ 6 files changed, 24 insertions(+), 8 deletions(-) diff --git a/docs/book/client/intro.md b/docs/book/client/intro.md index 253da98836..ed728e5b08 100644 --- a/docs/book/client/intro.md +++ b/docs/book/client/intro.md @@ -77,7 +77,7 @@ Parameter | Description `strictredirects` | Whether to strictly follow the RFC when redirecting (see this section) | boolean | FALSE `useragent` | User agent identifier string (sent in request headers) | string | `Zend\Http\Client` `timeout` | Connection timeout (seconds) | integer | 10 -`httpversion` | HTTP protocol version (usually '1.1' or '1.0') | string | 1.1 +`httpversion` | HTTP protocol version (usually '1.1', '1.0' or '2.0') | string | 1.1 `adapter` | Connection adapter class to use (see this section) | mixed | `Zend\Http\Client\Adapter\Socket` `keepalive` | Whether to enable keep-alive connections with the server. Useful and might improve performance if several consecutive requests to the same server are performed. | boolean | FALSE `storeresponse` | Whether to store last response for later retrieval with getLastResponse(). If set to FALSE, getLastResponse() will return NULL. | boolean | TRUE diff --git a/docs/book/request.md b/docs/book/request.md index d26677471b..9db6a4adb1 100644 --- a/docs/book/request.md +++ b/docs/book/request.md @@ -75,7 +75,7 @@ Method signature | De `setUri(string|Uri $uri) : self` | Set the URI/URL for this request; this can be a string or an instance of `Zend\Uri\Http`. `getUri() : Uri` | Return the URI for this request object. `getUriString() : string` | Return the URI for this request object as a string. -`setVersion(string $version) : self` | Set the HTTP version for this object, one of 1.0 or 1.1 (`Request::VERSION_10`, `Request::VERSION_11`). +`setVersion(string $version) : self` | Set the HTTP version for this object, one of 1.0, 1.1 or 2.0 (`AbstractMessage::VERSION_10`, `AbstractMessage::VERSION_11`, `AbstractMessage::VERSION_20`). `getVersion() : string` | Return the HTTP version for this request. `setQuery(Parameters $query) : self` | Provide an alternate Parameter Container implementation for query parameters in this object. (This is NOT the primary API for value setting; for that, see `getQuery()`). `getQuery(string|null $name, mixed|null $default) : null|string|Parameters` | Return the parameter container responsible for query parameters or a single query parameter based on `$name`. diff --git a/docs/book/response.md b/docs/book/response.md index 5e6d0e8de7..c74db866a0 100644 --- a/docs/book/response.md +++ b/docs/book/response.md @@ -77,7 +77,7 @@ Method signature | Descrip `renderStatusLine() : string` | Render the status line header `setHeaders(Headers $headers) : self` | Provide an alternate Parameter Container implementation for headers in this object. (This is NOT the primary API for value setting; for that, see `getHeaders()`.) `getHeaders() : Headers` | Return the container responsible for storing HTTP headers. This container exposes the primary API for manipulating headers set in the HTTP response. See the section on [Headers](headers.md) for more information. -`setVersion(string $version) : self` | Set the HTTP version for this object, one of 1.0 or 1.1 (`Response::VERSION_10`, `Response::VERSION_11`). +`setVersion(string $version) : self` | Set the HTTP version for this object, one of 1.0, 1.1 or 2.0 (`Response::VERSION_10`, `Response::VERSION_11`, `Response::VERSION_20`). `getVersion() : string` | Return the HTTP version for this response. `setStatusCode(int $code) : self` | Set HTTP status code. `getStatusCode() : int` | Retrieve HTTP status code. diff --git a/src/AbstractMessage.php b/src/AbstractMessage.php index e09247a214..2c5724d03f 100644 --- a/src/AbstractMessage.php +++ b/src/AbstractMessage.php @@ -21,6 +21,7 @@ abstract class AbstractMessage extends Message */ const VERSION_10 = '1.0'; const VERSION_11 = '1.1'; + const VERSION_20 = '2.0'; /**#@-*/ /** @@ -34,16 +35,16 @@ abstract class AbstractMessage extends Message protected $headers; /** - * Set the HTTP version for this object, one of 1.0 or 1.1 - * (AbstractMessage::VERSION_10, AbstractMessage::VERSION_11) + * Set the HTTP version for this object, one of 1.0, 1.1 or 2.0 + * (AbstractMessage::VERSION_10, AbstractMessage::VERSION_11, AbstractMessage::VERSION_20) * - * @param string $version (Must be 1.0 or 1.1) + * @param string $version (Must be 1.0, 1.1 or 2.0) * @return AbstractMessage * @throws Exception\InvalidArgumentException */ public function setVersion($version) { - if ($version != self::VERSION_10 && $version != self::VERSION_11) { + if ($version != self::VERSION_10 && $version != self::VERSION_11 && $version != self::VERSION_20) { throw new Exception\InvalidArgumentException( 'Not valid or not supported HTTP version: ' . $version ); diff --git a/src/Response.php b/src/Response.php index f3547195c3..374360a5d6 100644 --- a/src/Response.php +++ b/src/Response.php @@ -249,7 +249,7 @@ public static function fromString($string) */ protected function parseStatusLine($line) { - $regex = '/^HTTP\/(?P1\.[01]) (?P\d{3})(?:[ ]+(?P.*))?$/'; + $regex = '/^HTTP\/(?P1\.[01]|2\.0) (?P\d{3})(?:[ ]+(?P.*))?$/'; $matches = []; if (! preg_match($regex, $line, $matches)) { throw new Exception\InvalidArgumentException( diff --git a/test/ResponseTest.php b/test/ResponseTest.php index c3e66ef2c3..893eb356cd 100644 --- a/test/ResponseTest.php +++ b/test/ResponseTest.php @@ -22,6 +22,10 @@ public function testResponseFactoryFromStringCreatesValidResponse() $response = Response::fromString($string); $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals('Foo Bar', $response->getContent()); + + $string = 'HTTP/2.0 200 OK' . "\r\n\r\n" . 'Foo Bar'; + $response = Response::fromString($string); + $this->assertEquals('2.0', $response->getVersion()); } public function testResponseCanRenderStatusLine() @@ -33,6 +37,17 @@ public function testResponseCanRenderStatusLine() $response->setReasonPhrase('Foo Bar'); $this->assertEquals('HTTP/1.1 404 Foo Bar', $response->renderStatusLine()); + + $response->setVersion('2.0'); + $this->assertEquals('HTTP/2.0 404 Foo Bar', $response->renderStatusLine()); + } + + public function testInvalidHTTPVersion() + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('A valid response status line was not found in the provided string'); + $string = 'HTTP/2.1 200 OK' . "\r\n\r\n" . 'Foo Bar'; + $response = \Zend\Http\Response::fromString($string); } public function testResponseUsesHeadersContainerByDefault() From b2ab5ffafdbf5a78f339540cbe4fdb310285b19a Mon Sep 17 00:00:00 2001 From: Stefan Brauner Date: Thu, 14 Feb 2019 23:31:43 +0100 Subject: [PATCH 4/9] changed "2.0" to "2" --- docs/book/request.md | 2 +- docs/book/response.md | 2 +- src/AbstractMessage.php | 10 +++++----- src/Response.php | 2 +- test/ResponseTest.php | 10 +++++----- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/docs/book/request.md b/docs/book/request.md index 9db6a4adb1..4c24473552 100644 --- a/docs/book/request.md +++ b/docs/book/request.md @@ -75,7 +75,7 @@ Method signature | De `setUri(string|Uri $uri) : self` | Set the URI/URL for this request; this can be a string or an instance of `Zend\Uri\Http`. `getUri() : Uri` | Return the URI for this request object. `getUriString() : string` | Return the URI for this request object as a string. -`setVersion(string $version) : self` | Set the HTTP version for this object, one of 1.0, 1.1 or 2.0 (`AbstractMessage::VERSION_10`, `AbstractMessage::VERSION_11`, `AbstractMessage::VERSION_20`). +`setVersion(string $version) : self` | Set the HTTP version for this object, one of 1.0, 1.1 or 2 (`AbstractMessage::VERSION_10`, `AbstractMessage::VERSION_11`, `AbstractMessage::VERSION_2`). `getVersion() : string` | Return the HTTP version for this request. `setQuery(Parameters $query) : self` | Provide an alternate Parameter Container implementation for query parameters in this object. (This is NOT the primary API for value setting; for that, see `getQuery()`). `getQuery(string|null $name, mixed|null $default) : null|string|Parameters` | Return the parameter container responsible for query parameters or a single query parameter based on `$name`. diff --git a/docs/book/response.md b/docs/book/response.md index c74db866a0..4a9ae76f6b 100644 --- a/docs/book/response.md +++ b/docs/book/response.md @@ -77,7 +77,7 @@ Method signature | Descrip `renderStatusLine() : string` | Render the status line header `setHeaders(Headers $headers) : self` | Provide an alternate Parameter Container implementation for headers in this object. (This is NOT the primary API for value setting; for that, see `getHeaders()`.) `getHeaders() : Headers` | Return the container responsible for storing HTTP headers. This container exposes the primary API for manipulating headers set in the HTTP response. See the section on [Headers](headers.md) for more information. -`setVersion(string $version) : self` | Set the HTTP version for this object, one of 1.0, 1.1 or 2.0 (`Response::VERSION_10`, `Response::VERSION_11`, `Response::VERSION_20`). +`setVersion(string $version) : self` | Set the HTTP version for this object, one of 1.0, 1.1 or 2 (`Response::VERSION_10`, `Response::VERSION_11`, `Response::VERSION_2`). `getVersion() : string` | Return the HTTP version for this response. `setStatusCode(int $code) : self` | Set HTTP status code. `getStatusCode() : int` | Retrieve HTTP status code. diff --git a/src/AbstractMessage.php b/src/AbstractMessage.php index 2c5724d03f..bd3856eeaa 100644 --- a/src/AbstractMessage.php +++ b/src/AbstractMessage.php @@ -21,7 +21,7 @@ abstract class AbstractMessage extends Message */ const VERSION_10 = '1.0'; const VERSION_11 = '1.1'; - const VERSION_20 = '2.0'; + const VERSION_2 = '2'; /**#@-*/ /** @@ -35,16 +35,16 @@ abstract class AbstractMessage extends Message protected $headers; /** - * Set the HTTP version for this object, one of 1.0, 1.1 or 2.0 - * (AbstractMessage::VERSION_10, AbstractMessage::VERSION_11, AbstractMessage::VERSION_20) + * Set the HTTP version for this object, one of 1.0, 1.1 or 2 + * (AbstractMessage::VERSION_10, AbstractMessage::VERSION_11, AbstractMessage::VERSION_2) * - * @param string $version (Must be 1.0, 1.1 or 2.0) + * @param string $version (Must be 1.0, 1.1 or 2) * @return AbstractMessage * @throws Exception\InvalidArgumentException */ public function setVersion($version) { - if ($version != self::VERSION_10 && $version != self::VERSION_11 && $version != self::VERSION_20) { + if ($version != self::VERSION_10 && $version != self::VERSION_11 && $version != self::VERSION_2) { throw new Exception\InvalidArgumentException( 'Not valid or not supported HTTP version: ' . $version ); diff --git a/src/Response.php b/src/Response.php index 374360a5d6..1dbfd92b90 100644 --- a/src/Response.php +++ b/src/Response.php @@ -249,7 +249,7 @@ public static function fromString($string) */ protected function parseStatusLine($line) { - $regex = '/^HTTP\/(?P1\.[01]|2\.0) (?P\d{3})(?:[ ]+(?P.*))?$/'; + $regex = '/^HTTP\/(?P1\.[01]|2) (?P\d{3})(?:[ ]+(?P.*))?$/'; $matches = []; if (! preg_match($regex, $line, $matches)) { throw new Exception\InvalidArgumentException( diff --git a/test/ResponseTest.php b/test/ResponseTest.php index 893eb356cd..c0ff58bf21 100644 --- a/test/ResponseTest.php +++ b/test/ResponseTest.php @@ -23,9 +23,9 @@ public function testResponseFactoryFromStringCreatesValidResponse() $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals('Foo Bar', $response->getContent()); - $string = 'HTTP/2.0 200 OK' . "\r\n\r\n" . 'Foo Bar'; + $string = 'HTTP/2 200 OK' . "\r\n\r\n" . 'Foo Bar'; $response = Response::fromString($string); - $this->assertEquals('2.0', $response->getVersion()); + $this->assertEquals('2', $response->getVersion()); } public function testResponseCanRenderStatusLine() @@ -38,15 +38,15 @@ public function testResponseCanRenderStatusLine() $response->setReasonPhrase('Foo Bar'); $this->assertEquals('HTTP/1.1 404 Foo Bar', $response->renderStatusLine()); - $response->setVersion('2.0'); - $this->assertEquals('HTTP/2.0 404 Foo Bar', $response->renderStatusLine()); + $response->setVersion('2'); + $this->assertEquals('HTTP/2 404 Foo Bar', $response->renderStatusLine()); } public function testInvalidHTTPVersion() { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('A valid response status line was not found in the provided string'); - $string = 'HTTP/2.1 200 OK' . "\r\n\r\n" . 'Foo Bar'; + $string = 'HTTP/2.0 200 OK' . "\r\n\r\n" . 'Foo Bar'; $response = \Zend\Http\Response::fromString($string); } From 17a0fce0e9aa5d781888a4070dc4aa070bfd5e55 Mon Sep 17 00:00:00 2001 From: Stefan Brauner Date: Thu, 14 Feb 2019 23:39:31 +0100 Subject: [PATCH 5/9] reverted changing Request to AbstractMessage --- docs/book/request.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/book/request.md b/docs/book/request.md index 4c24473552..1fe0331a0a 100644 --- a/docs/book/request.md +++ b/docs/book/request.md @@ -75,7 +75,7 @@ Method signature | De `setUri(string|Uri $uri) : self` | Set the URI/URL for this request; this can be a string or an instance of `Zend\Uri\Http`. `getUri() : Uri` | Return the URI for this request object. `getUriString() : string` | Return the URI for this request object as a string. -`setVersion(string $version) : self` | Set the HTTP version for this object, one of 1.0, 1.1 or 2 (`AbstractMessage::VERSION_10`, `AbstractMessage::VERSION_11`, `AbstractMessage::VERSION_2`). +`setVersion(string $version) : self` | Set the HTTP version for this object, one of 1.0, 1.1 or 2 (`Request::VERSION_10`, `Request::VERSION_11`, `Request::VERSION_2`). `getVersion() : string` | Return the HTTP version for this request. `setQuery(Parameters $query) : self` | Provide an alternate Parameter Container implementation for query parameters in this object. (This is NOT the primary API for value setting; for that, see `getQuery()`). `getQuery(string|null $name, mixed|null $default) : null|string|Parameters` | Return the parameter container responsible for query parameters or a single query parameter based on `$name`. From b7d97d223be07d57e0d0e36397946b409a3ac56d Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 15 Feb 2019 01:23:31 +0100 Subject: [PATCH 6/9] Update src/AbstractMessage.php Co-Authored-By: av3 --- src/AbstractMessage.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AbstractMessage.php b/src/AbstractMessage.php index bd3856eeaa..4fbd423fba 100644 --- a/src/AbstractMessage.php +++ b/src/AbstractMessage.php @@ -44,7 +44,7 @@ abstract class AbstractMessage extends Message */ public function setVersion($version) { - if ($version != self::VERSION_10 && $version != self::VERSION_11 && $version != self::VERSION_2) { + if (! in_array($version, [self::VERSION_10, self::VERSION_11, self::VERSION_2])) { throw new Exception\InvalidArgumentException( 'Not valid or not supported HTTP version: ' . $version ); From bea753d7ce14a91fc950a7240f396b94f9112544 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Tue, 19 Feb 2019 12:44:50 -0600 Subject: [PATCH 7/9] docs: Not version that adds HTTP/2 support Need to indicate that HTTP/2 support is only available in 2.10.0 and up. --- docs/book/client/intro.md | 2 +- docs/book/request.md | 2 +- docs/book/response.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/book/client/intro.md b/docs/book/client/intro.md index ed728e5b08..11249a1504 100644 --- a/docs/book/client/intro.md +++ b/docs/book/client/intro.md @@ -77,7 +77,7 @@ Parameter | Description `strictredirects` | Whether to strictly follow the RFC when redirecting (see this section) | boolean | FALSE `useragent` | User agent identifier string (sent in request headers) | string | `Zend\Http\Client` `timeout` | Connection timeout (seconds) | integer | 10 -`httpversion` | HTTP protocol version (usually '1.1', '1.0' or '2.0') | string | 1.1 +`httpversion` | HTTP protocol version (usually '1.1', '1.0' or '2'; 2 is only supported starting in 2.10.0) | string | 1.1 `adapter` | Connection adapter class to use (see this section) | mixed | `Zend\Http\Client\Adapter\Socket` `keepalive` | Whether to enable keep-alive connections with the server. Useful and might improve performance if several consecutive requests to the same server are performed. | boolean | FALSE `storeresponse` | Whether to store last response for later retrieval with getLastResponse(). If set to FALSE, getLastResponse() will return NULL. | boolean | TRUE diff --git a/docs/book/request.md b/docs/book/request.md index 1fe0331a0a..a55295744d 100644 --- a/docs/book/request.md +++ b/docs/book/request.md @@ -75,7 +75,7 @@ Method signature | De `setUri(string|Uri $uri) : self` | Set the URI/URL for this request; this can be a string or an instance of `Zend\Uri\Http`. `getUri() : Uri` | Return the URI for this request object. `getUriString() : string` | Return the URI for this request object as a string. -`setVersion(string $version) : self` | Set the HTTP version for this object, one of 1.0, 1.1 or 2 (`Request::VERSION_10`, `Request::VERSION_11`, `Request::VERSION_2`). +`setVersion(string $version) : self` | Set the HTTP version for this object, one of 1.0, 1.1 or 2 (`Request::VERSION_10`, `Request::VERSION_11`, `Request::VERSION_2`). HTTP/2 support was added in zend-http 2.10.0. `getVersion() : string` | Return the HTTP version for this request. `setQuery(Parameters $query) : self` | Provide an alternate Parameter Container implementation for query parameters in this object. (This is NOT the primary API for value setting; for that, see `getQuery()`). `getQuery(string|null $name, mixed|null $default) : null|string|Parameters` | Return the parameter container responsible for query parameters or a single query parameter based on `$name`. diff --git a/docs/book/response.md b/docs/book/response.md index 4a9ae76f6b..4f32707428 100644 --- a/docs/book/response.md +++ b/docs/book/response.md @@ -77,7 +77,7 @@ Method signature | Descrip `renderStatusLine() : string` | Render the status line header `setHeaders(Headers $headers) : self` | Provide an alternate Parameter Container implementation for headers in this object. (This is NOT the primary API for value setting; for that, see `getHeaders()`.) `getHeaders() : Headers` | Return the container responsible for storing HTTP headers. This container exposes the primary API for manipulating headers set in the HTTP response. See the section on [Headers](headers.md) for more information. -`setVersion(string $version) : self` | Set the HTTP version for this object, one of 1.0, 1.1 or 2 (`Response::VERSION_10`, `Response::VERSION_11`, `Response::VERSION_2`). +`setVersion(string $version) : self` | Set the HTTP version for this object, one of 1.0, 1.1 or 2 (`Response::VERSION_10`, `Response::VERSION_11`, `Response::VERSION_2`). HTTP/2 support was added in zend-http 2.10.0. `getVersion() : string` | Return the HTTP version for this response. `setStatusCode(int $code) : self` | Set HTTP status code. `getStatusCode() : int` | Retrieve HTTP status code. From f31cff84f34fbbf73bf0db21fb9ca8db33cdc51c Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Tue, 19 Feb 2019 12:46:52 -0600 Subject: [PATCH 8/9] qa: extract data provider for HTTP/2 tests Extracts `validHttpVersions()` and `validResponseHttpVersionProvider()` methods to ensure we test various functionalities against each of the three supported HTTP versions. Updates `testResponseFactoryFromStringCreatesValidResponse()` to use the latter, and splits `testResponseCanRenderStatusLine()` into `testResponseCanRenderStatusLineUsingDefaultReasonPhrase()` and `testResponseCanRenderStatusLineUsingCustomReasonPhrase()`, each using `validHttpVersions()` as a data provider. --- test/ResponseTest.php | 71 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 56 insertions(+), 15 deletions(-) diff --git a/test/ResponseTest.php b/test/ResponseTest.php index c0ff58bf21..6d1f48abef 100644 --- a/test/ResponseTest.php +++ b/test/ResponseTest.php @@ -16,33 +16,74 @@ class ResponseTest extends TestCase { - public function testResponseFactoryFromStringCreatesValidResponse() + public function validHttpVersions() { - $string = 'HTTP/1.0 200 OK' . "\r\n\r\n" . 'Foo Bar'; - $response = Response::fromString($string); - $this->assertEquals(200, $response->getStatusCode()); - $this->assertEquals('Foo Bar', $response->getContent()); + yield 'http/1.0' => ['1.0']; + yield 'http/1.1' => ['1.1']; + yield 'http/2' => ['2']; + } + + public function validResponseHttpVersionProvider() + { + $responseTemplate = "HTTP/%s 200 OK\r\n\r\nFoo Bar"; + foreach ($this->validHttpVersions() as $testCase => $data) { + $version = array_shift($data); + yield $testCase => [ + 'response' => sprintf($responseTemplate, $version), + 'expectedVersion' => $version, + 'expectedStatus' => '200', + 'expectedContent' => 'Foo Bar', + ]; + } + } - $string = 'HTTP/2 200 OK' . "\r\n\r\n" . 'Foo Bar'; + /** + * @dataProvider validResponseHttpVersionProvider + * @param string $string Response string + * @param string $expectedVersion + * @param string $expectedStatus + * @param string $expectedContent + */ + public function testResponseFactoryFromStringCreatesValidResponse( + $string, + $expectedVersion, + $expectedStatus, + $expectedContent + ) { $response = Response::fromString($string); - $this->assertEquals('2', $response->getVersion()); + $this->assertEquals($expectedVersion, $response->getVersion()); + $this->assertEquals($expectedStatus, $response->getStatusCode()); + $this->assertEquals($expectedContent, $response->getContent()); } - public function testResponseCanRenderStatusLine() + /** + * @dataProvider validHttpVersions + * @param string $version + */ + public function testResponseCanRenderStatusLineUsingDefaultReasonPhrase($version) { + $expected = sprintf('HTTP/%s 404 Not Found', $version); $response = new Response(); - $response->setVersion('1.1'); + $response->setVersion($version); $response->setStatusCode(Response::STATUS_CODE_404); - $this->assertEquals('HTTP/1.1 404 Not Found', $response->renderStatusLine()); + $this->assertEquals($expected, $response->renderStatusLine()); + } + /** + * @dataProvider validHttpVersions + * @param string $version + */ + public function testResponseCanRenderStatusLineUsingCustomReasonPhrase($version) + { + $expected = sprintf('HTTP/%s 404 Foo Bar', $version); + $response = new Response(); + $response->setVersion($version); + $response->setStatusCode(Response::STATUS_CODE_404); $response->setReasonPhrase('Foo Bar'); - $this->assertEquals('HTTP/1.1 404 Foo Bar', $response->renderStatusLine()); - - $response->setVersion('2'); - $this->assertEquals('HTTP/2 404 Foo Bar', $response->renderStatusLine()); + $this->assertEquals($expected, $response->renderStatusLine()); } - public function testInvalidHTTPVersion() + public function testInvalidHTTP2VersionString() { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('A valid response status line was not found in the provided string'); From a7dcedf397803a3e879ad39fc50bba3bf88e99d2 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Tue, 19 Feb 2019 12:47:22 -0600 Subject: [PATCH 9/9] Adds CHANGELOG entry for #173 --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c73fe4534a..f8cbc89f4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ All notable changes to this project will be documented in this file, in reverse ### Added -- Nothing. +- [#173](https://github.com/zendframework/zend-http/pull/173) adds support for HTTP/2 requests and responses. ### Changed