Fix the JSON content type regex to match all MIME types

Signed-off-by: Stanimir Bozhilov <stanimir@audriga.com>
This commit is contained in:
Stanimir Bozhilov 2022-09-26 11:51:44 +02:00
parent d80f8f6c82
commit 46c10c77e1
2 changed files with 92 additions and 1 deletions

View file

@ -108,7 +108,7 @@ class Request implements \ArrayAccess, \Countable, IRequest {
protected $contentDecoded = false;
/** @var string */
protected string $jsonContentTypeRegex = '/application\/(\w+\+)?json/';
protected string $jsonContentTypeRegex = '{^application/(?:[a-z0-9.-]+\+)?json\b}';
/**
* @param array $vars An associative array with the following optional values:

View file

@ -232,6 +232,30 @@ class RequestTest extends \Test\TestCase {
$this->assertSame('Example User', $request['displayName']);
}
public function testCustomJsonPost() {
global $data;
$data = '{"propertyA":"sometestvalue", "propertyB":"someothertestvalue"}';
// Note: the content type used here is fictional and intended to check if the regex for JSON content types works fine
$vars = [
'method' => 'POST',
'server' => ['CONTENT_TYPE' => 'application/custom-type+json; utf-8']
];
$request = new Request(
$vars,
$this->requestId,
$this->config,
$this->csrfTokenManager,
$this->stream
);
$this->assertSame('POST', $request->method);
$result = $request->post;
$this->assertSame('sometestvalue', $result['propertyA']);
$this->assertSame('someothertestvalue', $result['propertyB']);
}
public function testNotJsonPost() {
global $data;
$data = 'this is not valid json';
@ -274,6 +298,27 @@ class RequestTest extends \Test\TestCase {
// ensure there's no error attempting to decode the content
}
public function testNotCustomJsonPost() {
global $data;
$data = 'this is not valid json';
$vars = [
'method' => 'POST',
'server' => ['CONTENT_TYPE' => 'application/custom-type+json; utf-8']
];
$request = new Request(
$vars,
$this->requestId,
$this->config,
$this->csrfTokenManager,
$this->stream
);
$this->assertEquals('POST', $request->method);
$result = $request->post;
// ensure there's no error attempting to decode the content
}
public function testPatch() {
global $data;
$data = http_build_query(['name' => 'John Q. Public', 'nickname' => 'Joey'], '', '&');
@ -390,6 +435,52 @@ class RequestTest extends \Test\TestCase {
$this->assertSame(null, $result['displayName']);
}
public function testCustomJsonPatchAndPut() {
global $data;
// PUT content
$data = '{"propertyA": "sometestvalue", "propertyB": "someothertestvalue"}';
$vars = [
'method' => 'PUT',
'server' => ['CONTENT_TYPE' => 'application/custom-type+json; utf-8'],
];
$request = new Request(
$vars,
$this->requestId,
$this->config,
$this->csrfTokenManager,
$this->stream
);
$this->assertSame('PUT', $request->method);
$result = $request->put;
$this->assertSame('sometestvalue', $result['propertyA']);
$this->assertSame('someothertestvalue', $result['propertyB']);
// PATCH content
$data = '{"propertyA": "sometestvalue", "propertyB": null}';
$vars = [
'method' => 'PATCH',
'server' => ['CONTENT_TYPE' => 'application/custom-type+json; utf-8'],
];
$request = new Request(
$vars,
$this->requestId,
$this->config,
$this->csrfTokenManager,
$this->stream
);
$this->assertSame('PATCH', $request->method);
$result = $request->patch;
$this->assertSame('sometestvalue', $result['propertyA']);
$this->assertSame(null, $result['propertyB']);
}
public function testPutStream() {
global $data;
$data = file_get_contents(__DIR__ . '/../../../data/testimage.png');