-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #2329
- Loading branch information
Showing
4 changed files
with
161 additions
and
0 deletions.
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
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,47 @@ | ||
<?php | ||
/** | ||
* Slim Framework (https://slimframework.com) | ||
* | ||
* @link https://github.com/slimphp/Slim | ||
* @copyright Copyright (c) 2011-2017 Josh Lockhart | ||
* @license https://github.com/slimphp/Slim/blob/4.x/LICENSE.md (MIT License) | ||
*/ | ||
namespace Slim\Middleware; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
/** | ||
* Override HTTP Request method by given body param or custom header | ||
*/ | ||
class MethodOverrideMiddleware | ||
{ | ||
/** | ||
* Invoke | ||
* | ||
* @param ServerRequestInterface $request PSR7 server request | ||
* @param ResponseInterface $response PSR7 response | ||
* @param callable $next Middleware callable | ||
* @return ResponseInterface PSR7 response | ||
*/ | ||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) | ||
{ | ||
$methodHeader = $request->getHeaderLine('X-Http-Method-Override'); | ||
|
||
if ($methodHeader) { | ||
$request = $request->withMethod($methodHeader); | ||
} elseif (strtoupper($request->getMethod()) == 'POST') { | ||
$body = $request->getParsedBody(); | ||
|
||
if (!empty($body['_METHOD'])) { | ||
$request = $request->withMethod($body['_METHOD']); | ||
} | ||
|
||
if ($request->getBody()->eof()) { | ||
$request->getBody()->rewind(); | ||
} | ||
} | ||
|
||
return $next($request, $response); | ||
} | ||
} |
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
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,111 @@ | ||
<?php | ||
/** | ||
* Slim Framework (https://slimframework.com) | ||
* | ||
* @link https://github.com/slimphp/Slim | ||
* @copyright Copyright (c) 2011-2017 Josh Lockhart | ||
* @license https://github.com/slimphp/Slim/blob/4.x/LICENSE.md (MIT License) | ||
*/ | ||
namespace Slim\Tests\Middleware; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Slim\Http\Headers; | ||
use Slim\Http\Request; | ||
use Slim\Http\RequestBody; | ||
use Slim\Http\Response; | ||
use Slim\Http\Uri; | ||
use Slim\Middleware\MethodOverrideMiddleware; | ||
|
||
/** | ||
* @covers \Slim\Middleware\MethodOverrideMiddleware | ||
*/ | ||
class MethodOverrideMiddlewareTest extends TestCase | ||
{ | ||
public function testHeader() | ||
{ | ||
$mw = new MethodOverrideMiddleware(); | ||
|
||
$uri = new Uri('http', 'example.com'); | ||
$headers = new Headers([ | ||
'HTTP_X_HTTP_METHOD_OVERRIDE' => 'PUT', | ||
]); | ||
$request = new Request('GET', $uri, $headers, [], [], new RequestBody()); | ||
$response = new Response(); | ||
|
||
$next = function (ServerRequestInterface $req, ResponseInterface $res) { | ||
$this->assertEquals('PUT', $req->getMethod()); | ||
|
||
return $res; | ||
}; | ||
\Closure::bind($next, $this); | ||
|
||
$mw($request, $response, $next); | ||
} | ||
|
||
public function testBodyParam() | ||
{ | ||
$mw = new MethodOverrideMiddleware(); | ||
|
||
$uri = new Uri('http', 'example.com'); | ||
$body = new RequestBody(); | ||
$body->write('_METHOD=PUT'); | ||
$headers = new Headers([ | ||
'Content-Type' => 'application/x-www-form-urlencoded', | ||
]); | ||
$request = new Request('POST', $uri, $headers, [], [], $body); | ||
$response = new Response(); | ||
|
||
$next = function (ServerRequestInterface $req, ResponseInterface $res) { | ||
$this->assertEquals('PUT', $req->getMethod()); | ||
|
||
return $res; | ||
}; | ||
\Closure::bind($next, $this); | ||
|
||
$mw($request, $response, $next); | ||
} | ||
|
||
public function testHeaderPreferred() | ||
{ | ||
$mw = new MethodOverrideMiddleware(); | ||
|
||
$uri = new Uri('http', 'example.com'); | ||
$body = new RequestBody(); | ||
$body->write('_METHOD=PUT'); | ||
$headers = new Headers([ | ||
'Content-Type' => 'application/x-www-form-urlencoded', | ||
'HTTP_X_HTTP_METHOD_OVERRIDE' => 'DELETE', | ||
]); | ||
$request = new Request('POST', $uri, $headers, [], [], $body); | ||
$response = new Response(); | ||
|
||
$next = function (ServerRequestInterface $req, ResponseInterface $res) { | ||
$this->assertEquals('DELETE', $req->getMethod()); | ||
|
||
return $res; | ||
}; | ||
\Closure::bind($next, $this); | ||
|
||
$mw($request, $response, $next); | ||
} | ||
|
||
public function testNoOverride() | ||
{ | ||
$mw = new MethodOverrideMiddleware(); | ||
|
||
$uri = new Uri('http', 'example.com'); | ||
$request = new Request('POST', $uri, new Headers(), [], [], new RequestBody()); | ||
$response = new Response(); | ||
|
||
$next = function (ServerRequestInterface $req, ResponseInterface $res) { | ||
$this->assertEquals('POST', $req->getMethod()); | ||
|
||
return $res; | ||
}; | ||
\Closure::bind($next, $this); | ||
|
||
$mw($request, $response, $next); | ||
} | ||
} |