Skip to content

Commit 376930f

Browse files
committed
Throw exception if string $parameters is passed to method without request body
1 parent 32e41e8 commit 376930f

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

src/Codeception/Module/REST.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -638,19 +638,26 @@ protected function execute($method, $url, $parameters = [], $files = [])
638638

639639
$this->params = $parameters;
640640

641-
$parameters = $this->encodeApplicationJson($method, $parameters);
642641
$isQueryParamsAwareMethod = in_array($method, self::QUERY_PARAMS_AWARE_METHODS, true);
643642

644-
if (is_array($parameters) || $isQueryParamsAwareMethod) {
645-
if (!empty($parameters) && $isQueryParamsAwareMethod) {
646-
if (strpos($url, '?') !== false) {
647-
$url .= '&';
648-
} else {
649-
$url .= '?';
650-
}
651-
$url .= http_build_query($parameters);
643+
if ($isQueryParamsAwareMethod) {
644+
if (!is_array($parameters)) {
645+
throw new ModuleException(__CLASS__, $method . ' parameters must be passed in array format');
652646
}
647+
} else {
648+
$parameters = $this->encodeApplicationJson($method, $parameters);
649+
}
650+
651+
if (is_array($parameters) || $isQueryParamsAwareMethod) {
653652
if ($isQueryParamsAwareMethod) {
653+
if (!empty($parameters)) {
654+
if (strpos($url, '?') !== false) {
655+
$url .= '&';
656+
} else {
657+
$url .= '?';
658+
}
659+
$url .= http_build_query($parameters);
660+
}
654661
$this->debugSection("Request", "$method $url");
655662
$files = [];
656663
} else {
@@ -707,7 +714,6 @@ protected function encodeApplicationJson($method, $parameters)
707714
{
708715
if (
709716
array_key_exists('Content-Type', $this->connectionModule->headers)
710-
&& !in_array($method, self::QUERY_PARAMS_AWARE_METHODS, true)
711717
&& ($this->connectionModule->headers['Content-Type'] === 'application/json'
712718
|| preg_match('!^application/.+\+json$!', $this->connectionModule->headers['Content-Type'])
713719
)

tests/unit/Codeception/Module/RestTest.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,8 @@ public function testApplicationJsonIncludesObjectSerialized()
220220
*/
221221
public function testGetApplicationJsonNotIncludesJsonAsContent($method)
222222
{
223-
$method = 'send' . $method;
224223
$this->module->haveHttpHeader('Content-Type', 'application/json');
225-
$this->module->$method('/', ['name' => 'john']);
224+
$this->module->send($method, '/', ['name' => 'john']);
226225
/** @var $request \Symfony\Component\BrowserKit\Request **/
227226
$request = $this->module->client->getRequest();
228227
$this->assertNull($request->getContent());
@@ -232,11 +231,22 @@ public function testGetApplicationJsonNotIncludesJsonAsContent($method)
232231
public function queryParamsAwareMethods()
233232
{
234233
return [
235-
['Get'],
236-
['Head'],
234+
'GET' => ['GET'],
235+
'HEAD' => ['HEAD'],
236+
'DELETE' => ['DELETE'],
237+
'OPTIONS' => ['OPTIONS'],
237238
];
238239
}
239240

241+
/**
242+
* @dataProvider queryParamsAwareMethods
243+
*/
244+
public function testRequestThrowsExceptionIfParametersIsString($method)
245+
{
246+
$this->expectExceptionMessage($method, ' parameters must be passed in array format');
247+
$this->module->send($method, '/', 'string');
248+
}
249+
240250
public function testUrlIsFull()
241251
{
242252
$this->module->sendGET('/api/v1/users');

0 commit comments

Comments
 (0)