]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
t/www_static: ensure If-Modified-Since handling works
authorEric Wong <e@80x24.org>
Mon, 30 Sep 2024 21:30:05 +0000 (21:30 +0000)
committerEric Wong <e@80x24.org>
Wed, 2 Oct 2024 17:40:34 +0000 (17:40 +0000)
We've had If-Modified-Since to reduce client traffic for a
while, so ensure it's tested properly and continues to work
in the future.

t/www_static.t

index b1181cab8ed138b17a0df994921c92adef55f0f5..8fb86a82e815730810332913005c43029816e466 100644 (file)
@@ -109,6 +109,42 @@ $client = sub {
        IO::Uncompress::Gunzip::gunzip(\$in => \$out);
        like($out, qr/\A<html>/, 'got HTML start after gunzip');
        like($out, qr{</html>$}, 'got HTML end after gunzip');
+       unlink "$tmpdir/dir/foo.gz";
+       $get = GET('/dir/foo');
+
+       require HTTP::Date;
+       HTTP::Date->import('time2str');
+       $get->header('If-Modified-Since' => time2str(0));
+       $res = $cb->($get);
+       is $res->code, 304, '304 on If-Modified-Since hit';
+       $get->header('If-Modified-Since' => time2str(1));
+       $res = $cb->($get);
+       is $res->code, 200, '200 on If-Modified-Since miss';
+SKIP: {
+       # validating with curl ensures we didn't carry the same
+       # misunderstandings across both the code being tested
+       # and the test itself.
+       $ENV{TEST_EXPENSIVE} or
+               skip 'TEST_EXPENSIVE unset for validation w/curl', 1;
+       my $uri = $ENV{PLACK_TEST_EXTERNALSERVER_URI} or
+               skip 'curl test skipped w/o external server', 1;
+       my $dst = "$tmpdir/foo.dst";
+       my $dst2 = "$dst.2";
+       $uri .= '/dir/foo';
+       state $curl = require_cmd 'curl', 1;
+       xsys_e $curl, '-gsSfR', '-o', $dst, $uri;
+       xsys_e [ $curl, '-vgsSfR', '-o', $dst2, $uri, '-z', $dst ],
+               undef, { 2 => \(my $curl_err) };
+       like $curl_err, qr! HTTP/1\.[012] 304 !sm,
+               'got 304 response w/ If-Modified-Since';
+       is -s $dst2, undef, 'nothing downloaded on 304';
+       utime 1, 1, "$tmpdir/dir/foo";
+       xsys_e [ $curl, '-vgsSfR', '-o', $dst2, $uri, '-z', $dst ],
+               undef, { 2 => \$curl_err };
+       is -s $dst2, -s $dst, 'got 200 on If-Modified-Since mismatch';
+       like $curl_err, qr! HTTP/1\.[012] 200 !sm,
+               'got 200 response w/ If-Modified-Since';
+} # SKIP
        remove_tree "$tmpdir/dir";
 };