Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/JsonSchema/Uri/Retrievers/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private function fetchMessageBody($response)
*/
protected function fetchContentType($response)
{
if (0 < preg_match("/Content-Type:(\V*)/ims", $response, $match)) {
if (0 < preg_match("/Content-Type:([^;\v]*)/ims", $response, $match)) {
Comment thread
DannyvdSluijs marked this conversation as resolved.
Outdated
$this->contentType = trim($match[1]);

return true;
Expand Down
2 changes: 1 addition & 1 deletion src/JsonSchema/Uri/Retrievers/FileGetContents.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private function fetchContentType(array $headers): bool
*/
protected static function getContentTypeMatchInHeader($header)
{
if (0 < preg_match("/Content-Type:(\V*)/ims", $header, $match)) {
if (0 < preg_match("/Content-Type:([^;\v]*)/ims", $header, $match)) {
Comment thread
DannyvdSluijs marked this conversation as resolved.
Outdated
return trim($match[1]);
}

Expand Down
26 changes: 26 additions & 0 deletions tests/Uri/Retrievers/CurlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,32 @@ public function testNoContentType(): void

self::assertStringEqualsFileCanonicalizing(realpath(__DIR__ . '/../../fixtures/foobar.json'), $result);
}

/**
* @dataProvider contentTypeParameterProvider
*/
public function testContentTypeIgnoresParameters(string $response, string $expected): void
{
$c = new Curl();

$reflector = new \ReflectionObject($c);
$fetchContentType = $reflector->getMethod('fetchContentType');
if (PHP_VERSION_ID < 80100) {
$fetchContentType->setAccessible(true);
}

$this->assertTrue($fetchContentType->invoke($c, $response));
$this->assertSame($expected, $c->getContentType());
}

public function contentTypeParameterProvider(): array
{
return [
'json with charset' => ["Content-Type: application/json; charset=utf-8\r\n\r\n{}", 'application/json'],
'schema media type with charset' => ["Content-Type: application/schema+json; charset=utf-8\r\n\r\n{}", 'application/schema+json'],
'multiple parameters' => ["Content-Type: application/json; charset=utf-8; profile=schema\r\n\r\n{}", 'application/json'],
];
Comment thread
DannyvdSluijs marked this conversation as resolved.
}
}
}

Expand Down
26 changes: 26 additions & 0 deletions tests/Uri/Retrievers/FileGetContentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,32 @@ public function testContentType(): void
$this->assertFalse($fetchContentType->invoke($res, ['X-Some-Header: whateverValue']));
}

/**
* @dataProvider contentTypeParameterProvider
*/
public function testContentTypeIgnoresParameters(string $header, string $expected): void
{
$res = new FileGetContents();

$reflector = new \ReflectionObject($res);
$fetchContentType = $reflector->getMethod('fetchContentType');
if (PHP_VERSION_ID < 80100) {
$fetchContentType->setAccessible(true);
}

$this->assertTrue($fetchContentType->invoke($res, [$header]));
$this->assertSame($expected, $res->getContentType());
}

public function contentTypeParameterProvider(): array
{
return [
'json with charset' => ['Content-Type: application/json; charset=utf-8', 'application/json'],
'schema media type with charset' => ['Content-Type: application/schema+json; charset=utf-8', 'application/schema+json'],
'multiple parameters' => ['Content-Type: application/json; charset=utf-8; profile=schema', 'application/json'],
];
}

public function testCanHandleHttp301PermanentRedirect(): void
{
$res = new FileGetContents();
Expand Down
Loading