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

Commit

Permalink
Merge branch 'feature/162-require-request-method' into release-2.0
Browse files Browse the repository at this point in the history
Close #162
Close #302
  • Loading branch information
weierophinney committed May 29, 2018
2 parents bfe9800 + 80b9392 commit 71599e6
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 67 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ All notable changes to this project will be documented in this file, in reverse

### Changed

- Nothing.
- [#162](https://github.com/zendframework/zend-diactoros/pull/162) modifies `Serializer\Request` such that it now no longer raises an `UnexpectedValueException` via its `toString()` method
when an unexpected HTTP method is encountered; this can be done safely, as the value can never
be invalid due to other changes in the same patch.

- [#162](https://github.com/zendframework/zend-diactoros/pull/162) modifies `RequestTrait` such that it now invalidates non-string method arguments to either
the constructor or `withMethod()`, raising an `InvalidArgumentException` for any that do not validate.

### Deprecated

Expand Down
3 changes: 0 additions & 3 deletions src/Request/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@ 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 Down
23 changes: 10 additions & 13 deletions src/RequestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ trait RequestTrait
/**
* @var string
*/
private $method = '';
private $method = 'GET';

/**
* The request-target, if it has been provided or calculated.
Expand All @@ -64,9 +64,10 @@ trait RequestTrait
*/
private function initialize($uri = null, $method = null, $body = 'php://memory', array $headers = [])
{
$this->validateMethod($method);
if ($method !== null) {
$this->setMethod($method);
}

$this->method = $method ?: '';
$this->uri = $this->createUri($uri);
$this->stream = $this->getStream($body, 'wb+');

Expand Down Expand Up @@ -204,9 +205,8 @@ public function getMethod()
*/
public function withMethod($method)
{
$this->validateMethod($method);
$new = clone $this;
$new->method = $method;
$new->setMethod($method);
return $new;
}

Expand Down Expand Up @@ -284,21 +284,17 @@ public function withUri(UriInterface $uri, $preserveHost = false)
}

/**
* Validate the HTTP method
* Set and validate the HTTP method
*
* @param null|string $method
* @param string $method
* @throws InvalidArgumentException on invalid HTTP method.
*/
private function validateMethod($method)
private function setMethod($method)
{
if (null === $method) {
return;
}

if (! is_string($method)) {
throw new InvalidArgumentException(sprintf(
'Unsupported HTTP method; must be a string, received %s',
(is_object($method) ? get_class($method) : gettype($method))
is_object($method) ? get_class($method) : gettype($method)
));
}

Expand All @@ -308,6 +304,7 @@ private function validateMethod($method)
$method
));
}
$this->method = $method;
}

/**
Expand Down
36 changes: 0 additions & 36 deletions src/ServerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,42 +225,6 @@ public function withoutAttribute($attribute)
return $new;
}

/**
* Proxy to receive the request method.
*
* This overrides the parent functionality to ensure the method is never
* empty; if no method is present, it returns 'GET'.
*
* @return string
*/
public function getMethod()
{
if (empty($this->method)) {
return 'GET';
}
return $this->method;
}

/**
* Set the request method.
*
* Unlike the regular Request implementation, the server-side
* normalizes the method to uppercase to ensure consistency
* and make checking the method simpler.
*
* This methods returns a new instance.
*
* @param string $method
* @return self
*/
public function withMethod($method)
{
$this->validateMethod($method);
$new = clone $this;
$new->method = $method;
return $new;
}

/**
* Recursively validate the structure in an uploaded files array.
*
Expand Down
10 changes: 0 additions & 10 deletions test/Request/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,14 +337,4 @@ public function testFromStreamStopsReadingAfterScanningHeader()

$this->assertInstanceOf(RelativeStream::class, $stream->getBody());
}

public function testToStringRaisesExceptionOnEmptyMethod()
{
$request = (new Request())
->withUri(new Uri('http://example.com/foo/bar?baz=bat'));

$this->expectException(UnexpectedValueException::class);

Serializer::toString($request);
}
}
25 changes: 21 additions & 4 deletions test/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,33 @@ public function setUp()
$this->request = new Request();
}

public function testMethodIsEmptyByDefault()
public function testMethodIsGetByDefault()
{
$this->assertSame('', $this->request->getMethod());
$this->assertSame('GET', $this->request->getMethod());
}

public function testMethodMutatorReturnsCloneWithChangedMethod()
{
$request = $this->request->withMethod('GET');
$request = $this->request->withMethod('POST');
$this->assertNotSame($this->request, $request);
$this->assertSame('GET', $request->getMethod());
$this->assertEquals('POST', $request->getMethod());
}

public function invalidMethod()
{
return [
[null],
[''],
];
}

/**
* @dataProvider invalidMethod
*/
public function testWithInvalidMethod($method)
{
$this->expectException(InvalidArgumentException::class);
$this->request->withMethod($method);
}

public function testReturnsUnpopulatedUriByDefault()
Expand Down

0 comments on commit 71599e6

Please sign in to comment.