Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/115' into develop
Browse files Browse the repository at this point in the history
Forward port #115
  • Loading branch information
weierophinney committed Dec 16, 2015
2 parents 1a2c690 + 58d548e commit 82bae1f
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ All notable changes to this project will be documented in this file, in reverse
- [#113](https://github.com/zendframework/zend-diactoros/pull/113) fixes an
issue in the response serializer, ensuring that the status code in the
deserialized response is an integer.
- [#115](https://github.com/zendframework/zend-diactoros/pull/115) fixes an
issue in the various text-basd response types (`TextResponse`, `HtmlResponse`,
and `JsonResponse`); due to the fact that the constructor was not
rewinding the message body stream, `getContents()` was thus returning `null`,
as the pointer was at the end of the stream. The constructor now rewinds the
stream after populating it in the constructor.

## 1.3.0 - 2015-12-15

Expand Down
1 change: 1 addition & 0 deletions src/Response/HtmlResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ private function createBody($html)

$body = new Stream('php://temp', 'wb+');
$body->write($html);
$body->rewind();
return $body;
}
}
1 change: 1 addition & 0 deletions src/Response/JsonResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function __construct(
) {
$body = new Stream('php://temp', 'wb+');
$body->write($this->jsonEncode($data, $encodingOptions));
$body->rewind();

$headers = $this->injectContentType('application/json', $headers);

Expand Down
1 change: 1 addition & 0 deletions src/Response/TextResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ private function createBody($text)

$body = new Stream('php://temp', 'wb+');
$body->write($text);
$body->rewind();
return $body;
}
}
9 changes: 9 additions & 0 deletions test/Response/HtmlResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,13 @@ public function testRaisesExceptionforNonStringNonStreamBodyContent($body)
{
$response = new HtmlResponse($body);
}

public function testConstructorRewindsBodyStream()
{
$html = '<p>test data</p>';
$response = new HtmlResponse($html);

$actual = $response->getBody()->getContents();
$this->assertEquals($html, $actual);
}
}
9 changes: 9 additions & 0 deletions test/Response/JsonResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,13 @@ public function testUsesSaneDefaultJsonEncodingFlags($value, $key)
sprintf('Did not encode %s properly; expected (%s), received (%s)', $key, $expected, $contents)
);
}

public function testConstructorRewindsBodyStream()
{
$json = ['test' => 'data'];
$response = new JsonResponse($json);

$actual = json_decode($response->getBody()->getContents(), true);
$this->assertEquals($json, $actual);
}
}
12 changes: 12 additions & 0 deletions test/Response/TextResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,16 @@ public function testRaisesExceptionforNonStringNonStreamBodyContent($body)
{
new TextResponse($body);
}

/**
* @group 115
*/
public function testConstructorRewindsBodyStream()
{
$text = 'test data';
$response = new TextResponse($text);

$actual = $response->getBody()->getContents();
$this->assertEquals($text, $actual);
}
}

0 comments on commit 82bae1f

Please sign in to comment.