Skip to content

Commit

Permalink
Deal with form data
Browse files Browse the repository at this point in the history
  • Loading branch information
tyx committed Dec 13, 2015
1 parent d1a02d8 commit 902357c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 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
28 changes: 28 additions & 0 deletions tests/Units/Rest/RestApiBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,34 @@ public function responseDataProvider()
];
}

/**
* @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
Expand Down

0 comments on commit 902357c

Please sign in to comment.