ENHANCEMENT: simpleXML() now catches the error if you try to call it on anything other then xml. MINOR: added test to RESTFul Service

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.3@80340 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Will Rossiter 2009-06-29 04:46:53 +00:00 committed by Sam Minnee
parent 3edc596bdb
commit bfce550b30
2 changed files with 29 additions and 1 deletions

View File

@ -318,7 +318,14 @@ class RestfulService_Response extends HTTPResponse {
}
function simpleXML() {
if(!$this->simpleXML) $this->simpleXML = new SimpleXMLElement($this->body);
if(!$this->simpleXML) {
try {
$this->simpleXML = new SimpleXMLElement($this->body);
}
catch(Exception $e) {
user_error("String could not be parsed as XML. " . $e, E_USER_WARNING);
}
}
return $this->simpleXML;
}

View File

@ -52,6 +52,14 @@ class RestfulServiceTest extends SapphireTest {
$this->assertContains("<post_item name=\"$key\">$value</post_item>", $test1);
}
}
/**
* @expectedException PHPUnit_Framework_Error
*/
function testIncorrectData() {
$connection = new RestfulService(Director::absoluteBaseURL(), 0);
$test1 = $connection->request('RestfulServiceTest_Controller/invalid?usetestmanifest=1&flush=1');
$test1->xpath("\\fail");
}
}
class RestfulServiceTest_Controller extends Controller {
@ -87,6 +95,19 @@ XML;
header('Content-type: text/xml');
echo $out;
}
public function invalid() {
ContentNegotiator::disable();
BasicAuth::disable();
$out = <<<XML
<?xml version="1.0"?>
<test>
<fail><invalid>
</test>
XML;
header('Content-type: text/xml');
echo $out;
}
}
?>