2017-07-16 14:52:38 +03:00
|
|
|
<?php
|
|
|
|
namespace GraphQL\Tests\Server;
|
|
|
|
|
2017-07-19 15:30:39 +03:00
|
|
|
use GraphQL\Error\Error;
|
2017-07-17 12:57:30 +03:00
|
|
|
use GraphQL\Error\InvariantViolation;
|
2017-07-16 14:52:38 +03:00
|
|
|
use GraphQL\Server\Helper;
|
|
|
|
use GraphQL\Server\OperationParams;
|
2017-08-07 22:00:17 +03:00
|
|
|
use GraphQL\Server\RequestError;
|
2017-08-15 16:45:23 +03:00
|
|
|
use GraphQL\Tests\Server\Psr7\PsrRequestStub;
|
|
|
|
use GraphQL\Tests\Server\Psr7\PsrStreamStub;
|
2017-07-16 14:52:38 +03:00
|
|
|
|
|
|
|
class RequestParsingTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
2017-07-17 12:57:30 +03:00
|
|
|
public function testParsesGraphqlRequest()
|
2017-07-16 14:52:38 +03:00
|
|
|
{
|
|
|
|
$query = '{my query}';
|
2017-08-15 16:45:23 +03:00
|
|
|
$parsed = [
|
|
|
|
'raw' => $this->parseRawRequest('application/graphql', $query),
|
|
|
|
'psr' => $this->parsePsrRequest('application/graphql', $query)
|
|
|
|
];
|
2017-07-16 14:52:38 +03:00
|
|
|
|
2017-08-15 16:45:23 +03:00
|
|
|
foreach ($parsed as $source => $parsedBody) {
|
|
|
|
$this->assertValidOperationParams($parsedBody, $query, null, null, null, $source);
|
|
|
|
$this->assertFalse($parsedBody->isReadOnly(), $source);
|
|
|
|
}
|
2017-07-16 14:52:38 +03:00
|
|
|
}
|
|
|
|
|
2017-07-17 12:57:30 +03:00
|
|
|
public function testParsesUrlencodedRequest()
|
2017-07-16 14:52:38 +03:00
|
|
|
{
|
|
|
|
$query = '{my query}';
|
|
|
|
$variables = ['test' => 1, 'test2' => 2];
|
|
|
|
$operation = 'op';
|
|
|
|
|
|
|
|
$post = [
|
|
|
|
'query' => $query,
|
|
|
|
'variables' => $variables,
|
|
|
|
'operation' => $operation
|
|
|
|
];
|
2017-08-15 16:45:23 +03:00
|
|
|
$parsed = [
|
|
|
|
'raw' => $this->parseRawFormUrlencodedRequest($post),
|
|
|
|
'psr' => $this->parsePsrFormUrlEncodedRequest($post)
|
|
|
|
];
|
2017-07-16 14:52:38 +03:00
|
|
|
|
2017-08-15 16:45:23 +03:00
|
|
|
foreach ($parsed as $method => $parsedBody) {
|
|
|
|
$this->assertValidOperationParams($parsedBody, $query, null, $variables, $operation, $method);
|
|
|
|
$this->assertFalse($parsedBody->isReadOnly(), $method);
|
|
|
|
}
|
2017-07-16 14:52:38 +03:00
|
|
|
}
|
|
|
|
|
2017-07-17 12:57:30 +03:00
|
|
|
public function testParsesGetRequest()
|
2017-07-16 14:52:38 +03:00
|
|
|
{
|
|
|
|
$query = '{my query}';
|
|
|
|
$variables = ['test' => 1, 'test2' => 2];
|
|
|
|
$operation = 'op';
|
|
|
|
|
|
|
|
$get = [
|
|
|
|
'query' => $query,
|
|
|
|
'variables' => $variables,
|
|
|
|
'operation' => $operation
|
|
|
|
];
|
2017-08-15 16:45:23 +03:00
|
|
|
$parsed = [
|
|
|
|
'raw' => $this->parseRawGetRequest($get),
|
|
|
|
'psr' => $this->parsePsrGetRequest($get)
|
|
|
|
];
|
2017-07-16 14:52:38 +03:00
|
|
|
|
2017-08-15 16:45:23 +03:00
|
|
|
foreach ($parsed as $method => $parsedBody) {
|
|
|
|
$this->assertValidOperationParams($parsedBody, $query, null, $variables, $operation, $method);
|
|
|
|
$this->assertTrue($parsedBody->isReadonly(), $method);
|
|
|
|
}
|
2017-07-16 14:52:38 +03:00
|
|
|
}
|
|
|
|
|
2017-07-17 12:57:30 +03:00
|
|
|
public function testParsesJSONRequest()
|
2017-07-16 14:52:38 +03:00
|
|
|
{
|
|
|
|
$query = '{my query}';
|
|
|
|
$variables = ['test' => 1, 'test2' => 2];
|
|
|
|
$operation = 'op';
|
|
|
|
|
|
|
|
$body = [
|
|
|
|
'query' => $query,
|
|
|
|
'variables' => $variables,
|
|
|
|
'operation' => $operation
|
|
|
|
];
|
2017-08-15 16:45:23 +03:00
|
|
|
$parsed = [
|
|
|
|
'raw' => $this->parseRawRequest('application/json', json_encode($body)),
|
|
|
|
'psr' => $this->parsePsrRequest('application/json', json_encode($body))
|
|
|
|
];
|
|
|
|
foreach ($parsed as $method => $parsedBody) {
|
|
|
|
$this->assertValidOperationParams($parsedBody, $query, null, $variables, $operation, $method);
|
|
|
|
$this->assertFalse($parsedBody->isReadOnly(), $method);
|
|
|
|
}
|
2017-07-16 14:52:38 +03:00
|
|
|
}
|
|
|
|
|
2017-07-19 15:30:39 +03:00
|
|
|
public function testParsesVariablesAsJSON()
|
|
|
|
{
|
|
|
|
$query = '{my query}';
|
|
|
|
$variables = ['test' => 1, 'test2' => 2];
|
|
|
|
$operation = 'op';
|
|
|
|
|
|
|
|
$body = [
|
|
|
|
'query' => $query,
|
|
|
|
'variables' => json_encode($variables),
|
|
|
|
'operation' => $operation
|
|
|
|
];
|
2017-08-15 16:45:23 +03:00
|
|
|
$parsed = [
|
|
|
|
'raw' => $this->parseRawRequest('application/json', json_encode($body)),
|
|
|
|
'psr' => $this->parsePsrRequest('application/json', json_encode($body))
|
|
|
|
];
|
|
|
|
foreach ($parsed as $method => $parsedBody) {
|
|
|
|
$this->assertValidOperationParams($parsedBody, $query, null, $variables, $operation, $method);
|
|
|
|
$this->assertFalse($parsedBody->isReadOnly(), $method);
|
|
|
|
}
|
2017-07-19 15:30:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testIgnoresInvalidVariablesJson()
|
|
|
|
{
|
|
|
|
$query = '{my query}';
|
|
|
|
$variables = '"some invalid json';
|
|
|
|
$operation = 'op';
|
|
|
|
|
|
|
|
$body = [
|
|
|
|
'query' => $query,
|
|
|
|
'variables' => $variables,
|
|
|
|
'operation' => $operation
|
|
|
|
];
|
2017-08-15 16:45:23 +03:00
|
|
|
$parsed = [
|
|
|
|
'raw' => $this->parseRawRequest('application/json', json_encode($body)),
|
|
|
|
'psr' => $this->parsePsrRequest('application/json', json_encode($body)),
|
|
|
|
];
|
|
|
|
foreach ($parsed as $method => $parsedBody) {
|
|
|
|
$this->assertValidOperationParams($parsedBody, $query, null, $variables, $operation, $method);
|
|
|
|
$this->assertFalse($parsedBody->isReadOnly(), $method);
|
|
|
|
}
|
2017-07-19 15:30:39 +03:00
|
|
|
}
|
|
|
|
|
2017-07-16 14:52:38 +03:00
|
|
|
public function testParsesBatchJSONRequest()
|
|
|
|
{
|
|
|
|
$body = [
|
|
|
|
[
|
|
|
|
'query' => '{my query}',
|
|
|
|
'variables' => ['test' => 1, 'test2' => 2],
|
|
|
|
'operation' => 'op'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'queryId' => 'my-query-id',
|
|
|
|
'variables' => ['test' => 1, 'test2' => 2],
|
|
|
|
'operation' => 'op2'
|
|
|
|
],
|
|
|
|
];
|
2017-08-15 16:45:23 +03:00
|
|
|
$parsed = [
|
|
|
|
'raw' => $this->parseRawRequest('application/json', json_encode($body)),
|
|
|
|
'psr' => $this->parsePsrRequest('application/json', json_encode($body))
|
|
|
|
];
|
|
|
|
foreach ($parsed as $method => $parsedBody) {
|
|
|
|
$this->assertInternalType('array', $parsedBody, $method);
|
|
|
|
$this->assertCount(2, $parsedBody, $method);
|
|
|
|
$this->assertValidOperationParams($parsedBody[0], $body[0]['query'], null, $body[0]['variables'], $body[0]['operation'], $method);
|
|
|
|
$this->assertValidOperationParams($parsedBody[1], null, $body[1]['queryId'], $body[1]['variables'], $body[1]['operation'], $method);
|
|
|
|
}
|
2017-07-16 14:52:38 +03:00
|
|
|
}
|
|
|
|
|
2017-08-15 16:45:23 +03:00
|
|
|
public function testFailsParsingInvalidRawJsonRequest()
|
2017-07-16 14:52:38 +03:00
|
|
|
{
|
|
|
|
$body = 'not really{} a json';
|
|
|
|
|
2017-08-15 16:45:23 +03:00
|
|
|
try {
|
|
|
|
$this->parseRawRequest('application/json', $body);
|
|
|
|
$this->fail('Expected exception not thrown');
|
|
|
|
} catch (RequestError $e) {
|
|
|
|
$this->assertEquals('Could not parse JSON: Syntax error', $e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->parsePsrRequest('application/json', $body);
|
|
|
|
$this->fail('Expected exception not thrown');
|
|
|
|
} catch (InvariantViolation $e) {
|
|
|
|
// Expecting parsing exception to be thrown somewhere else:
|
|
|
|
$this->assertEquals(
|
2017-08-19 19:01:46 +03:00
|
|
|
'PSR-7 request is expected to provide parsed body for "application/json" requests but got null',
|
2017-08-15 16:45:23 +03:00
|
|
|
$e->getMessage()
|
|
|
|
);
|
|
|
|
}
|
2017-07-16 14:52:38 +03:00
|
|
|
}
|
|
|
|
|
2017-08-15 16:45:23 +03:00
|
|
|
// There is no equivalent for psr request, because it should throw
|
|
|
|
|
2017-07-16 14:52:38 +03:00
|
|
|
public function testFailsParsingNonArrayOrObjectJsonRequest()
|
|
|
|
{
|
|
|
|
$body = '"str"';
|
|
|
|
|
2017-08-15 16:45:23 +03:00
|
|
|
try {
|
|
|
|
$this->parseRawRequest('application/json', $body);
|
|
|
|
$this->fail('Expected exception not thrown');
|
|
|
|
} catch (RequestError $e) {
|
|
|
|
$this->assertEquals('GraphQL Server expects JSON object or array, but got "str"', $e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->parsePsrRequest('application/json', $body);
|
|
|
|
$this->fail('Expected exception not thrown');
|
|
|
|
} catch (RequestError $e) {
|
|
|
|
$this->assertEquals('GraphQL Server expects JSON object or array, but got "str"', $e->getMessage());
|
|
|
|
}
|
|
|
|
|
2017-07-16 14:52:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testFailsParsingInvalidContentType()
|
|
|
|
{
|
2017-08-15 16:45:23 +03:00
|
|
|
$contentType = 'not-supported-content-type';
|
|
|
|
$body = 'test';
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->parseRawRequest($contentType, $body);
|
|
|
|
$this->fail('Expected exception not thrown');
|
|
|
|
} catch (RequestError $e) {
|
|
|
|
$this->assertEquals('Unexpected content type: "not-supported-content-type"', $e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->parsePsrRequest($contentType, $body);
|
|
|
|
$this->fail('Expected exception not thrown');
|
|
|
|
} catch (RequestError $e) {
|
|
|
|
$this->assertEquals('Unexpected content type: "not-supported-content-type"', $e->getMessage());
|
|
|
|
}
|
2017-07-16 14:52:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testFailsWithMissingContentType()
|
|
|
|
{
|
2017-08-15 16:45:23 +03:00
|
|
|
try {
|
|
|
|
$this->parseRawRequest(null, 'test');
|
|
|
|
$this->fail('Expected exception not thrown');
|
|
|
|
} catch (RequestError $e) {
|
|
|
|
$this->assertEquals('Missing "Content-Type" header', $e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->parsePsrRequest(null, 'test');
|
|
|
|
$this->fail('Expected exception not thrown');
|
|
|
|
} catch (RequestError $e) {
|
|
|
|
$this->assertEquals('Missing "Content-Type" header', $e->getMessage());
|
|
|
|
}
|
2017-07-16 14:52:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testFailsOnMethodsOtherThanPostOrGet()
|
|
|
|
{
|
2017-08-15 16:45:23 +03:00
|
|
|
try {
|
|
|
|
$this->parseRawRequest('application/json', json_encode([]), "PUT");
|
|
|
|
$this->fail('Expected exception not thrown');
|
|
|
|
} catch (RequestError $e) {
|
|
|
|
$this->assertEquals('HTTP Method "PUT" is not supported', $e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->parsePsrRequest('application/json', json_encode([]), "PUT");
|
|
|
|
$this->fail('Expected exception not thrown');
|
|
|
|
} catch (RequestError $e) {
|
|
|
|
$this->assertEquals('HTTP Method "PUT" is not supported', $e->getMessage());
|
|
|
|
}
|
2017-07-16 14:52:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $contentType
|
|
|
|
* @param string $content
|
|
|
|
* @param $method
|
|
|
|
*
|
|
|
|
* @return OperationParams|OperationParams[]
|
|
|
|
*/
|
|
|
|
private function parseRawRequest($contentType, $content, $method = 'POST')
|
|
|
|
{
|
|
|
|
$_SERVER['CONTENT_TYPE'] = $contentType;
|
|
|
|
$_SERVER['REQUEST_METHOD'] = $method;
|
|
|
|
|
|
|
|
$helper = new Helper();
|
2017-07-17 12:57:30 +03:00
|
|
|
return $helper->parseHttpRequest(function() use ($content) {
|
2017-07-16 14:52:38 +03:00
|
|
|
return $content;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-15 16:45:23 +03:00
|
|
|
/**
|
|
|
|
* @param string $contentType
|
|
|
|
* @param string $content
|
|
|
|
* @param $method
|
|
|
|
*
|
|
|
|
* @return OperationParams|OperationParams[]
|
|
|
|
*/
|
|
|
|
private function parsePsrRequest($contentType, $content, $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);
|
|
|
|
}
|
|
|
|
|
2017-07-16 14:52:38 +03:00
|
|
|
/**
|
|
|
|
* @param array $postValue
|
|
|
|
* @return OperationParams|OperationParams[]
|
|
|
|
*/
|
2017-08-15 16:45:23 +03:00
|
|
|
private function parseRawFormUrlencodedRequest($postValue)
|
2017-07-16 14:52:38 +03:00
|
|
|
{
|
|
|
|
$_SERVER['CONTENT_TYPE'] = 'application/x-www-form-urlencoded';
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST';
|
|
|
|
$_POST = $postValue;
|
|
|
|
|
|
|
|
$helper = new Helper();
|
2017-07-17 12:57:30 +03:00
|
|
|
return $helper->parseHttpRequest(function() {
|
|
|
|
throw new InvariantViolation("Shouldn't read from php://input for urlencoded request");
|
|
|
|
});
|
2017-07-16 14:52:38 +03:00
|
|
|
}
|
|
|
|
|
2017-08-15 16:45:23 +03:00
|
|
|
/**
|
|
|
|
* @param $postValue
|
|
|
|
* @return array|Helper
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-07-16 14:52:38 +03:00
|
|
|
/**
|
|
|
|
* @param $getValue
|
2017-07-17 12:57:30 +03:00
|
|
|
* @return OperationParams
|
2017-07-16 14:52:38 +03:00
|
|
|
*/
|
2017-08-15 16:45:23 +03:00
|
|
|
private function parseRawGetRequest($getValue)
|
2017-07-16 14:52:38 +03:00
|
|
|
{
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
|
|
|
$_GET = $getValue;
|
|
|
|
|
|
|
|
$helper = new Helper();
|
2017-07-17 12:57:30 +03:00
|
|
|
return $helper->parseHttpRequest(function() {
|
|
|
|
throw new InvariantViolation("Shouldn't read from php://input for urlencoded request");
|
|
|
|
});
|
2017-07-16 14:52:38 +03:00
|
|
|
}
|
|
|
|
|
2017-08-15 16:45:23 +03:00
|
|
|
/**
|
|
|
|
* @param $getValue
|
|
|
|
* @return array|Helper
|
|
|
|
*/
|
|
|
|
private function parsePsrGetRequest($getValue)
|
|
|
|
{
|
|
|
|
$psrRequest = new PsrRequestStub();
|
|
|
|
$psrRequest->method = 'GET';
|
|
|
|
$psrRequest->queryParams = $getValue;
|
|
|
|
|
|
|
|
$helper = new Helper();
|
|
|
|
return $helper->parsePsrRequest($psrRequest);
|
|
|
|
}
|
|
|
|
|
2017-07-16 14:52:38 +03:00
|
|
|
/**
|
|
|
|
* @param OperationParams $params
|
|
|
|
* @param string $query
|
|
|
|
* @param string $queryId
|
|
|
|
* @param array $variables
|
|
|
|
* @param string $operation
|
|
|
|
*/
|
2017-08-15 16:45:23 +03:00
|
|
|
private function assertValidOperationParams($params, $query, $queryId = null, $variables = null, $operation = null, $message = '')
|
2017-07-16 14:52:38 +03:00
|
|
|
{
|
2017-08-15 16:45:23 +03:00
|
|
|
$this->assertInstanceOf(OperationParams::class, $params, $message);
|
2017-07-16 14:52:38 +03:00
|
|
|
|
2017-08-15 16:45:23 +03:00
|
|
|
$this->assertSame($query, $params->query, $message);
|
|
|
|
$this->assertSame($queryId, $params->queryId, $message);
|
|
|
|
$this->assertSame($variables, $params->variables, $message);
|
|
|
|
$this->assertSame($operation, $params->operation, $message);
|
2017-07-16 14:52:38 +03:00
|
|
|
}
|
|
|
|
}
|