Skip to content

[HttpFoundation] Add support for QUERY method #61175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/Symfony/Component/HttpFoundation/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class Request
public const METHOD_OPTIONS = 'OPTIONS';
public const METHOD_TRACE = 'TRACE';
public const METHOD_CONNECT = 'CONNECT';
public const METHOD_QUERY = 'QUERY';

/**
* @var string[]
Expand Down Expand Up @@ -350,6 +351,7 @@ public static function create(string $uri, string $method = 'GET', array $parame
case 'POST':
case 'PUT':
case 'DELETE':
case 'QUERY':
if (!isset($server['CONTENT_TYPE'])) {
$server['CONTENT_TYPE'] = 'application/x-www-form-urlencoded';
}
Expand Down Expand Up @@ -1351,15 +1353,15 @@ public function isMethod(string $method): bool
*/
public function isMethodSafe(): bool
{
return \in_array($this->getMethod(), ['GET', 'HEAD', 'OPTIONS', 'TRACE'], true);
return \in_array($this->getMethod(), ['GET', 'HEAD', 'OPTIONS', 'TRACE', 'QUERY'], true);
}

/**
* Checks whether or not the method is idempotent.
*/
public function isMethodIdempotent(): bool
{
return \in_array($this->getMethod(), ['HEAD', 'GET', 'PUT', 'DELETE', 'TRACE', 'OPTIONS', 'PURGE'], true);
return \in_array($this->getMethod(), ['HEAD', 'GET', 'PUT', 'DELETE', 'TRACE', 'OPTIONS', 'PURGE', 'QUERY'], true);
}

/**
Expand All @@ -1369,7 +1371,7 @@ public function isMethodIdempotent(): bool
*/
public function isMethodCacheable(): bool
{
return \in_array($this->getMethod(), ['GET', 'HEAD'], true);
return \in_array($this->getMethod(), ['GET', 'HEAD', 'QUERY'], true);
}

/**
Expand Down
21 changes: 21 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,24 @@ public function testCreate()
// Fragment should not be included in the URI
$request = Request::create('http://test.com/foo#bar');
$this->assertEquals('http://test.com/foo', $request->getUri());

$query = <<<'GRAPHQL'
query HeroNameAndFriends($episode: Episode) {
hero(episode: $episode) {
name
friends {
name
}
}
}
GRAPHQL;

$request = Request::create('http://example.com/graphql', 'QUERY', content: json_encode(['query' => $query, 'variables' => ['foo' => 'bar']]));
$this->assertEquals(
['query' => $query, 'variables' => ['foo' => 'bar']],
$request->toArray()
);
$this->assertTrue($request->isMethodSafe(), 'QUERY requests should be safe');
}

public function testCreateWithRequestUri()
Expand Down Expand Up @@ -2330,6 +2348,7 @@ public static function methodIdempotentProvider()
['OPTIONS', true],
['TRACE', true],
['CONNECT', false],
['QUERY', true],
];
}

Expand All @@ -2356,6 +2375,7 @@ public static function methodSafeProvider()
['OPTIONS', true],
['TRACE', true],
['CONNECT', false],
['QUERY', true],
];
}

Expand All @@ -2382,6 +2402,7 @@ public static function methodCacheableProvider()
['OPTIONS', false],
['TRACE', false],
['CONNECT', false],
['QUERY', true],
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpKernel/HttpCache/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ public function getPath(string $key): string
*/
protected function generateCacheKey(Request $request): string
{
return 'md'.hash('sha256', $request->getUri());
return 'md'.hash('sha256', $request->isMethod('QUERY') ? $request->getContent() : $request->getUri());
}

/**
Expand Down
20 changes: 20 additions & 0 deletions src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2070,6 +2070,26 @@ public function testTraceLevelShort()
$this->assertTrue($this->response->headers->has('X-Symfony-Cache'));
$this->assertEquals('miss', $this->response->headers->get('X-Symfony-Cache'));
}

public function testQueryRequestAreCacheable()
{
$this->setNextResponse(200, ['Cache-Control' => 'public, s-maxage=3600']);
$this->request('QUERY', content: '{"foo": "bar"}');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using named parameters in tests is a no-go. Tests should use the API covered by our BC promise, as that helps detecting unintentional BC breaks in some cases (if we have to make unexpected changes in tests so that they work). And parameter names are not covered by our BC promise in Symfony (except for attribute constructors)

$this->assertHttpKernelIsCalled();
$this->assertEquals(200, $this->response->getStatusCode());
$this->assertTraceContains('miss');
$this->assertTraceContains('store');

$this->request('QUERY', content: '{"foo": "bar"}');
$this->assertTraceContains('fresh');
$this->assertHttpKernelIsNotCalled();
$this->assertEquals(200, $this->response->getStatusCode());

$this->request('QUERY', content: '{"foo": "baz"}');
$this->assertHttpKernelIsCalled();
$this->assertEquals(200, $this->response->getStatusCode());
$this->assertTraceContains('miss');
}
}

class TestKernel implements HttpKernelInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function assertExceptionsAreNotCaught()
$this->assertFalse($this->kernel->isCatchingExceptions());
}

public function request($method, $uri = '/', $server = [], $cookies = [], $esi = false, $headers = [])
public function request($method, $uri = '/', $server = [], $cookies = [], $esi = false, $headers = [], $content = null)
{
if (null === $this->kernel) {
throw new \LogicException('You must call setNextResponse() before calling request().');
Expand All @@ -129,7 +129,7 @@ public function request($method, $uri = '/', $server = [], $cookies = [], $esi =

$this->esi = $esi ? new Esi() : null;
$this->cache = new HttpCache($this->kernel, $this->store, $this->esi, $this->cacheConfig);
$this->request = Request::create($uri, $method, [], $cookies, [], $server);
$this->request = Request::create($uri, $method, [], $cookies, [], $server, $content);
$this->request->headers->add($headers);

$this->response = $this->cache->handle($this->request, HttpKernelInterface::MAIN_REQUEST, $this->catch);
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy