diff --git a/src/JsonSchema/Uri/Retrievers/FileGetContents.php b/src/JsonSchema/Uri/Retrievers/FileGetContents.php index 7019814f..34e6ede0 100644 --- a/src/JsonSchema/Uri/Retrievers/FileGetContents.php +++ b/src/JsonSchema/Uri/Retrievers/FileGetContents.php @@ -68,7 +68,7 @@ public function retrieve($uri) */ private function fetchContentType(array $headers) { - foreach ($headers as $header) { + foreach (array_reverse($headers) as $header) { if ($this->contentType = self::getContentTypeMatchInHeader($header)) { return true; } diff --git a/tests/Uri/Retrievers/FileGetContentsTest.php b/tests/Uri/Retrievers/FileGetContentsTest.php index 0514a7d5..2301f3bc 100644 --- a/tests/Uri/Retrievers/FileGetContentsTest.php +++ b/tests/Uri/Retrievers/FileGetContentsTest.php @@ -1,75 +1,49 @@ retrieve(__DIR__ . '/Fixture/missing.json'); - } - - public function testFetchFile() - { - $res = new FileGetContents(); - $result = $res->retrieve(__DIR__ . '/../Fixture/child.json'); - $this->assertNotEmpty($result); - } - - public function testFalseReturn() - { - $res = new FileGetContents(); - - $this->setExpectedException( - '\JsonSchema\Exception\ResourceNotFoundException', - 'JSON schema not found at http://example.com/false' - ); - $res->retrieve('http://example.com/false'); - } - - public function testFetchDirectory() - { - $res = new FileGetContents(); + $res = new FileGetContents(); + $res->retrieve(__DIR__ . '/Fixture/missing.json'); + } - $this->setExpectedException( - '\JsonSchema\Exception\ResourceNotFoundException', - 'JSON schema not found at file:///this/is/a/directory/' - ); - $res->retrieve('file:///this/is/a/directory/'); - } + public function testFetchFile() + { + $res = new FileGetContents(); + $result = $res->retrieve(__DIR__ . '/../Fixture/child.json'); + $this->assertNotEmpty($result); + } - public function testContentType() - { - $res = new FileGetContents(); + public function testContentType() + { + $res = new FileGetContents(); - $reflector = new \ReflectionObject($res); - $fetchContentType = $reflector->getMethod('fetchContentType'); - $fetchContentType->setAccessible(true); + $reflector = new \ReflectionObject($res); + $fetchContentType = $reflector->getMethod('fetchContentType'); + $fetchContentType->setAccessible(true); - $this->assertTrue($fetchContentType->invoke($res, array('Content-Type: application/json'))); - $this->assertFalse($fetchContentType->invoke($res, array('X-Some-Header: whateverValue'))); - } + $this->assertTrue($fetchContentType->invoke($res, array('Content-Type: application/json'))); + $this->assertFalse($fetchContentType->invoke($res, array('X-Some-Header: whateverValue'))); } -} -namespace JsonSchema\Uri\Retrievers -{ - function file_get_contents($uri) + public function testCanHandleHttp301PermanentRedirect() { - switch ($uri) { - case 'http://example.com/false': return false; - case 'file:///this/is/a/directory/': return ''; - default: return \file_get_contents($uri); - } + $res = new FileGetContents(); + + $res->retrieve('http://asyncapi.com/definitions/2.0.0/asyncapi.json'); + + $this->assertSame('application/schema+json', $res->getContentType()); } }