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

Added stream rewind in JsonResponse constructor #115

Merged
merged 1 commit into from
Dec 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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);
}
}