This repository has been archived by the owner on Jan 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
X-HTTP-Method-Override listener #81
Closed
Closed
Changes from 4 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4677f11
X-HTTP-Method-Override listener
9db72f2
Changes following @webimpress his comments
e36c3eb
Changes following @webimpress his comments
da71cb0
Changes following @webimpress his comments
4b1f500
Small code style improvements/typo fixes
ac6e42e
Made HttpMethodOverrideListener extend AbstractListenerAggregate
d9586c5
Removed useless comment
43768f7
Added configuration of http-override-methods
30887a7
Added configuration of http-override-methods
37a4d74
Added HttpMethodOverrideListenerFactory test
3023a4a
Fix for travis test fail (line length)
0277845
changed test to use prophecy
8bac35c
year 2016
dabc4e0
added trailing commas
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
/** | ||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause | ||
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com) | ||
*/ | ||
|
||
namespace ZF\ContentNegotiation; | ||
|
||
use Zend\Mvc\MvcEvent; | ||
use ZF\ApiProblem\ApiProblem; | ||
use ZF\ApiProblem\ApiProblemResponse; | ||
use Zend\Http\Request as HttpRequest; | ||
|
||
class HttpMethodOverrideListener | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected $methods = [ | ||
HttpRequest::METHOD_HEAD, | ||
HttpRequest::METHOD_POST, | ||
HttpRequest::METHOD_PUT, | ||
HttpRequest::METHOD_DELETE, | ||
HttpRequest::METHOD_PATCH, | ||
]; | ||
|
||
/** | ||
* Checks for X-HTTP-Method-Override header and sets header inside request object. | ||
* | ||
* @param MvcEvent $event | ||
* @return void|ApiProblemResponse | ||
*/ | ||
public function __invoke(MvcEvent $event) | ||
{ | ||
$request = $event->getRequest(); | ||
|
||
if (!$request instanceof HttpRequest) { | ||
return; | ||
} | ||
|
||
if (!$request->getHeaders()->has('X-HTTP-Method-Override')) { | ||
return; | ||
} | ||
|
||
$header = $request->getHeader('X-HTTP-Method-Override'); | ||
|
||
$method = $header->getFieldValue(); | ||
|
||
if (! in_array($method, $this->methods)) { | ||
return new ApiProblemResponse(new ApiProblem( | ||
400, | ||
'unrecognized method in X-HTTP-Method-Ovverride header' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
)); | ||
} | ||
|
||
$request->setMethod($method); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
/** | ||
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause | ||
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com) | ||
*/ | ||
|
||
namespace ZFTest\ContentNegotiation; | ||
|
||
use PHPUnit_Framework_TestCase as TestCase; | ||
use Zend\Http\Request as HttpRequest; | ||
use Zend\Mvc\MvcEvent; | ||
use ZF\ApiProblem\ApiProblemResponse; | ||
use ZF\ContentNegotiation\HttpMethodOverrideListener; | ||
|
||
class HttpMethodOverrideListenerTest extends TestCase | ||
{ | ||
use RouteMatchFactoryTrait; | ||
|
||
/** | ||
* @var HttpMethodOverrideListener | ||
*/ | ||
protected $listener; | ||
|
||
/** | ||
* Set up test | ||
*/ | ||
public function setUp() | ||
{ | ||
$this->listener = new HttpMethodOverrideListener(); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function httpMethods() | ||
{ | ||
return [ | ||
'head' => [HttpRequest::METHOD_HEAD], | ||
'post' => [HttpRequest::METHOD_POST], | ||
'put' => [HttpRequest::METHOD_PUT], | ||
'delete' => [HttpRequest::METHOD_DELETE], | ||
'patch' => [HttpRequest::METHOD_PATCH], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider httpMethods | ||
*/ | ||
public function testHttpMethodOverrideListener($method) | ||
{ | ||
$listener = $this->listener; | ||
|
||
$request = new HttpRequest(); | ||
$request->setMethod('GET'); | ||
$request->getHeaders()->addHeaderLine('X-HTTP-Method-Override', $method); | ||
|
||
$event = new MvcEvent(); | ||
$event->setRequest($request); | ||
$event->setRouteMatch($this->createRouteMatch([])); | ||
|
||
$result = $listener($event); | ||
$this->assertEquals($method, $request->getMethod()); | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does it for? |
||
public function testHttpMethodOverrideListenerReturnsProblemResponse() | ||
{ | ||
$listener = $this->listener; | ||
|
||
$request = new HttpRequest(); | ||
$request->setMethod('GET'); | ||
$request->getHeaders()->addHeaderLine('X-HTTP-Method-Override', 'TEST'); | ||
|
||
$event = new MvcEvent(); | ||
$event->setRequest($request); | ||
|
||
$result = $listener($event); | ||
$this->assertInstanceOf(ApiProblemResponse::class, $result); | ||
$problem = $result->getApiProblem(); | ||
$this->assertEquals(400, $problem->status); | ||
$this->assertContains('unrecognized method in X-HTTP-Method-Ovverride header', $problem->detail); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please change to uppercase
U
inunrecognized
word