From b2f7d6777b33a1a3a70a9a1fee766e642f6a6ff3 Mon Sep 17 00:00:00 2001 From: Marcus Winkler Date: Wed, 12 Mar 2014 20:32:00 +0000 Subject: [PATCH 1/2] throw exception if scalar json response is received --- library/Zend/Json/Server/Response.php | 4 ++++ tests/ZendTest/Json/Server/ClientTest.php | 8 ++++++++ tests/ZendTest/Json/Server/ResponseTest.php | 14 ++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/library/Zend/Json/Server/Response.php b/library/Zend/Json/Server/Response.php index 30566fdef07..c8965afdd4c 100644 --- a/library/Zend/Json/Server/Response.php +++ b/library/Zend/Json/Server/Response.php @@ -79,10 +79,14 @@ public function setOptions(array $options) * * @param string $json * @return void + * @throws Exception\RuntimeException */ public function loadJson($json) { $options = Json::decode($json, Json::TYPE_ARRAY); + if (!is_array($options)) { + throw new Exception\RuntimeException('json is not a valid response; array expected'); + } $this->setOptions($options); } diff --git a/tests/ZendTest/Json/Server/ClientTest.php b/tests/ZendTest/Json/Server/ClientTest.php index f8a09669dfc..dd7c308fecf 100644 --- a/tests/ZendTest/Json/Server/ClientTest.php +++ b/tests/ZendTest/Json/Server/ClientTest.php @@ -217,6 +217,14 @@ public function testCustomHttpClientUserAgentIsNotOverridden() $this->assertSame($expectedUserAgent, $this->httpClient->getHeader('User-Agent')); } + public function testScalarServerResponseThrowsException() + { + $response = $this->makeHttpResponseFrom('false'); + $this->httpAdapter->setResponse($response); + $this->setExpectedException('Zend\Json\Exception\ExceptionInterface'); + $this->jsonClient->call('foo'); + } + // Helpers public function setServerResponseTo($nativeVars) { diff --git a/tests/ZendTest/Json/Server/ResponseTest.php b/tests/ZendTest/Json/Server/ResponseTest.php index e9c3aa41dd8..d33937c3091 100644 --- a/tests/ZendTest/Json/Server/ResponseTest.php +++ b/tests/ZendTest/Json/Server/ResponseTest.php @@ -173,6 +173,20 @@ public function testCastToStringShouldCastToJSON() $this->assertEquals($this->response->getId(), $test['id']); } + /** + * @dataProvider provideScalarJSONResponses + */ + public function testLoadingScalarJSONResponseShouldThrowException($json) + { + $this->setExpectedException('Zend\Json\Server\Exception\RuntimeException'); + $this->response->loadJson($json); + } + + public function provideScalarJSONResponses() + { + return array(array(''), array('true'), array('null'), array('3'), array('"invalid"')); + } + public function getOptions() { return array( From 45db1d2552e8355ca4830869e88e3aaed7a41d8f Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Fri, 4 Apr 2014 00:44:32 +0200 Subject: [PATCH 2/2] Minor CS cleanups, adding `@group` annotations to tests for #5956 --- library/Zend/Json/Server/Response.php | 2 ++ tests/ZendTest/Json/Server/ClientTest.php | 3 +++ tests/ZendTest/Json/Server/ResponseTest.php | 7 +++++++ 3 files changed, 12 insertions(+) diff --git a/library/Zend/Json/Server/Response.php b/library/Zend/Json/Server/Response.php index c8965afdd4c..074f39a16c7 100644 --- a/library/Zend/Json/Server/Response.php +++ b/library/Zend/Json/Server/Response.php @@ -84,9 +84,11 @@ public function setOptions(array $options) public function loadJson($json) { $options = Json::decode($json, Json::TYPE_ARRAY); + if (!is_array($options)) { throw new Exception\RuntimeException('json is not a valid response; array expected'); } + $this->setOptions($options); } diff --git a/tests/ZendTest/Json/Server/ClientTest.php b/tests/ZendTest/Json/Server/ClientTest.php index dd7c308fecf..04999f6f78c 100644 --- a/tests/ZendTest/Json/Server/ClientTest.php +++ b/tests/ZendTest/Json/Server/ClientTest.php @@ -217,6 +217,9 @@ public function testCustomHttpClientUserAgentIsNotOverridden() $this->assertSame($expectedUserAgent, $this->httpClient->getHeader('User-Agent')); } + /** + * @group 5956 + */ public function testScalarServerResponseThrowsException() { $response = $this->makeHttpResponseFrom('false'); diff --git a/tests/ZendTest/Json/Server/ResponseTest.php b/tests/ZendTest/Json/Server/ResponseTest.php index d33937c3091..b921e789b74 100644 --- a/tests/ZendTest/Json/Server/ResponseTest.php +++ b/tests/ZendTest/Json/Server/ResponseTest.php @@ -174,6 +174,10 @@ public function testCastToStringShouldCastToJSON() } /** + * @param string $json + * + * @group 5956 + * * @dataProvider provideScalarJSONResponses */ public function testLoadingScalarJSONResponseShouldThrowException($json) @@ -182,6 +186,9 @@ public function testLoadingScalarJSONResponseShouldThrowException($json) $this->response->loadJson($json); } + /** + * @return string[][] + */ public function provideScalarJSONResponses() { return array(array(''), array('true'), array('null'), array('3'), array('"invalid"'));