From ffb22ed10bcc7e9dbbb60b9670ee9a293e1f5ac9 Mon Sep 17 00:00:00 2001 From: Nikolay Slyunkov Date: Wed, 16 Dec 2015 20:04:20 +0400 Subject: [PATCH] Added stream rewind in JsonResponse constructor --- src/Response/JsonResponse.php | 1 + test/Response/JsonResponseTest.php | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/src/Response/JsonResponse.php b/src/Response/JsonResponse.php index 5e414580..c247fbb4 100644 --- a/src/Response/JsonResponse.php +++ b/src/Response/JsonResponse.php @@ -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); diff --git a/test/Response/JsonResponseTest.php b/test/Response/JsonResponseTest.php index 144d7446..f65e9b4b 100644 --- a/test/Response/JsonResponseTest.php +++ b/test/Response/JsonResponseTest.php @@ -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); + } }