Skip to content

Commit 4c8c6bd

Browse files
authored
Append query params to URL for HEAD requests
1 parent e314fe7 commit 4c8c6bd

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

src/Codeception/Module/REST.php

+9-4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@
7171
*/
7272
class REST extends CodeceptionModule implements DependsOnModule, PartedModule, API, ConflictsWithModule
7373
{
74+
const QUERY_PARAMS_AWARE_METHODS = ['GET', 'HEAD'];
75+
7476
protected $config = [
7577
'url' => '',
7678
'aws' => ''
@@ -610,17 +612,18 @@ protected function execute($method, $url, $parameters = [], $files = [])
610612
$this->params = $parameters;
611613

612614
$parameters = $this->encodeApplicationJson($method, $parameters);
615+
$isQueryParamsAwareMethod = in_array($method, self::QUERY_PARAMS_AWARE_METHODS, true);
613616

614-
if (is_array($parameters) || $method === 'GET') {
615-
if (!empty($parameters) && $method === 'GET') {
617+
if (is_array($parameters) || $isQueryParamsAwareMethod) {
618+
if (!empty($parameters) && $isQueryParamsAwareMethod) {
616619
if (strpos($url, '?') !== false) {
617620
$url .= '&';
618621
} else {
619622
$url .= '?';
620623
}
621624
$url .= http_build_query($parameters);
622625
}
623-
if ($method == 'GET') {
626+
if ($isQueryParamsAwareMethod) {
624627
$this->debugSection("Request", "$method $url");
625628
$files = [];
626629
} else {
@@ -675,7 +678,9 @@ protected function binaryToDebugString($data)
675678

676679
protected function encodeApplicationJson($method, $parameters)
677680
{
678-
if ($method !== 'GET' && array_key_exists('Content-Type', $this->connectionModule->headers)
681+
if (
682+
array_key_exists('Content-Type', $this->connectionModule->headers)
683+
&& !in_array($method, self::QUERY_PARAMS_AWARE_METHODS, true)
679684
&& ($this->connectionModule->headers['Content-Type'] === 'application/json'
680685
|| preg_match('!^application/.+\+json$!', $this->connectionModule->headers['Content-Type'])
681686
)

tests/data/rest/server.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function RESTServer()
1414

1515
// get the request data
1616
$data = NULL;
17-
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
17+
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
1818
$data = $_GET;
1919
} else if ($tmp = file_get_contents('php://input')) {
2020
$data = json_decode($tmp);

tests/unit/Codeception/Module/RestTest.php

+21-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use Codeception\Configuration;
4+
use Codeception\Example;
35
use Codeception\Module\UniversalFramework;
46
use Codeception\Test\Unit;
57
use Codeception\Util\Stub;
@@ -17,9 +19,9 @@ class RestTest extends Unit
1719

1820
public function _setUp()
1921
{
20-
$index = \Codeception\Configuration::dataDir() . '/rest/index.php';
22+
$index = Configuration::dataDir() . '/rest/index.php';
2123

22-
$container = \Codeception\Util\Stub::make('Codeception\Lib\ModuleContainer');
24+
$container = Stub::make('Codeception\Lib\ModuleContainer');
2325
$connectionModule = new UniversalFramework($container, ['index' => $index]);
2426
$connectionModule->_initialize();
2527
$this->module = Stub::make('\Codeception\Module\REST');
@@ -204,16 +206,30 @@ public function testApplicationJsonIncludesObjectSerialized()
204206
$this->assertJson($request->getContent());
205207
}
206208

207-
public function testGetApplicationJsonNotIncludesJsonAsContent()
209+
/**
210+
* @param string $method
211+
*
212+
* @dataProvider queryParamsAwareMethods
213+
*/
214+
public function testGetApplicationJsonNotIncludesJsonAsContent($method)
208215
{
216+
$method = 'send' . $method;
209217
$this->module->haveHttpHeader('Content-Type', 'application/json');
210-
$this->module->sendGET('/', ['name' => 'john']);
218+
$this->module->$method('/', ['name' => 'john']);
211219
/** @var $request \Symfony\Component\BrowserKit\Request **/
212220
$request = $this->module->client->getRequest();
213221
$this->assertNull($request->getContent());
214222
$this->assertContains('john', $request->getParameters());
215223
}
216224

225+
public function queryParamsAwareMethods()
226+
{
227+
return [
228+
['Get'],
229+
['Head'],
230+
];
231+
}
232+
217233
public function testUrlIsFull()
218234
{
219235
$this->module->sendGET('/api/v1/users');
@@ -529,7 +545,7 @@ public function testRestExecute($configUrl, $requestUrl, $expectedFullUrl)
529545

530546
$config = ['url' => $configUrl];
531547

532-
/** @var \Codeception\Module\REST */
548+
/** @var REST */
533549
$module = Stub::make('\Codeception\Module\REST');
534550
$module->_setConfig($config);
535551
$module->_inject($connectionModule);

0 commit comments

Comments
 (0)