Navigation Menu

Skip to content

Commit

Permalink
Fix issue #7
Browse files Browse the repository at this point in the history
  • Loading branch information
byjg committed Apr 11, 2019
1 parent 382c30f commit 2df79d9
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/ServerRequestHandler.php
Expand Up @@ -233,26 +233,29 @@ public function handle($routePattern = null, $outputBuffer = true, $session = tr
&& basename($_SERVER['SCRIPT_FILENAME']) !== basename($debugBacktrace[0]['file'])
) {
$file = $_SERVER['SCRIPT_FILENAME'];
if (strpos($file, '.php') !== false) {
if (strrchr($file, '.') === ".php") {
require_once($file);
} else {
header("Content-Type: " . $this->mimeContentType($file));
if (!defined("RESTSERVER_TEST")) {
header("Content-Type: " . $this->mimeContentType($file));
}

echo file_get_contents($file);
}
return;
return true;
}

$this->process();
return $this->process();
}

/**
* Get the Mime Type based on the filename
*
* @param string $filename
* @return string
* @throws Error404Exception
*/
protected function mimeContentType($filename)
public function mimeContentType($filename)
{

$mimeTypes = array(
Expand Down Expand Up @@ -304,7 +307,11 @@ protected function mimeContentType($filename)
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
);

$ext = strtolower(array_pop(explode('.', $filename)));
if (!file_exists($filename)) {
throw new Error404Exception();
}

$ext = substr(strrchr($filename, "."), 1);
if (array_key_exists($ext, $mimeTypes)) {
return $mimeTypes[$ext];
} elseif (function_exists('finfo_open')) {
Expand Down
64 changes: 64 additions & 0 deletions tests/ServerRequestHandlerTest.php
Expand Up @@ -2,6 +2,7 @@

namespace Tests;

use ByJG\RestServer\Exception\Error404Exception;
use ByJG\RestServer\HttpRequest;
use ByJG\RestServer\HttpResponse;
use ByJG\RestServer\RoutePattern;
Expand All @@ -11,6 +12,8 @@
require __DIR__ . '/AssertHandler.php';
require __DIR__ . '/ServerRequestHandlerExposed.php';

define("RESTSERVER_TEST", "RESTSERVER_TEST");

class ServerRequestHandlerTest extends TestCase
{
/**
Expand Down Expand Up @@ -152,6 +155,38 @@ public function testHandle5()
$this->object->handle(null, false, false);
}

/**
* @throws Error404Exception
* @throws \ByJG\RestServer\Exception\ClassNotFoundException
* @throws \ByJG\RestServer\Exception\Error405Exception
* @throws \ByJG\RestServer\Exception\Error520Exception
* @throws \ByJG\RestServer\Exception\InvalidClassException
*/
public function testHandle6()
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = "file://" . __DIR__ . "/mimefiles/test.json";
$_SERVER['SCRIPT_FILENAME'] = __DIR__ . "/mimefiles/test.json";

$this->assertTrue($this->object->handle(null, false, false));
}

/**
* @throws Error404Exception
* @throws \ByJG\RestServer\Exception\ClassNotFoundException
* @throws \ByJG\RestServer\Exception\Error405Exception
* @throws \ByJG\RestServer\Exception\Error520Exception
* @throws \ByJG\RestServer\Exception\InvalidClassException
*/
public function testHandle7()
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = "file://" . __DIR__ . "/mimefiles/test.php";
$_SERVER['SCRIPT_FILENAME'] = __DIR__ . "/mimefiles/test.php";

$this->assertTrue($this->object->handle(null, false, false));
}

public function testSortPaths()
{
// Expose the method
Expand Down Expand Up @@ -248,4 +283,33 @@ public function testSortPaths()
$pathResult
);
}

public function mimeDataProvider()
{
return [
[ __DIR__ . "/mimefiles/test.json", "application/json"],
[ __DIR__ . "/mimefiles/test.pdf", "application/pdf"],
[ __DIR__ . "/mimefiles/test.png", "image/png"],
];

}

/**
* @dataProvider mimeDataProvider
* @param $entry
* @param $expected
* @throws Error404Exception
*/
public function testMimeContentType($entry, $expected)
{
$this->assertEquals($expected, $this->object->mimeContentType($entry));
}

/**
* @expectedException \ByJG\RestServer\Exception\Error404Exception
*/
public function testMimeContentTypeNotFound()
{
$this->assertEquals("", $this->object->mimeContentType("test/aaaa"));
}
}
3 changes: 3 additions & 0 deletions tests/mimefiles/test.json
@@ -0,0 +1,3 @@
{
"key": "file"
}
Binary file added tests/mimefiles/test.pdf
Binary file not shown.
3 changes: 3 additions & 0 deletions tests/mimefiles/test.php
@@ -0,0 +1,3 @@
<?php

echo "Running PHP\n";
Binary file added tests/mimefiles/test.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2df79d9

Please sign in to comment.