Skip to content

Commit

Permalink
Update middleware to implement MiddlewareInterface.
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed May 30, 2019
1 parent 855fb59 commit 20b65b7
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 41 deletions.
20 changes: 12 additions & 8 deletions src/Middleware/DebugKitMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@
use Cake\Core\Configure;
use Cake\Event\EventManager;
use DebugKit\ToolbarService;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

/**
* PSR-7 Middleware that enables DebugKit for the layers below.
* Middleware that enables DebugKit for the layers below.
*/
class DebugKitMiddleware
class DebugKitMiddleware implements MiddlewareInterface
{
/**
* @var \DebugKit\ToolbarService
Expand All @@ -44,19 +48,19 @@ public function __construct(?ToolbarService $service = null)
* DebugKit will augment the response and add the toolbar if possible.
*
* @param \Psr\Http\Message\ServerRequestInterface $request The request.
* @param \Psr\Http\Message\ResponseInterface $response The response.
* @param callable $next Callback to invoke the next middleware.
* @return \Psr\Http\Message\ResponseInterface A response
* @param \Psr\Http\Server\RequestHandlerInterface $handler The request handler.
* @return \Psr\Http\Message\ResponseInterface A response.
*/
public function __invoke($request, $response, $next)
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$response = $handler->handle($request);

if (!$this->service->isEnabled()) {
return $next($request, $response);
return $response;
}

$this->service->loadPanels();
$this->service->initializePanels();
$response = $next($request, $response);
$row = $this->service->saveData($request, $response);
if (!$row) {
return $response;
Expand Down
82 changes: 49 additions & 33 deletions tests/TestCase/Middleware/DebugKitMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
use DebugKit\Middleware\DebugKitMiddleware;
use Psr\Http\Server\RequestHandlerInterface;

/**
* Test the middleware object
Expand Down Expand Up @@ -66,6 +67,15 @@ public function tearDown(): void
Configure::write('DebugKit', $this->oldConfig);
}

protected function handler()
{
$handler = $this->getMockBuilder(RequestHandlerInterface::class)
->setMethods(['handle'])
->getMock();

return $handler;
}

/**
* Ensure data is saved for HTML requests
*
Expand All @@ -83,12 +93,13 @@ public function testInvokeSaveData()
'body' => '<html><title>test</title><body><p>some text</p></body>',
]);

$layer = new DebugKitMiddleware();
$next = function ($req, $res) {
return $res;
};
$handler = $this->handler();
$handler->expects($this->once())
->method('handle')
->willReturn($response);

$response = $layer($request, $response, $next);
$middleware = new DebugKitMiddleware();
$response = $middleware->process($request, $handler);
$this->assertInstanceOf(Response::class, $response, 'Should return the response');

$requests = TableRegistry::get('DebugKit.Requests');
Expand All @@ -115,8 +126,8 @@ public function testInvokeSaveData()
'<script id="__debug_kit" data-id="' . $result->id . '" ' .
'data-url="http://localhost/" src="/debug_kit/js/toolbar.js?' . $timeStamp . '"></script>' .
'</body>';
$body = $response->getBody();
$this->assertTextEquals($expected, '' . $body);
$body = (string)$response->getBody();
$this->assertTextEquals($expected, $body);
}

/**
Expand All @@ -135,24 +146,27 @@ public function testInvokeNoModifyBinaryResponse()
'type' => 'text/html',
]);

$layer = new DebugKitMiddleware();
$next = function ($req, $res) {
$stream = new CallbackStream(function () {
return 'hi!';
});

return $res->withBody($stream);
};
$result = $layer($request, $response, $next);
$handler = $this->handler();
$handler->expects($this->once())
->method('handle')
->will($this->returnCallback(function ($req) use ($response) {
$stream = new CallbackStream(function () {
return 'hi!';
});

return $response->withBody($stream);
}));
$middleware = new DebugKitMiddleware();
$result = $middleware->process($request, $handler);
$this->assertInstanceOf(Response::class, $result, 'Should return a response');

$requests = TableRegistry::get('DebugKit.Requests');
$total = $requests->find()->where(['url' => '/articles'])->count();

$this->assertEquals(1, $total, 'Should track response');
$body = $result->getBody();
$this->assertStringNotContainsString('__debug_kit', '' . $body);
$this->assertStringNotContainsString('<script', '' . $body);
$body = (string)$result->getBody();
$this->assertStringNotContainsString('__debug_kit', $body);
$this->assertStringNotContainsString('<script', $body);
}

/**
Expand All @@ -172,19 +186,20 @@ public function testInvokeNoModifyNonHtmlResponse()
'body' => 'OK',
]);

$layer = new DebugKitMiddleware();
$next = function ($req, $res) {
return $res;
};
$result = $layer($request, $response, $next);
$handler = $this->handler();
$handler->expects($this->once())
->method('handle')
->willReturn($response);
$middleware = new DebugKitMiddleware();
$result = $middleware->process($request, $handler);
$this->assertInstanceOf(Response::class, $result, 'Should return a response');

$requests = TableRegistry::get('DebugKit.Requests');
$total = $requests->find()->where(['url' => '/articles'])->count();

$this->assertEquals(1, $total, 'Should track response');
$body = $result->getBody();
$this->assertSame('OK', '' . $body);
$body = (string)$result->getBody();
$this->assertSame('OK', $body);
}

/**
Expand All @@ -205,19 +220,20 @@ public function testInvokeNoModifyRequestAction()
'body' => '<body><p>things</p></body>',
]);

$layer = new DebugKitMiddleware();
$next = function ($req, $res) {
return $res;
};
$result = $layer($request, $response, $next);
$handler = $this->handler();
$handler->expects($this->once())
->method('handle')
->willReturn($response);
$middleware = new DebugKitMiddleware();
$result = $middleware->process($request, $handler);
$this->assertInstanceOf(Response::class, $result, 'Should return a response');

$requests = TableRegistry::get('DebugKit.Requests');
$total = $requests->find()->where(['url' => '/articles'])->count();

$this->assertEquals(0, $total, 'Should not track sub-requests');
$body = $result->getBody();
$this->assertStringNotContainsString('<script', '' . $body);
$body = (string)$result->getBody();
$this->assertStringNotContainsString('<script', $body);
}

/**
Expand Down

0 comments on commit 20b65b7

Please sign in to comment.