Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for data encoding #59

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ class Request
*/
private $encoding = Request::ENCODING_QUERY;

/**
* Cache of encoded data.
*
* @var string|null
*/
private $encodedData = null;

/**
* @param cURL $curl
*/
Expand All @@ -120,7 +127,7 @@ public function setMethod($method)
throw new \InvalidArgumentException("Method [$method] not a valid HTTP method.");
}

if ($this->data && !static::$methods[$method]) {
if ($this->data && !$this->allowsData()) {
throw new \LogicException('Request has POST data, but tried changing HTTP method to one that does not allow POST data');
}

Expand Down Expand Up @@ -314,18 +321,29 @@ public function formatHeaders()
return $headers;
}

/**
* Detemine if a request allows data to be set.
*
* @return bool
*/
public function allowsData()
{
return static::$methods[$this->method];
}

/**
* Set the POST data to be sent with the request.
*
* @param mixed $data
*/
public function setData($data)
{
if ($data && !static::$methods[$this->method]) {
if ($data !== null && !$this->allowsData()) {
throw new \InvalidArgumentException("HTTP method [$this->method] does not allow POST data.");
}

$this->data = $data;
$this->encodedData = null;

return $this;
}
Expand All @@ -337,7 +355,7 @@ public function setData($data)
*/
public function hasData()
{
return (bool) $this->data;
return (bool) $this->getEncodedData();
}

/**
Expand Down Expand Up @@ -372,6 +390,7 @@ public function setEncoding($encoding)
}

$this->encoding = $encoding;
$this->encodedData = null;

return $this;
}
Expand All @@ -393,6 +412,10 @@ public function getEncoding()
*/
public function encodeData()
{
if ($this->data === null) {
return '';
}

switch ($this->encoding) {
case static::ENCODING_JSON:
return json_encode($this->data);
Expand All @@ -405,6 +428,19 @@ public function encodeData()
}
}

/**
* Get the encoded POST data as a string.
*
* @return string
*/
public function getEncodedData()
{
if ($this->encodedData === null) {
$this->encodedData = $this->encodeData();
}
return $this->encodedData;
}

/**
* Set a specific curl option for the request.
*
Expand Down
4 changes: 2 additions & 2 deletions src/cURL.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public function buildUrl($url, array $query)
*
* @return Request
*/
public function newRequest($method, $url, $data = array(), $encoding = Request::ENCODING_QUERY)
public function newRequest($method, $url, $data = null, $encoding = Request::ENCODING_QUERY)
{
$class = $this->requestClass;
$request = new $class($this);
Expand Down Expand Up @@ -264,7 +264,7 @@ public function prepareRequest(Request $request)
curl_setopt($this->ch, CURLOPT_HTTPHEADER, $request->formatHeaders());

if ($request->hasData()) {
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $request->encodeData());
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $request->getEncodedData());
}

if ($method === 'head') {
Expand Down
44 changes: 44 additions & 0 deletions tests/unit/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,50 @@ public function encodeData()
$this->assertEquals('<rawData>ArbitraryValue</rawData>', $r->encodeData());
}

/** @test */
public function encodeJsonData()
{
$r = $this->makeRequest();
$r->setMethod('post');
$r->setEncoding(Request::ENCODING_JSON);

$r->setData([]);
$this->assertEquals('[]', $r->encodeData());

$r->setData(new \stdClass);
$this->assertEquals('{}', $r->encodeData());
}

/** @test */
public function changeDataUpdatesEncodedData()
{
$r = $this->makeRequest();
$r->setMethod('post');
$r->setEncoding(Request::ENCODING_JSON);

$r->setData(['foo' => 'bar']);
$data1 = $r->getEncodedData();
$r->setData(['bar' => 'qux']);
$data2 = $r->getEncodedData();

$this->assertNotEquals($data1, $data2);
}

/** @test */
public function changeEncodingUpdatesEncodedData()
{
$r = $this->makeRequest();
$r->setMethod('post');
$r->setData(['foo' => 'bar']);

$r->setEncoding(Request::ENCODING_JSON);
$jsonData = $r->getEncodedData();
$r->setEncoding(Request::ENCODING_QUERY);
$queryData = $r->getEncodedData();

$this->assertNotEquals($jsonData, $queryData);
}

/** @test */
public function formatHeaders()
{
Expand Down