Skip to content

Commit

Permalink
Fix loading invalid XML
Browse files Browse the repository at this point in the history
Add tests for invalid XML + invalid JSON.
  • Loading branch information
markstory committed Jan 28, 2013
1 parent feb5ebb commit d48501f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/Cake/Network/Http/Response.php
Expand Up @@ -325,12 +325,12 @@ protected function _getJson() {
* @return null|SimpleXML
*/
protected function _getXml() {
try {
$data = new \SimpleXMLElement($this->_body);
} catch (Exception $e) {
return null;
$restore = libxml_use_internal_errors();
$data = simplexml_load_string($this->_body);
if ($data) {
return $data;
}
return $data;
return null;
}

/**
Expand Down
8 changes: 8 additions & 0 deletions lib/Cake/Test/TestCase/Network/Http/ResponseTest.php
Expand Up @@ -85,6 +85,10 @@ public function testBodyJson() {
$response = new Response([], $encoded);
$this->assertTrue(isset($response->json));
$this->assertEquals($data['property'], $response->json['property']);

$data = '';
$response = new Response([], $data);
$this->assertFalse(isset($response->json));
}

/**
Expand All @@ -102,6 +106,10 @@ public function testBodyXml() {
$response = new Response([], $data);
$this->assertTrue(isset($response->xml));
$this->assertEquals('Test', (string)$response->xml->test);

$data = '';
$response = new Response([], $data);
$this->assertFalse(isset($response->xml));
}

/**
Expand Down

0 comments on commit d48501f

Please sign in to comment.