Skip to content

Commit

Permalink
Removed the ability to add custom parsers for custom body types
Browse files Browse the repository at this point in the history
  • Loading branch information
WyriHaximus authored and clue committed Sep 25, 2017
1 parent 485793c commit 3c94b05
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 56 deletions.
39 changes: 10 additions & 29 deletions src/Middleware/RequestBodyParserMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,22 @@

final class RequestBodyParserMiddleware
{
private $types = array();

public function __construct()
{
$this->addType('application/x-www-form-urlencoded', function (ServerRequestInterface $request) {
$ret = array();
parse_str((string)$request->getBody(), $ret);

return $request->withParsedBody($ret);
});
}

public function addType($type, $callback)
{
$this->types[$type] = $callback;
}

public function __invoke(ServerRequestInterface $request, $next)
{
$type = $request->getHeaderLine('Content-Type');

if (!isset($this->types[$type])) {
return $next($request);
}

try {
$parser = $this->types[$type];
/** @var ServerRequestInterface $request */
$request = $parser($request);
} catch (\Exception $e) {
return $next($request);
} catch (\Throwable $t) {
return $next($request);
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);
}
}
54 changes: 27 additions & 27 deletions tests/Middleware/RequestBodyParserMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,6 @@

final class RequestBodyParserMiddlewareTest extends TestCase
{
public function testParse()
{
$middleware = new RequestBodyParserMiddleware();
$middleware->addType('react/http', function (ServerRequestInterface $request) {
return $request->withParsedBody('parsed');
});

$request = new ServerRequest(
200,
'https://example.com/',
array(
'Content-Type' => 'react/http',
),
'not yet parsed'
);

/** @var ServerRequestInterface $parsedRequest */
$parsedRequest = $middleware(
$request,
function (ServerRequestInterface $request) {
return $request;
}
);

$this->assertSame('parsed', $parsedRequest->getParsedBody());
$this->assertSame('not yet parsed', (string)$parsedRequest->getBody());
}
public function testFormUrlencodedParsing()
{
$middleware = new RequestBodyParserMiddleware();
Expand Down Expand Up @@ -83,4 +56,31 @@ function (ServerRequestInterface $request) {
);
$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());
}
}

0 comments on commit 3c94b05

Please sign in to comment.