Skip to content
Permalink
Browse files

Add validation for the HTTP method.

This emulates the validation that Diactoros does.
  • Loading branch information...
markstory committed Sep 4, 2016
1 parent 149c9ba commit 83dbcd8b8d572237ddc93ff52628e0ee8c5d9c55
Showing with 24 additions and 0 deletions.
  1. +9 −0 src/Network/Request.php
  2. +15 −0 tests/TestCase/Network/RequestTest.php
@@ -997,6 +997,15 @@ public function getMethod()
public function withMethod($method)
{
$new = clone $this;
if (!is_string($method) ||
!preg_match('/^[!#$%&\'*+.^_`\|~0-9a-z-]+$/i', $method)
) {
throw new InvalidArgumentException(sprintf(
'Unsupported HTTP method "%s" provided',
$method
));
}
$new->_environment['REQUEST_METHOD'] = $method;
return $new;
}
@@ -765,6 +765,21 @@ public function testWithMethod()
$this->assertEquals('put', $new->getMethod());
}
/**
* Test withMethod() and invalid data
*
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Unsupported HTTP method "no good" provided
* @return void
*/
public function testWithMethodInvalid()
{
$request = new Request([
'environment' => ['REQUEST_METHOD' => 'delete']
]);
$request->withMethod('no good');
}
/**
* Test host retrieval.
*

0 comments on commit 83dbcd8

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