Skip to content

Commit

Permalink
Expose RestApiContext::request and RestApiContext::response
Browse files Browse the repository at this point in the history
In order to allow deep testing from other contexts
Rename RestApiContext::headers into RestApiContext::requestHeaders
BC keep RestApiContext::getHeaders as alias but deprecated
  • Loading branch information
Guillaume MOREL committed Mar 24, 2015
1 parent 1195e71 commit 32646ef
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 34 deletions.
83 changes: 60 additions & 23 deletions src/RestApiContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
{
Expand All @@ -80,15 +92,15 @@ public function theResponseCodeShouldBe($code)
*/
public function iSetHeaderEqualTo($headerName, $headerValue)
{
$this->setHeader($headerName, $headerValue);
$this->setRequestHeader($headerName, $headerValue);
}

/**
* @Given /^I add "([^"]*)" header equal to "([^"]*)"$/
*/
public function iAddHeaderEqualTo($headerName, $headerValue)
{
$this->addHeader($headerName, $headerValue);
$this->addRequestHeader($headerName, $headerValue);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -120,48 +132,73 @@ 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]);
}
}

/**
* @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);
}

/**
Expand Down Expand Up @@ -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();
}
}
115 changes: 104 additions & 11 deletions tests/Units/RestApiContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,20 @@
use Rezzza\JsonApiBehatExtension\RestApiContext as SUT;

/**
* Description of RestApiContext
*
* @author Mikaël FIMA <[email protected]>
* @author Guillaume MOREL <[email protected]>
*/
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))
;
Expand All @@ -34,7 +31,7 @@ public function testAddHeader(array $addHeadersSteps, array $expectedHeaders)
}

$this
->array($sut->getHeaders())->isIdenticalTo($expectedHeaders)
->array($sut->getRequestHeaders())->isIdenticalTo($expectedHeaders)
;
}

Expand All @@ -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))
;
Expand All @@ -68,7 +64,7 @@ public function testSetHeader(array $setHeadersSteps, array $expectedHeaders)
}

$this
->array($sut->getHeaders())->isIdenticalTo($expectedHeaders)
->array($sut->getRequestHeaders())->isIdenticalTo($expectedHeaders)
;
}

Expand All @@ -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;
}
}

0 comments on commit 32646ef

Please sign in to comment.