diff --git a/tests/Db/SQLite3/TestDbDriverSQLite3.php b/tests/Db/SQLite3/TestDbDriverSQLite3.php index 57bc3f29..1dba8fec 100644 --- a/tests/Db/SQLite3/TestDbDriverSQLite3.php +++ b/tests/Db/SQLite3/TestDbDriverSQLite3.php @@ -11,6 +11,9 @@ class TestDbDriverSQLite3 extends \PHPUnit\Framework\TestCase { protected $ch; function setUp() { + if(!extension_loaded("sqlite3")) { + $this->markTestSkipped("SQLite extension not loaded"); + } $this->clearData(); $conf = new Conf(); $conf->dbDriver = Db\SQLite3\Driver::class; diff --git a/tests/Db/SQLite3/TestDbResultSQLite3.php b/tests/Db/SQLite3/TestDbResultSQLite3.php index 33d46e1e..73d530ba 100644 --- a/tests/Db/SQLite3/TestDbResultSQLite3.php +++ b/tests/Db/SQLite3/TestDbResultSQLite3.php @@ -9,6 +9,9 @@ class TestDbResultSQLite3 extends \PHPUnit\Framework\TestCase { protected $c; function setUp() { + if(!extension_loaded("sqlite3")) { + $this->markTestSkipped("SQLite extension not loaded"); + } $c = new \SQLite3(":memory:"); $c->enableExceptions(true); $this->c = $c; diff --git a/tests/Db/SQLite3/TestDbStatementSQLite3.php b/tests/Db/SQLite3/TestDbStatementSQLite3.php index f6accc9d..b45aaf45 100644 --- a/tests/Db/SQLite3/TestDbStatementSQLite3.php +++ b/tests/Db/SQLite3/TestDbStatementSQLite3.php @@ -11,6 +11,9 @@ class TestDbStatementSQLite3 extends \PHPUnit\Framework\TestCase { static protected $imp = Db\SQLite3\Statement::class; function setUp() { + if(!extension_loaded("sqlite3")) { + $this->markTestSkipped("SQLite extension not loaded"); + } $c = new \SQLite3(":memory:"); $c->enableExceptions(true); $this->c = $c; diff --git a/tests/Db/SQLite3/TestDbUpdateSQLite3.php b/tests/Db/SQLite3/TestDbUpdateSQLite3.php index 3fe66d71..0aee2020 100644 --- a/tests/Db/SQLite3/TestDbUpdateSQLite3.php +++ b/tests/Db/SQLite3/TestDbUpdateSQLite3.php @@ -16,6 +16,9 @@ class TestDbUpdateSQLite3 extends \PHPUnit\Framework\TestCase { const MINIMAL2 = "pragma user_version=2"; function setUp() { + if(!extension_loaded("sqlite3")) { + $this->markTestSkipped("SQLite extension not loaded"); + } $this->clearData(); $this->vfs = vfsStream::setup("schemata", null, ['SQLite3' => []]); $conf = new Conf(); diff --git a/tests/Feed/TestFeed.php b/tests/Feed/TestFeed.php index 5e4d1f6c..c78181cd 100644 --- a/tests/Feed/TestFeed.php +++ b/tests/Feed/TestFeed.php @@ -8,19 +8,10 @@ class TestFeed extends \PHPUnit\Framework\TestCase { use Test\Tools; protected static $host = "http://localhost:8000/"; - protected static $serverUp = true; protected $base = ""; - - function time(string $t): string { - return gmdate("D, d M Y H:i:s \G\M\T", strtotime($t)); - } - - static function setUpBeforeClass() { - if(!@file_get_contents(self::$host."IsUp")) self::$serverUp = false; - } function setUp() { - if(!self::$serverUp) { + if(!@file_get_contents(self::$host."IsUp")) { $this->markTestSkipped("Test Web server is not accepting requests"); } $this->base = self::$host."Feed/"; @@ -28,6 +19,23 @@ class TestFeed extends \PHPUnit\Framework\TestCase { Data::$conf = new Conf(); } + function testHandleCacheHeaders() { + $t = time(); + $e = "78567a"; + $f = new Feed(null, $this->base."Caching/304Random", $this->dateTransform($t, "http"), $e); + $this->assertTime($t, $f->lastModified); + $this->assertSame($e, $f->resource->getETag()); + $f = new Feed(null, $this->base."Caching/304ETagOnly", $this->dateTransform($t, "http"), $e); + $this->assertTime($t, $f->lastModified); + $this->assertSame($e, $f->resource->getETag()); + $f = new Feed(null, $this->base."Caching/304LastModOnly", $this->dateTransform($t, "http"), $e); + $this->assertTime($t, $f->lastModified); + $this->assertSame($e, $f->resource->getETag()); + $f = new Feed(null, $this->base."Caching/304None", $this->dateTransform($t, "http"), $e); + $this->assertTime($t, $f->lastModified); + $this->assertSame($e, $f->resource->getETag()); + } + function testComputeNextFetchOnError() { for($a = 0; $a < 100; $a++) { if($a < 3) { diff --git a/tests/docroot/Feed/Caching/304ETagOnly.php b/tests/docroot/Feed/Caching/304ETagOnly.php new file mode 100644 index 00000000..2b482dd8 --- /dev/null +++ b/tests/docroot/Feed/Caching/304ETagOnly.php @@ -0,0 +1,7 @@ + 304, + 'cache' => false, + 'fields' => [ + "ETag: ".$_SERVER['HTTP_IF_NONE_MATCH'], + ], +]; \ No newline at end of file diff --git a/tests/docroot/Feed/Caching/304LastModOnly.php b/tests/docroot/Feed/Caching/304LastModOnly.php new file mode 100644 index 00000000..444ccdbc --- /dev/null +++ b/tests/docroot/Feed/Caching/304LastModOnly.php @@ -0,0 +1,7 @@ + 304, + 'cache' => false, + 'fields' => [ + 'Last-Modified: '.$_SERVER['HTTP_IF_MODIFIED_SINCE'], + ], +]; \ No newline at end of file diff --git a/tests/docroot/Feed/Caching/304None.php b/tests/docroot/Feed/Caching/304None.php new file mode 100644 index 00000000..f8dcef02 --- /dev/null +++ b/tests/docroot/Feed/Caching/304None.php @@ -0,0 +1,4 @@ + 304, + 'cache' => false, +]; \ No newline at end of file diff --git a/tests/docroot/Feed/Caching/304Random.php b/tests/docroot/Feed/Caching/304Random.php new file mode 100644 index 00000000..c01917e3 --- /dev/null +++ b/tests/docroot/Feed/Caching/304Random.php @@ -0,0 +1,7 @@ + 304, + 'lastMod' => random_int(0,2^31), + 'fields' => [ + "ETag: ".bin2hex(random_bytes(8)), + ], +]; \ No newline at end of file diff --git a/tests/lib/Database/DriverSQLite3.php b/tests/lib/Database/DriverSQLite3.php index d1e9fa31..b78fefdc 100644 --- a/tests/lib/Database/DriverSQLite3.php +++ b/tests/lib/Database/DriverSQLite3.php @@ -6,6 +6,9 @@ use JKingWeb\Arsse\Db\SQLite3\Driver; trait DriverSQLite3 { function setUpDriver() { + if(!extension_loaded("sqlite3")) { + $this->markTestSkipped("SQLite extension not loaded"); + } Data::$conf->dbSQLite3File = ":memory:"; $this->drv = new Driver(true); } diff --git a/tests/server.php b/tests/server.php index c80b14d8..9f4b062e 100644 --- a/tests/server.php +++ b/tests/server.php @@ -54,7 +54,7 @@ http_response_code((int) $response['code']); // if the response has a body, set the content type and (possibly) the ETag. if(strlen($response['content'])) { header("Content-Type: ".$response['mime']); - if($response['cache']) header("ETag: ".md5($response['content'])); + if($response['cache']) header('ETag: "'.md5($response['content']).'"'); } // if caching is enabled, set the last-modified date if($response['cache']) header("Last-Modified: ".gmdate("D, d M Y H:i:s \G\M\T", $response['lastMod'])); diff --git a/tests/test b/tests/test index 4e18ac21..134b98d8 100755 --- a/tests/test +++ b/tests/test @@ -1,7 +1,7 @@ #! /bin/sh base=`dirname "$0"` php -n -S localhost:8000 "$base/server.php" >/dev/null & -php "$base/../vendor/phpunit/phpunit/phpunit" -c "$base/phpunit.xml" +php "$base/../vendor/phpunit/phpunit/phpunit" -c "$base/phpunit.xml" $* sleep 1s pid=`lsof -n -i:8000 | grep -Eo "php\s+[0-9]+" | grep -Eo "[0-9]+"` kill $pid \ No newline at end of file diff --git a/tests/test.bat b/tests/test.bat index 21ff7fd4..12854932 100644 --- a/tests/test.bat +++ b/tests/test.bat @@ -2,7 +2,7 @@ setlocal set base=%~dp0 start /b php -n -S localhost:8000 "%base%\server.php" >nul -php "%base%\..\vendor\phpunit\phpunit\phpunit" -c "%base%\phpunit.xml" +php "%base%\..\vendor\phpunit\phpunit\phpunit" -c "%base%\phpunit.xml" %* timeout /nobreak /t 1 >nul for /f "usebackq tokens=5" %%a in (`netstat -aon ^| find "LISTENING" ^| find ":8000"`) do ( taskkill /f /pid %%a >nul