Skip to content

Commit

Permalink
add Content-Range unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
songlipeng2003 committed Dec 2, 2022
1 parent 8377bec commit d5f5fbd
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

php send static files use web server, now support apache, nginx, Lighttpd

## Change Log

[2.1.0] - 2022-12-02
* Add Content-Range support, but not support Multipart ranges

## require
php >= 7.3

Expand Down
8 changes: 4 additions & 4 deletions lib/XSendfile/XSendfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ public static function xSendfile( $file, $downFilename = null, $serverType = nul
}
}
try {
$fp = fopen($path, 'rb');
$fp = fopen($file, 'rb');
if ($fp === false) {
throw new \RuntimeException("Failed to open file " . $path . " for reading ");
throw new \RuntimeException("Failed to open file " . $file . " for reading ");
}
// using this php://output hack because fpassthru is unsuitable: https://github.com/php/php-src/issues/9673
$output = fopen('php://output', 'wb');
Expand All @@ -191,7 +191,7 @@ public static function xSendfile( $file, $downFilename = null, $serverType = nul
throw new \RuntimeException("Failed to send file");
}
return;
} catch (Throwable $ex) {
} catch (\Throwable $ex) {
if(!headers_sent()) {
http_response_code(500);
header("Content-Length: ", true);
Expand All @@ -210,5 +210,5 @@ public static function xSendfile( $file, $downFilename = null, $serverType = nul
public static function pathToUri( $path ) {
return '/' . ltrim( str_replace( [ $_SERVER['DOCUMENT_ROOT'], '\\' ], [ '', '/' ], $path ), '/' );
}

}
42 changes: 34 additions & 8 deletions test/XSendfile/XSendfileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,42 @@ public function testLighttpd()
* @runInSeparateProcess
* @depends testChrome
*/
// public function testImageFile()
// {
public function testFile()
{
$_SERVER["HTTP_USER_AGENT"] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36';

// $_SERVER["HTTP_USER_AGENT"] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36';
ob_start();
XSendfile::xSendfile($this->file);
$output = ob_get_contents();
ob_end_clean();

// XSendfile::xSendfile($this->file);
$headers_list = xdebug_get_headers();

// $headers_list = xdebug_get_headers();
$this->assertNotEmpty($headers_list);
$this->assertContains("Content-type: text/plain;charset=UTF-8", $headers_list);
$this->assertEquals("hello\n", $output);
}

// $this->assertNotEmpty($headers_list);
// $this->assertContains("Content-type: text/plain;charset=UTF-8", $headers_list);
// }
/**
* @runInSeparateProcess
*/
public function testHttpRange()
{
$_SERVER['HTTP_RANGE'] = 'bytes=1-2';
$_SERVER["HTTP_USER_AGENT"] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36';

ob_start();
XSendfile::xSendfile($this->file);
$output = ob_get_contents();
ob_end_clean();

$headers_list = xdebug_get_headers();

// var_dump($headers_list);

$this->assertNotEmpty($headers_list);
$this->assertContains("Content-Length: 2", $headers_list);
$this->assertContains("Content-Range: bytes 1-2/6", $headers_list);
$this->assertEquals('el', $output);
}
}

0 comments on commit d5f5fbd

Please sign in to comment.