Json flapping

This commit is contained in:
Greyscale 2024-04-14 18:38:17 +02:00
parent b492d7cfc1
commit f1fe4cc483
5 changed files with 145 additions and 0 deletions

View file

@ -34,6 +34,7 @@
"cocur/slugify": "^4.0",
"doctrine/annotations": "^1.10",
"donatj/flags": "^1.4",
"ergebnis/json": "^1.2",
"fakerphp/faker": "^1.14.1",
"friendsofphp/php-cs-fixer": "^3.0",
"kint-php/kint": "^4.0",

View file

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Benzine;
use Benzine\Middleware\JsonResponseExecTimeMiddleware;
use Benzine\Middleware\JsonResponseUnpackerMiddleware;
use Benzine\Middleware\JsonValidationMiddleware;
use Benzine\ORM\Connection\Databases;
use Benzine\ORM\Laminator;
@ -106,6 +107,7 @@ class App
$this->app->addBodyParsingMiddleware();
$this->app->addRoutingMiddleware();
$this->app->add($container->get(JsonResponseExecTimeMiddleware::class));
$this->app->add($container->get(JsonResponseUnpackerMiddleware::class));
// Determine if we're going to enable debug mode
$this->debugMode = $this->environmentService->get('DEBUG_MODE', 'off') == 'on';

View file

@ -0,0 +1,107 @@
<?php
declare(strict_types=1);
namespace Benzine\Middleware;
use Ergebnis\Json\Json;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Slim\Psr7\Response;
class JsonResponse implements ResponseInterface
{
protected Response $response;
public function __construct(Response $response)
{
$this->response = $response;
}
public function getJson() : Json
{
$this->getBody()->rewind();
$json = Json::fromString($this->getBody()->getContents());
$this->getBody()->rewind();
return $json;
}
public function setJson(Json $json) : self
{
$this->getBody()->rewind();
$this->getBody()->write($json->toString());
$this->getBody()->rewind();
return $this;
}
public function getProtocolVersion()
{
return $this->response->getProtocolVersion();
}
public function withProtocolVersion(string $version)
{
return $this->response->withProtocolVersion($version);
}
public function getHeaders()
{
return $this->response->getHeaders();
}
public function hasHeader(string $name)
{
return $this->response->hasHeader($name);
}
public function getHeader(string $name)
{
return $this->response->getHeader($name);
}
public function getHeaderLine(string $name)
{
return $this->response->getHeaderLine($name);
}
public function withHeader(string $name, $value)
{
return $this->response->withHeader($name, $value);
}
public function withAddedHeader(string $name, $value)
{
return $this->response->withAddedHeader($name, $value);
}
public function withoutHeader(string $name)
{
return $this->response->withoutHeader($name);
}
public function getBody()
{
return $this->response->getBody();
}
public function withBody(StreamInterface $body)
{
return $this->response->withBody($body);
}
public function getStatusCode()
{
return $this->response->getStatusCode();
}
public function withStatus(int $code, string $reasonPhrase = '')
{
return $this->response->withStatus($code, $reasonPhrase);
}
public function getReasonPhrase()
{
return $this->response->getReasonPhrase();
}
}

View file

@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
namespace Benzine\Middleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Slim\Psr7\Response;
class JsonResponseUnpackerMiddleware implements MiddlewareInterface
{
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$response = $handler->handle($request);
if(!($response->hasHeader("Content-Type") && $response->getHeader("Content-Type")[0] === "application/json") ) {
return $response;
}
return new JsonResponse($response);
}
}

View file

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Benzine\Tests\Traits;
use Benzine\App as BenzineApp;
use Benzine\Middleware\JsonResponse;
use DI\Container;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Http\Message\ResponseInterface;
@ -13,6 +14,8 @@ use Psr\Http\Message\UriInterface;
use Slim\App as SlimApp;
use Slim\Factory\ServerRequestCreatorFactory;
use Slim\Psr7\Factory\ServerRequestFactory;
use Slim\Psr7\Request;
use Slim\Psr7\Response;
/**
* Container Trait.
@ -141,4 +144,12 @@ trait AppTestTrait
$this->assertJson($actual);
$this->assertSame($expected, (array) json_decode($actual, true));
}
protected function send(Request $request) : JsonResponse|Response|ResponseInterface {
return $this
->app
->loadAllRoutes($request)
->getApp()
->handle($request);
}
}