diff --git a/src/RestApiContext.php b/src/RestApiContext.php index 92a97b1..0090595 100644 --- a/src/RestApiContext.php +++ b/src/RestApiContext.php @@ -14,26 +14,35 @@ class RestApiContext extends BehatContext implements JsonStorageAware { private $asserter; + /** @var HttpClient */ private $httpClient; + /** @var array|\Guzzle\Http\Message\RequestInterface */ private $request; + /** @var \Guzzle\Http\Message\Response|array */ private $response; - private $headers = array(); + /** @var array */ + private $requestHeaders = array(); + /** @var bool */ private $enableJsonInspection = true; + /** @var JsonStorage */ private $jsonStorage; public function __construct(HttpClient $httpClient, $asserter, $enableJsonInspection) { - $this->headers = array(); + $this->requestHeaders = array(); $this->httpClient = $httpClient; $this->asserter = $asserter; $this->enableJsonInspection = (bool) $enableJsonInspection; } + /** + * {@inheritdoc} + */ public function setJsonStorage(JsonStorage $jsonStorage) { $this->jsonStorage = $jsonStorage; @@ -53,10 +62,13 @@ public function iSendARequest($method, $url) /** * Sends HTTP request to specific URL with raw body from PyString. * - * @param string $method request method - * @param string $url relative url + * @param string $method request method + * @param string $url relative url + * @param PyStringNode $body * * @When /^(?:I )?send a ([A-Z]+) request to "([^"]+)" with body:$/ + * @throws BadResponseException + * @throws \Exception */ public function iSendARequestWithBody($method, $url, PyStringNode $body) { @@ -80,7 +92,7 @@ public function theResponseCodeShouldBe($code) */ public function iSetHeaderEqualTo($headerName, $headerValue) { - $this->setHeader($headerName, $headerValue); + $this->setRequestHeader($headerName, $headerValue); } /** @@ -88,7 +100,7 @@ public function iSetHeaderEqualTo($headerName, $headerValue) */ public function iAddHeaderEqualTo($headerName, $headerValue) { - $this->addHeader($headerName, $headerValue); + $this->addRequestHeader($headerName, $headerValue); } /** @@ -98,9 +110,9 @@ public function iAddHeaderEqualTo($headerName, $headerValue) */ public function iSetBasicAuthenticationWithAnd($username, $password) { - $this->removeHeader('Authorization'); + $this->removeRequestHeader('Authorization'); $authorization = base64_encode($username . ':' . $password); - $this->addHeader('Authorization', 'Basic ' . $authorization); + $this->addRequestHeader('Authorization', 'Basic ' . $authorization); } /** @@ -120,37 +132,62 @@ public function printResponse() ); } + /** + * @return array|\Guzzle\Http\Message\RequestInterface + */ + public function getRequest() + { + return $this->request; + } + + /** + * @return array|\Guzzle\Http\Message\Response + */ + public function getResponse() + { + return $this->response; + } + /** * @return array + * @deprecated BC Alias, prefer using getRequestHeaders() */ public function getHeaders() { - return $this->headers; + return $this->getRequestHeaders(); + } + + /** + * @return array + */ + public function getRequestHeaders() + { + return $this->requestHeaders; } /** * @param string $name * @param string $value */ - protected function addHeader($name, $value) + protected function addRequestHeader($name, $value) { - if (isset($this->headers[$name])) { - if (!is_array($this->headers[$name])) { - $this->headers[$name] = array($this->headers[$name]); + if (isset($this->requestHeaders[$name])) { + if (!is_array($this->requestHeaders[$name])) { + $this->requestHeaders[$name] = array($this->requestHeaders[$name]); } - $this->headers[$name][] = $value; + $this->requestHeaders[$name][] = $value; } else { - $this->headers[$name] = $value; + $this->requestHeaders[$name] = $value; } } /** * @param string $headerName */ - protected function removeHeader($headerName) + protected function removeRequestHeader($headerName) { - if (array_key_exists($headerName, $this->headers)) { - unset($this->headers[$headerName]); + if (array_key_exists($headerName, $this->requestHeaders)) { + unset($this->requestHeaders[$headerName]); } } @@ -158,10 +195,10 @@ protected function removeHeader($headerName) * @param string $name * @param string $value */ - protected function setHeader($name, $value) + protected function setRequestHeader($name, $value) { - $this->removeHeader($name); - $this->addHeader($name, $value); + $this->removeRequestHeader($name); + $this->addRequestHeader($name, $value); } /** @@ -190,8 +227,8 @@ private function sendRequest($method, $url, $body = null) private function createRequest($method, $url, $body = null) { - $this->request = $this->httpClient->createRequest($method, $url, $this->headers, $body); + $this->request = $this->httpClient->createRequest($method, $url, $this->requestHeaders, $body); // Reset headers used for the HTTP request - $this->headers = array(); + $this->requestHeaders = array(); } } diff --git a/tests/Units/RestApiContext.php b/tests/Units/RestApiContext.php index 0521a2d..70eb644 100644 --- a/tests/Units/RestApiContext.php +++ b/tests/Units/RestApiContext.php @@ -6,23 +6,20 @@ use Rezzza\JsonApiBehatExtension\RestApiContext as SUT; /** - * Description of RestApiContext - * * @author Mikaƫl FIMA + * @author Guillaume MOREL */ class RestApiContext extends atoum { - /** * Adding headers * @dataProvider addHeaderDataProvider */ - public function testAddHeader(array $addHeadersSteps, array $expectedHeaders) + public function testAddRequestHeader(array $addHeadersSteps, array $expectedHeaders) { $this ->given( - $httpClient = new \mock\Guzzle\Http\Client(), - $httpClient->getMockController()->send = new \Guzzle\Http\Message\Response(200) + $httpClient = $this->mockHttpClient(200) ) ->and($sut = new SUT($httpClient, null, false)) ; @@ -34,7 +31,7 @@ public function testAddHeader(array $addHeadersSteps, array $expectedHeaders) } $this - ->array($sut->getHeaders())->isIdenticalTo($expectedHeaders) + ->array($sut->getRequestHeaders())->isIdenticalTo($expectedHeaders) ; } @@ -51,12 +48,11 @@ public function addHeaderDataProvider() * Setting headers * @dataProvider setHeaderDataProvider */ - public function testSetHeader(array $setHeadersSteps, array $expectedHeaders) + public function testSetRequestHeader(array $setHeadersSteps, array $expectedHeaders) { $this ->given( - $httpClient = new \mock\Guzzle\Http\Client(), - $httpClient->getMockController()->send = new \Guzzle\Http\Message\Response(200) + $httpClient = $this->mockHttpClient(200) ) ->and($sut = new SUT($httpClient, null, false)) ; @@ -68,7 +64,7 @@ public function testSetHeader(array $setHeadersSteps, array $expectedHeaders) } $this - ->array($sut->getHeaders())->isIdenticalTo($expectedHeaders) + ->array($sut->getRequestHeaders())->isIdenticalTo($expectedHeaders) ; } @@ -80,4 +76,101 @@ public function setHeaderDataProvider() array(array(array("name" => "value"), array("name" => "value2")), array("name" => "value2")), ); } + + /** + * @dataProvider requestDataProvider + * @param array $requestHeaders + */ + public function test_get_request(array $requestHeaders) + { + // Given + $mockHttpClient = $this->mockHttpClient(200, array()); + + $restApiContext = new SUT($mockHttpClient, null, false); + foreach ($requestHeaders as $requestHeaderKey => $requestHeaderValue) { + $restApiContext->iAddHeaderEqualTo($requestHeaderKey, $requestHeaderValue); + } + + // When + $restApiContext->iSendARequest('GET', 'http://verylastroom.com/'); + + // Then + $request = $restApiContext->getRequest(); + $intersect = array_intersect_key($requestHeaders, $request->getHeaders()->toArray()); + + $this->array($requestHeaders)->isEqualTo($intersect); + } + + public function requestDataProvider() + { + return array( + array( + 'requestHeaders' => array( + array("name" => "value") + ) + ), + array( + 'requestHeaders' => array( + array("name1" => "value1"), array("name2" => "value2") + ) + ) + ); + } + + /** + * @dataProvider responseDataProvider + * @param int $statusCode + * @param array $responseHeaders + */ + public function test_get_response($statusCode, array $responseHeaders) + { + // Given + $mockHttpClient = $this->mockHttpClient($statusCode, $responseHeaders); + + $restApiContext = new SUT($mockHttpClient, null, false); + + // When + $restApiContext->iSendARequest('GET', 'http://verylastroom.com/'); + + // Then + $response = $restApiContext->getResponse(); + $intersect = array_intersect_key($responseHeaders, $response->getHeaders()->toArray()); + + $this->array($responseHeaders)->isEqualTo($intersect); + } + + public function responseDataProvider() + { + return array( + array( + 'statusCode' => 200, + 'requestHeaders' => array( + array("name" => "value") + ) + ), + array( + 'statusCode' => 400, + 'requestHeaders' => array( + array("name1" => "value1"), array("name2" => "value2") + ) + ) + ); + } + + /** + * @param int $responseStatusCode + * @param array $headers + * + * @return \Guzzle\Http\Client + */ + private function mockHttpClient($responseStatusCode, array $headers = array()) + { + $mockHttpClient = new \mock\Guzzle\Http\Client(); + $mockHttpClient->getMockController()->send = new \Guzzle\Http\Message\Response( + $responseStatusCode, + $headers + ); + + return $mockHttpClient; + } }