Created middleware to parse PUT request bodies in rest requests

This commit is contained in:
Alejandro Celaya 2016-08-21 13:52:15 +02:00
parent b6fee0ebaf
commit 372488cbb4
4 changed files with 134 additions and 0 deletions

View file

@ -18,7 +18,9 @@ return [
Action\ResolveUrlAction::class => AnnotatedFactory::class, Action\ResolveUrlAction::class => AnnotatedFactory::class,
Action\GetVisitsAction::class => AnnotatedFactory::class, Action\GetVisitsAction::class => AnnotatedFactory::class,
Action\ListShortcodesAction::class => AnnotatedFactory::class, Action\ListShortcodesAction::class => AnnotatedFactory::class,
Action\EditTagsAction::class => AnnotatedFactory::class,
Middleware\BodyParserMiddleware::class => AnnotatedFactory::class,
Middleware\CrossDomainMiddleware::class => InvokableFactory::class, Middleware\CrossDomainMiddleware::class => InvokableFactory::class,
Middleware\CheckAuthenticationMiddleware::class => AnnotatedFactory::class, Middleware\CheckAuthenticationMiddleware::class => AnnotatedFactory::class,
], ],

View file

@ -7,6 +7,7 @@ return [
'rest' => [ 'rest' => [
'path' => '/rest', 'path' => '/rest',
'middleware' => [ 'middleware' => [
Middleware\BodyParserMiddleware::class,
Middleware\CheckAuthenticationMiddleware::class, Middleware\CheckAuthenticationMiddleware::class,
Middleware\CrossDomainMiddleware::class, Middleware\CrossDomainMiddleware::class,
], ],

View file

@ -0,0 +1,52 @@
<?php
namespace Shlinkio\Shlink\Rest\Middleware;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Zend\Stratigility\MiddlewareInterface;
class BodyParserMiddleware implements MiddlewareInterface
{
/**
* Process an incoming request and/or response.
*
* Accepts a server-side request and a response instance, and does
* something with them.
*
* If the response is not complete and/or further processing would not
* interfere with the work done in the middleware, or if the middleware
* wants to delegate to another process, it can use the `$out` callable
* if present.
*
* If the middleware does not return a value, execution of the current
* request is considered complete, and the response instance provided will
* be considered the response to return.
*
* Alternately, the middleware may return a response instance.
*
* Often, middleware will `return $out();`, with the assumption that a
* later middleware will return a response.
*
* @param Request $request
* @param Response $response
* @param null|callable $out
* @return null|Response
*/
public function __invoke(Request $request, Response $response, callable $out = null)
{
$method = $request->getMethod();
if (! in_array($method, ['PUT', 'PATCH'])) {
return $out($request, $response);
}
$contentType = $request->getHeaderLine('Content-type');
$rawBody = (string) $request->getBody();
if (in_array($contentType, ['application/json', 'text/json', 'application/x-json'])) {
return $out($request->withParsedBody(json_decode($rawBody, true)), $response);
}
$parsedBody = [];
parse_str($rawBody, $parsedBody);
return $out($request->withParsedBody($parsedBody), $response);
}
}

View file

@ -0,0 +1,79 @@
<?php
namespace ShlinkioTest\Shlink\Rest\Middleware;
use PHPUnit_Framework_TestCase as TestCase;
use Psr\Http\Message\ServerRequestInterface as Request;
use Shlinkio\Shlink\Rest\Middleware\BodyParserMiddleware;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\Stream;
class BodyParserMiddlewareTest extends TestCase
{
/**
* @var BodyParserMiddleware
*/
private $middleware;
public function setUp()
{
$this->middleware = new BodyParserMiddleware();
}
/**
* @test
*/
public function requestsFromOtherMethodsJustFallbackToNextMiddleware()
{
$request = ServerRequestFactory::fromGlobals()->withMethod('GET');
$test = $this;
$this->middleware->__invoke($request, new Response(), function ($req, $resp) use ($test, $request) {
$test->assertSame($request, $req);
});
$request = $request->withMethod('POST');
$test = $this;
$this->middleware->__invoke($request, new Response(), function ($req, $resp) use ($test, $request) {
$test->assertSame($request, $req);
});
}
/**
* @test
*/
public function jsonRequestsAreJsonDecoded()
{
$body = new Stream('php://temp', 'wr');
$body->write('{"foo": "bar", "bar": ["one", 5]}');
$request = ServerRequestFactory::fromGlobals()->withMethod('PUT')
->withBody($body)
->withHeader('content-type', 'application/json');
$test = $this;
$this->middleware->__invoke($request, new Response(), function (Request $req, $resp) use ($test, $request) {
$test->assertNotSame($request, $req);
$test->assertEquals([
'foo' => 'bar',
'bar' => ['one', 5],
], $req->getParsedBody());
});
}
/**
* @test
*/
public function regularRequestsAreUrlDecoded()
{
$body = new Stream('php://temp', 'wr');
$body->write('foo=bar&bar[]=one&bar[]=5');
$request = ServerRequestFactory::fromGlobals()->withMethod('PUT')
->withBody($body);
$test = $this;
$this->middleware->__invoke($request, new Response(), function (Request $req, $resp) use ($test, $request) {
$test->assertNotSame($request, $req);
$test->assertEquals([
'foo' => 'bar',
'bar' => ['one', 5],
], $req->getParsedBody());
});
}
}