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
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]*)/im', $response, $match)) {
$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]*)/im', $header, $match)) {
return trim($match[1]);
}

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

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

/**
* @dataProvider contentTypeProvider
*/
public function testFetchContentType(string $response, ?string $expected, bool $matches): void
{
$c = new Curl();

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

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

public function contentTypeProvider(): array
{
return [
'json without parameters' => ["Content-Type: application/json\r\n\r\n{}", 'application/json', true],
'json with charset' => ["Content-Type: application/json; charset=utf-8\r\n\r\n{}", 'application/json', true],
'schema media type with charset' => ["Content-Type: application/schema+json; charset=utf-8\r\n\r\n{}", 'application/schema+json', true],
'multiple parameters' => ["Content-Type: application/json; charset=utf-8; profile=schema\r\n\r\n{}", 'application/json', true],
'X-Content-Type is not a content type' => ["HTTP/1.1 200 OK\r\nX-Content-Type: text/plain\r\n\r\n{}", null, false],
];
}
}
}

Expand Down
21 changes: 18 additions & 3 deletions tests/Uri/Retrievers/FileGetContentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ public function testFetchFile(): void
$this->assertNotEmpty($result);
}

public function testContentType(): void
/**
* @dataProvider contentTypeProvider
*/
public function testFetchContentType(string $header, ?string $expected, bool $matches): void
{
$res = new FileGetContents();

Expand All @@ -35,8 +38,20 @@ public function testContentType(): void
$fetchContentType->setAccessible(true);
}

$this->assertTrue($fetchContentType->invoke($res, ['Content-Type: application/json']));
$this->assertFalse($fetchContentType->invoke($res, ['X-Some-Header: whateverValue']));
$this->assertSame($matches, $fetchContentType->invoke($res, [$header]));
$this->assertSame($expected, $res->getContentType());
}

public function contentTypeProvider(): array
{
return [
'json without parameters' => ['Content-Type: application/json', 'application/json', true],
'json with charset' => ['Content-Type: application/json; charset=utf-8', 'application/json', true],
'schema media type with charset' => ['Content-Type: application/schema+json; charset=utf-8', 'application/schema+json', true],
'multiple parameters' => ['Content-Type: application/json; charset=utf-8; profile=schema', 'application/json', true],
'non-content-type header' => ['X-Some-Header: whateverValue', null, false],
'X-Content-Type is not a content type' => ['X-Content-Type: text/plain', null, false],
];
}

public function testCanHandleHttp301PermanentRedirect(): void
Expand Down
Loading