-
-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #220 from WyriHaximus/middleware-body-parser
Middleware request body parser
- Loading branch information
Showing
3 changed files
with
197 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,27 @@ | ||
<?php | ||
|
||
namespace React\Http\Middleware; | ||
|
||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
final class RequestBodyParserMiddleware | ||
{ | ||
public function __invoke(ServerRequestInterface $request, $next) | ||
{ | ||
$type = strtolower($request->getHeaderLine('Content-Type')); | ||
|
||
if ($type === 'application/x-www-form-urlencoded') { | ||
return $next($this->parseFormUrlencoded($request)); | ||
} | ||
|
||
return $next($request); | ||
} | ||
|
||
private function parseFormUrlencoded(ServerRequestInterface $request) | ||
{ | ||
$ret = array(); | ||
parse_str((string)$request->getBody(), $ret); | ||
|
||
return $request->withParsedBody($ret); | ||
} | ||
} |
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,136 @@ | ||
<?php | ||
|
||
namespace React\Tests\Http\Middleware; | ||
|
||
use Psr\Http\Message\ServerRequestInterface; | ||
use React\Http\Middleware\RequestBodyParserMiddleware; | ||
use React\Http\ServerRequest; | ||
use React\Tests\Http\TestCase; | ||
|
||
final class RequestBodyParserMiddlewareTest extends TestCase | ||
{ | ||
public function testFormUrlencodedParsing() | ||
{ | ||
$middleware = new RequestBodyParserMiddleware(); | ||
$request = new ServerRequest( | ||
'POST', | ||
'https://example.com/', | ||
array( | ||
'Content-Type' => 'application/x-www-form-urlencoded', | ||
), | ||
'hello=world' | ||
); | ||
|
||
/** @var ServerRequestInterface $parsedRequest */ | ||
$parsedRequest = $middleware( | ||
$request, | ||
function (ServerRequestInterface $request) { | ||
return $request; | ||
} | ||
); | ||
|
||
$this->assertSame( | ||
array('hello' => 'world'), | ||
$parsedRequest->getParsedBody() | ||
); | ||
$this->assertSame('hello=world', (string)$parsedRequest->getBody()); | ||
} | ||
|
||
public function testFormUrlencodedParsingIgnoresCaseForHeadersButRespectsContentCase() | ||
{ | ||
$middleware = new RequestBodyParserMiddleware(); | ||
$request = new ServerRequest( | ||
'POST', | ||
'https://example.com/', | ||
array( | ||
'CONTENT-TYPE' => 'APPLICATION/X-WWW-Form-URLEncoded', | ||
), | ||
'Hello=World' | ||
); | ||
|
||
/** @var ServerRequestInterface $parsedRequest */ | ||
$parsedRequest = $middleware( | ||
$request, | ||
function (ServerRequestInterface $request) { | ||
return $request; | ||
} | ||
); | ||
|
||
$this->assertSame( | ||
array('Hello' => 'World'), | ||
$parsedRequest->getParsedBody() | ||
); | ||
$this->assertSame('Hello=World', (string)$parsedRequest->getBody()); | ||
} | ||
|
||
public function testFormUrlencodedParsingNestedStructure() | ||
{ | ||
$middleware = new RequestBodyParserMiddleware(); | ||
$request = new ServerRequest( | ||
'POST', | ||
'https://example.com/', | ||
array( | ||
'Content-Type' => 'application/x-www-form-urlencoded', | ||
), | ||
'foo=bar&baz[]=cheese&bar[]=beer&bar[]=wine&market[fish]=salmon&market[meat][]=beef&market[meat][]=chicken&market[]=bazaar' | ||
); | ||
|
||
/** @var ServerRequestInterface $parsedRequest */ | ||
$parsedRequest = $middleware( | ||
$request, | ||
function (ServerRequestInterface $request) { | ||
return $request; | ||
} | ||
); | ||
|
||
$this->assertSame( | ||
array( | ||
'foo' => 'bar', | ||
'baz' => array( | ||
'cheese', | ||
), | ||
'bar' => array( | ||
'beer', | ||
'wine', | ||
), | ||
'market' => array( | ||
'fish' => 'salmon', | ||
'meat' => array( | ||
'beef', | ||
'chicken', | ||
), | ||
0 => 'bazaar', | ||
), | ||
), | ||
$parsedRequest->getParsedBody() | ||
); | ||
$this->assertSame('foo=bar&baz[]=cheese&bar[]=beer&bar[]=wine&market[fish]=salmon&market[meat][]=beef&market[meat][]=chicken&market[]=bazaar', (string)$parsedRequest->getBody()); | ||
} | ||
|
||
public function testDoesNotParseJsonByDefault() | ||
{ | ||
$middleware = new RequestBodyParserMiddleware(); | ||
$request = new ServerRequest( | ||
'POST', | ||
'https://example.com/', | ||
array( | ||
'Content-Type' => 'application/json', | ||
), | ||
'{"hello":"world"}' | ||
); | ||
|
||
/** @var ServerRequestInterface $parsedRequest */ | ||
$parsedRequest = $middleware( | ||
$request, | ||
function (ServerRequestInterface $request) { | ||
return $request; | ||
} | ||
); | ||
|
||
$this->assertSame( | ||
null, | ||
$parsedRequest->getParsedBody() | ||
); | ||
$this->assertSame('{"hello":"world"}', (string)$parsedRequest->getBody()); | ||
} | ||
} |