Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test for invalid json in request body #65

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
13 changes: 10 additions & 3 deletions src/helpers/RequestParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace alsvanzelf\jsonapi\helpers;

use alsvanzelf\jsonapi\Document;
use alsvanzelf\jsonapi\exceptions\Exception;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ServerRequestInterface;

Expand Down Expand Up @@ -44,6 +45,8 @@ public function __construct($selfLink='', array $queryParameters=[], array $docu

/**
* @return self
*
* @throws Exception if the body's json is invalid
*/
public static function fromSuperglobals() {
$selfLink = '';
Expand All @@ -60,6 +63,9 @@ public static function fromSuperglobals() {

if ($documentIsJsonapi || $documentIsJson) {
$document = json_decode(file_get_contents('php://input'), true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new Exception('error parsing request body: '.json_last_error_msg());
}

if ($document === null) {
$document = [];
Expand All @@ -73,6 +79,8 @@ public static function fromSuperglobals() {
/**
* @param ServerRequestInterface|RequestInterface $request
* @return self
*
* @throws Exception if the body's json is invalid
*/
public static function fromPsrRequest(RequestInterface $request) {
$selfLink = (string) $request->getUri();
Expand All @@ -90,9 +98,8 @@ public static function fromPsrRequest(RequestInterface $request) {
}
else {
$document = json_decode($request->getBody()->getContents(), true);

if ($document === null) {
$document = [];
if (json_last_error() !== JSON_ERROR_NONE) {
throw new Exception('error parsing request body: '.json_last_error_msg());
}
}

Expand Down
13 changes: 13 additions & 0 deletions tests/helpers/RequestParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace alsvanzelf\jsonapiTests\helpers;

use alsvanzelf\jsonapi\Document;
use alsvanzelf\jsonapi\exceptions\Exception;
use alsvanzelf\jsonapi\helpers\RequestParser;
use alsvanzelf\jsonapiTests\helpers\TestableNonInterfaceRequestInterface;
use alsvanzelf\jsonapiTests\helpers\TestableNonInterfaceServerRequestInterface;
Expand Down Expand Up @@ -178,6 +179,18 @@ public function testFromPsrRequest_WithEmptyDocument() {
$this->assertSame([], $requestParser->getDocument());
}

public function testFromPsrRequest_WithBrokenDocument() {
$selfLink = '';
$queryParameters = [];
$document = '{"data": {foo: "bar"}}';
$request = new TestableNonInterfaceRequestInterface($selfLink, $queryParameters, $document);

$this->expectException(Exception::class);
$this->expectExceptionMessage('error parsing request body: ');

RequestParser::fromPsrRequest($request);
}

public function testFromPsrRequest_WithServerRequestInterface() {
$queryParameters = [
'sort' => 'name,-location',
Expand Down
5 changes: 4 additions & 1 deletion tests/helpers/TestableNonInterfaceStreamInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ public function getContents() {
if ($this->document === null) {
return '';
}
if (is_string($this->document) === false) {
return (string) json_encode($this->document);
}

return (string) json_encode($this->document);
return $this->document;
}

// not used in current implementation
Expand Down