From 07309ad3fbc3f3d29b0da8921716392b8e593eb0 Mon Sep 17 00:00:00 2001 From: Don Armstrong Date: Thu, 9 Jun 2016 21:04:49 -0700 Subject: [PATCH] test for Etag support in bugreport --- Debbugs/CGI.pm | 2 +- cgi/bugreport.cgi | 6 ++---- t/07_bugreport.t | 40 +++++++++++++++++++++++++++++++++------- 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/Debbugs/CGI.pm b/Debbugs/CGI.pm index e4681bd..34646b8 100644 --- a/Debbugs/CGI.pm +++ b/Debbugs/CGI.pm @@ -959,7 +959,7 @@ sub calculate_etags { ); my @additional_data = @{$param{additional_data}}; for my $file (@{$param{files}}) { - my $st = File::stat($file) or warn "Unable to stat $file:: $!"; + my $st = stat($file) or warn "Unable to stat $file:: $!"; push @additional_data,$st->mtime; push @additional_data,$st->size; } diff --git a/cgi/bugreport.cgi b/cgi/bugreport.cgi index 86152f9..4cdf5a1 100755 --- a/cgi/bugreport.cgi +++ b/cgi/bugreport.cgi @@ -15,11 +15,11 @@ use MIME::Decoder; use IO::Scalar; use IO::File; -use Debbugs::Config qw(:globals :text); +use Debbugs::Config qw(:globals :text :config); # for read_log_records use Debbugs::Log qw(:read); -use Debbugs::CGI qw(:url :html :util); +use Debbugs::CGI qw(:url :html :util :cache); use Debbugs::CGI::Bugreport qw(:all); use Debbugs::Common qw(buglog getmaintainers make_list bug_status); use Debbugs::Packages qw(getpkgsrc); @@ -224,7 +224,6 @@ if ( $mbox ) { -cache_control => 'public, max-age=600', -etag => $etag, content_disposition => qq(attachment; filename="bug_${ref}.mbox"), - (length $mtime)?(-last_modified => $mtime):(), ); } else { @@ -233,7 +232,6 @@ if ( $mbox ) { -cache_control => 'public, max-age=86400', -etag => $etag, content_disposition => qq(attachment; filename="bug_${ref}_message_${msg_num}.mbox"), - (length $mtime)?(-last_modified => $mtime):(), ); } if ($mbox_status_message and @records > 1) { diff --git a/t/07_bugreport.t b/t/07_bugreport.t index 78d89b1..5dfca05 100644 --- a/t/07_bugreport.t +++ b/t/07_bugreport.t @@ -1,7 +1,7 @@ # -*- mode: cperl;-*- -use Test::More tests => 16; +use Test::More tests => 18; use warnings; use strict; @@ -19,6 +19,7 @@ use Cwd qw(getcwd); use Debbugs::MIME qw(create_mime_message); use File::Basename qw(dirname basename); use Test::WWW::Mechanize; +use HTTP::Status qw(RC_NOT_MODIFIED); # The test functions are placed here to make things easier use lib qw(t/lib); use DebbugsTest qw(:all); @@ -58,11 +59,27 @@ EOF # start up an HTTP::Server::Simple my $bugreport_cgi_handler = sub { - # I do not understand why this is necessary. - $ENV{DEBBUGS_CONFIG_FILE} = "$config{config_dir}/debbugs_config"; - my $content = qx(perl -I. -T cgi/bugreport.cgi); - $content =~ s/^\s*Content-Type:[^\n]+\n*//si; - print $content; + # I do not understand why this is necessary. + $ENV{DEBBUGS_CONFIG_FILE} = "$config{config_dir}/debbugs_config"; + my $fh; + open($fh,'-|',-e './cgi/version.cgi'? 'perl -I. -T ./cgi/bugreport.cgi' : 'perl -I. -T ../cgi/bugreport.cgi'); + my $headers; + my $status = 200; + while (<$fh>) { + if (/^\s*$/ and $status) { + print "HTTP/1.1 $status OK\n"; + print $headers; + $status = 0; + print $_; + } elsif ($status) { + $headers .= $_; + if (/^Status:\s*(\d+)/i) { + $status = $1; + } + } else { + print $_; + } + } }; my $port = 11342; @@ -76,6 +93,11 @@ $mech->get_ok('http://localhost:'.$port.'/?bug=1', 'Page received ok'); ok($mech->content() =~ qr/\\#1.+Submitting a bug/i, 'Title of bug is submitting a bug'); +my $etag = $mech->response->header('Etag'); +$mech->get('http://localhost:'.$port.'/?bug=1', + 'If-None-Match' => $etag); +is($mech->res->code, RC_NOT_MODIFIED, + 'Not modified when the same ETag sent for bug'); $mech->get_ok('http://localhost:'.$port.'/?bug=1;mbox=yes', 'Page received ok'); @@ -86,9 +108,13 @@ ok($mech->content() =~ qr/^From /m, $mech->get_ok('http://localhost:'.$port.'/?bug=1;mboxmaint=yes', 'Page received ok'); -print STDERR $mech->content(); ok($mech->content() !~ qr/[\x01\x02\x03\x05\x06\x07]/i, 'No unescaped states'); +$etag = $mech->response->header('Etag'); +$mech->get('http://localhost:'.$port.'/?bug=1;mboxmaint=yes', + 'If-None-Match' => $etag); +is($mech->res->code, RC_NOT_MODIFIED, + 'Not modified when the same ETag sent for bug maintmbox'); # now test the output of some control commands my @control_commands = -- 2.39.2