Skip to content
This repository has been archived by the owner on Sep 10, 2024. It is now read-only.

Commit

Permalink
fix: fix request test
Browse files Browse the repository at this point in the history
  • Loading branch information
albertcht committed Sep 10, 2021
1 parent e6e5e36 commit 4c5fe33
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
10 changes: 5 additions & 5 deletions src/Transformers/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,27 +183,27 @@ public static function handleStatic($swooleRequest, $swooleResponse, string $pub
{
$uri = $swooleRequest->server['request_uri'] ?? '';
$extension = strtok(pathinfo($uri, PATHINFO_EXTENSION), '?');
$fileName = @realpath($publicPath . $uri);
$filePath = @realpath($publicPath . $uri);

if (!$fileName) {
if (!$filePath) {
return false;
}

if ($extension && in_array($extension, static::EXTENSION_BLACKLIST)) {
return false;
}

if (substr($fileName, 0, strlen($publicPath)) != $publicPath) {
if (substr($filePath, 0, strlen($publicPath)) != $publicPath) {
return false;
}

if (! is_file($fileName) || ! filesize($fileName)) {
if (! is_file($filePath) || ! filesize($filePath)) {
return false;
}

$swooleResponse->status(IlluminateResponse::HTTP_OK);
$swooleResponse->header('Content-Type', MimeType::get($extension));
$swooleResponse->sendfile($fileName);
$swooleResponse->sendfile($filePath);

return true;
}
Expand Down
17 changes: 12 additions & 5 deletions tests/Transformers/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ public function testToIlluminate()

public function testHandleStatic()
{
$realPath = false;
$this->mockMethod('realpath', function () use (&$realPath) {
$realPath = true;
return '/foo.css';
});

$isFile = false;
$this->mockMethod('is_file', function () use (&$isFile) {
return $isFile = true;
Expand All @@ -50,11 +56,12 @@ public function testHandleStatic()
->with('Content-Type', 'text/css')
->once();
$response->shouldReceive('sendfile')
->with('/foo.bar')
->with('/foo.css')
->once();

Request::handleStatic(new SwooleRequestStub, $response, '/');

$this->assertTrue($realPath);
$this->assertTrue($isFile);
$this->assertTrue($fileSize);
}
Expand All @@ -70,16 +77,16 @@ public function testHandleStaticWithBlackList()

public function testHandleStaticWithNoneFile()
{
$isFile = false;
$this->mockMethod('is_file', function () use (&$isFile) {
$isFile = true;
$realPath = false;
$this->mockMethod('realpath', function () use (&$realPath) {
$realPath = true;

return false;
});

$result = Request::handleStatic(new SwooleRequestStub, null, '/');
$this->assertFalse($result);
$this->assertTrue($isFile);
$this->assertTrue($realPath);
}

protected function mockMethod($name, \Closure $function, $namespace = null)
Expand Down

0 comments on commit 4c5fe33

Please sign in to comment.