Skip to content

Commit 47708f5

Browse files
Merge pull request #709 from DannyvdSluijs/Issue-694
Addresses Content-Type mismatch on HTTP 301 Moved Permanently with FileGetContents
2 parents db59a8d + 4f0e8f2 commit 47708f5

File tree

2 files changed

+35
-61
lines changed

2 files changed

+35
-61
lines changed

src/JsonSchema/Uri/Retrievers/FileGetContents.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function retrieve($uri)
6868
*/
6969
private function fetchContentType(array $headers)
7070
{
71-
foreach ($headers as $header) {
71+
foreach (array_reverse($headers) as $header) {
7272
if ($this->contentType = self::getContentTypeMatchInHeader($header)) {
7373
return true;
7474
}
+34-60
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,49 @@
11
<?php
22

3-
namespace JsonSchema\Tests\Uri\Retrievers
4-
{
5-
use JsonSchema\Uri\Retrievers\FileGetContents;
6-
use PHPUnit\Framework\TestCase;
3+
namespace JsonSchema\Tests\Uri\Retrievers;
4+
5+
use JsonSchema\Uri\Retrievers\FileGetContents;
6+
use PHPUnit\Framework\TestCase;
77

8+
/**
9+
* @group FileGetContents
10+
*/
11+
class FileGetContentsTest extends TestCase
12+
{
813
/**
9-
* @group FileGetContents
14+
* @expectedException \JsonSchema\Exception\ResourceNotFoundException
1015
*/
11-
class FileGetContentsTest extends TestCase
16+
public function testFetchMissingFile()
1217
{
13-
/**
14-
* @expectedException \JsonSchema\Exception\ResourceNotFoundException
15-
*/
16-
public function testFetchMissingFile()
17-
{
18-
$res = new FileGetContents();
19-
$res->retrieve(__DIR__ . '/Fixture/missing.json');
20-
}
21-
22-
public function testFetchFile()
23-
{
24-
$res = new FileGetContents();
25-
$result = $res->retrieve(__DIR__ . '/../Fixture/child.json');
26-
$this->assertNotEmpty($result);
27-
}
28-
29-
public function testFalseReturn()
30-
{
31-
$res = new FileGetContents();
32-
33-
$this->setExpectedException(
34-
'\JsonSchema\Exception\ResourceNotFoundException',
35-
'JSON schema not found at http://example.com/false'
36-
);
37-
$res->retrieve('http://example.com/false');
38-
}
39-
40-
public function testFetchDirectory()
41-
{
42-
$res = new FileGetContents();
18+
$res = new FileGetContents();
19+
$res->retrieve(__DIR__ . '/Fixture/missing.json');
20+
}
4321

44-
$this->setExpectedException(
45-
'\JsonSchema\Exception\ResourceNotFoundException',
46-
'JSON schema not found at file:///this/is/a/directory/'
47-
);
48-
$res->retrieve('file:///this/is/a/directory/');
49-
}
22+
public function testFetchFile()
23+
{
24+
$res = new FileGetContents();
25+
$result = $res->retrieve(__DIR__ . '/../Fixture/child.json');
26+
$this->assertNotEmpty($result);
27+
}
5028

51-
public function testContentType()
52-
{
53-
$res = new FileGetContents();
29+
public function testContentType()
30+
{
31+
$res = new FileGetContents();
5432

55-
$reflector = new \ReflectionObject($res);
56-
$fetchContentType = $reflector->getMethod('fetchContentType');
57-
$fetchContentType->setAccessible(true);
33+
$reflector = new \ReflectionObject($res);
34+
$fetchContentType = $reflector->getMethod('fetchContentType');
35+
$fetchContentType->setAccessible(true);
5836

59-
$this->assertTrue($fetchContentType->invoke($res, array('Content-Type: application/json')));
60-
$this->assertFalse($fetchContentType->invoke($res, array('X-Some-Header: whateverValue')));
61-
}
37+
$this->assertTrue($fetchContentType->invoke($res, array('Content-Type: application/json')));
38+
$this->assertFalse($fetchContentType->invoke($res, array('X-Some-Header: whateverValue')));
6239
}
63-
}
6440

65-
namespace JsonSchema\Uri\Retrievers
66-
{
67-
function file_get_contents($uri)
41+
public function testCanHandleHttp301PermanentRedirect()
6842
{
69-
switch ($uri) {
70-
case 'http://example.com/false': return false;
71-
case 'file:///this/is/a/directory/': return '';
72-
default: return \file_get_contents($uri);
73-
}
43+
$res = new FileGetContents();
44+
45+
$res->retrieve('http://asyncapi.com/definitions/2.0.0/asyncapi.json');
46+
47+
$this->assertSame('application/schema+json', $res->getContentType());
7448
}
7549
}

0 commit comments

Comments
 (0)