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
15 changes: 6 additions & 9 deletions src/Server/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -493,21 +493,18 @@ public function parsePsrRequest(ServerRequestInterface $request)
);
}

// Try parsing ourselves if PSR-7 implementation doesn't parse JSON automatically
if (is_array($bodyParams) && empty($bodyParams)) {
$bodyParams = json_decode($request->getBody(), true);

if (json_last_error()) {
throw new RequestError("Could not parse JSON: " . json_last_error_msg());
}
}

if (!is_array($bodyParams)) {
throw new RequestError(
"GraphQL Server expects JSON object or array, but got " .
Utils::printSafeJson($bodyParams)
);
}

if (empty($bodyParams)) {
throw new InvariantViolation(
"PSR-7 request is expected to provide parsed body for \"application/json\" requests but got empty array"
);
}
} else {
$bodyParams = $request->getParsedBody();

Expand Down
25 changes: 17 additions & 8 deletions tests/Server/RequestParsingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,20 @@ public function testFailsParsingInvalidRawJsonRequestPsr()
$this->parsePsrRequest('application/json', $body);
}

public function testFailsParsingNonPreParsedPsrRequest()
{
try {
$this->parsePsrRequest('application/json', json_encode([]));
$this->fail('Expected exception not thrown');
} catch (InvariantViolation $e) {
// Expecting parsing exception to be thrown somewhere else:
$this->assertEquals(
'PSR-7 request is expected to provide parsed body for "application/json" requests but got empty array',
$e->getMessage()
);
}
}

// There is no equivalent for psr request, because it should throw

public function testFailsParsingNonArrayOrObjectJsonRequestRaw()
Expand Down Expand Up @@ -224,19 +238,14 @@ public function testFailsWithMissingContentTypePsr()

public function testFailsOnMethodsOtherThanPostOrGetRaw()
{

$this->setExpectedException(RequestError::class, 'HTTP Method "PUT" is not supported');
$this->parseRawRequest('application/json', json_encode([]), "PUT");
}
$this->parseRawRequest('application/json', json_encode([]), "PUT");
}

public function testFailsOnMethodsOtherThanPostOrGetPsr()
{
$body = [
'query' => '{my query}',
];

$this->setExpectedException(RequestError::class, 'HTTP Method "PUT" is not supported');
$this->parsePsrRequest('application/json', json_encode([]), "PUT");
$this->parsePsrRequest('application/json', json_encode([]), "PUT");
}

/**
Expand Down
24 changes: 5 additions & 19 deletions tests/Server/StandardServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,18 @@ public function testSimpleRequestExecutionWithOutsideParsing()

public function testSimplePsrRequestExecution()
{
$body = json_encode([
$body = [
'query' => '{f1}'
]);
];

$expected = [
'data' => [
'f1' => 'f1'
]
];

$preParsedRequest = $this->preparePsrRequest('application/json', $body, true);
$this->assertPsrRequestEquals($expected, $preParsedRequest);

$notPreParsedRequest = $this->preparePsrRequest('application/json', $body, false);
$this->assertPsrRequestEquals($expected, $notPreParsedRequest);
$request = $this->preparePsrRequest('application/json', $body);
$this->assertPsrRequestEquals($expected, $request);
}

private function executePsrRequest($psrRequest)
Expand All @@ -75,22 +72,11 @@ private function assertPsrRequestEquals($expected, $request)
return $result;
}

private function preparePsrRequest($contentType, $content, $preParseBody)
private function preparePsrRequest($contentType, $parsedBody)
{
$psrRequestBody = new PsrStreamStub();
$psrRequestBody->content = $content;

$psrRequest = new PsrRequestStub();
$psrRequest->headers['content-type'] = [$contentType];
$psrRequest->method = 'POST';
$psrRequest->body = $psrRequestBody;

if ($preParseBody && $contentType === 'application/json') {
$parsedBody = json_decode($content, true);
} else {
$parsedBody = [];
}

$psrRequest->parsedBody = $parsedBody;
return $psrRequest;
}
Expand Down