From b7475c066e0af8a74b73d79ffadbb76539dfb0d7 Mon Sep 17 00:00:00 2001 From: Danny van der Sluijs Date: Mon, 5 Feb 2024 20:47:18 +0100 Subject: [PATCH 1/4] test: Add test failure --- tests/Uri/Retrievers/FileGetContentsTest.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/Uri/Retrievers/FileGetContentsTest.php b/tests/Uri/Retrievers/FileGetContentsTest.php index 0514a7d5..d8b4b45a 100644 --- a/tests/Uri/Retrievers/FileGetContentsTest.php +++ b/tests/Uri/Retrievers/FileGetContentsTest.php @@ -59,6 +59,15 @@ public function testContentType() $this->assertTrue($fetchContentType->invoke($res, array('Content-Type: application/json'))); $this->assertFalse($fetchContentType->invoke($res, array('X-Some-Header: whateverValue'))); } + + public function testCanHandleHttp301PermanentRedirect() + { + $res = new FileGetContents(); + + $res->retrieve('http://asyncapi.com/definitions/2.0.0/asyncapi.json'); + + $this->assertSame('application/schema+json', $res->getContentType()); + } } } From e6ea5223e3546a7a8f4fb823379775d98cbd8e6f Mon Sep 17 00:00:00 2001 From: Danny van der Sluijs Date: Sun, 26 May 2024 21:16:16 +0200 Subject: [PATCH 2/4] test: Remove file_get_contents override and incorrect/invaluable tests Overriding file_get_contents introduces different behaviour from the native function, such as the $http_response_headers missing. Removing the override revealed two test (testing a specific return path of the file_get_contents method rather then testing the behaviour or result of the subject under test) which had different behaviour between the test and runtime, therefor these tests have been removed. --- tests/Uri/Retrievers/FileGetContentsTest.php | 34 -------------------- 1 file changed, 34 deletions(-) diff --git a/tests/Uri/Retrievers/FileGetContentsTest.php b/tests/Uri/Retrievers/FileGetContentsTest.php index d8b4b45a..bd284f2c 100644 --- a/tests/Uri/Retrievers/FileGetContentsTest.php +++ b/tests/Uri/Retrievers/FileGetContentsTest.php @@ -26,28 +26,6 @@ public function testFetchFile() $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(); - - $this->setExpectedException( - '\JsonSchema\Exception\ResourceNotFoundException', - 'JSON schema not found at file:///this/is/a/directory/' - ); - $res->retrieve('file:///this/is/a/directory/'); - } - public function testContentType() { $res = new FileGetContents(); @@ -70,15 +48,3 @@ public function testCanHandleHttp301PermanentRedirect() } } } - -namespace JsonSchema\Uri\Retrievers -{ - function file_get_contents($uri) - { - switch ($uri) { - case 'http://example.com/false': return false; - case 'file:///this/is/a/directory/': return ''; - default: return \file_get_contents($uri); - } - } -} From 4479f6e308ef24dec58e6abc01c81ccf08a55711 Mon Sep 17 00:00:00 2001 From: Danny van der Sluijs Date: Sun, 26 May 2024 21:17:10 +0200 Subject: [PATCH 3/4] fix: Parse headers in reverse order to match with last header avoiding matches on HTTP 301 redirect headers which are listed first --- src/JsonSchema/Uri/Retrievers/FileGetContents.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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; } From 4f0e8f2e1d80c3411d2f7047007af9710fcf837e Mon Sep 17 00:00:00 2001 From: Danny van der Sluijs Date: Sun, 26 May 2024 21:23:33 +0200 Subject: [PATCH 4/4] style: Correct code style violations --- tests/Uri/Retrievers/FileGetContentsTest.php | 83 ++++++++++---------- 1 file changed, 41 insertions(+), 42 deletions(-) diff --git a/tests/Uri/Retrievers/FileGetContentsTest.php b/tests/Uri/Retrievers/FileGetContentsTest.php index bd284f2c..2301f3bc 100644 --- a/tests/Uri/Retrievers/FileGetContentsTest.php +++ b/tests/Uri/Retrievers/FileGetContentsTest.php @@ -1,50 +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 testContentType() + { + $res = new FileGetContents(); + + $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'))); + } + + public function testCanHandleHttp301PermanentRedirect() { - /** - * @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 testContentType() - { - $res = new FileGetContents(); - - $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'))); - } - - public function testCanHandleHttp301PermanentRedirect() - { - $res = new FileGetContents(); - - $res->retrieve('http://asyncapi.com/definitions/2.0.0/asyncapi.json'); - - $this->assertSame('application/schema+json', $res->getContentType()); - } + $res = new FileGetContents(); + + $res->retrieve('http://asyncapi.com/definitions/2.0.0/asyncapi.json'); + + $this->assertSame('application/schema+json', $res->getContentType()); } }