Add Request::getFormFields. Add Test for sending form-encoded request

This commit is contained in:
PJ Dietz 2014-07-26 11:49:15 -04:00
parent 297c4aa2e8
commit 4db7c8da7d
4 changed files with 82 additions and 8 deletions

View File

@ -330,6 +330,17 @@ class Request extends Message implements RequestInterface
$this->setQuery($query); $this->setQuery($query);
} }
/**
* Return the form fields for this request.
*
* @return array
*/
public function getFormFields()
{
parse_str($this->getBody(), $fields);
return $fields;
}
/** /**
* Set the body by supplying an associative array of form fields. * Set the body by supplying an associative array of form fields.
* *

View File

@ -5,6 +5,7 @@ namespace pjdietz\WellRESTed\Test;
use Faker\Factory; use Faker\Factory;
use pjdietz\ShamServer\ShamServer; use pjdietz\ShamServer\ShamServer;
use pjdietz\WellRESTed\Client; use pjdietz\WellRESTed\Client;
use pjdietz\WellRESTed\Request;
class ClientTest extends \PHPUnit_Framework_TestCase class ClientTest extends \PHPUnit_Framework_TestCase
{ {
@ -138,6 +139,42 @@ class ClientTest extends \PHPUnit_Framework_TestCase
]; ];
} }
/**
* @dataProvider formProvider
*/
public function testSendForm($form)
{
$host = "localhost";
$port = 8080;
$script = realpath(__DIR__ . "/sham-routers/formFields.php");
$server = new ShamServer($host, $port, $script);
$rqst = new Request("http://$host:$port");
$rqst->setMethod("POST");
$rqst->setFormFields($form);
$client = new Client();
$resp = $client->request($rqst);
$body = json_decode($resp->getBody(), true);
$this->assertEquals($form, $body);
$server->stop();
}
public function formProvider()
{
$faker = Factory::create();
return [
[
[
"firstName" => $faker->firstName,
"lastName" => $faker->lastName,
"email" => $faker->email
]
],
];
}
public function testSetCustomCurlOptionsOnInstantiation() public function testSetCustomCurlOptionsOnInstantiation()
{ {
$host = "localhost"; $host = "localhost";

View File

@ -178,15 +178,11 @@ class RequestTest extends \PHPUnit_Framework_TestCase
$this->assertNull($rqst->getBody()); $this->assertNull($rqst->getBody());
} }
public function testSetFormFields() /**
* @dataProvider formProvider
*/
public function testFormFieldsEncodeProperly($form)
{ {
$faker = Factory::create();
$form = [
"firstName" => $faker->firstName,
"lastName" => $faker->lastName,
"username" => $faker->userName
];
$rqst = new Request(); $rqst = new Request();
$rqst->setFormFields($form); $rqst->setFormFields($form);
$body = $rqst->getBody(); $body = $rqst->getBody();
@ -194,6 +190,31 @@ class RequestTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($form, $fields); $this->assertEquals($form, $fields);
} }
/**
* @dataProvider formProvider
*/
public function testFormFieldsDecodeProperly($form)
{
$rqst = new Request();
$rqst->setFormFields($form);
$fields = $rqst->getFormFields();
$this->assertEquals($form, $fields);
}
public function formProvider()
{
$faker = Factory::create();
return [
[
[
"firstName" => $faker->firstName,
"lastName" => $faker->lastName,
"username" => $faker->userName
]
]
];
}
/** /**
* @dataProvider headerProvider * @dataProvider headerProvider
*/ */

View File

@ -0,0 +1,5 @@
<?php
print(json_encode($_POST));
exit;