Skip to content

Commit 74596f3

Browse files
committed
feat(http): add QUERY method support
- Add QUERY to HTTP method constants, routing, CURLRequest, and feature tests - Treat QUERY as raw-body input for validation and form requests - Document QUERY routing, request testing, and CURLRequest usage - Add coverage for routing, CSRF behavior, request input, and test helpers Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
1 parent 678db72 commit 74596f3

26 files changed

Lines changed: 208 additions & 17 deletions

‎system/HTTP/CURLRequest.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,16 @@ public function put(string $url, array $options = []): ResponseInterface
292292
return $this->request(Method::PUT, $url, $options);
293293
}
294294

295+
/**
296+
* Convenience method for sending a QUERY request.
297+
*
298+
* @param array<string, mixed> $options
299+
*/
300+
public function query(string $url, array $options = []): ResponseInterface
301+
{
302+
return $this->request(Method::QUERY, $url, $options);
303+
}
304+
295305
/**
296306
* Set the HTTP Authentication.
297307
*

‎system/HTTP/FormRequest.php‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,10 @@ public function getValidatedInput(): ValidatedInput
223223
* multiple sources. By default, data is sourced from the appropriate
224224
* part of the request based on HTTP method and Content-Type:
225225
*
226-
* - JSON (any method) - decoded JSON body
227-
* - PUT / PATCH / DELETE - raw body (unless multipart/form-data)
228-
* - GET / HEAD - query-string parameters
229-
* - Everything else (POST) - POST body
226+
* - JSON (any method) - decoded JSON body
227+
* - PUT / PATCH / DELETE / QUERY - raw body (unless multipart/form-data)
228+
* - GET / HEAD - query-string parameters
229+
* - Everything else (POST) - POST body
230230
*
231231
* @return array<string, mixed>
232232
*/
@@ -239,7 +239,7 @@ protected function validationData(): array
239239
}
240240

