Fix issue in Response when setting headers in constructor
Update tests for Response
This commit is contained in:
parent
69b9bb51aa
commit
4ae6e5f2e4
|
|
@ -53,7 +53,9 @@ class Response extends Message implements ResponseInterface
|
||||||
$this->setStatusCode($statusCode);
|
$this->setStatusCode($statusCode);
|
||||||
|
|
||||||
if (is_array($headers)) {
|
if (is_array($headers)) {
|
||||||
$this->headers = $headers;
|
foreach ($headers as $key => $value) {
|
||||||
|
$this->setHeader($key, $value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!is_null($body)) {
|
if (!is_null($body)) {
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,25 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
use pjdietz\WellRESTed\Request;
|
namespace pjdietz\WellRESTed\Test;
|
||||||
|
|
||||||
|
use Faker\Factory;
|
||||||
use pjdietz\WellRESTed\Response;
|
use pjdietz\WellRESTed\Response;
|
||||||
use pjdietz\WellRESTed\Test;
|
|
||||||
|
|
||||||
class ResponseBuilderTest extends \PHPUnit_Framework_TestCase
|
class ResponseBuilderTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
public function testConstructor()
|
/**
|
||||||
|
* @dataProvider statusCodeProvider
|
||||||
|
*/
|
||||||
|
public function testSetStatusCodeInConstructor($statusCode, $reasonPhrase, $statusLine)
|
||||||
{
|
{
|
||||||
$resp = new Response(200, "This is the body", array("Content-type" => "text/plain"));
|
$resp = new Response($statusCode);
|
||||||
|
$this->assertEquals($statusCode, $resp->getStatusCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dataProvider statusCodeProvider
|
* @dataProvider statusCodeProvider
|
||||||
*/
|
*/
|
||||||
public function testStatusLine($statusCode, $reasonPhrase, $statusLine)
|
public function testReadStatusLine($statusCode, $reasonPhrase, $statusLine)
|
||||||
{
|
{
|
||||||
$resp = new Response();
|
$resp = new Response();
|
||||||
$resp->setStatusCode($statusCode, $reasonPhrase);
|
$resp->setStatusCode($statusCode, $reasonPhrase);
|
||||||
|
|
@ -24,17 +29,7 @@ class ResponseBuilderTest extends \PHPUnit_Framework_TestCase
|
||||||
/**
|
/**
|
||||||
* @dataProvider statusCodeProvider
|
* @dataProvider statusCodeProvider
|
||||||
*/
|
*/
|
||||||
public function testReasonPhrase($statusCode, $reasonPhrase, $statusLine)
|
public function testReadSuccess($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)
|
|
||||||
{
|
{
|
||||||
$resp = new Response();
|
$resp = new Response();
|
||||||
$resp->setStatusCode($statusCode, $reasonPhrase);
|
$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()
|
public function statusCodeProvider()
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
|
@ -94,7 +99,7 @@ class ResponseBuilderTest extends \PHPUnit_Framework_TestCase
|
||||||
* @dataProvider invalidReasonPhraseProvider
|
* @dataProvider invalidReasonPhraseProvider
|
||||||
* @expectedException \InvalidArgumentException
|
* @expectedException \InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
public function testInvalidReasonPhrase($statusCode, $reasonPhrase)
|
public function testFailOnInvalidReasonPhrase($statusCode, $reasonPhrase)
|
||||||
{
|
{
|
||||||
$resp = new Response();
|
$resp = new Response();
|
||||||
$resp->setStatusCode($statusCode, $reasonPhrase);
|
$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");
|
$path = tempnam(sys_get_temp_dir(), "TST");
|
||||||
$resp = new Response();
|
$resp = new Response();
|
||||||
|
|
@ -118,10 +140,53 @@ class ResponseBuilderTest extends \PHPUnit_Framework_TestCase
|
||||||
unlink($path);
|
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");
|
$path = tempnam(sys_get_temp_dir(), "TST");
|
||||||
$body = "This is the body";
|
$faker = Factory::create();
|
||||||
|
$body = $faker->text();
|
||||||
|
|
||||||
$f = fopen($path, "w");
|
$f = fopen($path, "w");
|
||||||
fwrite($f, $body);
|
fwrite($f, $body);
|
||||||
|
|
@ -142,7 +207,7 @@ class ResponseBuilderTest extends \PHPUnit_Framework_TestCase
|
||||||
$this->assertEquals($captured, $body);
|
$this->assertEquals($captured, $body);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testMissingRespondBodyFile()
|
public function testMissingResponseFile()
|
||||||
{
|
{
|
||||||
$path = tempnam(sys_get_temp_dir(), "TST");
|
$path = tempnam(sys_get_temp_dir(), "TST");
|
||||||
|
|
||||||
|
|
@ -160,17 +225,4 @@ class ResponseBuilderTest extends \PHPUnit_Framework_TestCase
|
||||||
$this->assertEquals("", $captured);
|
$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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue