Skip to content

Commit a2809a2

Browse files
committed
fix hasData for non-post requests
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
1 parent ad486e9 commit a2809a2

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/Request.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ public function setData($data)
337337
*/
338338
public function hasData()
339339
{
340-
return (bool) $this->encodeData();
340+
return static::$methods[$this->method] && (bool) $this->encodeData();
341341
}
342342

343343
/**
@@ -401,7 +401,7 @@ public function encodeData()
401401
case static::ENCODING_RAW:
402402
return $this->data;
403403
default:
404-
$msg = "Encoding [" . $this->encoding . "] not a known Request::ENCODING_* constant";
404+
$msg = "Encoding [$this->encoding] not a known Request::ENCODING_* constant";
405405
throw new \UnexpectedValueException($msg);
406406
}
407407
}

tests/unit/RequestTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,14 @@ public function cookies()
124124
$this->assertEquals('foo', $r->getCookie('baz'));
125125
$this->assertEquals('baz=foo', $r->getHeader('cookie'));
126126
}
127+
128+
/** @test */
129+
public function emptyJsonGetRequestHasNoData()
130+
{
131+
$r = $this->makeRequest();
132+
$r->setEncoding(Request::ENCODING_JSON);
133+
$r->setMethod('get');
134+
135+
$this->assertFalse($r->hasData());
136+
}
127137
}

0 commit comments

Comments
 (0)