diff options
-rw-r--r-- | src/PurpleCode/PCurl/PCurl.php | 15 | ||||
-rw-r--r-- | src/PurpleCode/PCurl/PCurlJsonResponse.php | 32 | ||||
-rw-r--r-- | src/PurpleCode/PCurl/PCurlResponse.php | 39 | ||||
-rw-r--r-- | src/PurpleCode/PCurl/PJsonCurl.php | 20 | ||||
-rw-r--r-- | src/PurpleCode/PCurl/PObjectCurl.php | 8 | ||||
-rw-r--r-- | test/PCurlTest.php | 25 | ||||
-rw-r--r-- | test/PJsonCurlTest.php | 39 | ||||
-rw-r--r-- | test/test.json | 1 | ||||
-rw-r--r-- | test/testInvalid.json | 1 |
9 files changed, 134 insertions, 46 deletions
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 @@ +<?php + +/** + * PCurl is a REST client libary for PHP. + * + * See http://github.com/purplecode/php.curl for details. + * + * This code is licensed for use, modification, and distribution + * under the terms of the MIT License (see http://en.wikipedia.org/wiki/MIT_License) + */ + +namespace PurpleCode\PCurl; + +use PurpleCode\PCurl\PCurlResponse; + +class PCurlJsonResponse extends PCurlResponse { + private $parsedResponse; + + public function __construct($header, $body, $arrayResponse){ + if(! PCurlJsonResponse::isValidJson($body)) + throw new PCurlException("Invalid JSON response format"); + parent::__construct($header, json_decode($body, $arrayResponse)); + } + + /** + * Fast way to check if response body is in JSon format - see RFC4627, regexp part. + */ + private static function isValidJson($text){ + return !preg_match('/[^,:{}\\[\\]0-9.\\-+Eaeflnr-u \\n\\r\\t]/', preg_replace('/"(\\.|[^"\\\\])*"/', '', $text)); + } + +} diff --git a/src/PurpleCode/PCurl/PCurlResponse.php b/src/PurpleCode/PCurl/PCurlResponse.php new file mode 100644 index 0000000..d616a95 --- /dev/null +++ b/src/PurpleCode/PCurl/PCurlResponse.php @@ -0,0 +1,39 @@ +<?php + +/** + * PCurl is a REST client libary for PHP. + * + * See http://github.com/purplecode/php.curl for details. + * + * This code is licensed for use, modification, and distribution + * under the terms of the MIT License (see http://en.wikipedia.org/wiki/MIT_License) + */ + +namespace PurpleCode\PCurl; + + +class PCurlResponse { + private $body; + private $header; + + public function __construct($header, $body){ + $this->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 @@ +<?php + + +require_once (dirname(__FILE__) . '/../src/PurpleCode/PCurl/PCurlResponse.php'); +require_once (dirname(__FILE__) . '/../src/PurpleCode/PCurl/PCurlJsonResponse.php'); +require_once (dirname(__FILE__) . '/../src/PurpleCode/PCurl/PCurl.php'); +require_once (dirname(__FILE__) . '/../src/PurpleCode/PCurl/PJsonCurl.php'); + +use PurpleCode\PCurl\PJsonCurl; + +class PCurlTest extends PHPUnit_Framework_TestCase { + + private function getCACertBundlePath() { + return __DIR__.'\ca-cert.crt'; + } + + public function testShouldGetJsonFileAndParse() { + // given + $cut = new PJsonCurl('file://test.json'); + + // when + $response = $cut->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 |