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

Commit

Permalink
Added stream rewind in JsonResponse constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolay Slyunkov committed Dec 16, 2015
1 parent bbd7ffe commit ffb22ed
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
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);
}
}

0 comments on commit ffb22ed

Please sign in to comment.