From ed3e3c12d67822fdf49dc7a23be1241647dc6c29 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Mon, 22 May 2017 13:01:38 -0400 Subject: [PATCH] More Feed tests: more cache header handling --- lib/Feed.php | 7 ++++- locale/en.php | 2 +- tests/Feed/TestFeed.php | 27 ++++++++++++++++- tests/docroot/Feed/Caching/200Future.php | 13 +++++++++ tests/docroot/Feed/Caching/200Past.php | 13 +++++++++ .../docroot/Feed/Caching/200RSS2Multiple.php | 29 +++++++++++++++++++ .../Feed/Caching/200RSS2PubDateOnly.php | 18 ++++++++++++ .../Feed/Caching/200RSS2UpdateDate.php | 19 ++++++++++++ tests/phpunit.xml | 6 ++-- tests/test | 1 + tests/test.bat | 1 + 11 files changed, 130 insertions(+), 6 deletions(-) create mode 100644 tests/docroot/Feed/Caching/200Future.php create mode 100644 tests/docroot/Feed/Caching/200Past.php create mode 100644 tests/docroot/Feed/Caching/200RSS2Multiple.php create mode 100644 tests/docroot/Feed/Caching/200RSS2PubDateOnly.php create mode 100644 tests/docroot/Feed/Caching/200RSS2UpdateDate.php diff --git a/lib/Feed.php b/lib/Feed.php index 324cbf5d..014297bd 100644 --- a/lib/Feed.php +++ b/lib/Feed.php @@ -153,9 +153,14 @@ class Feed { return $out; } - public function matchToDatabase(int $feedID): bool { + public function matchToDatabase(int $feedID = null): bool { // first perform deduplication on items $items = $this->deduplicateItems($this->data->items); + // if we haven't been given a database feed ID to check against, all items are new + if(is_null($feedID)) { + $this->newItems = $items; + return true; + } // get as many of the latest articles in the database as there are in the feed $articles = Data::$db->articleMatchLatest($feedID, sizeof($items)); // arrays holding new, edited, and tentatively new items; items may be tentatively new because we perform two passes diff --git a/locale/en.php b/locale/en.php index 137af980..aa6e1b29 100644 --- a/locale/en.php +++ b/locale/en.php @@ -96,5 +96,5 @@ return [ 'Exception.JKingWeb/Arsse/Feed/Exception.malformed' => 'Could not parse feed "{url}" because it is malformed', 'Exception.JKingWeb/Arsse/Feed/Exception.xmlEntity' => 'Refused to parse feed "{url}" because it contains an XXE attack', 'Exception.JKingWeb/Arsse/Feed/Exception.subscriptionNotFound' => 'Unable to find a feed at location "{url}"', - 'Exception.JKingWeb/Arsse/Feed/Exception.unsupportedFormat' => 'Feed "{url}" is of an unsupported format' + 'Exception.JKingWeb/Arsse/Feed/Exception.unsupportedFeedFormat' => 'Feed "{url}" is of an unsupported format' ]; \ No newline at end of file diff --git a/tests/Feed/TestFeed.php b/tests/Feed/TestFeed.php index c78181cd..ac2a9f90 100644 --- a/tests/Feed/TestFeed.php +++ b/tests/Feed/TestFeed.php @@ -19,7 +19,8 @@ class TestFeed extends \PHPUnit\Framework\TestCase { Data::$conf = new Conf(); } - function testHandleCacheHeaders() { + function testHandleCacheHeadersOn304() { + // upon 304, the client should re-use the caching header values it supplied the server $t = time(); $e = "78567a"; $f = new Feed(null, $this->base."Caching/304Random", $this->dateTransform($t, "http"), $e); @@ -35,6 +36,30 @@ class TestFeed extends \PHPUnit\Framework\TestCase { $this->assertTime($t, $f->lastModified); $this->assertSame($e, $f->resource->getETag()); } + + function testHandleCacheHeadersOn200() { + // these tests should trust the server-returned time, even in cases of obviously incorrect results + $t = time() - 2000; + $f = new Feed(null, $this->base."Caching/200Past"); + $this->assertTime($t, $f->lastModified); + $this->assertNotEmpty($f->resource->getETag()); + $t = time() - 2000; + $f = new Feed(null, $this->base."Caching/200Past", $this->dateTransform(time(), "http")); + $this->assertTime($t, $f->lastModified); + $this->assertNotEmpty($f->resource->getETag()); + $t = time() + 2000; + $f = new Feed(null, $this->base."Caching/200Future"); + $this->assertTime($t, $f->lastModified); + $this->assertNotEmpty($f->resource->getETag()); + // these tests have no HTTP headers and rely on article dates + $t = strtotime("2002-05-19T15:21:36Z"); + $f = new Feed(null, $this->base."Caching/200RSS2PubDateOnly"); + $this->assertTime($t, $f->lastModified); + $f = new Feed(null, $this->base."Caching/200RSS2UpdateDate"); + $this->assertTime($t, $f->lastModified); + $f = new Feed(null, $this->base."Caching/200RSS2Multiple"); + $this->assertTime($t, $f->lastModified); + } function testComputeNextFetchOnError() { for($a = 0; $a < 100; $a++) { diff --git a/tests/docroot/Feed/Caching/200Future.php b/tests/docroot/Feed/Caching/200Future.php new file mode 100644 index 00000000..0de46b58 --- /dev/null +++ b/tests/docroot/Feed/Caching/200Future.php @@ -0,0 +1,13 @@ + 200, + 'lastMod' => time() + 2000, + 'content' => << + + Test feed + http://example.com/ + A basic feed for testing + + +MESSAGE_BODY +]; \ No newline at end of file diff --git a/tests/docroot/Feed/Caching/200Past.php b/tests/docroot/Feed/Caching/200Past.php new file mode 100644 index 00000000..09a28c17 --- /dev/null +++ b/tests/docroot/Feed/Caching/200Past.php @@ -0,0 +1,13 @@ + 200, + 'lastMod' => time() - 2000, + 'content' => << + + Test feed + http://example.com/ + A basic feed for testing + + +MESSAGE_BODY +]; \ No newline at end of file diff --git a/tests/docroot/Feed/Caching/200RSS2Multiple.php b/tests/docroot/Feed/Caching/200RSS2Multiple.php new file mode 100644 index 00000000..c34d92a8 --- /dev/null +++ b/tests/docroot/Feed/Caching/200RSS2Multiple.php @@ -0,0 +1,29 @@ + 200, + 'cache' => false, + 'content' => << + + Test feed + http://example.com/ + A basic feed for testing + + + Sample article 1 + Sun, 18 May 1995 15:21:36 GMT + 2002-02-19T15:21:36Z + + + Sample article 2 + Sun, 19 May 2002 15:21:36 GMT + 2002-04-19T15:21:36Z + + + Sample article 3 + Sun, 18 May 2000 15:21:36 GMT + 1999-05-19T15:21:36Z + + + +MESSAGE_BODY +]; \ No newline at end of file diff --git a/tests/docroot/Feed/Caching/200RSS2PubDateOnly.php b/tests/docroot/Feed/Caching/200RSS2PubDateOnly.php new file mode 100644 index 00000000..b501a92c --- /dev/null +++ b/tests/docroot/Feed/Caching/200RSS2PubDateOnly.php @@ -0,0 +1,18 @@ + 200, + 'cache' => false, + 'content' => << + + Test feed + http://example.com/ + A basic feed for testing + + + Sample article + Sun, 19 May 2002 15:21:36 GMT + + + +MESSAGE_BODY +]; \ No newline at end of file diff --git a/tests/docroot/Feed/Caching/200RSS2UpdateDate.php b/tests/docroot/Feed/Caching/200RSS2UpdateDate.php new file mode 100644 index 00000000..6d7bc25c --- /dev/null +++ b/tests/docroot/Feed/Caching/200RSS2UpdateDate.php @@ -0,0 +1,19 @@ + 200, + 'cache' => false, + 'content' => << + + Test feed + http://example.com/ + A basic feed for testing + + + Sample article + Sun, 18 May 2002 15:21:36 GMT + 2002-05-19T15:21:36Z + + + +MESSAGE_BODY +]; \ No newline at end of file diff --git a/tests/phpunit.xml b/tests/phpunit.xml index 83fd0b38..e84dd97a 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -19,7 +19,7 @@ Lang/TestLangComplex.php Lang/TestLangErrors.php - + Conf/TestConf.php @@ -28,13 +28,13 @@ User/TestUserInternalDriver.php User/TestAuthorization.php - + Db/SQLite3/TestDbResultSQLite3.php Db/SQLite3/TestDbStatementSQLite3.php Db/SQLite3/TestDbDriverSQLite3.php Db/SQLite3/TestDbUpdateSQLite3.php - + Db/SQLite3/Database/TestDatabaseUserSQLite3.php Db/SQLite3/Database/TestDatabaseFolderSQLite3.php Db/SQLite3/Database/TestDatabaseSubscriptionSQLite3.php diff --git a/tests/test b/tests/test index 134b98d8..5695b587 100755 --- a/tests/test +++ b/tests/test @@ -1,6 +1,7 @@ #! /bin/sh base=`dirname "$0"` php -n -S localhost:8000 "$base/server.php" >/dev/null & +sleep 1s 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]+"` diff --git a/tests/test.bat b/tests/test.bat index 12854932..7e7a18a3 100644 --- a/tests/test.bat +++ b/tests/test.bat @@ -2,6 +2,7 @@ setlocal set base=%~dp0 start /b php -n -S localhost:8000 "%base%\server.php" >nul +timeout /nobreak /t 1 >nul 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 (