Skip to content

Commit

Permalink
fix hasData for non-post requests
Browse files Browse the repository at this point in the history
the default value of the $data property is an empty array, which for
some encodings would be encoded into a non-empty string. this would
cause hasData to return true even for request types that are not meant
to send data, which again would trigger an if condition that calls
curl_setopt with CURLOPT_POSTFIELDS.

fixes #61
  • Loading branch information
anlutro committed Dec 14, 2018
1 parent ad486e9 commit a2809a2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ public function setData($data)
*/
public function hasData()
{
return (bool) $this->encodeData();
return static::$methods[$this->method] && (bool) $this->encodeData();
}

/**
Expand Down Expand Up @@ -401,7 +401,7 @@ public function encodeData()
case static::ENCODING_RAW:
return $this->data;
default:
$msg = "Encoding [" . $this->encoding . "] not a known Request::ENCODING_* constant";
$msg = "Encoding [$this->encoding] not a known Request::ENCODING_* constant";
throw new \UnexpectedValueException($msg);
}
}
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,14 @@ public function cookies()
$this->assertEquals('foo', $r->getCookie('baz'));
$this->assertEquals('baz=foo', $r->getHeader('cookie'));
}

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

$this->assertFalse($r->hasData());
}
}

0 comments on commit a2809a2

Please sign in to comment.