diff options
author | diosmosis <benakamoorthi@fastmail.fm> | 2014-06-19 06:01:45 +0400 |
---|---|---|
committer | diosmosis <benakamoorthi@fastmail.fm> | 2014-06-19 06:01:52 +0400 |
commit | f169ee42e8bc15ffa63b5589d16280aa10e88ee1 (patch) | |
tree | 758dae3cfd2d94f827756d5db2bbb74ed6016e46 /tests | |
parent | 6c1970444bb93e8bb1370f7248682fd7b515b308 (diff) |
Add ability to serve only part of a static file. Includes tests for new functionality.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/PHPUnit/Core/ServeStaticFileTest.php | 102 | ||||
-rw-r--r-- | tests/resources/staticFileServer.php | 15 |
2 files changed, 117 insertions, 0 deletions
diff --git a/tests/PHPUnit/Core/ServeStaticFileTest.php b/tests/PHPUnit/Core/ServeStaticFileTest.php index b3536b7aa3..42aab636d4 100644 --- a/tests/PHPUnit/Core/ServeStaticFileTest.php +++ b/tests/PHPUnit/Core/ServeStaticFileTest.php @@ -36,8 +36,16 @@ define("UNIT_TEST_MODE", "unitTestMode"); define("NULL_FILE_SRV_MODE", "nullFile"); define("GHOST_FILE_SRV_MODE", "ghostFile"); define("TEST_FILE_SRV_MODE", "testFile"); +define("PARTIAL_TEST_FILE_SRV_MODE", "partialTestFile"); +define("WHOLE_TEST_FILE_WITH_RANGE_SRV_MODE", "wholeTestFileWithRange"); + +define("PARTIAL_BYTE_START", 1204); +define("PARTIAL_BYTE_END", 14724); // If the static file server has not been requested, the standard unit test case class is defined +/** + * @group ServeStaticFileTest + */ class Test_Piwik_ServeStaticFile extends PHPUnit_Framework_TestCase { public function tearDown() @@ -389,6 +397,90 @@ class Test_Piwik_ServeStaticFile extends PHPUnit_Framework_TestCase } /** + * @group Core + */ + public function test_partialFileServeNoCompression() + { + $this->removeCompressedFiles(); + + $curlHandle = curl_init(); + curl_setopt($curlHandle, CURLOPT_URL, $this->getPartialTestFileSrvModeUrl()); + curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true); + $partialResponse = curl_exec($curlHandle); + $responseInfo = curl_getinfo($curlHandle); + curl_close($curlHandle); + + clearstatcache(); + + // check no compressed files created + $this->assertFalse(file_exists($this->getCompressedFileLocation() . ".deflate")); + $this->assertFalse(file_exists($this->getCompressedFileLocation() . ".gz")); + + // check $partialResponse + $this->assertEquals(PARTIAL_BYTE_END - PARTIAL_BYTE_START, $responseInfo["size_download"]); + + $expectedPartialContents = substr(file_get_contents(TEST_FILE_LOCATION), PARTIAL_BYTE_START, + PARTIAL_BYTE_END - PARTIAL_BYTE_START); + $this->assertEquals($expectedPartialContents, $partialResponse); + } + + /** + * @group Core + */ + public function test_partialFileServeWithCompression() + { + $this->removeCompressedFiles(); + + $curlHandle = curl_init(); + curl_setopt($curlHandle, CURLOPT_URL, $this->getPartialTestFileSrvModeUrl()); + curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curlHandle, CURLOPT_ENCODING, "deflate"); + $partialResponse = curl_exec($curlHandle); + $responseInfo = curl_getinfo($curlHandle); + curl_close($curlHandle); + + clearstatcache(); + + // check the correct compressed file is created + $this->assertTrue(file_exists($this->getCompressedFileLocation() . ".deflate")); + $this->assertFalse(file_exists($this->getCompressedFileLocation() . ".gz")); + + // check $partialResponse + $expectedPartialContents = substr(file_get_contents(TEST_FILE_LOCATION), PARTIAL_BYTE_START, + PARTIAL_BYTE_END - PARTIAL_BYTE_START); + $this->assertEquals($expectedPartialContents, $partialResponse); + + $this->removeCompressedFiles(); + } + + /** + * @group Core + */ + public function test_wholeFileServeWithByteRange() + { + $this->removeCompressedFiles(); + + $curlHandle = curl_init(); + curl_setopt($curlHandle, CURLOPT_URL, $this->getWholeTestFileWithRangeSrvModeUrl()); + curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curlHandle, CURLOPT_ENCODING, "deflate"); + $fullResponse = curl_exec($curlHandle); + $responseInfo = curl_getinfo($curlHandle); + curl_close($curlHandle); + + clearstatcache(); + + // check the correct compressed file is created + $this->assertTrue(file_exists($this->getCompressedFileLocation() . ".deflate")); + $this->assertFalse(file_exists($this->getCompressedFileLocation() . ".gz")); + + // check $fullResponse + $this->assertEquals(file_get_contents(TEST_FILE_LOCATION), $fullResponse); + + $this->removeCompressedFiles(); + } + + /** * Helper methods */ private function getStaticSrvUrl() @@ -415,6 +507,16 @@ class Test_Piwik_ServeStaticFile extends PHPUnit_Framework_TestCase return $this->getStaticSrvUrl() . TEST_FILE_SRV_MODE; } + private function getPartialTestFileSrvModeUrl() + { + return $this->getStaticSrvUrl() . PARTIAL_TEST_FILE_SRV_MODE; + } + + private function getWholeTestFileWithRangeSrvModeUrl() + { + return $this->getStaticSrvUrl() . WHOLE_TEST_FILE_WITH_RANGE_SRV_MODE; + } + private function setZlibOutputRequest($url) { return $url . "&" . ZLIB_OUTPUT_REQUEST_VAR . "=1"; diff --git a/tests/resources/staticFileServer.php b/tests/resources/staticFileServer.php index cdaa54830f..69ed020696 100644 --- a/tests/resources/staticFileServer.php +++ b/tests/resources/staticFileServer.php @@ -54,6 +54,11 @@ define("ZLIB_OUTPUT_REQUEST_VAR", "zlibOutput"); define("NULL_FILE_SRV_MODE", "nullFile"); define("GHOST_FILE_SRV_MODE", "ghostFile"); define("TEST_FILE_SRV_MODE", "testFile"); +define("PARTIAL_TEST_FILE_SRV_MODE", "partialTestFile"); +define("WHOLE_TEST_FILE_WITH_RANGE_SRV_MODE", "wholeTestFileWithRange"); + +define("PARTIAL_BYTE_START", 1204); +define("PARTIAL_BYTE_END", 14724); /** @@ -89,4 +94,14 @@ switch ($staticFileServerMode) { ProxyHttp::serverStaticFile(TEST_FILE_LOCATION, TEST_FILE_CONTENT_TYPE); break; + + case PARTIAL_TEST_FILE_SRV_MODE: + + ProxyHttp::serverStaticFile(TEST_FILE_LOCATION, TEST_FILE_CONTENT_TYPE, $expireFarFutureDays = 100, PARTIAL_BYTE_START, PARTIAL_BYTE_END); + break; + + case WHOLE_TEST_FILE_WITH_RANGE_SRV_MODE: + + ProxyHttp::serverStaticFile(TEST_FILE_LOCATION, TEST_FILE_CONTENT_TYPE, $expireFarFutureDays = 100, 0, filesize(TEST_FILE_LOCATION)); + break; }
\ No newline at end of file |