From 4ae6e5f2e428c98dbe03ff1e939de185add70eed Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Sat, 26 Jul 2014 01:47:05 -0400 Subject: [PATCH] Fix issue in Response when setting headers in constructor Update tests for Response --- src/pjdietz/WellRESTed/Response.php | 4 +- test/ResponseTest.php | 120 ++++++++++++++++++++-------- 2 files changed, 89 insertions(+), 35 deletions(-) diff --git a/src/pjdietz/WellRESTed/Response.php b/src/pjdietz/WellRESTed/Response.php index 4f95575..606c0ba 100644 --- a/src/pjdietz/WellRESTed/Response.php +++ b/src/pjdietz/WellRESTed/Response.php @@ -53,7 +53,9 @@ class Response extends Message implements ResponseInterface $this->setStatusCode($statusCode); if (is_array($headers)) { - $this->headers = $headers; + foreach ($headers as $key => $value) { + $this->setHeader($key, $value); + } } if (!is_null($body)) { diff --git a/test/ResponseTest.php b/test/ResponseTest.php index 3d30368..f7237f0 100644 --- a/test/ResponseTest.php +++ b/test/ResponseTest.php @@ -1,20 +1,25 @@ "text/plain")); + $resp = new Response($statusCode); + $this->assertEquals($statusCode, $resp->getStatusCode()); } /** * @dataProvider statusCodeProvider */ - public function testStatusLine($statusCode, $reasonPhrase, $statusLine) + public function testReadStatusLine($statusCode, $reasonPhrase, $statusLine) { $resp = new Response(); $resp->setStatusCode($statusCode, $reasonPhrase); @@ -24,17 +29,7 @@ class ResponseBuilderTest extends \PHPUnit_Framework_TestCase /** * @dataProvider statusCodeProvider */ - public function testReasonPhrase($statusCode, $reasonPhrase, $statusLine) - { - $resp = new Response(); - $resp->setStatusCode($statusCode, $reasonPhrase); - $this->assertEquals(substr($statusLine, 13), $resp->getReasonPhrase()); - } - - /** - * @dataProvider statusCodeProvider - */ - public function testSuccess($statusCode, $reasonPhrase, $statusLine) + public function testReadSuccess($statusCode, $reasonPhrase, $statusLine) { $resp = new Response(); $resp->setStatusCode($statusCode, $reasonPhrase); @@ -45,6 +40,16 @@ class ResponseBuilderTest extends \PHPUnit_Framework_TestCase } } + /** + * @dataProvider statusCodeProvider + */ + public function testReadReasonPhrase($statusCode, $reasonPhrase, $statusLine) + { + $resp = new Response(); + $resp->setStatusCode($statusCode, $reasonPhrase); + $this->assertEquals(substr($statusLine, 13), $resp->getReasonPhrase()); + } + public function statusCodeProvider() { return [ @@ -94,7 +99,7 @@ class ResponseBuilderTest extends \PHPUnit_Framework_TestCase * @dataProvider invalidReasonPhraseProvider * @expectedException \InvalidArgumentException */ - public function testInvalidReasonPhrase($statusCode, $reasonPhrase) + public function testFailOnInvalidReasonPhrase($statusCode, $reasonPhrase) { $resp = new Response(); $resp->setStatusCode($statusCode, $reasonPhrase); @@ -109,7 +114,24 @@ class ResponseBuilderTest extends \PHPUnit_Framework_TestCase ]; } - public function testBodyFile() + public function testSetBody() + { + $faker = Factory::create(); + $body = $faker->text(); + $resp = new Response(); + $resp->setBody($body); + $this->assertEquals($body, $resp->getBody()); + } + + public function testSetBodyInConstructor() + { + $faker = Factory::create(); + $body = $faker->text(); + $resp = new Response(200, $body); + $this->assertEquals($body, $resp->getBody()); + } + + public function testSetBodyFile() { $path = tempnam(sys_get_temp_dir(), "TST"); $resp = new Response(); @@ -118,10 +140,53 @@ class ResponseBuilderTest extends \PHPUnit_Framework_TestCase unlink($path); } - public function testRespondBodyFile() + /** + * @dataProvider headerProvider + */ + public function testSetHeaders($headerKey, $headerValue, $testName) + { + $resp = new Response(); + $resp->setHeader($headerKey, $headerValue); + $this->assertEquals($headerValue, $resp->getHeader($testName)); + } + + /** + * @dataProvider headerProvider + */ + public function testSetHeadersInConstructor($headerKey, $headerValue, $testName) + { + $resp = new Response(200, "Body", array($headerKey => $headerValue)); + $this->assertEquals($headerValue, $resp->getHeader($testName)); + } + + public function headerProvider() + { + return [ + ["Content-Encoding", "gzip", "CONTENT-ENCODING"], + ["Content-Length", "2048", "content-length"], + ["Content-Type", "text/plain", "Content-Type"] + ]; + } + + public function testOutputResponse() + { + $faker = Factory::create(); + $body = $faker->text(); + + $resp = new Response(200, $body, ["Content-type" => "text/plain"]); + ob_start(); + @$resp->respond(); + $captured = ob_get_contents(); + ob_end_clean(); + + $this->assertEquals($body, $captured); + } + + public function testOutputResponseFromFile() { $path = tempnam(sys_get_temp_dir(), "TST"); - $body = "This is the body"; + $faker = Factory::create(); + $body = $faker->text(); $f = fopen($path, "w"); fwrite($f, $body); @@ -142,7 +207,7 @@ class ResponseBuilderTest extends \PHPUnit_Framework_TestCase $this->assertEquals($captured, $body); } - public function testMissingRespondBodyFile() + public function testMissingResponseFile() { $path = tempnam(sys_get_temp_dir(), "TST"); @@ -160,17 +225,4 @@ class ResponseBuilderTest extends \PHPUnit_Framework_TestCase $this->assertEquals("", $captured); } - public function testRespondBody() - { - $body = "This is the body"; - - $resp = new Response(200, $body, array("Content-type" => "text/plain")); - ob_start(); - @$resp->respond(); - $captured = ob_get_contents(); - ob_end_clean(); - - $this->assertEquals($body, $captured); - } - }