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;
|
|
|
|
|
|
|
|
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-07-17 12:57:30 +03:00
|
|
|
$parsedBody = $this->parseRawRequest('application/graphql', $query);
|
2017-07-16 14:52:38 +03:00
|
|
|
|
2017-07-17 12:57:30 +03:00
|
|
|
$this->assertValidOperationParams($parsedBody, $query);
|
|
|
|
$this->assertFalse($parsedBody->isReadOnly());
|
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-07-17 12:57:30 +03:00
|
|
|
$parsedBody = $this->parseFormUrlencodedRequest($post);
|
|
|
|
$this->assertValidOperationParams($parsedBody, $query, null, $variables, $operation);
|
|
|
|
$this->assertFalse($parsedBody->isReadOnly());
|
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-07-17 12:57:30 +03:00
|
|
|
$parsedBody = $this->parseGetRequest($get);
|
|
|
|
$this->assertValidOperationParams($parsedBody, $query, null, $variables, $operation);
|
|
|
|
$this->assertTrue($parsedBody->isReadonly());
|
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-07-17 12:57:30 +03:00
|
|
|
$parsedBody = $this->parseRawRequest('application/json', json_encode($body));
|
|
|
|
$this->assertValidOperationParams($parsedBody, $query, null, $variables, $operation);
|
|
|
|
$this->assertFalse($parsedBody->isReadOnly());
|
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
|
|
|
|
];
|
|
|
|
$parsedBody = $this->parseRawRequest('application/json', json_encode($body));
|
|
|
|
$this->assertValidOperationParams($parsedBody, $query, null, $variables, $operation);
|
|
|
|
$this->assertFalse($parsedBody->isReadOnly());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIgnoresInvalidVariablesJson()
|
|
|
|
{
|
|
|
|
$query = '{my query}';
|
|
|
|
$variables = '"some invalid json';
|
|
|
|
$operation = 'op';
|
|
|
|
|
|
|
|
$body = [
|
|
|
|
'query' => $query,
|
|
|
|
'variables' => $variables,
|
|
|
|
'operation' => $operation
|
|
|
|
];
|
|
|
|
$parsedBody = $this->parseRawRequest('application/json', json_encode($body));
|
|
|
|
$this->assertValidOperationParams($parsedBody, $query, null, $variables, $operation);
|
|
|
|
$this->assertFalse($parsedBody->isReadOnly());
|
|
|
|
}
|
|
|
|
|
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-07-17 12:57:30 +03:00
|
|
|
$parsedBody = $this->parseRawRequest('application/json', json_encode($body));
|
|
|
|
$this->assertInternalType('array', $parsedBody);
|
|
|
|
$this->assertCount(2, $parsedBody);
|
|
|
|
|
|
|
|
$this->assertValidOperationParams($parsedBody[0], $body[0]['query'], null, $body[0]['variables'], $body[0]['operation']);
|
|
|
|
$this->assertValidOperationParams($parsedBody[1], null, $body[1]['queryId'], $body[1]['variables'], $body[1]['operation']);
|
2017-07-16 14:52:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testFailsParsingInvalidJsonRequest()
|
|
|
|
{
|
|
|
|
$body = 'not really{} a json';
|
|
|
|
|
2017-07-19 15:30:39 +03:00
|
|
|
$this->setExpectedException(Error::class, 'Could not parse JSON: Syntax error');
|
2017-07-16 14:52:38 +03:00
|
|
|
$this->parseRawRequest('application/json', $body);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFailsParsingNonArrayOrObjectJsonRequest()
|
|
|
|
{
|
|
|
|
$body = '"str"';
|
|
|
|
|
2017-07-19 15:30:39 +03:00
|
|
|
$this->setExpectedException(Error::class, 'GraphQL Server expects JSON object or array, but got "str"');
|
2017-07-16 14:52:38 +03:00
|
|
|
$this->parseRawRequest('application/json', $body);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFailsParsingInvalidContentType()
|
|
|
|
{
|
2017-07-19 15:30:39 +03:00
|
|
|
$this->setExpectedException(Error::class, 'Unexpected content type: "not-supported-content-type"');
|
2017-07-16 14:52:38 +03:00
|
|
|
$this->parseRawRequest('not-supported-content-type', 'test');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFailsWithMissingContentType()
|
|
|
|
{
|
2017-07-19 15:30:39 +03:00
|
|
|
$this->setExpectedException(Error::class, 'Missing "Content-Type" header');
|
2017-07-16 14:52:38 +03:00
|
|
|
$this->parseRawRequest(null, 'test');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFailsOnMethodsOtherThanPostOrGet()
|
|
|
|
{
|
2017-07-19 15:30:39 +03:00
|
|
|
$this->setExpectedException(Error::class, 'HTTP Method "PUT" is not supported');
|
2017-07-16 14:52:38 +03:00
|
|
|
$this->parseRawRequest(null, 'test', "PUT");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $postValue
|
|
|
|
* @return OperationParams|OperationParams[]
|
|
|
|
*/
|
|
|
|
private function parseFormUrlencodedRequest($postValue)
|
|
|
|
{
|
|
|
|
$_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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $getValue
|
2017-07-17 12:57:30 +03:00
|
|
|
* @return OperationParams
|
2017-07-16 14:52:38 +03:00
|
|
|
*/
|
|
|
|
private function parseGetRequest($getValue)
|
|
|
|
{
|
|
|
|
$_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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param OperationParams $params
|
|
|
|
* @param string $query
|
|
|
|
* @param string $queryId
|
|
|
|
* @param array $variables
|
|
|
|
* @param string $operation
|
|
|
|
*/
|
|
|
|
private function assertValidOperationParams($params, $query, $queryId = null, $variables = null, $operation = null)
|
|
|
|
{
|
|
|
|
$this->assertInstanceOf(OperationParams::class, $params);
|
|
|
|
|
|
|
|
$this->assertSame($query, $params->query);
|
|
|
|
$this->assertSame($queryId, $params->queryId);
|
|
|
|
$this->assertSame($variables, $params->variables);
|
|
|
|
$this->assertSame($operation, $params->operation);
|
|
|
|
}
|
|
|
|
}
|