Skip to content

Commit

Permalink
Fix early echo of stream responses.
Browse files Browse the repository at this point in the history
Calling body() now invokes the callback which outputs and sends headers.
That isn't what we want to do when we're trying to inject HTML. Instead
use getBody() to access the stream object.
  • Loading branch information
markstory committed Jan 13, 2017
1 parent 9e5eaa6 commit 4a741d9
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/ToolbarService.php
Expand Up @@ -210,8 +210,8 @@ public function injectScripts($row, $response)
if (strpos($response->type(), 'html') === false) {
return $response;
}
$body = $response->body();
if (!is_string($body)) {
$body = $response->getBody();
if (!$body->isSeekable()) {
return $response;
}
$pos = strrpos($body, '</body>');
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Routing/Filter/DebugBarFilterTest.php
Expand Up @@ -130,13 +130,13 @@ public function testAfterDispatchIgnoreStreamBodies()
'type' => 'text/html',
]);
$response->body(function () {
echo 'I am a teapot!';
return 'I am a teapot!';
});

$bar = new DebugBarFilter($this->events, []);
$event = new Event('Dispatcher.afterDispatch', $bar, compact('request', 'response'));
$bar->afterDispatch($event);
$this->assertInstanceOf('Closure', $response->body());
$this->assertEquals('I am a teapot!', $response->body());
}

/**
Expand Down
5 changes: 2 additions & 3 deletions tests/TestCase/ToolbarServiceTest.php
Expand Up @@ -198,16 +198,15 @@ public function testInjectScriptsStreamBodies()
'type' => 'text/html',
]);
$response->body(function () {
echo 'I am a teapot!';
return 'I am a teapot!';
});

$bar = new ToolbarService($this->events, []);
$row = new RequestEntity(['id' => 'abc123']);

$result = $bar->injectScripts($row, $response);
$this->assertInstanceOf('Cake\Network\Response', $result);
$this->assertInstanceOf('Closure', $result->body());
$this->assertInstanceOf('Closure', $response->body());
$this->assertEquals('I am a teapot!', $result->body());
}


Expand Down

0 comments on commit 4a741d9

Please sign in to comment.