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

Commit

Permalink
Merge branch 'hotfix/#5956-zend-json-server-options-enforced-as-array'
Browse files Browse the repository at this point in the history
Close #5956
  • Loading branch information
Ocramius committed Apr 3, 2014
2 parents eeb9267 + 45db1d2 commit a52e105
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
6 changes: 6 additions & 0 deletions library/Zend/Json/Server/Response.php
Expand Up @@ -79,10 +79,16 @@ 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);
}

Expand Down
11 changes: 11 additions & 0 deletions tests/ZendTest/Json/Server/ClientTest.php
Expand Up @@ -217,6 +217,17 @@ public function testCustomHttpClientUserAgentIsNotOverridden()
$this->assertSame($expectedUserAgent, $this->httpClient->getHeader('User-Agent'));
}

/**
* @group 5956
*/
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)
{
Expand Down
21 changes: 21 additions & 0 deletions tests/ZendTest/Json/Server/ResponseTest.php
Expand Up @@ -173,6 +173,27 @@ public function testCastToStringShouldCastToJSON()
$this->assertEquals($this->response->getId(), $test['id']);
}

/**
* @param string $json
*
* @group 5956
*
* @dataProvider provideScalarJSONResponses
*/
public function testLoadingScalarJSONResponseShouldThrowException($json)
{
$this->setExpectedException('Zend\Json\Server\Exception\RuntimeException');
$this->response->loadJson($json);
}

/**
* @return string[][]
*/
public function provideScalarJSONResponses()
{
return array(array(''), array('true'), array('null'), array('3'), array('"invalid"'));
}

public function getOptions()
{
return array(
Expand Down

0 comments on commit a52e105

Please sign in to comment.