2017-07-16 14:52:38 +03:00
|
|
|
<?php
|
2018-09-01 23:00:00 +03:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2017-07-16 14:52:38 +03:00
|
|
|
namespace GraphQL\Tests\Server;
|
|
|
|
|
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;
|
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;
|
2017-07-16 14:52:38 +03:00
|
|
|
|
2018-07-29 18:43:10 +03:00
|
|
|
class RequestParsingTest extends TestCase
|
2017-07-16 14:52:38 +03:00
|
|
|
{
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testParsesGraphqlRequest() : void
|
2017-07-16 14:52:38 +03:00
|
|
|
{
|
2018-09-01 23:00:00 +03:00
|
|
|
$query = '{my query}';
|
2017-08-15 16:45:23 +03:00
|
|
|
$parsed = [
|
|
|
|
'raw' => $this->parseRawRequest('application/graphql', $query),
|
2018-09-01 23:00:00 +03:00
|
|
|
'psr' => $this->parsePsrRequest('application/graphql', $query),
|
2017-08-15 16:45:23 +03:00
|
|
|
];
|
2017-07-16 14:52:38 +03:00
|
|
|
|
2017-08-15 16:45:23 +03:00
|
|
|
foreach ($parsed as $source => $parsedBody) {
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertValidOperationParams($parsedBody, $query, null, null, null, $source);
|
|
|
|
self::assertFalse($parsedBody->isReadOnly(), $source);
|
2017-08-15 16:45:23 +03:00
|
|
|
}
|
2017-07-16 14:52:38 +03:00
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
$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);
|
2018-09-01 23:00:00 +03:00
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testParsesUrlencodedRequest() : void
|
2017-07-16 14:52:38 +03:00
|
|
|
{
|
2018-09-01 23:00:00 +03:00
|
|
|
$query = '{my query}';
|
2017-07-16 14:52:38 +03:00
|
|
|
$variables = ['test' => 1, 'test2' => 2];
|
|
|
|
$operation = 'op';
|
|
|
|
|
2018-09-01 23:00:00 +03:00
|
|
|
$post = [
|
|
|
|
'query' => $query,
|
|
|
|
'variables' => $variables,
|
|
|
|
'operationName' => $operation,
|
2017-07-16 14:52:38 +03:00
|
|
|
];
|
2017-08-15 16:45:23 +03:00
|
|
|
$parsed = [
|
|
|
|
'raw' => $this->parseRawFormUrlencodedRequest($post),
|
2018-09-01 23:00:00 +03:00
|
|
|
'psr' => $this->parsePsrFormUrlEncodedRequest($post),
|
2017-08-15 16:45:23 +03:00
|
|
|
];
|
2017-07-16 14:52:38 +03:00
|
|
|
|
2017-08-15 16:45:23 +03:00
|
|
|
foreach ($parsed as $method => $parsedBody) {
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertValidOperationParams($parsedBody, $query, null, $variables, $operation, $method);
|
|
|
|
self::assertFalse($parsedBody->isReadOnly(), $method);
|
2017-08-15 16:45:23 +03:00
|
|
|
}
|
2017-07-16 14:52:38 +03:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testParsesGetRequest() : void
|
2017-07-16 14:52:38 +03:00
|
|
|
{
|
2018-09-01 23:00:00 +03:00
|
|
|
$query = '{my query}';
|
2017-07-16 14:52:38 +03:00
|
|
|
$variables = ['test' => 1, 'test2' => 2];
|
|
|
|
$operation = 'op';
|
|
|
|
|
2018-09-01 23:00:00 +03:00
|
|
|
$get = [
|
|
|
|
'query' => $query,
|
|
|
|
'variables' => $variables,
|
|
|
|
'operationName' => $operation,
|
2017-07-16 14:52:38 +03:00
|
|
|
];
|
2017-08-15 16:45:23 +03:00
|
|
|
$parsed = [
|
|
|
|
'raw' => $this->parseRawGetRequest($get),
|
2018-09-01 23:00:00 +03:00
|
|
|
'psr' => $this->parsePsrGetRequest($get),
|
2017-08-15 16:45:23 +03:00
|
|
|
];
|
2017-07-16 14:52:38 +03:00
|
|
|
|
2017-08-15 16:45:23 +03:00
|
|
|
foreach ($parsed as $method => $parsedBody) {
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertValidOperationParams($parsedBody, $query, null, $variables, $operation, $method);
|
|
|
|
self::assertTrue($parsedBody->isReadonly(), $method);
|
2017-08-15 16:45:23 +03:00
|
|
|
}
|
2017-07-16 14:52:38 +03:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testParsesMultipartFormdataRequest() : void
|
2018-07-05 09:52:29 +03:00
|
|
|
{
|
2018-09-01 23:00:00 +03:00
|
|
|
$query = '{my query}';
|
2018-07-05 09:52:29 +03:00
|
|
|
$variables = ['test' => 1, 'test2' => 2];
|
|
|
|
$operation = 'op';
|
|
|
|
|
2018-09-01 23:00:00 +03:00
|
|
|
$post = [
|
|
|
|
'query' => $query,
|
|
|
|
'variables' => $variables,
|
|
|
|
'operationName' => $operation,
|
2018-07-05 09:52:29 +03:00
|
|
|
];
|
|
|
|
$parsed = [
|
2018-10-09 17:51:23 +03:00
|
|
|
'raw' => $this->parseRawMultipartFormDataRequest($post),
|
|
|
|
'psr' => $this->parsePsrMultipartFormDataRequest($post),
|
2018-07-05 09:52:29 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($parsed as $method => $parsedBody) {
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertValidOperationParams($parsedBody, $query, null, $variables, $operation, $method);
|
|
|
|
self::assertFalse($parsedBody->isReadOnly(), $method);
|
2018-07-05 09:52:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testParsesJSONRequest() : void
|
2017-07-16 14:52:38 +03:00
|
|
|
{
|
2018-09-01 23:00:00 +03:00
|
|
|
$query = '{my query}';
|
2017-07-16 14:52:38 +03:00
|
|
|
$variables = ['test' => 1, 'test2' => 2];
|
|
|
|
$operation = 'op';
|
|
|
|
|
2018-09-01 23:00:00 +03:00
|
|
|
$body = [
|
|
|
|
'query' => $query,
|
|
|
|
'variables' => $variables,
|
|
|
|
'operationName' => $operation,
|
2017-07-16 14:52:38 +03:00
|
|
|
];
|
2017-08-15 16:45:23 +03:00
|
|
|
$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)),
|
2017-08-15 16:45:23 +03:00
|
|
|
];
|
|
|
|
foreach ($parsed as $method => $parsedBody) {
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertValidOperationParams($parsedBody, $query, null, $variables, $operation, $method);
|
|
|
|
self::assertFalse($parsedBody->isReadOnly(), $method);
|
2017-08-15 16:45:23 +03:00
|
|
|
}
|
2017-07-16 14:52:38 +03:00
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testParsesVariablesAsJSON() : void
|
2017-07-19 15:30:39 +03:00
|
|
|
{
|
2018-09-01 23:00:00 +03:00
|
|
|
$query = '{my query}';
|
2017-07-19 15:30:39 +03:00
|
|
|
$variables = ['test' => 1, 'test2' => 2];
|
|
|
|
$operation = 'op';
|
|
|
|
|
2018-09-01 23:00:00 +03:00
|
|
|
$body = [
|
|
|
|
'query' => $query,
|
|
|
|
'variables' => json_encode($variables),
|
|
|
|
'operationName' => $operation,
|
2017-07-19 15:30:39 +03:00
|
|
|
];
|
2017-08-15 16:45:23 +03:00
|
|
|
$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)),
|
2017-08-15 16:45:23 +03:00
|
|
|
];
|
|
|
|
foreach ($parsed as $method => $parsedBody) {
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertValidOperationParams($parsedBody, $query, null, $variables, $operation, $method);
|
|
|
|
self::assertFalse($parsedBody->isReadOnly(), $method);
|
2017-08-15 16:45:23 +03:00
|
|
|
}
|
2017-07-19 15:30:39 +03:00
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testIgnoresInvalidVariablesJson() : void
|
2017-07-19 15:30:39 +03:00
|
|
|
{
|
2018-09-01 23:00:00 +03:00
|
|
|
$query = '{my query}';
|
2017-07-19 15:30:39 +03:00
|
|
|
$variables = '"some invalid json';
|
|
|
|
$operation = 'op';
|
|
|
|
|
2018-09-01 23:00:00 +03:00
|
|
|
$body = [
|
|
|
|
'query' => $query,
|
|
|
|
'variables' => $variables,
|
|
|
|
'operationName' => $operation,
|
2017-07-19 15:30:39 +03:00
|
|
|
];
|
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) {
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertValidOperationParams($parsedBody, $query, null, $variables, $operation, $method);
|
|
|
|
self::assertFalse($parsedBody->isReadOnly(), $method);
|
2017-08-15 16:45:23 +03:00
|
|
|
}
|
2017-07-19 15:30:39 +03:00
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testParsesBatchJSONRequest() : void
|
2017-07-16 14:52:38 +03:00
|
|
|
{
|
2018-09-01 23:00:00 +03:00
|
|
|
$body = [
|
2017-07-16 14:52:38 +03:00
|
|
|
[
|
2018-09-01 23:00:00 +03:00
|
|
|
'query' => '{my query}',
|
|
|
|
'variables' => ['test' => 1, 'test2' => 2],
|
|
|
|
'operationName' => 'op',
|
2017-07-16 14:52:38 +03:00
|
|
|
],
|
|
|
|
[
|
2018-09-01 23:00:00 +03:00
|
|
|
'queryId' => 'my-query-id',
|
|
|
|
'variables' => ['test' => 1, 'test2' => 2],
|
|
|
|
'operationName' => 'op2',
|
2017-07-16 14:52:38 +03:00
|
|
|
],
|
|
|
|
];
|
2017-08-15 16:45:23 +03:00
|
|
|
$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)),
|
2017-08-15 16:45:23 +03:00
|
|
|
];
|
|
|
|
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'],
|
|
|
|
$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'],
|
|
|
|
$method
|
|
|
|
);
|
2017-08-15 16:45:23 +03:00
|
|
|
}
|
2017-07-16 14:52:38 +03:00
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testFailsParsingInvalidRawJsonRequestRaw() : void
|
2017-07-16 14:52:38 +03:00
|
|
|
{
|
|
|
|
$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);
|
|
|
|
}
|
2017-08-15 16:45:23 +03:00
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testFailsParsingInvalidRawJsonRequestPsr() : void
|
2017-12-21 09:52:43 +03:00
|
|
|
{
|
|
|
|
$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);
|
2017-07-16 14:52:38 +03:00
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testFailsParsingNonPreParsedPsrRequest() : void
|
2017-12-21 09:01:57 +03:00
|
|
|
{
|
|
|
|
try {
|
2018-01-13 14:08:07 +03:00
|
|
|
$this->parsePsrRequest('application/json', json_encode(null));
|
2018-10-09 17:51:23 +03:00
|
|
|
self::fail('Expected exception not thrown');
|
2017-12-21 09:01:57 +03:00
|
|
|
} catch (InvariantViolation $e) {
|
|
|
|
// Expecting parsing exception to be thrown somewhere else:
|
2018-09-19 18:12:09 +03:00
|
|
|
self::assertEquals(
|
2018-01-13 14:08:07 +03:00
|
|
|
'PSR-7 request is expected to provide parsed body for "application/json" requests but got null',
|
2017-12-21 09:01:57 +03:00
|
|
|
$e->getMessage()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
/**
|
|
|
|
* There is no equivalent for psr request, because it should throw
|
|
|
|
*/
|
|
|
|
public function testFailsParsingNonArrayOrObjectJsonRequestRaw() : void
|
2017-07-16 14:52:38 +03:00
|
|
|
{
|
|
|
|
$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);
|
|
|
|
}
|
2017-08-15 16:45:23 +03:00
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testFailsParsingNonArrayOrObjectJsonRequestPsr() : void
|
2017-12-21 09:52:43 +03:00
|
|
|
{
|
|
|
|
$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);
|
|
|
|
}
|
2017-08-15 16:45:23 +03:00
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testFailsParsingInvalidContentTypeRaw() : void
|
2017-12-21 09:52:43 +03:00
|
|
|
{
|
|
|
|
$contentType = 'not-supported-content-type';
|
2018-09-01 23:00:00 +03:00
|
|
|
$body = 'test';
|
2017-12-21 09:52:43 +03:00
|
|
|
|
2018-07-29 18:43:10 +03:00
|
|
|
$this->expectException(RequestError::class);
|
|
|
|
$this->expectExceptionMessage('Unexpected content type: "not-supported-content-type"');
|
2017-12-21 09:52:43 +03:00
|
|
|
$this->parseRawRequest($contentType, $body);
|
2017-07-16 14:52:38 +03:00
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testFailsParsingInvalidContentTypePsr() : void
|
2017-07-16 14:52:38 +03:00
|
|
|
{
|
2017-08-15 16:45:23 +03:00
|
|
|
$contentType = 'not-supported-content-type';
|
2018-09-01 23:00:00 +03:00
|
|
|
$body = 'test';
|
2017-08-15 16:45:23 +03:00
|
|
|
|
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);
|
|
|
|
}
|
2017-07-16 14:52:38 +03:00
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testFailsWithMissingContentTypeRaw() : void
|
2017-07-16 14:52:38 +03:00
|
|
|
{
|
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');
|
|
|
|
}
|
2017-08-15 16:45:23 +03:00
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testFailsWithMissingContentTypePsr() : void
|
2017-12-21 09:52:43 +03:00
|
|
|
{
|
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');
|
2017-07-16 14:52:38 +03:00
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testFailsOnMethodsOtherThanPostOrGetRaw() : void
|
2017-07-16 14:52:38 +03:00
|
|
|
{
|
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
|
|
|
}
|
2017-08-15 16:45:23 +03:00
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testFailsOnMethodsOtherThanPostOrGetPsr() : void
|
2017-12-21 09:52:43 +03:00
|
|
|
{
|
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');
|
2017-07-16 14:52:38 +03:00
|
|
|
}
|
|
|
|
}
|