241241
if (
242-
in_array($this->request->getMethod(), [Method::PUT, Method::PATCH, Method::DELETE], true)
242+
in_array($this->request->getMethod(), [Method::PUT, Method::PATCH, Method::DELETE, Method::QUERY], true)
243243
&& ! str_contains($contentType, 'multipart/form-data')
244244
) {
245245
return $this->request->getRawInput() ?? [];

‎system/HTTP/IncomingRequest.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ public function getJsonVar($index = null, bool $assoc = false, ?int $filter = nu
503503
}
504504

505505
/**
506-
* A convenience method that grabs the raw input stream(send method in PUT, PATCH, DELETE) and decodes
506+
* A convenience method that grabs the raw input stream (send method in PUT, PATCH, DELETE, QUERY) and decodes
507507
* the String into an array.
508508
*
509509
* @return array
@@ -516,7 +516,7 @@ public function getRawInput()
516516
}
517517

518518
/**
519-
* Gets a specific variable from raw input stream (send method in PUT, PATCH, DELETE).
519+
* Gets a specific variable from raw input stream (send method in PUT, PATCH, DELETE, QUERY).
520520
*
521521
* @param array|string|null $index The variable that you want which can use dot syntax for getting specific values.
522522
* @param int|null $filter Filter Constant

‎system/HTTP/Method.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@ class Method
9090
*/
9191
public const PUT = 'PUT';
9292

93+
/**
94+
* Safe: Yes
95+
* Idempotent: Yes
96+
* Cacheable: Yes
97+
*
98+
* @see https://www.rfc-editor.org/rfc/rfc10008.html
99+
*/
100+
public const QUERY = 'QUERY';
101+
93102
/**
94103
* Safe: Yes
95104
* Idempotent: Yes
@@ -115,6 +124,7 @@ public static function all(): array
115124
self::PATCH,
116125
self::POST,
117126
self::PUT,
127+
self::QUERY,
118128
self::TRACE,
119129
];
120130
}

‎system/Router/RouteCollection.php‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ class RouteCollection implements RouteCollectionInterface
143143
Method::OPTIONS => [],
144144
Method::GET => [],
145145
Method::HEAD => [],
146+
Method::QUERY => [],
146147
Method::POST => [],
147148
Method::PATCH => [],
148149
Method::PUT => [],
@@ -168,6 +169,7 @@ class RouteCollection implements RouteCollectionInterface
168169
Method::OPTIONS => [],
169170
Method::GET => [],
170171
Method::HEAD => [],
172+
Method::QUERY => [],
171173
Method::POST => [],
172174
Method::PATCH => [],
173175
Method::PUT => [],
@@ -1102,6 +1104,19 @@ public function head(string $from, $to, ?array $options = null): RouteCollection
11021104
return $this;
11031105
}
11041106

1107+
/**
1108+
* Specifies a route that is only available to QUERY requests.
1109+
*
1110+
* @param array<string, mixed>|(Closure(mixed...): (ResponseInterface|string|void))|string $to
1111+
* @param array<string, mixed>|null $options
1112+
*/
1113+
public function query(string $from, $to, ?array $options = null): RouteCollectionInterface
1114+
{
1115+
$this->create(Method::QUERY, $from, $to, $options);
1116+
1117+
return $this;
1118+
}
1119+
11051120
/**
11061121
* Specifies a route that is only available to PATCH requests.
11071122
*

‎system/Router/Router.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class Router implements RouterInterface
4343
public const HTTP_METHODS = [
4444
Method::GET,
4545
Method::HEAD,
46+
Method::QUERY,
4647
Method::POST,
4748
Method::PATCH,
4849
Method::PUT,

‎system/Test/FeatureTestTrait.php‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,21 @@ public function options(string $path, ?array $params = null)
296296
return $this->call(Method::OPTIONS, $path, $params);
297297
}
298298

299+
/**
300+
* Performs a QUERY request.
301+
*
302+
* @param array<string, mixed>|null $params
303+
*
304+
* @return TestResponse
305+
*
306+
* @throws RedirectException
307+
* @throws Exception
308+
*/
309+
public function query(string $path, ?array $params = null)
310+
{
311+
return $this->call(Method::QUERY, $path, $params);
312+
}
313+
299314
/**
300315
* Setup a Request object to use so that CodeIgniter
301316
* won't try to auto-populate some of the items.

‎system/Validation/Validation.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ public function withRequest(RequestInterface $request): ValidationInterface
535535
return $this;
536536
}
537537

538-
if (in_array($request->getMethod(), [Method::PUT, Method::PATCH, Method::DELETE], true)
538+
if (in_array($request->getMethod(), [Method::PUT, Method::PATCH, Method::DELETE, Method::QUERY], true)
539539
&& ! str_contains($request->getHeaderLine('Content-Type'), 'multipart/form-data')
540540
) {
541541
$this->data = $request->getRawInput();

‎tests/system/Commands/Utilities/RoutesTest.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ protected function setUp(): void
3232
{
3333
$this->resetServices();
3434
parent::setUp();
35+
36+
service('superglobals')
37+
->setServer('HTTP_HOST', 'example.com')
38+
->setServer('SERVER_NAME', 'example.com');
3539
}
3640

3741
protected function tearDown(): void
@@ -68,6 +72,7 @@ public function testRoutesCommand(): void
6872
| GET | closure | » | (Closure) | | |
6973
| GET | testing | testing-index | \App\Controllers\TestController::index | | |
7074
| HEAD | testing | testing-index | \App\Controllers\TestController::index | | |
75+
| QUERY | testing | testing-index | \App\Controllers\TestController::index | | |
7176
| POST | testing | testing-index | \App\Controllers\TestController::index | | |
7277
| PATCH | testing | testing-index | \App\Controllers\TestController::index | | |
7378
| PUT | testing | testing-index | \App\Controllers\TestController::index | | |
@@ -104,6 +109,7 @@ public function testRoutesCommandSortByHandler(): void
104109
| GET | / | » | \App\Controllers\Home::index | | |
105110
| GET | testing | testing-index | \App\Controllers\TestController::index | | |
106111
| HEAD | testing | testing-index | \App\Controllers\TestController::index | | |
112+
| QUERY | testing | testing-index | \App\Controllers\TestController::index | | |
107113
| POST | testing | testing-index | \App\Controllers\TestController::index | | |
108114
| PATCH | testing | testing-index | \App\Controllers\TestController::index | | |
109115
| PUT | testing | testing-index | \App\Controllers\TestController::index | | |
@@ -133,6 +139,7 @@ public function testRoutesCommandHostHostname(): void
133139
| GET | all | » | \App\Controllers\AllDomain::index | | |
134140
| GET | testing | testing-index | \App\Controllers\TestController::index | | |
135141
| HEAD | testing | testing-index | \App\Controllers\TestController::index | | |
142+
| QUERY | testing | testing-index | \App\Controllers\TestController::index | | |
136143
| POST | testing | testing-index | \App\Controllers\TestController::index | | |
137144
| PATCH | testing | testing-index | \App\Controllers\TestController::index | | |
138145
| PUT | testing | testing-index | \App\Controllers\TestController::index | | |
@@ -162,6 +169,7 @@ public function testRoutesCommandHostSubdomain(): void
162169
| GET | all | » | \App\Controllers\AllDomain::index | | |
163170
| GET | testing | testing-index | \App\Controllers\TestController::index | | |
164171
| HEAD | testing | testing-index | \App\Controllers\TestController::index | | |
172+
| QUERY | testing | testing-index | \App\Controllers\TestController::index | | |
165173
| POST | testing | testing-index | \App\Controllers\TestController::index | | |
166174
| PATCH | testing | testing-index | \App\Controllers\TestController::index | | |
167175
| PUT | testing | testing-index | \App\Controllers\TestController::index | | |
@@ -193,6 +201,7 @@ public function testRoutesCommandAutoRouteImproved(): void
193201
| GET | closure | » | (Closure) | | |
194202
| GET | testing | testing-index | \App\Controllers\TestController::index | | |
195203
| HEAD | testing | testing-index | \App\Controllers\TestController::index | | |
204+
| QUERY | testing | testing-index | \App\Controllers\TestController::index | | |
196205
| POST | testing | testing-index | \App\Controllers\TestController::index | | |
197206
| PATCH | testing | testing-index | \App\Controllers\TestController::index | | |
198207
| PUT | testing | testing-index | \App\Controllers\TestController::index | | |
@@ -228,6 +237,7 @@ public function testRoutesCommandRouteLegacy(): void
228237
| GET | closure | » | (Closure) | | |
229238
| GET | testing | testing-index | \App\Controllers\TestController::index | | |
230239
| HEAD | testing | testing-index | \App\Controllers\TestController::index | | |
240+
| QUERY | testing | testing-index | \App\Controllers\TestController::index | | |
231241
| POST | testing | testing-index | \App\Controllers\TestController::index | | |
232242
| PATCH | testing | testing-index | \App\Controllers\TestController::index | | |
233243
| PUT | testing | testing-index | \App\Controllers\TestController::index | | |

‎tests/system/HTTP/CURLRequestTest.php‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,35 @@ public function testOptionsSetsCorrectMethod(): void
175175
$this->assertSame('OPTIONS', $options[CURLOPT_CUSTOMREQUEST]);
176176
}
177177

178+
public function testQuerySetsCorrectMethod(): void
179+
{
180+
$this->request->query('http://example.com');
181+
182+
$this->assertSame('QUERY', $this->request->getMethod());
183+
184+
$options = $this->request->curl_options;
185+
186+
$this->assertArrayHasKey(CURLOPT_CUSTOMREQUEST, $options);
187+
$this->assertSame('QUERY', $options[CURLOPT_CUSTOMREQUEST]);
188+
}
189+
190+
public function testQueryWithJsonSetsBodyAndContentType(): void
191+
{
192+
$params = [
193+
'foo' => 'bar',
194+
];
195+
$this->request->query('http://example.com', [
196+
'json' => $params,
197+
]);
198+
199+
$options = $this->request->curl_options;
200+
201+
$this->assertSame('QUERY', $this->request->getMethod());
202+
$this->assertSame('QUERY', $options[CURLOPT_CUSTOMREQUEST]);
203+
$this->assertSame(json_encode($params), $options[CURLOPT_POSTFIELDS]);
204+
$this->assertContains('Content-Type: application/json', $options[CURLOPT_HTTPHEADER]);
205+
}
206+
178207
public function testOptionsBaseURIOption(): void
179208
{
180209
$options = ['baseURI' => 'http://www.foo.com/api/v1/'];

0 commit comments

Comments
 (0)