-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #96 from bystro/SendRequestAsHTMLformVer2.0
Sending POST request as HTML form
- Loading branch information
Showing
6 changed files
with
162 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
Feature: Test request to API sent like a HTML form | ||
In order to test my API | ||
As a developper | ||
I want to be able to perform HTTP request with HTML form data | ||
|
||
Scenario: Sending POST request as a HTML form | ||
When I send a POST request to "post-html-form" as HTML form with body: | ||
| object | name | value | | ||
| field | username | pablo | | ||
| field | password | money | | ||
| field | terms_accepted | 1 | | ||
Then the response status code should be 200 | ||
And the response should be in JSON | ||
And the JSON node "content_type_header_value" should contain "application/x-www-form-urlencoded" | ||
And the JSON node "post_fields_count" should be equal to "3" | ||
And the JSON node "post_fields.username" should be equal to "pablo" | ||
And the JSON node "post_fields.password" should be equal to "money" | ||
And the JSON node "post_fields.terms_accepted" should be equal to "1" | ||
|
||
Scenario: Sending POST request as a HTML form with files | ||
When I send a POST request to "post-html-form-with-files" as HTML form with body: | ||
| object | name | value | | ||
| field | username | pablo | | ||
| field | password | money | | ||
| field | terms_accepted | 1 | | ||
| file | test-img | features/bootstrap/fixtures/test-img.jpg | | ||
| file | json-schema | features/bootstrap/fixtures/json-schema.json | | ||
Then the response status code should be 200 | ||
And the response should be in JSON | ||
And the JSON node "content_type_header_value" should contain "multipart/form-data" | ||
And the JSON node "post_fields_count" should be equal to "3" | ||
And the JSON node "post_files_count" should be equal to "2" | ||
And the JSON node "post_fields.username" should be equal to "pablo" | ||
And the JSON node "post_fields.password" should be equal to "money" | ||
And the JSON node "post_fields.terms_accepted" should be equal to "1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
namespace Ubirak\RestApiBehatExtension\Html; | ||
|
||
class Form | ||
{ | ||
private $body = []; | ||
|
||
private $contentTypeHeaderValue = ''; | ||
|
||
public function __construct(array $body) | ||
{ | ||
$this->body = $body; | ||
} | ||
|
||
public function getBody() | ||
{ | ||
if ($this->bodyHasFileObject()) { | ||
return $this->getMultipartStreamBody(); | ||
} | ||
|
||
return $this->getNameValuePairBody(); | ||
} | ||
|
||
/** | ||
* | ||
* @return string | ||
*/ | ||
public function getContentTypeHeaderValue() | ||
{ | ||
return $this->contentTypeHeaderValue; | ||
} | ||
|
||
/** | ||
* | ||
* @param string $value | ||
*/ | ||
private function setContentTypeHeaderValue($value) | ||
{ | ||
$this->contentTypeHeaderValue = $value; | ||
} | ||
|
||
/** | ||
* | ||
* @return boolean | ||
*/ | ||
private function bodyHasFileObject() | ||
{ | ||
foreach ($this->body as $element) { | ||
if ($element['object'] == 'file') { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* | ||
* @return \GuzzleHttp\Psr7\MultipartStream | ||
*/ | ||
private function getMultipartStreamBody() | ||
{ | ||
$multiparts = array_map( | ||
function ($element) { | ||
|
||
if ($element['object'] == 'file') { | ||
return ['name' => $element['name'], 'contents' => fopen($element['value'], 'r')]; | ||
} | ||
|
||
return ['name' => $element['name'], 'contents' => $element['value']]; | ||
}, $this->body | ||
); | ||
|
||
$boundary = sha1(uniqid('', true)); | ||
|
||
$this->setContentTypeHeaderValue('multipart/form-data; boundary=' . $boundary); | ||
|
||
return new \GuzzleHttp\Psr7\MultipartStream($multiparts, $boundary); | ||
} | ||
|
||
/** | ||
* | ||
* @return string | ||
*/ | ||
private function getNameValuePairBody() | ||
{ | ||
$body = []; | ||
foreach ($this->body as $element) { | ||
$body[$element['name']] = $element['value']; | ||
} | ||
|
||
$this->setContentTypeHeaderValue('application/x-www-form-urlencoded'); | ||
|
||
return http_build_query($body, null, '&'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters