Skip to content
Closed
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
11 changes: 5 additions & 6 deletions src/Server/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use function json_last_error_msg;
use function sprintf;
use function stripos;
use const JSON_ERROR_NONE;

/**
* Contains functionality that could be re-used by various server implementations
Expand Down Expand Up @@ -83,7 +84,7 @@ public function parseHttpRequest(?callable $readRawBodyFn = null)
: $this->readRawBody();
$bodyParams = json_decode($rawBody ?: '', true);

if (json_last_error()) {
if (json_last_error() !== JSON_ERROR_NONE) {
throw new RequestError('Could not parse JSON: ' . json_last_error_msg());
}

Expand Down Expand Up @@ -535,12 +536,10 @@ public function parsePsrRequest(ServerRequestInterface $request)
if (stripos($contentType[0], 'application/graphql') !== false) {
$bodyParams = ['query' => $request->getBody()->getContents()];
} elseif (stripos($contentType[0], 'application/json') !== false) {
$bodyParams = $request->getParsedBody();
$bodyParams = json_decode($request->getBody()->getContents(), true);

if ($bodyParams === null) {
throw new InvariantViolation(
'PSR-7 request is expected to provide parsed body for "application/json" requests but got null'
);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new RequestError('Could not parse JSON: ' . json_last_error_msg());
}

if (! is_array($bodyParams)) {
Expand Down
4 changes: 4 additions & 0 deletions tests/Server/Psr7/PsrRequestStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class PsrRequestStub implements ServerRequestInterface
/** @var mixed[] */
public $parsedBody;

public function __construct() {
$this->body = new PsrStreamStub();
}

/**
* Retrieves the HTTP protocol version as a string.
*
Expand Down
2 changes: 1 addition & 1 deletion tests/Server/Psr7/PsrStreamStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
class PsrStreamStub implements StreamInterface
{
public $content;
public $content = "";

/**
* Reads all data from the stream into a string, from the beginning to end.
Expand Down
18 changes: 2 additions & 16 deletions tests/Server/RequestParsingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,25 +414,11 @@ public function testFailsParsingInvalidRawJsonRequestPsr() : void
{
$body = 'not really{} a json';

$this->expectException(InvariantViolation::class);
$this->expectExceptionMessage('PSR-7 request is expected to provide parsed body for "application/json" requests but got null');
$this->expectException(RequestError::class);
$this->expectExceptionMessage('Could not parse JSON: Syntax error');
$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:
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
*/
Expand Down
10 changes: 5 additions & 5 deletions tests/Server/StandardServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private function parseRawRequest($contentType, $content, $method = 'POST')

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

$expected = [
'data' => ['f1' => 'f1'],
Expand All @@ -65,12 +65,12 @@ public function testSimplePsrRequestExecution() : void
$this->assertPsrRequestEquals($expected, $request);
}

private function preparePsrRequest($contentType, $parsedBody)
private function preparePsrRequest($contentType, $body)
{
$psrRequest = new PsrRequestStub();
$psrRequest->headers['content-type'] = [$contentType];
$psrRequest->method = 'POST';
$psrRequest->parsedBody = $parsedBody;
$psrRequest->body->content = $body;

return $psrRequest;
}
Expand All @@ -94,10 +94,10 @@ private function executePsrRequest($psrRequest)

public function testMultipleOperationPsrRequestExecution() : void
{
$body = [
$body = json_encode([
'query' => 'query firstOp {fieldWithPhpError} query secondOp {f1}',
'operationName' => 'secondOp',
];
]);

$expected = [
'data' => ['f1' => 'f1'],
Expand Down