Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Raise exception on serialization of Request object with emtpy HTTP me… #117

Merged
merged 1 commit into from
Mar 17, 2016
Merged
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
29 changes: 29 additions & 0 deletions src/MessageTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public function getProtocolVersion()
*/
public function withProtocolVersion($version)
{
$this->validateProtocolVersion($version);
$new = clone $this;
$new->protocol = $version;
return $new;
Expand Down Expand Up @@ -400,4 +401,32 @@ private static function assertValidHeaderValue(array $values)
{
array_walk($values, __NAMESPACE__ . '\HeaderSecurity::assertValid');
}

/**
* Validate the HTTP protocol version
*
* @param string $version
* @throws InvalidArgumentException on invalid HTTP protocol version
*/
private function validateProtocolVersion($version)
{
if (empty($version)) {
throw new InvalidArgumentException(sprintf(
'HTTP protocol version can not be empty'
));
}
if (! is_string($version)) {
throw new InvalidArgumentException(sprintf(
'Unsupported HTTP protocol version; must be a string, received %s',
(is_object($version) ? get_class($version) : gettype($version))
));
}
//HTTP uses a "<major>.<minor>" numbering scheme to indicate versions of the protocol
if (! preg_match('~^\d+\.\d+$~', $version)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HTTP/2 does not follow this format (see the HTTP/2 FAQ; no decimal point. I'll update this on merge.

throw new InvalidArgumentException(sprintf(
'Unsupported HTTP protocol version "%s" provided',
$version
));
}
}
}
6 changes: 5 additions & 1 deletion src/Request/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ public static function fromStream(StreamInterface $stream)
*/
public static function toString(RequestInterface $request)
{
$httpMethod = $request->getMethod();
if (empty($httpMethod)) {
throw new UnexpectedValueException('Object can not be serialized because HTTP method is empty');
}
$headers = self::serializeHeaders($request->getHeaders());
$body = (string) $request->getBody();
$format = '%s %s HTTP/%s%s%s';
Expand All @@ -89,7 +93,7 @@ public static function toString(RequestInterface $request)

return sprintf(
$format,
$request->getMethod(),
$httpMethod,
$request->getRequestTarget(),
$request->getProtocolVersion(),
$headers,
Expand Down
24 changes: 24 additions & 0 deletions test/MessageTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,30 @@ public function testProtocolMutatorReturnsCloneWithChanges()
$this->assertEquals('1.0', $message->getProtocolVersion());
}


public function invalidProtocolVersionProvider()
{
return [
'true' => [ true ],
'false' => [ false ],
'int' => [ 1 ],
'float' => [ 1.1 ],
'array' => [ ['1.1'] ],
'stdClass' => [ (object) [ 'version' => '1.0'] ],
'wrong-format' => [ '1'],
'wrong-format2' => [ '1.2.3'],
];
}
/**
* @dataProvider invalidProtocolVersionProvider
*/
public function testWithProtocolVersionRaisesExceptionForInvalidVersion($version)
{
$this->setExpectedException('InvalidArgumentException');
$request = new Request();
$request->withProtocolVersion($version);
}

public function testUsesStreamProvidedInConstructorAsBody()
{
$stream = $this->getMock('Psr\Http\Message\StreamInterface');
Expand Down
11 changes: 11 additions & 0 deletions test/Request/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Zend\Diactoros\Request\Serializer;
use Zend\Diactoros\Stream;
use Zend\Diactoros\Uri;
use UnexpectedValueException;

class SerializerTest extends TestCase
{
Expand Down Expand Up @@ -326,4 +327,14 @@ public function testFromStreamStopsReadingAfterScanningHeader()
$stream = Serializer::fromStream($stream);
$this->assertInstanceOf('Zend\Diactoros\RelativeStream', $stream->getBody());
}

/**
* @expectedException UnexpectedValueException
*/
public function testToStringRaisesExceptionOnEmptyMethod()
{
$request = (new Request())
->withUri(new Uri('http://example.com/foo/bar?baz=bat'));
$message = Serializer::toString($request);
}
}