Skip to content

Commit

Permalink
Implement protocolVersion methods.
Browse files Browse the repository at this point in the history
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 a1e5181
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/Network/Request.php
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
Expand Down
44 changes: 44 additions & 0 deletions tests/TestCase/Network/RequestTest.php
Expand Up @@ -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.
*
Expand Down

0 comments on commit a1e5181

Please sign in to comment.