-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #709 from DannyvdSluijs/Issue-694
Addresses Content-Type mismatch on HTTP 301 Moved Permanently with FileGetContents
- Loading branch information
Showing
2 changed files
with
35 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,49 @@ | ||
<?php | ||
|
||
namespace JsonSchema\Tests\Uri\Retrievers | ||
{ | ||
use JsonSchema\Uri\Retrievers\FileGetContents; | ||
use PHPUnit\Framework\TestCase; | ||
namespace JsonSchema\Tests\Uri\Retrievers; | ||
|
||
use JsonSchema\Uri\Retrievers\FileGetContents; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @group FileGetContents | ||
*/ | ||
class FileGetContentsTest extends TestCase | ||
{ | ||
/** | ||
* @group FileGetContents | ||
* @expectedException \JsonSchema\Exception\ResourceNotFoundException | ||
*/ | ||
class FileGetContentsTest extends TestCase | ||
public function testFetchMissingFile() | ||
{ | ||
/** | ||
* @expectedException \JsonSchema\Exception\ResourceNotFoundException | ||
*/ | ||
public function testFetchMissingFile() | ||
{ | ||
$res = new FileGetContents(); | ||
$res->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()); | ||
} | ||
} |