diff --git a/src/PhpDebugBarMiddleware.php b/src/PhpDebugBarMiddleware.php index 4bb20ee..6856bb8 100644 --- a/src/PhpDebugBarMiddleware.php +++ b/src/PhpDebugBarMiddleware.php @@ -48,7 +48,11 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res $debugBarBody = $this->debugBarRenderer->render(); if ($this->isHtmlResponse($outResponse)) { - $outResponse->getBody()->write($debugBarHead . $debugBarBody); + $body = $outResponse->getBody(); + if (! $body->eof() && $body->isSeekable()) { + $body->seek(0, SEEK_END); + } + $body->write($debugBarHead . $debugBarBody); return $outResponse; } diff --git a/test/PhpDebugBarMiddlewareTest.php b/test/PhpDebugBarMiddlewareTest.php index 5169a55..1f6766c 100644 --- a/test/PhpDebugBarMiddlewareTest.php +++ b/test/PhpDebugBarMiddlewareTest.php @@ -80,4 +80,25 @@ public function testAttachToHtmlResponse() $this->assertSame($response, $result); $this->assertSame("ResponseBodyRenderHeadRenderBody", (string) $result->getBody()); } + + public function testAppendsToEndOfHtmlResponse() + { + $html = 'FooContent'; + $request = new ServerRequest([], [], null, null, 'php://input', ['Accept' => 'text/html']); + $response = new Response\HtmlResponse($html); + $calledOut = false; + $outFunction = function ($request, $response) use (&$calledOut) { + $calledOut = true; + return $response; + }; + + $this->debugbarRenderer->expects($this->once())->method('renderHead')->willReturn('RenderHead'); + $this->debugbarRenderer->expects($this->once())->method('render')->willReturn('RenderBody'); + + $result = call_user_func($this->middleware, $request, $response, $outFunction); + + $this->assertTrue($calledOut, 'Out is not called'); + $this->assertSame($response, $result); + $this->assertSame($html . 'RenderHeadRenderBody', (string) $result->getBody()); + } }