Skip to content

Commit

Permalink
Removed $keepOriginalBody constructor argument and renamed $value to …
Browse files Browse the repository at this point in the history
…$parser
  • Loading branch information
WyriHaximus authored and clue committed Sep 25, 2017
1 parent a4e9092 commit 485793c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 42 deletions.
17 changes: 5 additions & 12 deletions src/Middleware/RequestBodyParserMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,10 @@

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

/**
* @param bool $keepOriginalBody Keep the original body after parsing or not
*/
public function __construct($keepOriginalBody = false)
public function __construct()
{
$this->keepOriginalBody = $keepOriginalBody;
$this->addType('application/x-www-form-urlencoded', function (ServerRequestInterface $request) {
$ret = array();
parse_str((string)$request->getBody(), $ret);
Expand All @@ -37,15 +32,13 @@ public function __invoke(ServerRequestInterface $request, $next)
}

try {
$value = $this->types[$type];
$parser = $this->types[$type];
/** @var ServerRequestInterface $request */
$request = $value($request);
$request = $parser($request);
} catch (\Exception $e) {
return $next($request);
}

if (!$this->keepOriginalBody) {
$request = $request->withBody(Psr7\stream_for());
} catch (\Throwable $t) {
return $next($request);
}

return $next($request);
Expand Down
31 changes: 1 addition & 30 deletions tests/Middleware/RequestBodyParserMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,38 +37,9 @@ function (ServerRequestInterface $request) {
}
);

$this->assertSame('parsed', $parsedRequest->getParsedBody());
$this->assertSame('', (string)$parsedRequest->getBody());
}

public function testParseKeepBody()
{
$middleware = new RequestBodyParserMiddleware(true);
$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 @@ -110,6 +81,6 @@ function (ServerRequestInterface $request) {
),
$parsedRequest->getParsedBody()
);
$this->assertSame('', (string)$parsedRequest->getBody());
$this->assertSame('foo=bar&baz[]=cheese&bar[]=beer&bar[]=wine&market[fish]=salmon&market[meat][]=beef&market[meat][]=chicken&market[]=bazaar', (string)$parsedRequest->getBody());
}
}

0 comments on commit 485793c

Please sign in to comment.