Skip to content

Commit

Permalink
Json flapping
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewbaggett committed Apr 14, 2024
1 parent b492d7c commit f1fe4cc
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Benzine;

use Benzine\Middleware\JsonResponseExecTimeMiddleware;
use Benzine\Middleware\JsonResponseUnpackerMiddleware;
use Benzine\Middleware\JsonValidationMiddleware;
use Benzine\ORM\Connection\Databases;
use Benzine\ORM\Laminator;
Expand Down Expand Up @@ -106,6 +107,7 @@ public function __construct()
$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';
Expand Down
107 changes: 107 additions & 0 deletions src/Middleware/JsonResponse.php
Original file line number Diff line number Diff line change
@@ -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();
}

}
24 changes: 24 additions & 0 deletions src/Middleware/JsonResponseUnpackerMiddleware.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
11 changes: 11 additions & 0 deletions tests/Traits/AppTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;
Expand All @@ -13,6 +14,8 @@
use Slim\App as SlimApp;
use Slim\Factory\ServerRequestCreatorFactory;
use Slim\Psr7\Factory\ServerRequestFactory;
use Slim\Psr7\Request;
use Slim\Psr7\Response;

/**
* Container Trait.
Expand Down Expand Up @@ -141,4 +144,12 @@ protected function assertJsonData(ResponseInterface $response, array $expected):
$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);
}
}

0 comments on commit f1fe4cc

Please sign in to comment.