Browse Source

More Feed tests: cache header handling

- Also made all SQLite tests skippable if the extension is not loaded
- Fixed format of ETags in test server
- Passed any CLI arguments to PHPUnit in the test runner
microsub
J. King 7 years ago
parent
commit
ceeb23aea3
  1. 3
      tests/Db/SQLite3/TestDbDriverSQLite3.php
  2. 3
      tests/Db/SQLite3/TestDbResultSQLite3.php
  3. 3
      tests/Db/SQLite3/TestDbStatementSQLite3.php
  4. 3
      tests/Db/SQLite3/TestDbUpdateSQLite3.php
  5. 28
      tests/Feed/TestFeed.php
  6. 7
      tests/docroot/Feed/Caching/304ETagOnly.php
  7. 7
      tests/docroot/Feed/Caching/304LastModOnly.php
  8. 4
      tests/docroot/Feed/Caching/304None.php
  9. 7
      tests/docroot/Feed/Caching/304Random.php
  10. 3
      tests/lib/Database/DriverSQLite3.php
  11. 2
      tests/server.php
  12. 2
      tests/test
  13. 2
      tests/test.bat

3
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;

3
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;

3
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;

3
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();

28
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) {

7
tests/docroot/Feed/Caching/304ETagOnly.php

@ -0,0 +1,7 @@
<?php return [
'code' => 304,
'cache' => false,
'fields' => [
"ETag: ".$_SERVER['HTTP_IF_NONE_MATCH'],
],
];

7
tests/docroot/Feed/Caching/304LastModOnly.php

@ -0,0 +1,7 @@
<?php return [
'code' => 304,
'cache' => false,
'fields' => [
'Last-Modified: '.$_SERVER['HTTP_IF_MODIFIED_SINCE'],
],
];

4
tests/docroot/Feed/Caching/304None.php

@ -0,0 +1,4 @@
<?php return [
'code' => 304,
'cache' => false,
];

7
tests/docroot/Feed/Caching/304Random.php

@ -0,0 +1,7 @@
<?php return [
'code' => 304,
'lastMod' => random_int(0,2^31),
'fields' => [
"ETag: ".bin2hex(random_bytes(8)),
],
];

3
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);
}

2
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']));

2
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

2
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

Loading…
Cancel
Save