Skip to content
Permalink
Browse files

Implement protocolVersion methods.

I decided to lazily populate the this property as I don't think most
server requests will use this, however, it is part of the
ServerRequestInterface.
  • Loading branch information...
markstory committed Sep 10, 2016
1 parent e74bb82 commit a1e5181fe16fa0cdbd751728c6b5f3cfd14a24cc
Showing with 90 additions and 0 deletions.
  1. +46 −0 src/Network/Request.php
  2. +44 −0 tests/TestCase/Network/RequestTest.php
@@ -186,6 +186,13 @@ class Request implements ArrayAccess
*/
protected $uploadedFiles = [];
/**
* The HTTP protocol version used.
*
* @var string|null
*/
protected $protocol;
/**
* Wrapper method to create a new request from PHP superglobals.
*
@@ -1498,6 +1505,45 @@ public function withParsedBody($data)
return $new;
}
/**
* Retrieves the HTTP protocol version as a string.
*
* @return string HTTP protocol version.
*/
public function getProtocolVersion()
{
if ($this->protocol) {
return $this->protocol;
}
// Lazily populate this data as it is generally not used.
preg_match('/^HTTP\/([\d.]+)$/', $this->env('SERVER_PROTOCOL'), $match);
$protocol = '1.1';
if (isset($match[1])) {
$protocol = $match[1];
}
$this->protocol = $protocol;
return $this->protocol;
}
/**
* Return an instance with the specified HTTP protocol version.
*
* The version string MUST contain only the HTTP version number (e.g.,
* "1.1", "1.0").
*
* @param string $version HTTP protocol version
* @return static
*/
public function withProtocolVersion($version)
{
if (!preg_match('/^(1\.[01]|2)$/', $version)) {
throw new InvalidArgumentException("Unsupported protocol version '{$version}' provided");
}
$new = clone $this;
$new->protocol = $version;
return $new;
}
/**
* Get/Set value from the request's environment data.
* Fallback to using env() if key not set in $environment property.
@@ -905,6 +905,50 @@ public function testWithMethodInvalid()
$request->withMethod('no good');
}
/**
* Test getProtocolVersion()
*
* @return void
*/
public function testGetProtocolVersion()
{
$request = new Request();
$this->assertEquals('1.1', $request->getProtocolVersion());
// SERVER var.
$request = new Request([
'environment' => ['SERVER_PROTOCOL' => 'HTTP/1.0']
]);
$this->assertEquals('1.0', $request->getProtocolVersion());
}
/**
* Test withProtocolVersion()
*
* @return void
*/
public function testWithProtocolVersion()
{
$request = new Request();
$new = $request->withProtocolVersion('1.0');
$this->assertNotSame($new, $request);
$this->assertEquals('1.1', $request->getProtocolVersion());
$this->assertEquals('1.0', $new->getProtocolVersion());
}
/**
* Test withProtocolVersion() and invalid data
*
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Unsupported protocol version 'no good' provided
* @return void
*/
public function testWithProtocolVersionInvalid()
{
$request = new Request();
$request->withProtocolVersion('no good');
}
/**
* Test host retrieval.
*

0 comments on commit a1e5181

Please sign in to comment.
You can’t perform that action at this time.