Commit 23908a15 authored by François Agneray's avatar François Agneray
Browse files

#67 => done

parent 93b1de0f
Pipeline #5155 passed with stages
in 2 minutes and 1 second
......@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- #64: Data management system
### Changed
- #67: Body Parsing and Content Length slim's middlewares
- #61: Docker images are stored on the gitlab internal registry
- #63: Update composer.json dependencies and using php version 8
- #65: Delete cascade (instance, dataset, group, settings_select)
......
......@@ -10,8 +10,8 @@
*/
declare(strict_types=1);
$app->add(new App\Middleware\JsonBodyParserMiddleware());
$app->add(new App\Middleware\ContentTypeJsonMiddleware());
$app->add(new Slim\Middleware\ContentLengthMiddleware());
$app->add(new App\Middleware\CorsMiddleware());
$app->add(new App\Middleware\MetamodelSqlLoggerMiddleware($container->get('logger'), $container->get('em')));
$app->add(new App\Middleware\AuthorizationMiddleware($container->get(SETTINGS)['token']));
......@@ -30,6 +30,9 @@ require __DIR__ . '/../app/dependencies.php';
AppFactory::setContainer($container);
$app = AppFactory::create();
// Parse json, form data and xml
$app->addBodyParsingMiddleware();
$ded = $container->get(SETTINGS)['displayErrorDetails'];
if (is_string($ded)) {
$displayErrorDetails = $ded === 'true';
......
<?php
/*
* This file is part of Anis Server.
*
* (c) Laboratoire d'Astrophysique de Marseille / CNRS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace App\Middleware;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
use Psr\Http\Server\MiddlewareInterface;
/**
* Middleware to handle request parsed body
*
* @author François Agneray <francois.agneray@lam.fr>
* @package App\Middleware
*/
final class JsonBodyParserMiddleware implements MiddlewareInterface
{
/**
* Read body http request data if request content-type is application/json
* And set parsed body with json decoded information
*
* @param ServerRequest $request PSR-7 request
* @param RequestHandler $handler PSR-15 request handler
*
* @return Response
*/
public function process(Request $request, RequestHandler $handler): Response
{
$contentType = $request->getHeaderLine('Content-Type');
if (strstr($contentType, 'application/json')) {
$contents = json_decode(file_get_contents('php://input'), true);
if (json_last_error() === JSON_ERROR_NONE) {
$request = $request->withParsedBody($contents);
}
}
return $handler->handle($request);
}
}
<?php
/*
* This file is part of Anis Server.
*
* (c) Laboratoire d'Astrophysique de Marseille / CNRS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace App\Tests\Middleware;
use PHPUnit\Framework\TestCase;
use Nyholm\Psr7\ServerRequest;
use Nyholm\Psr7\Response;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
final class JsonBodyParserMiddlewareTest extends TestCase
{
public function testJsonBodyParser(): void
{
$request = new ServerRequest('GET', '/');
$requestHandler = $this->getMockBuilder(RequestHandler::class)
->disableOriginalConstructor()
->setMethods(['handle'])
->getMock();
$requestHandler->method('handle')
->with($this->identicalTo($request))
->will($this->returnValue(new Response(200, [], 'Hello world')));
$jsonBodyParserMiddleware = new \App\Middleware\JsonBodyParserMiddleware(array(), true);
$response = $jsonBodyParserMiddleware->process($request, $requestHandler);
$this->assertSame((string) $response->getBody(), 'Hello world');
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment