Skip to content

Commit

Permalink
Merge pull request #40 from tyx/fix/send-request-with-parameters
Browse files Browse the repository at this point in the history
Deal with form data
  • Loading branch information
shouze committed Dec 13, 2015
2 parents ded910a + 902357c commit 421c50f
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 83 deletions.
14 changes: 12 additions & 2 deletions features/send_request.feature
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,22 @@ Feature: Test send API request
When I send a POST request to "echo" with body:
"""
{
"name" : "name",
"pass": "pass"
"username" : "pablo",
"password": "money"
}
"""
Then the JSON node "method" should be equal to "POST"
And the JSON node "headers.content-type[0]" should be equal to "application/json"
And the JSON node "username" should be equal to "pablo"
And the JSON node "password" should be equal to "money"

Scenario: Sending POST request with form data
When I send a POST request to "echo" with form data:
| username | pablo |
| password | money |
Then the response status code should be 200
And the JSON node "username" should be equal to "pablo"
And the JSON node "password" should be equal to "money"

Scenario: Add same header 2 times
Given I add "header" header equal to "value"
Expand Down
8 changes: 5 additions & 3 deletions src/Rest/RestApiBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function getHttpClient()
/**
* @param string $method
* @param string $url
* @param string $body
* @param string|array $body
*/
public function sendRequest($method, $url, $body = null)
{
Expand All @@ -116,17 +116,19 @@ public function sendRequest($method, $url, $body = null)
/**
* @param string $method
* @param string $uri With or without host
* @param string|resource|array $body
* @param string|array $body
*/
private function send($method, $uri, $body = null)
{
if (!$this->hasHost($uri)) {
$uri = rtrim($this->httpClient->getConfiguration()->getBaseUri(), '/') . '/' . ltrim($uri, '/');
}
$body = is_array($body) ? http_build_query($body) : $body;
$stream = new Stream('php://memory', 'rw');
if ($body) {
if (is_string($body)) {
$stream->write($body);
}

$this->request = new Request($uri, $method, $stream, $this->requestHeaders);
$this->response = $this->httpClient->sendRequest($this->request);
// Reset headers used for the HTTP request
Expand Down
11 changes: 11 additions & 0 deletions src/RestApiContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Rezzza\RestApiBehatExtension\Rest\RestApiBrowser;
Expand Down Expand Up @@ -47,6 +48,16 @@ public function iSendARequestWithBody($method, $url, PyStringNode $body)
$this->restApiBrowser->sendRequest($method, $url, (string) $body);
}

/**
* Sends HTTP request to specific URL with POST parameters.
*
* @When I send a :method request to :url with form data:
*/
public function iSendAPostRequestToWithFormData($method, $url, TableNode $formData)
{
$this->restApiBrowser->sendRequest($method, $url, $formData->getRowsHash());
}

/**
* @param string $code status code
*
Expand Down
132 changes: 54 additions & 78 deletions tests/Units/Rest/RestApiBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,6 @@ public function testAddRequestHeader(array $addHeadersSteps, array $expectedHead
;
}

/**
* @param string $baseUrl
* @param int $responseStatusCode
* @param array $headers
*
* @return \Ivory\HttpAdapter\HttpAdapterInterface
*/
private function mockHttpClient($baseUrl, $responseStatusCode, array $headers = [])
{
$mockHttpClient = new \Ivory\HttpAdapter\MockHttpAdapter();
$mockHttpClient->getConfiguration()->setBaseUri($baseUrl);
$messageFactory = new \Ivory\HttpAdapter\Message\MessageFactory($baseUrl);
$mockHttpClient->appendResponse(
$messageFactory->createResponse(
$responseStatusCode,
\Ivory\HttpAdapter\Message\RequestInterface::PROTOCOL_VERSION_1_1,
$headers
)
);
return $mockHttpClient;
}

public function addHeaderDataProvider()
{
return [
Expand Down Expand Up @@ -100,58 +78,6 @@ public function setHeaderDataProvider()
}

/**
* @dataProvider requestDataProvider
* @param string $url
* @param array $requestHeaders
*/
public function test_get_request($url, array $requestHeaders)
{
// Given
$mockHttpClient = $this->mockHttpClient('http://verylastroom.com', 200, []);

$restApiContext = new SUT(null, null, $mockHttpClient);
foreach ($requestHeaders as $requestHeaderKey => $requestHeaderValue) {
$restApiContext->addRequestHeader($requestHeaderKey, $requestHeaderValue);
}

// When
$restApiContext->sendRequest('GET', $url);

// Then
$request = $restApiContext->getRequest();
$intersect = array_intersect_key($requestHeaders, $request->getHeaders());

$this->array($requestHeaders)->isEqualTo($intersect);
}

public function requestDataProvider()
{
return [
[
'url' => 'http://verylastroom.com/',
'requestHeaders' => [
"name" => "value"
]
],
[
'url' => 'http://verylastroom.com/',
'requestHeaders' => [
"name1" => "value1",
"name2" => "value2"

]
],
[
'url' => '/?test=a:2', // Without host with weird query string
'requestHeaders' => [
"name1" => "value1",
"name2" => "value2"
]
]
];
}

/**
* @dataProvider urlWithSlashesProvider
* @param string $baseUrl
* @param string $stepUrl
Expand All @@ -166,7 +92,7 @@ public function test_create_request_with_slashes_to_clean($baseUrl, $stepUrl, $e
$restApiContext->sendRequest('GET', $stepUrl);
// Then
$request = $restApiContext->getRequest();
$this->phpString($request->getUri()->__toString())->isEqualTo($expectedUrl);
$this->castToString($request->getUri())->isEqualTo($expectedUrl);
}

public function urlWithSlashesProvider()
Expand Down Expand Up @@ -200,7 +126,7 @@ public function urlWithSlashesProvider()
* @param int $statusCode
* @param array $responseHeaders
*/
public function test_get_response($statusCode, array $responseHeaders)
public function test_get_return_the_response_we_expected($statusCode, array $responseHeaders)
{
// Given
$mockHttpClient = $this->mockHttpClient('http://verylastroom.com', $statusCode, $responseHeaders);
Expand All @@ -222,17 +148,67 @@ public function responseDataProvider()
return [
[
'statusCode' => 200,
'requestHeaders' => [
'responseHeaders' => [
"name" => "value"
]
],
[
'statusCode' => 400,
'requestHeaders' => [
'responseHeaders' => [
"name1" => "value1",
"name2" => "value2"
]
]
];
}

/**
* @dataProvider formDataUseCase
*/
public function test_we_can_send_body_as_form_data($formData, $expectedBody)
{
$this
->given(
$mockHttpAdapter = $this->mockHttpClient('http://verylastroom.com', 200, []),
$restApiBrowser = new SUT(null, null, $mockHttpAdapter)
)
->when(
$restApiBrowser->sendRequest('POST', '/api', $formData)
)
->then
->castToString($mockHttpAdapter->getReceivedRequests()[0]->getBody())
->isEqualTo($expectedBody)
;
}

public function formDataUseCase()
{
return [
[[], ''],
[['username' => 'jean-marc'], 'username=jean-marc'],
[['username' => 'jean-marc', 'password' => 'ecureuil'], 'username=jean-marc&password=ecureuil'],
];
}

/**
* @param string $baseUrl
* @param int $responseStatusCode
* @param array $headers
*
* @return \Ivory\HttpAdapter\HttpAdapterInterface
*/
private function mockHttpClient($baseUrl, $responseStatusCode, array $headers = [])
{
$mockHttpClient = new \Ivory\HttpAdapter\MockHttpAdapter();
$mockHttpClient->getConfiguration()->setBaseUri($baseUrl);
$messageFactory = new \Ivory\HttpAdapter\Message\MessageFactory($baseUrl);
$mockHttpClient->appendResponse(
$messageFactory->createResponse(
$responseStatusCode,
\Ivory\HttpAdapter\Message\RequestInterface::PROTOCOL_VERSION_1_1,
$headers
)
);
return $mockHttpClient;
}
}

0 comments on commit 421c50f

Please sign in to comment.