Merge pull request #17304 from owncloud/fix-17265
Check if response could get generated
This commit is contained in:
commit
073e654692
|
@ -61,9 +61,16 @@ class JSONResponse extends Response {
|
|||
* Returns the rendered json
|
||||
* @return string the rendered json
|
||||
* @since 6.0.0
|
||||
* @throws \Exception If data could not get encoded
|
||||
*/
|
||||
public function render(){
|
||||
return json_encode($this->data);
|
||||
public function render() {
|
||||
$response = json_encode($this->data);
|
||||
if($response === false) {
|
||||
throw new \Exception(sprintf('Could not json_encode due to invalid ' .
|
||||
'non UTF-8 characters in the array: %s', var_export($this->data, true)));
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -76,6 +76,17 @@ class JSONResponseTest extends \Test\TestCase {
|
|||
$this->assertEquals($expected, $this->json->render());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Exception
|
||||
* @expectedExceptionMessage Could not json_encode due to invalid non UTF-8 characters in the array: array (
|
||||
* @requires PHP 5.5
|
||||
*/
|
||||
public function testRenderWithNonUtf8Encoding() {
|
||||
$params = ['test' => hex2bin('e9')];
|
||||
$this->json->setData($params);
|
||||
$this->json->render();
|
||||
}
|
||||
|
||||
public function testConstructorAllowsToSetData() {
|
||||
$data = array('hi');
|
||||
$code = 300;
|
||||
|
|
Loading…
Reference in New Issue