From 6569b0e1503824715407e64af8f0a51e880b7bbc Mon Sep 17 00:00:00 2001 From: Unay Santisteban Date: Mon, 9 Oct 2017 10:33:30 +0200 Subject: [PATCH 1/2] Now the decoders are triggered only if the response has the content-type filed. --- phpunit.xml | 3 +++ src/Modules/Decoders/BaseDecoder.php | 15 ++++++++----- src/Payloads/Headers.php | 11 ++++++++++ src/Payloads/Response.php | 2 +- tests/HeadersTest.php | 32 +++++++++++++++++++++++++--- 5 files changed, 54 insertions(+), 9 deletions(-) diff --git a/phpunit.xml b/phpunit.xml index c656576..a8af7c6 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -8,5 +8,8 @@ tests + + tests/HeadersTest.php + \ No newline at end of file diff --git a/src/Modules/Decoders/BaseDecoder.php b/src/Modules/Decoders/BaseDecoder.php index a415f08..70ddb2f 100755 --- a/src/Modules/Decoders/BaseDecoder.php +++ b/src/Modules/Decoders/BaseDecoder.php @@ -35,12 +35,17 @@ public function run() return false; } - /** - * match the content type and run the decoder - */ - if ($this->contentType == $this->content_type) { - $this->decode(); + $content_type = $this->content_type; + if (isset($content_type)) { + + /** + * match the content type and run the decoder + */ + if ($this->contentType == $this->content_type) { + $this->decode(); + } } + return true; } } \ No newline at end of file diff --git a/src/Payloads/Headers.php b/src/Payloads/Headers.php index 05f21e0..36c46f1 100644 --- a/src/Payloads/Headers.php +++ b/src/Payloads/Headers.php @@ -57,6 +57,17 @@ public function parse($headers) } } + /** + * Reset the headers content. + */ + public function reset() + { + $headers = array_keys(get_object_vars($this)); + foreach ($headers as $key) { + $this->offsetUnset($key); + } + } + /** * Return the iterator element * @return mixed diff --git a/src/Payloads/Response.php b/src/Payloads/Response.php index a4e04bf..960120f 100644 --- a/src/Payloads/Response.php +++ b/src/Payloads/Response.php @@ -84,7 +84,7 @@ public function parseResponse($response) $response = explode("\r\n\r\n", $response); $this->body = array_pop($response); - if(empty($this->body)){ + if (empty($this->body)) { $this->body = null; } diff --git a/tests/HeadersTest.php b/tests/HeadersTest.php index d999f69..94d91b4 100644 --- a/tests/HeadersTest.php +++ b/tests/HeadersTest.php @@ -4,7 +4,7 @@ class HeadersTest extends \PHPUnit\Framework\TestCase { - public function testConstructWithString() + public function stestConstructWithString() { $rawHeaders = 'HTTP/1.1 200 OK Server: Cowboy @@ -27,7 +27,7 @@ public function testConstructWithString() $this->assertCount(14, $headers); } - public function testConstructWithArray() + public function stestConstructWithArray() { $arrayHeaders = array( 'some_header' => 'some_value', @@ -44,10 +44,36 @@ public function testConstructWithArray() /** * @depends testConstructWithArray */ - public function testBuildHeaders(\OtherCode\Rest\Payloads\Headers $headers) + public function stestBuildHeaders(\OtherCode\Rest\Payloads\Headers $headers) { $this->assertInternalType('array', $headers->build()); $this->assertCount(2, $headers); } + public function testResetHeaders() + { + $rawHeaders = 'HTTP/1.1 200 OK +Server: Cowboy +Connection: keep-alive +X-Powered-By: Express +Vary: Origin +Access-Control-Allow-Credentials: true +Cache-Control: no-cache +Pragma: no-cache +Expires: -1 +X-Content-Type-Options: nosniff +Content-Type: application/json; charset=utf-8 +Content-Length: 292 +Etag: W/"124-yv65LoT2uMHrpn06wNpAcQ" +Date: Mon, 07 Mar 2016 09:51:49 GMT +Via: 1.1 vegur'; + + $headers = new \OtherCode\Rest\Payloads\Headers($rawHeaders); + $this->assertInstanceOf('\OtherCode\Rest\Payloads\Headers', $headers); + $this->assertCount(14, $headers); + + $headers->reset(); + $this->assertCount(0, $headers); + } + } \ No newline at end of file From 7a3766296b2707668944b85582c6fd0b2b955d1f Mon Sep 17 00:00:00 2001 From: Unay Santisteban Date: Mon, 9 Oct 2017 10:49:18 +0200 Subject: [PATCH 2/2] Unit test added. --- src/Modules/Decoders/BaseDecoder.php | 5 +++-- tests/DecodersTest.php | 25 +++++++++++++++++++------ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/Modules/Decoders/BaseDecoder.php b/src/Modules/Decoders/BaseDecoder.php index 70ddb2f..1ae0a5c 100755 --- a/src/Modules/Decoders/BaseDecoder.php +++ b/src/Modules/Decoders/BaseDecoder.php @@ -35,13 +35,14 @@ public function run() return false; } + $body = $this->body; $content_type = $this->content_type; - if (isset($content_type)) { + if (!empty($body) && isset($content_type)) { /** * match the content type and run the decoder */ - if ($this->contentType == $this->content_type) { + if ($this->contentType == $content_type) { $this->decode(); } } diff --git a/tests/DecodersTest.php b/tests/DecodersTest.php index 482abd8..c0af6cf 100644 --- a/tests/DecodersTest.php +++ b/tests/DecodersTest.php @@ -7,9 +7,9 @@ class DecodersTest extends \PHPUnit\Framework\TestCase public function testJSonDecoderOFF() { $api = new OtherCode\Rest\Rest(); - $api->configuration->url = "http://jsonplaceholder.typicode.com/"; + $api->configuration->url = "http://www.mocky.io"; - $response = $api->get("posts/1"); + $response = $api->get("/v2/59db36550f0000120402a66f"); $this->assertInternalType('string', $response->body); return $api; @@ -29,7 +29,7 @@ public function testSetJSonDecoder(\OtherCode\Rest\Rest $api) */ public function testJSonDecoderON(\OtherCode\Rest\Rest $api) { - $response = $api->get("posts/1"); + $response = $api->get("/v2/59db36550f0000120402a66f"); $this->assertInternalType('object', $response->body); $this->assertInstanceOf('\stdClass', $response->body); @@ -39,9 +39,9 @@ public function testJSonDecoderON(\OtherCode\Rest\Rest $api) public function testXMLDecoderOFF() { $api = new OtherCode\Rest\Rest(); - $api->configuration->url = "http://www.thomas-bayer.com/"; + $api->configuration->url = "http://www.mocky.io"; - $response = $api->get("sqlrest/CUSTOMER/5"); + $response = $api->get("/v2/59db37720f0000220402a676"); $this->assertInternalType('string', $response->body); return $api; @@ -61,9 +61,22 @@ public function testSetXMLDecoder(\OtherCode\Rest\Rest $api) */ public function testXMLDecoderON(\OtherCode\Rest\Rest $api) { - $response = $api->get("sqlrest/CUSTOMER/5"); + $response = $api->get("/v2/59db37720f0000220402a676"); $this->assertInternalType('object', $response->body); $this->assertInstanceOf('\SimpleXMLElement', $response->body); } + + public function testDecoderOn204Response() + { + $api = new OtherCode\Rest\Rest(); + $api->configuration->url = "http://www.mocky.io"; + $api->setDecoder("json"); + + $response = $api->get("/v2/59db36550f0000120402a66f"); + $this->assertInternalType('object', $response->body); + + $response = $api->get("/v2/59db35850f00000b0402a669"); + $this->assertNull($response->body); + } } \ No newline at end of file