Skip to content

Commit 902357c

Browse files
committed
Deal with form data
1 parent d1a02d8 commit 902357c

File tree

4 files changed

+56
-5
lines changed

4 files changed

+56
-5
lines changed

features/send_request.feature

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,22 @@ Feature: Test send API request
1616
When I send a POST request to "echo" with body:
1717
"""
1818
{
19-
"name" : "name",
20-
"pass": "pass"
19+
"username" : "pablo",
20+
"password": "money"
2121
}
2222
"""
2323
Then the JSON node "method" should be equal to "POST"
2424
And the JSON node "headers.content-type[0]" should be equal to "application/json"
25+
And the JSON node "username" should be equal to "pablo"
26+
And the JSON node "password" should be equal to "money"
27+
28+
Scenario: Sending POST request with form data
29+
When I send a POST request to "echo" with form data:
30+
| username | pablo |
31+
| password | money |
32+
Then the response status code should be 200
33+
And the JSON node "username" should be equal to "pablo"
34+
And the JSON node "password" should be equal to "money"
2535

2636
Scenario: Add same header 2 times
2737
Given I add "header" header equal to "value"

src/Rest/RestApiBrowser.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function getHttpClient()
9292
/**
9393
* @param string $method
9494
* @param string $url
95-
* @param string $body
95+
* @param string|array $body
9696
*/
9797
public function sendRequest($method, $url, $body = null)
9898
{
@@ -116,17 +116,19 @@ public function sendRequest($method, $url, $body = null)
116116
/**
117117
* @param string $method
118118
* @param string $uri With or without host
119-
* @param string|resource|array $body
119+
* @param string|array $body
120120
*/
121121
private function send($method, $uri, $body = null)
122122
{
123123
if (!$this->hasHost($uri)) {
124124
$uri = rtrim($this->httpClient->getConfiguration()->getBaseUri(), '/') . '/' . ltrim($uri, '/');
125125
}
126+
$body = is_array($body) ? http_build_query($body) : $body;
126127
$stream = new Stream('php://memory', 'rw');
127-
if ($body) {
128+
if (is_string($body)) {
128129
$stream->write($body);
129130
}
131+
130132
$this->request = new Request($uri, $method, $stream, $this->requestHeaders);
131133
$this->response = $this->httpClient->sendRequest($this->request);
132134
// Reset headers used for the HTTP request

src/RestApiContext.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Behat\Behat\Context\Context;
77
use Behat\Behat\Context\SnippetAcceptingContext;
88
use Behat\Gherkin\Node\PyStringNode;
9+
use Behat\Gherkin\Node\TableNode;
910
use Psr\Http\Message\RequestInterface;
1011
use Psr\Http\Message\ResponseInterface;
1112
use Rezzza\RestApiBehatExtension\Rest\RestApiBrowser;
@@ -47,6 +48,16 @@ public function iSendARequestWithBody($method, $url, PyStringNode $body)
4748
$this->restApiBrowser->sendRequest($method, $url, (string) $body);
4849
}
4950

51+
/**
52+
* Sends HTTP request to specific URL with POST parameters.
53+
*
54+
* @When I send a :method request to :url with form data:
55+
*/
56+
public function iSendAPostRequestToWithFormData($method, $url, TableNode $formData)
57+
{
58+
$this->restApiBrowser->sendRequest($method, $url, $formData->getRowsHash());
59+
}
60+
5061
/**
5162
* @param string $code status code
5263
*

tests/Units/Rest/RestApiBrowser.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,34 @@ public function responseDataProvider()
162162
];
163163
}
164164

165+
/**
166+
* @dataProvider formDataUseCase
167+
*/
168+
public function test_we_can_send_body_as_form_data($formData, $expectedBody)
169+
{
170+
$this
171+
->given(
172+
$mockHttpAdapter = $this->mockHttpClient('http://verylastroom.com', 200, []),
173+
$restApiBrowser = new SUT(null, null, $mockHttpAdapter)
174+
)
175+
->when(
176+
$restApiBrowser->sendRequest('POST', '/api', $formData)
177+
)
178+
->then
179+
->castToString($mockHttpAdapter->getReceivedRequests()[0]->getBody())
180+
->isEqualTo($expectedBody)
181+
;
182+
}
183+
184+
public function formDataUseCase()
185+
{
186+
return [
187+
[[], ''],
188+
[['username' => 'jean-marc'], 'username=jean-marc'],
189+
[['username' => 'jean-marc', 'password' => 'ecureuil'], 'username=jean-marc&password=ecureuil'],
190+
];
191+
}
192+
165193
/**
166194
* @param string $baseUrl
167195
* @param int $responseStatusCode

0 commit comments

Comments
 (0)