Skip to content

Commit

Permalink
header replace set false will throw 500 exception
Browse files Browse the repository at this point in the history
  • Loading branch information
Recca Tsai committed Mar 6, 2017
1 parent 153e895 commit 1105a7e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 18 deletions.
8 changes: 4 additions & 4 deletions src/DebuggerManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,24 +128,24 @@ 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();
});
break;
default:
$headers = [
'content-type' => 'text/javascript; charset=utf-8',
'Content-Type' => 'text/javascript; charset=utf-8',
];
$content = $this->dispatch();
break;
}

return [
array_merge($headers, [
'content-length' => strlen($content),
'Content-Length' => strlen($content),
]),
$content,
];
Expand Down
13 changes: 11 additions & 2 deletions src/Middleware/RenderBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
22 changes: 11 additions & 11 deletions tests/DebuggerManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand All @@ -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'));
Expand All @@ -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'));
Expand Down Expand Up @@ -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()));
Expand Down
15 changes: 14 additions & 1 deletion tests/Middleware/RenderBarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ protected function tearDown()
m::close();
}

/**
* @runInSeparateProcess
*/
public function testHandleAssets()
{
$renderBar = new RenderBar(
Expand All @@ -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));
Expand Down

0 comments on commit 1105a7e

Please sign in to comment.