Skip to content

Commit

Permalink
Merge pull request #9 from othercodes/8-response-decoder-called-if-no…
Browse files Browse the repository at this point in the history
…-content-type-is-present

8 response decoder called if no content type is present
  • Loading branch information
othercodes authored Oct 10, 2017
2 parents bcdb3bb + 7a37662 commit f513fbe
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 15 deletions.
3 changes: 3 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
<testsuite name="rest">
<directory>tests</directory>
</testsuite>
<testsuite name="headers">
<file>tests/HeadersTest.php</file>
</testsuite>
</testsuites>
</phpunit>
16 changes: 11 additions & 5 deletions src/Modules/Decoders/BaseDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,18 @@ public function run()
return false;
}

/**
* match the content type and run the decoder
*/
if ($this->contentType == $this->content_type) {
$this->decode();
$body = $this->body;
$content_type = $this->content_type;
if (!empty($body) && isset($content_type)) {

/**
* match the content type and run the decoder
*/
if ($this->contentType == $content_type) {
$this->decode();
}
}

return true;
}
}
11 changes: 11 additions & 0 deletions src/Payloads/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Payloads/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
25 changes: 19 additions & 6 deletions tests/DecodersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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);
}
}
32 changes: 29 additions & 3 deletions tests/HeadersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class HeadersTest extends \PHPUnit\Framework\TestCase
{

public function testConstructWithString()
public function stestConstructWithString()
{
$rawHeaders = 'HTTP/1.1 200 OK
Server: Cowboy
Expand All @@ -27,7 +27,7 @@ public function testConstructWithString()
$this->assertCount(14, $headers);
}

public function testConstructWithArray()
public function stestConstructWithArray()
{
$arrayHeaders = array(
'some_header' => 'some_value',
Expand All @@ -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);
}

}

0 comments on commit f513fbe

Please sign in to comment.