Skip to content
Merged
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
24 changes: 20 additions & 4 deletions src/Server/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -525,11 +525,11 @@ public function parsePsrRequest(RequestInterface $request)

$this->assertJsonObjectOrArray($bodyParams);
} else {
parse_str((string) $request->getBody(), $bodyParams);

if (! is_array($bodyParams)) {
throw new RequestError('Unexpected content type: ' . Utils::printSafeJson($contentType[0]));
if ($request instanceof ServerRequestInterface) {
$bodyParams = $request->getParsedBody();
}

$bodyParams ??= $this->decodeContent((string) $request->getBody(), $contentType[0]);
}
}

Expand Down Expand Up @@ -558,6 +558,22 @@ protected function decodeJson(string $rawBody)
return $bodyParams;
}

/**
* @return array<string, mixed>
*
* @throws RequestError
*/
protected function decodeContent(string $rawBody, string $contentType): array
{
parse_str($rawBody, $bodyParams);

if (! is_array($bodyParams)) {
throw new RequestError('Unexpected content type: ' . Utils::printSafeJson($contentType));
}

return $bodyParams;
}

/**
* @param mixed $bodyParams
*
Expand Down
21 changes: 21 additions & 0 deletions tests/Server/RequestParsingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use GraphQL\Server\RequestError;
use InvalidArgumentException;
use Nyholm\Psr7\Request;
use Nyholm\Psr7\ServerRequest;
use Nyholm\Psr7\Stream;
use Nyholm\Psr7\Uri;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -110,6 +111,8 @@ public function testParsesUrlencodedRequest(): void
$parsed = [
'raw' => $this->parseRawFormUrlencodedRequest($post),
'psr' => $this->parsePsrFormUrlEncodedRequest($post),
'serverRequest' => $this->parsePsrFormUrlEncodedServerRequest($post, false),
'parsedServerRequest' => $this->parsePsrFormUrlEncodedServerRequest($post, true),
];

foreach ($parsed as $method => $parsedBody) {
Expand Down Expand Up @@ -155,6 +158,24 @@ private function parsePsrFormUrlEncodedRequest($postValue)
);
}

private function parsePsrFormUrlEncodedServerRequest($postValue, bool $parsed)
{
$helper = new Helper();

$request = new ServerRequest(
'POST',
'',
['Content-Type' => 'application/x-www-form-urlencoded'],
$parsed ? null : http_build_query($postValue),
);

if ($parsed) {
$request = $request->withParsedBody($postValue);
}

return $helper->parsePsrRequest($request);
}

public function testParsesGetRequest(): void
{
$query = '{my query}';
Expand Down