From 1105a7e42fd4de5befb4ff02aa16ed7e060a5aad Mon Sep 17 00:00:00 2001 From: Recca Tsai Date: Mon, 6 Mar 2017 19:58:49 +0800 Subject: [PATCH] header replace set false will throw 500 exception --- src/DebuggerManager.php | 8 ++++---- src/Middleware/RenderBar.php | 13 +++++++++++-- tests/DebuggerManagerTest.php | 22 +++++++++++----------- tests/Middleware/RenderBarTest.php | 15 ++++++++++++++- 4 files changed, 40 insertions(+), 18 deletions(-) diff --git a/src/DebuggerManager.php b/src/DebuggerManager.php index 7ca3237..d656b25 100644 --- a/src/DebuggerManager.php +++ b/src/DebuggerManager.php @@ -128,8 +128,8 @@ public function dispatchAssets($type) case 'js': case 'assets': $headers = [ - 'content-type' => $type === 'css' ? 'text/css; charset=utf-8' : 'text/javascript; charset=utf-8', - 'cache-control' => 'max-age=86400', + 'Content-Type' => $type === 'css' ? 'text/css; charset=utf-8' : 'text/javascript; charset=utf-8', + 'Cache-Control' => 'max-age=86400', ]; $content = $this->renderBuffer(function () { return $this->bar->dispatchAssets(); @@ -137,7 +137,7 @@ public function dispatchAssets($type) break; default: $headers = [ - 'content-type' => 'text/javascript; charset=utf-8', + 'Content-Type' => 'text/javascript; charset=utf-8', ]; $content = $this->dispatch(); break; @@ -145,7 +145,7 @@ public function dispatchAssets($type) return [ array_merge($headers, [ - 'content-length' => strlen($content), + 'Content-Length' => strlen($content), ]), $content, ]; diff --git a/src/Middleware/RenderBar.php b/src/Middleware/RenderBar.php index 0957985..c98533a 100644 --- a/src/Middleware/RenderBar.php +++ b/src/Middleware/RenderBar.php @@ -57,12 +57,21 @@ public function __construct(DebuggerManager $debuggerManager, Dispatcher $events * @param \Illuminate\Http\Request $request * @param \Closure $next * - * @return \Symfony\Component\HttpFoundation\Response + * @return \Symfony\Component\HttpFoundation\StreamedResponse */ public function handle($request, $next) { if ($request->has('_tracy_bar') === true) { - list($headers, $content) = $this->debuggerManager->dispatchAssets($request->get('_tracy_bar')); + return $this->responseFactory->stream(function () use ($request) { + list($headers, $content) = $this->debuggerManager->dispatchAssets($request->get('_tracy_bar')); + if (headers_sent() === false) { + foreach ($headers as $name => $value) { + header(sprintf('%s: %s', $name, $value), true, 200); + } + } + echo $content; + }, 200); + return $this->responseFactory->make($content, 200, $headers); } diff --git a/tests/DebuggerManagerTest.php b/tests/DebuggerManagerTest.php index aee575a..ac45d9e 100644 --- a/tests/DebuggerManagerTest.php +++ b/tests/DebuggerManagerTest.php @@ -90,9 +90,9 @@ public function testDispatchAssetsCss() $this->assertSame([ [ - 'content-type' => 'text/css; charset=utf-8', - 'cache-control' => 'max-age=86400', - 'content-length' => strlen($content), + 'Content-Type' => 'text/css; charset=utf-8', + 'Cache-Control' => 'max-age=86400', + 'Content-Length' => strlen($content), ], $content, ], $debuggerManager->dispatchAssets('css')); @@ -113,9 +113,9 @@ public function testDispatchAssetsJs() $this->assertSame([ [ - 'content-type' => 'text/javascript; charset=utf-8', - 'cache-control' => 'max-age=86400', - 'content-length' => strlen($content), + 'Content-Type' => 'text/javascript; charset=utf-8', + 'Cache-Control' => 'max-age=86400', + 'Content-Length' => strlen($content), ], $content, ], $debuggerManager->dispatchAssets('js')); @@ -136,9 +136,9 @@ public function testDispatchAssetsAssets() $this->assertSame([ [ - 'content-type' => 'text/javascript; charset=utf-8', - 'cache-control' => 'max-age=86400', - 'content-length' => strlen($content), + 'Content-Type' => 'text/javascript; charset=utf-8', + 'Cache-Control' => 'max-age=86400', + 'Content-Length' => strlen($content), ], $content, ], $debuggerManager->dispatchAssets('assets')); @@ -169,8 +169,8 @@ public function testDispatchAssetsContentId() $this->assertSame([ [ - 'content-type' => 'text/javascript; charset=utf-8', - 'content-length' => strlen($content), + 'Content-Type' => 'text/javascript; charset=utf-8', + 'Content-Length' => strlen($content), ], $content, ], $debuggerManager->dispatchAssets(uniqid())); diff --git a/tests/Middleware/RenderBarTest.php b/tests/Middleware/RenderBarTest.php index b824d16..155d7a3 100644 --- a/tests/Middleware/RenderBarTest.php +++ b/tests/Middleware/RenderBarTest.php @@ -14,6 +14,9 @@ protected function tearDown() m::close(); } + /** + * @runInSeparateProcess + */ public function testHandleAssets() { $renderBar = new RenderBar( @@ -36,7 +39,17 @@ public function testHandleAssets() $headers = ['foo' => 'bar'], $content = 'foo', ]); - $responseFactory->shouldReceive('make')->with($content, 200, $headers)->andReturn( + $responseFactory->shouldReceive('stream')->with(m::on(function($callback) use ($content) { + ob_start(); + $callback(); + $output = ob_get_clean(); + + if (function_exists('xdebug_get_headers') === true) { + $this->assertTrue(in_array('foo: bar', xdebug_get_headers(), true)); + } + + return $content === $output; + }), 200)->andReturn( $response = m::mock('Symfony\Component\HttpFoundation\Response') ); $this->assertSame($response, $renderBar->handle($request, $next));