From 090e64bd20a40ed55c79025e611ba2c6a45c80df Mon Sep 17 00:00:00 2001 From: theres Date: Tue, 15 Jul 2014 09:44:07 +0200 Subject: PCurl will now return PCurlResponse object instead of raw body. PJsonCurl also, but will try to parse JSON and throw an exception when fail to parse. --- src/PurpleCode/PCurl/PCurl.php | 15 +++--------- src/PurpleCode/PCurl/PCurlJsonResponse.php | 32 ++++++++++++++++++++++++ src/PurpleCode/PCurl/PCurlResponse.php | 39 ++++++++++++++++++++++++++++++ src/PurpleCode/PCurl/PJsonCurl.php | 20 +++------------ src/PurpleCode/PCurl/PObjectCurl.php | 8 +++--- test/PCurlTest.php | 25 +++++++++---------- test/PJsonCurlTest.php | 39 ++++++++++++++++++++++++++++++ test/test.json | 1 + test/testInvalid.json | 1 + 9 files changed, 134 insertions(+), 46 deletions(-) create mode 100644 src/PurpleCode/PCurl/PCurlJsonResponse.php create mode 100644 src/PurpleCode/PCurl/PCurlResponse.php create mode 100644 test/PJsonCurlTest.php create mode 100644 test/test.json create mode 100644 test/testInvalid.json diff --git a/src/PurpleCode/PCurl/PCurl.php b/src/PurpleCode/PCurl/PCurl.php index 9af612c..4e9d4b6 100644 --- a/src/PurpleCode/PCurl/PCurl.php +++ b/src/PurpleCode/PCurl/PCurl.php @@ -12,8 +12,10 @@ namespace PurpleCode\PCurl; require_once 'PCurlException.php'; +require_once 'PCurlResponse.php'; use PurpleCode\PCurl\PCurlException; +use PurpleCode\PCurl\PCurlResponse; class PCurl { @@ -110,9 +112,7 @@ class PCurl { $header = substr($response, 0, $header_size); $body = substr($response, $header_size); - $processedResponse = $this->postProcessResponseBody($header, $body); - - return $body; + return new PCurlResponse($header, $body); } /** @@ -145,15 +145,6 @@ class PCurl { } /** - * Allow to postprocess response body, ex. parse to specified format. - * @param string $header - * @param string $body - */ - protected function postProcessResponseBody($header, $body){ - return $body; - } - - /** * @return PCurl */ public function headers(array $headers) { diff --git a/src/PurpleCode/PCurl/PCurlJsonResponse.php b/src/PurpleCode/PCurl/PCurlJsonResponse.php new file mode 100644 index 0000000..df36ea4 --- /dev/null +++ b/src/PurpleCode/PCurl/PCurlJsonResponse.php @@ -0,0 +1,32 @@ +header = $header; + $this->body = $body; + } + + public function setBody($body){ + $this->body = $body; + } + + public function getBody(){ + return $this->body; + } + + public function setHeader($header){ + $this->header = $header; + } + + public function getHeader(){ + return $this->header; + } +} diff --git a/src/PurpleCode/PCurl/PJsonCurl.php b/src/PurpleCode/PCurl/PJsonCurl.php index 30776a0..aad8bd4 100644 --- a/src/PurpleCode/PCurl/PJsonCurl.php +++ b/src/PurpleCode/PCurl/PJsonCurl.php @@ -11,39 +11,27 @@ namespace PurpleCode\PCurl; +use PurpleCode\PCurl\PCurlJsonResponse; + class PJsonCurl extends PCurl { private $arrayResponse; - private $alwaysParseResponse; public function __construct($host) { parent::__construct($host); $this->arrayResponse = false; - $this->alwaysParseResponse = true; $this->contentTypeJson(); } public function call($method, $url, $payload = '') { $payload = json_encode($payload); - return parent::call($method, $url, $payload); - } - - protected function postProcessResponseBody($body, $header) { - $body = parent::postProcessResponseBody($body, $header); - if ($this->alwaysParseResponse || strpos($header,'application/json')!==false) { - return json_decode($body, $this->arrayResponse); - } - - return $body; + $response = parent::call($method, $url, $payload); + return new PCurlJsonResponse($response->getHeader(), $response->getBody(), $this->arrayResponse); } public function arrayResponse($arrayResponse = true) { $this->arrayResponse = $arrayResponse; } - public function alwaysParseResponse($alwaysParseResponse = true) { - $this->alwaysParseResponse = $alwaysParseResponse; - } - } diff --git a/src/PurpleCode/PCurl/PObjectCurl.php b/src/PurpleCode/PCurl/PObjectCurl.php index 238bf11..5fdca29 100644 --- a/src/PurpleCode/PCurl/PObjectCurl.php +++ b/src/PurpleCode/PCurl/PObjectCurl.php @@ -35,11 +35,9 @@ class PObjectCurl extends PCurl { public function call($method, $url, $payload = '') { $payload = $this->serialize($payload); - return parent::call($method, $url, $payload); - } - - protected function postProcessResponseBody($body, $header) { - return $this->deserialize($body); + $response = parent::call($method, $url, $payload); + $response->setBody($this->deserialize($response)); + return $response; } private function serialize($payload) { diff --git a/test/PCurlTest.php b/test/PCurlTest.php index 841603a..566ad0f 100644 --- a/test/PCurlTest.php +++ b/test/PCurlTest.php @@ -6,39 +6,38 @@ use PurpleCode\PCurl\PCurl; class PCurlTest extends PHPUnit_Framework_TestCase { - private function getCACertBundlePath() { - return __DIR__.'\ca-cert.crt'; - } + private function getCACertBundlePath() { + return __DIR__.'\ca-cert.crt'; + } public function testShouldGetGooglePageViaHttp() { // given - $cut = new PCurl('http://www.google.pl'); + $cut = new PCurl('http://www.google.pl'); // when $response = $cut->get('/'); - // then - $this->assertSelectRegExp('title', '/Google/', 1, $response); + $this->assertSelectRegExp('title', '/Google/', 1, $response->getBody()); } - public function testShouldGetGooglePageAndCheckHttpsCertificate() { + public function testShouldGetGooglePageAndCheckHttpsCertificate() { // given - $cut = new PCurl('https://www.google.pl'); - $cut->useSSLCertificate($this->getCACertBundlePath()); + $cut = new PCurl('https://www.google.pl'); + $cut->useSSLCertificate($this->getCACertBundlePath()); // when $response = $cut->get('/'); // then - $this->assertSelectRegExp('title', '/Google/', 1, $response); - } + $this->assertSelectRegExp('title', '/Google/', 1, $response->getBody()); + } public function testShouldGetGooglePageAndThrowExceptionOnMissingCertificate() { // given $cut = new PCurl('https://www.google.com'); - // then - $this->setExpectedException('PurpleCode\PCurl\PCurlException'); + // then + $this->setExpectedException('PurpleCode\PCurl\PCurlException'); // when $response = $cut->get('/'); diff --git a/test/PJsonCurlTest.php b/test/PJsonCurlTest.php new file mode 100644 index 0000000..8aec5b8 --- /dev/null +++ b/test/PJsonCurlTest.php @@ -0,0 +1,39 @@ +get(''); + + // then + $this->assertEquals(2, $response->getBody()->a->b); + $this->assertEquals("a", $response->getBody()->a->c); + } + + public function testShouldFailParseWrongFile() { + // given + $cut = new PJsonCurl('file://testInvaliud.json'); + + //then + $this->setExpectedException('PurpleCode\PCurl\PCurlException'); + + // when + $response = $cut->get(''); + } +} \ No newline at end of file diff --git a/test/test.json b/test/test.json new file mode 100644 index 0000000..bb4ed6f --- /dev/null +++ b/test/test.json @@ -0,0 +1 @@ +{"a":{"b":2,"c":"a"}} \ No newline at end of file diff --git a/test/testInvalid.json b/test/testInvalid.json new file mode 100644 index 0000000..8c548c8 --- /dev/null +++ b/test/testInvalid.json @@ -0,0 +1 @@ +{"a":3} \ No newline at end of file -- cgit v1.1