Skip to content

Commit

Permalink
Item10824: Implement HEAD support.
Browse files Browse the repository at this point in the history
Thanks to AndrewJones for the patch and MichaelDaum for the
cache changes.

I'm not sure the unit test is completely valid.  It passes regardless of
whether or not the patch is applied.  However the fix is definitely
working, as a HEAD request for System/Macros takes >7 seconds without it
and 500ms with it, so the early return is valuable.
  • Loading branch information
gac410 committed Aug 23, 2014
1 parent 0ddc620 commit 1bfb7b4
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 5 deletions.
69 changes: 67 additions & 2 deletions UnitTestContrib/test/unit/ViewScriptTests.pm
Expand Up @@ -45,6 +45,11 @@ $topic2rawON =~ s/>/>/g;
$topic2rawON =~ s/"/"/g;
$topic2rawON .= '</textarea>';

my $topic3 = <<'HERE';
CONTENT
* Set ALLOWTOPICVIEW = NotAUser
HERE

my $templateTopicContent1 = <<'HERE';
pretemplate%STARTTEXT%pre%TEXT%post%ENDTEXT%posttemplate
HERE
Expand Down Expand Up @@ -97,6 +102,12 @@ sub set_up {
$meta->save();
$meta->finish();

$topic = 'TestTopic3';
($meta) = Foswiki::Func::readTopic( $this->{test_web}, 'TestTopic3' );
$meta->text($topic3);
$meta->save();
$meta->finish();

($meta) = Foswiki::Func::readTopic( $this->{test_web}, 'ViewoneTemplate' );
$meta->text($templateTopicContent1);
$meta->save( user => $this->{test_user_wikiname} );
Expand Down Expand Up @@ -188,7 +199,7 @@ sub set_up {
}

sub setup_view {
my ( $this, $web, $topic, $tmpl, $raw, $ctype, $skin ) = @_;
my ( $this, $web, $topic, $tmpl, $raw, $ctype, $skin, $method ) = @_;
my $query = Unit::Request->new(
{
webName => [$web],
Expand All @@ -200,7 +211,8 @@ sub setup_view {
}
);
$query->path_info("/$web/$topic");
$query->method('POST');
$method ||= 'POST';
$query->method($method);
$this->createNewFoswikiSession( $this->{test_user_login}, $query );
my ($text) = $this->capture(
sub {
Expand Down Expand Up @@ -302,6 +314,59 @@ HERE
return;
}

# This test verifies the rendering of the HEAD
sub test_render_HEAD {
my $this = shift;
my $text;
my $hdr;
my $editUrl;

# setup_view( $this, $web, $topic, $tmpl, $raw, $ctype, $skin, $method ) = @_;

( $text, $hdr, $editUrl ) =
$this->setup_view( $this->{test_web}, 'TestTopic2', 'viewfour', '',
'text/plain', 'text', 'HEAD' );

$this->assert_matches(
qr#^Content-Type: text/plain#ms,
$hdr,
"HEAD test: contenttype=text/plain should return text/plain - got $hdr"
);
$this->assert_equals( "", $text,
"Unexpected output from HEAD Request, Text should not be returned, got: $text"
);

# Verify for a no-found topic
( $text, $hdr, $editUrl ) =
$this->setup_view( $this->{test_web}, 'AsdftTopic2', 'viewfour', '',
'text/plain', 'text', 'HEAD' );

$this->assert_equals( "", $text,
"Unexpected output from HEAD Request, Text should not be returned, got: $text"
);
$this->assert_matches( qr#^Status: 404#ms,
$hdr, "HEAD test: Not found should return 404 - got $hdr" );

# Verify for a not-authorized topic
$hdr = '';
try {
( $text, $hdr, $editUrl ) =
$this->setup_view( $this->{test_web}, 'TestTopic3', 'viewfour', '',
'text/plain', 'text', 'HEAD' );
}
catch Foswiki::AccessControlException with {
$this->assert( 1, "Successful ACL" );
};

$this->assert_equals( "", $text,
"Unexpected output from HEAD Request, Text should not be returned, got: $text"
);
$this->assert_equals( '',
$hdr, "HEAD test: Not found should throw an exception, got $hdr" );

return;
}

# This test verifies the handling of preamble (the text following
# %STARTTEXT%) and postamble (the text between %TEXT% and %ENDTEXT%).
sub test_prepostamble {
Expand Down
4 changes: 2 additions & 2 deletions core/lib/Foswiki/PageCache.pm
Expand Up @@ -386,9 +386,9 @@ sub isCacheable {

my $session = $Foswiki::Plugins::SESSION;

# POSTs aren't cacheable
# POSTs and HEADs aren't cacheable
my $method = $session->{request}->method;
$isCacheable = 0 if $method && $method eq 'POST';
$isCacheable = 0 if $method && $method =~ /^(?:POST|HEAD)$/;

if ($isCacheable) {

Expand Down
8 changes: 7 additions & 1 deletion core/lib/Foswiki/UI/View.pm
Expand Up @@ -73,6 +73,8 @@ sub view {

my $cache = $session->{cache};
my $response = $session->{response};
my $method = $session->{request}->method || '';

my $cachedPage;
$cachedPage = $cache->getPage( $web, $topic ) if $cache;
if ($cachedPage) {
Expand Down Expand Up @@ -285,10 +287,14 @@ sub view {
level => 'info',
action => 'view',
webTopic => $topicObject->web . '.' . $topicObject->topic,
extra => $logEntry,
extra => $logEntry . " ($method)"
}
);

if ( $method && $method eq 'HEAD' ) {
return $session->writeCompletePage( '', 'view', 'text/plain' );
}

# Note; must enter all contexts before the template is read, as
# TMPL:P is expanded on the fly in the template reader. :-(
my ( $revTitle, $revArg ) = ( '', '' );
Expand Down

0 comments on commit 1bfb7b4

Please sign in to comment.