From bfce550b30e4dbd43cee0624220ba15bb1c3062b Mon Sep 17 00:00:00 2001 From: Will Rossiter Date: Mon, 29 Jun 2009 04:46:53 +0000 Subject: [PATCH] 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 --- api/RestfulService.php | 9 ++++++++- tests/api/RestfulServiceTest.php | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/api/RestfulService.php b/api/RestfulService.php index 741459dcb..170a81d94 100644 --- a/api/RestfulService.php +++ b/api/RestfulService.php @@ -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; } diff --git a/tests/api/RestfulServiceTest.php b/tests/api/RestfulServiceTest.php index c71a92d85..ddf8d7ea9 100644 --- a/tests/api/RestfulServiceTest.php +++ b/tests/api/RestfulServiceTest.php @@ -52,6 +52,14 @@ class RestfulServiceTest extends SapphireTest { $this->assertContains("$value", $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; + header('Content-type: text/xml'); + echo $out; + } } ?>