graphql-php/tests/Server/RequestParsingTest.php

503 lines
17 KiB
PHP
Raw Normal View History

<?php
2018-09-01 23:00:00 +03:00
declare(strict_types=1);
namespace GraphQL\Tests\Server;
use GraphQL\Error\InvariantViolation;
use GraphQL\Server\Helper;
use GraphQL\Server\OperationParams;
use GraphQL\Server\RequestError;
use GraphQL\Tests\Server\Psr7\PsrRequestStub;
use GraphQL\Tests\Server\Psr7\PsrStreamStub;
2018-07-29 18:43:10 +03:00
use PHPUnit\Framework\TestCase;
2018-09-01 23:00:00 +03:00
use function json_decode;
use function json_encode;
2018-07-29 18:43:10 +03:00
class RequestParsingTest extends TestCase
{
public function testParsesGraphqlRequest() : void
{
2018-09-01 23:00:00 +03:00
$query = '{my query}';
$parsed = [
'raw' => $this->parseRawRequest('application/graphql', $query),
2018-09-01 23:00:00 +03:00
'psr' => $this->parsePsrRequest('application/graphql', $query),
];
foreach ($parsed as $source => $parsedBody) {
self::assertValidOperationParams($parsedBody, $query, null, null, null, null, $source);
2018-09-19 18:12:09 +03:00
self::assertFalse($parsedBody->isReadOnly(), $source);
}
}
2018-09-01 23:00:00 +03:00
/**
* @param string $contentType
* @param string $content
*
* @return OperationParams|OperationParams[]
*/
private function parseRawRequest($contentType, $content, string $method = 'POST')
{
$_SERVER['CONTENT_TYPE'] = $contentType;
$_SERVER['REQUEST_METHOD'] = $method;
$helper = new Helper();
2018-09-26 12:07:23 +03:00
return $helper->parseHttpRequest(static function () use ($content) {
2018-09-01 23:00:00 +03:00
return $content;
});
}
/**
* @param string $contentType
* @param string $content
*
* @return OperationParams|OperationParams[]
*/
private function parsePsrRequest($contentType, $content, string $method = 'POST')
{
$psrRequestBody = new PsrStreamStub();
$psrRequestBody->content = $content;
$psrRequest = new PsrRequestStub();
$psrRequest->headers['content-type'] = [$contentType];
$psrRequest->method = $method;
$psrRequest->body = $psrRequestBody;
if ($contentType === 'application/json') {
$parsedBody = json_decode($content, true);
$parsedBody = $parsedBody === false ? null : $parsedBody;
} else {
$parsedBody = null;
}
$psrRequest->parsedBody = $parsedBody;
$helper = new Helper();
return $helper->parsePsrRequest($psrRequest);
}
/**
* @param OperationParams $params
* @param string $query
* @param string $queryId
* @param mixed|null $variables
* @param string $operation
*/
2018-09-19 18:12:09 +03:00
private static function assertValidOperationParams(
2018-09-01 23:00:00 +03:00
$params,
$query,
$queryId = null,
$variables = null,
$operation = null,
$extensions = null,
2018-09-01 23:00:00 +03:00
$message = ''
) {
2018-09-19 18:12:09 +03:00
self::assertInstanceOf(OperationParams::class, $params, $message);
2018-09-01 23:00:00 +03:00
2018-09-19 18:12:09 +03:00
self::assertSame($query, $params->query, $message);
self::assertSame($queryId, $params->queryId, $message);
self::assertSame($variables, $params->variables, $message);
self::assertSame($operation, $params->operation, $message);
self::assertSame($extensions, $params->extensions, $message);
2018-09-01 23:00:00 +03:00
}
public function testParsesUrlencodedRequest() : void
{
2018-09-01 23:00:00 +03:00
$query = '{my query}';
$variables = ['test' => 1, 'test2' => 2];
$operation = 'op';
2018-09-01 23:00:00 +03:00
$post = [
'query' => $query,
'variables' => $variables,
'operationName' => $operation,
];
$parsed = [
'raw' => $this->parseRawFormUrlencodedRequest($post),
2018-09-01 23:00:00 +03:00
'psr' => $this->parsePsrFormUrlEncodedRequest($post),
];
foreach ($parsed as $method => $parsedBody) {
self::assertValidOperationParams($parsedBody, $query, null, $variables, $operation, null, $method);
2018-09-19 18:12:09 +03:00
self::assertFalse($parsedBody->isReadOnly(), $method);
}
}
2018-09-01 23:00:00 +03:00
/**
* @param mixed[] $postValue
2018-09-26 12:07:23 +03:00
*
2018-09-01 23:00:00 +03:00
* @return OperationParams|OperationParams[]
*/
private function parseRawFormUrlencodedRequest($postValue)
{
$_SERVER['CONTENT_TYPE'] = 'application/x-www-form-urlencoded';
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST = $postValue;
$helper = new Helper();
2018-09-26 12:07:23 +03:00
return $helper->parseHttpRequest(static function () {
2018-09-01 23:00:00 +03:00
throw new InvariantViolation("Shouldn't read from php://input for urlencoded request");
});
}
/**
* @param mixed[] $postValue
2018-09-26 12:07:23 +03:00
*
2018-09-01 23:00:00 +03:00
* @return OperationParams[]|OperationParams
*/
private function parsePsrFormUrlEncodedRequest($postValue)
{
$psrRequest = new PsrRequestStub();
$psrRequest->headers['content-type'] = ['application/x-www-form-urlencoded'];
$psrRequest->method = 'POST';
$psrRequest->parsedBody = $postValue;
$helper = new Helper();
return $helper->parsePsrRequest($psrRequest);
}
public function testParsesGetRequest() : void
{
2018-09-01 23:00:00 +03:00
$query = '{my query}';
$variables = ['test' => 1, 'test2' => 2];
$operation = 'op';
2018-09-01 23:00:00 +03:00
$get = [
'query' => $query,
'variables' => $variables,
'operationName' => $operation,
];
$parsed = [
'raw' => $this->parseRawGetRequest($get),
2018-09-01 23:00:00 +03:00
'psr' => $this->parsePsrGetRequest($get),
];
foreach ($parsed as $method => $parsedBody) {
self::assertValidOperationParams($parsedBody, $query, null, $variables, $operation, null, $method);
2018-09-19 18:12:09 +03:00
self::assertTrue($parsedBody->isReadonly(), $method);
}
}
2018-09-01 23:00:00 +03:00
/**
* @param mixed[] $getValue
2018-09-26 12:07:23 +03:00
*
2018-09-01 23:00:00 +03:00
* @return OperationParams
*/
private function parseRawGetRequest($getValue)
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$_GET = $getValue;
$helper = new Helper();
2018-09-26 12:07:23 +03:00
return $helper->parseHttpRequest(static function () {
2018-09-01 23:00:00 +03:00
throw new InvariantViolation("Shouldn't read from php://input for urlencoded request");
});
}
/**
* @param mixed[] $getValue
2018-09-26 12:07:23 +03:00
*
2018-09-01 23:00:00 +03:00
* @return OperationParams[]|OperationParams
*/
private function parsePsrGetRequest($getValue)
{
$psrRequest = new PsrRequestStub();
$psrRequest->method = 'GET';
$psrRequest->queryParams = $getValue;
$helper = new Helper();
return $helper->parsePsrRequest($psrRequest);
}
public function testParsesMultipartFormdataRequest() : void
{
2018-09-01 23:00:00 +03:00
$query = '{my query}';
$variables = ['test' => 1, 'test2' => 2];
$operation = 'op';
2018-09-01 23:00:00 +03:00
$post = [
'query' => $query,
'variables' => $variables,
'operationName' => $operation,
];
$parsed = [
'raw' => $this->parseRawMultipartFormDataRequest($post),
'psr' => $this->parsePsrMultipartFormDataRequest($post),
];
foreach ($parsed as $method => $parsedBody) {
self::assertValidOperationParams($parsedBody, $query, null, $variables, $operation, null, $method);
2018-09-19 18:12:09 +03:00
self::assertFalse($parsedBody->isReadOnly(), $method);
}
}
2018-09-01 23:00:00 +03:00
/**
* @param mixed[] $postValue
2018-09-26 12:07:23 +03:00
*
2018-09-01 23:00:00 +03:00
* @return OperationParams|OperationParams[]
*/
private function parseRawMultipartFormDataRequest($postValue)
{
$_SERVER['CONTENT_TYPE'] = 'multipart/form-data; boundary=----FormBoundary';
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST = $postValue;
$helper = new Helper();
2018-09-26 12:07:23 +03:00
return $helper->parseHttpRequest(static function () {
2018-09-01 23:00:00 +03:00
throw new InvariantViolation("Shouldn't read from php://input for multipart/form-data request");
});
}
/**
* @param mixed[] $postValue
2018-09-26 12:07:23 +03:00
*
2018-09-01 23:00:00 +03:00
* @return OperationParams|OperationParams[]
*/
private function parsePsrMultipartFormDataRequest($postValue)
{
$psrRequest = new PsrRequestStub();
$psrRequest->headers['content-type'] = ['multipart/form-data; boundary=----FormBoundary'];
$psrRequest->method = 'POST';
$psrRequest->parsedBody = $postValue;
$helper = new Helper();
return $helper->parsePsrRequest($psrRequest);
}
public function testParsesJSONRequest() : void
{
2018-09-01 23:00:00 +03:00
$query = '{my query}';
$variables = ['test' => 1, 'test2' => 2];
$operation = 'op';
2018-09-01 23:00:00 +03:00
$body = [
'query' => $query,
'variables' => $variables,
'operationName' => $operation,
];
$parsed = [
'raw' => $this->parseRawRequest('application/json', json_encode($body)),
2018-09-01 23:00:00 +03:00
'psr' => $this->parsePsrRequest('application/json', json_encode($body)),
];
foreach ($parsed as $method => $parsedBody) {
self::assertValidOperationParams($parsedBody, $query, null, $variables, $operation, null, $method);
2018-09-19 18:12:09 +03:00
self::assertFalse($parsedBody->isReadOnly(), $method);
}
}
public function testParsesVariablesAsJSON() : void
{
2018-09-01 23:00:00 +03:00
$query = '{my query}';
$variables = ['test' => 1, 'test2' => 2];
$operation = 'op';
2018-09-01 23:00:00 +03:00
$body = [
'query' => $query,
'variables' => json_encode($variables),
'operationName' => $operation,
];
$parsed = [
'raw' => $this->parseRawRequest('application/json', json_encode($body)),
2018-09-01 23:00:00 +03:00
'psr' => $this->parsePsrRequest('application/json', json_encode($body)),
];
foreach ($parsed as $method => $parsedBody) {
self::assertValidOperationParams($parsedBody, $query, null, $variables, $operation, null, $method);
2018-09-19 18:12:09 +03:00
self::assertFalse($parsedBody->isReadOnly(), $method);
}
}
public function testIgnoresInvalidVariablesJson() : void
{
2018-09-01 23:00:00 +03:00
$query = '{my query}';
$variables = '"some invalid json';
$operation = 'op';
2018-09-01 23:00:00 +03:00
$body = [
'query' => $query,
'variables' => $variables,
'operationName' => $operation,
];
$parsed = [
'raw' => $this->parseRawRequest('application/json', json_encode($body)),
'psr' => $this->parsePsrRequest('application/json', json_encode($body)),
];
foreach ($parsed as $method => $parsedBody) {
self::assertValidOperationParams($parsedBody, $query, null, $variables, $operation, null, $method);
2018-09-19 18:12:09 +03:00
self::assertFalse($parsedBody->isReadOnly(), $method);
}
}
public function testParsesApolloPersistedQueryJSONRequest() : void
{
2018-12-02 03:43:51 +03:00
$queryId = 'my-query-id';
$extensions = ['persistedQuery' => ['sha256Hash' => $queryId]];
$variables = ['test' => 1, 'test2' => 2];
$operation = 'op';
$body = [
'extensions' => $extensions,
'variables' => $variables,
'operationName' => $operation,
];
$parsed = [
'raw' => $this->parseRawRequest('application/json', json_encode($body)),
'psr' => $this->parsePsrRequest('application/json', json_encode($body)),
];
foreach ($parsed as $method => $parsedBody) {
self::assertValidOperationParams($parsedBody, null, $queryId, $variables, $operation, $extensions, $method);
self::assertFalse($parsedBody->isReadOnly(), $method);
}
}
public function testParsesBatchJSONRequest() : void
{
2018-09-01 23:00:00 +03:00
$body = [
[
2018-09-01 23:00:00 +03:00
'query' => '{my query}',
'variables' => ['test' => 1, 'test2' => 2],
'operationName' => 'op',
],
[
2018-09-01 23:00:00 +03:00
'queryId' => 'my-query-id',
'variables' => ['test' => 1, 'test2' => 2],
'operationName' => 'op2',
],
];
$parsed = [
'raw' => $this->parseRawRequest('application/json', json_encode($body)),
2018-09-01 23:00:00 +03:00
'psr' => $this->parsePsrRequest('application/json', json_encode($body)),
];
foreach ($parsed as $method => $parsedBody) {
2018-09-19 18:12:09 +03:00
self::assertInternalType('array', $parsedBody, $method);
self::assertCount(2, $parsedBody, $method);
self::assertValidOperationParams(
2018-09-01 23:00:00 +03:00
$parsedBody[0],
$body[0]['query'],
null,
$body[0]['variables'],
$body[0]['operationName'],
null,
2018-09-01 23:00:00 +03:00
$method
);
2018-09-19 18:12:09 +03:00
self::assertValidOperationParams(
2018-09-01 23:00:00 +03:00
$parsedBody[1],
null,
$body[1]['queryId'],
$body[1]['variables'],
$body[1]['operationName'],
null,
2018-09-01 23:00:00 +03:00
$method
);
}
}
public function testFailsParsingInvalidRawJsonRequestRaw() : void
{
$body = 'not really{} a json';
2018-07-29 18:43:10 +03:00
$this->expectException(RequestError::class);
$this->expectExceptionMessage('Could not parse JSON: Syntax error');
2018-09-01 23:00:00 +03:00
$this->parseRawRequest('application/json', $body);
}
public function testFailsParsingInvalidRawJsonRequestPsr() : void
{
$body = 'not really{} a json';
2018-07-29 18:43:10 +03:00
$this->expectException(InvariantViolation::class);
$this->expectExceptionMessage('PSR-7 request is expected to provide parsed body for "application/json" requests but got null');
2018-09-01 23:00:00 +03:00
$this->parsePsrRequest('application/json', $body);
}
public function testFailsParsingNonPreParsedPsrRequest() : void
{
try {
$this->parsePsrRequest('application/json', json_encode(null));
self::fail('Expected exception not thrown');
} catch (InvariantViolation $e) {
// Expecting parsing exception to be thrown somewhere else:
2018-09-19 18:12:09 +03:00
self::assertEquals(
'PSR-7 request is expected to provide parsed body for "application/json" requests but got null',
$e->getMessage()
);
}
}
/**
* There is no equivalent for psr request, because it should throw
*/
public function testFailsParsingNonArrayOrObjectJsonRequestRaw() : void
{
$body = '"str"';
2018-07-29 18:43:10 +03:00
$this->expectException(RequestError::class);
$this->expectExceptionMessage('GraphQL Server expects JSON object or array, but got "str"');
2018-09-01 23:00:00 +03:00
$this->parseRawRequest('application/json', $body);
}
public function testFailsParsingNonArrayOrObjectJsonRequestPsr() : void
{
$body = '"str"';
2018-07-29 18:43:10 +03:00
$this->expectException(RequestError::class);
$this->expectExceptionMessage('GraphQL Server expects JSON object or array, but got "str"');
2018-09-01 23:00:00 +03:00
$this->parsePsrRequest('application/json', $body);
}
public function testFailsParsingInvalidContentTypeRaw() : void
{
$contentType = 'not-supported-content-type';
2018-09-01 23:00:00 +03:00
$body = 'test';
2018-07-29 18:43:10 +03:00
$this->expectException(RequestError::class);
$this->expectExceptionMessage('Unexpected content type: "not-supported-content-type"');
$this->parseRawRequest($contentType, $body);
}
public function testFailsParsingInvalidContentTypePsr() : void
{
$contentType = 'not-supported-content-type';
2018-09-01 23:00:00 +03:00
$body = 'test';
2018-07-29 18:43:10 +03:00
$this->expectException(RequestError::class);
$this->expectExceptionMessage('Unexpected content type: "not-supported-content-type"');
2018-09-01 23:00:00 +03:00
$this->parseRawRequest($contentType, $body);
}
public function testFailsWithMissingContentTypeRaw() : void
{
2018-07-29 18:43:10 +03:00
$this->expectException(RequestError::class);
$this->expectExceptionMessage('Missing "Content-Type" header');
2018-09-01 23:00:00 +03:00
$this->parseRawRequest(null, 'test');
}
public function testFailsWithMissingContentTypePsr() : void
{
2018-07-29 18:43:10 +03:00
$this->expectException(RequestError::class);
$this->expectExceptionMessage('Missing "Content-Type" header');
2018-09-01 23:00:00 +03:00
$this->parsePsrRequest(null, 'test');
}
public function testFailsOnMethodsOtherThanPostOrGetRaw() : void
{
2018-07-29 18:43:10 +03:00
$this->expectException(RequestError::class);
$this->expectExceptionMessage('HTTP Method "PUT" is not supported');
2018-09-01 23:00:00 +03:00
$this->parseRawRequest('application/json', json_encode([]), 'PUT');
2018-01-13 13:20:00 +03:00
}
public function testFailsOnMethodsOtherThanPostOrGetPsr() : void
{
2018-07-29 18:43:10 +03:00
$this->expectException(RequestError::class);
$this->expectExceptionMessage('HTTP Method "PUT" is not supported');
2018-09-01 23:00:00 +03:00
$this->parsePsrRequest('application/json', json_encode([]), 'PUT');
}
}