Skip to content

Commit

Permalink
Item14033: Add unit tests, and address loss of "dot" as path delimiters.
Browse files Browse the repository at this point in the history
MichaelDaum pointed out that "dot" is fully acceptable in a URL as a
web/subweb separator instead of the slash.  Added unit tests to verify
this.  Still needs tests for Attachment, Rest and JSON type requests.
  • Loading branch information
gac410 committed Jun 28, 2016
1 parent 8541bf7 commit 788f884
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 20 deletions.
65 changes: 65 additions & 0 deletions UnitTestContrib/test/unit/RequestTests.pm
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,71 @@ EOF
);
}

sub test_Request_parse {
my $this = shift;
my @paths = my @comparisons = (

#Query Path Params, Web topic, invalidWeb, invalidTopic
[ '/', undef, '', undef, undef, undef ],
[ '/', { topic => 'Main.WebHome' }, 'Main', 'WebHome', undef, undef ],

# topic= overrides any pathinfo
[
'/Foo/Bar', { topic => 'Main.WebHome' },
'Main', 'WebHome',
undef, undef
],

# defaultweb is not processed by the request object, so web is unset by the Request.
[
'/', { defaultweb => 'Sandbox', topic => 'WebHome' },
'', 'WebHome', undef, undef
],

[ '/Main/WebHome', undef, 'Main', 'WebHome', undef, undef ],
[ '//Main//WebHome', undef, 'Main', 'WebHome', undef, undef ],
[ '/Main..WebHome', undef, 'Main', 'WebHome', undef, undef ],
[ '/blah/asdf', undef, undef, undef, 'blah', undef ],
[ '/Main.WebHome', undef, 'Main', 'WebHome', undef, undef ],
[ '/Web/SubWeb.WebHome', undef, 'Web/SubWeb', 'WebHome', undef, undef ],
[ '/Web/SubWeb/WebHome', undef, 'Web/SubWeb', 'WebHome', undef, undef ],
[ '/Web.Subweb.WebHome', undef, 'Web/Subweb', 'WebHome', undef, undef ],
[
'/Web.Subweb.Webhome/', undef, 'Web/Subweb/Webhome', undef,
undef, undef
],
[ '/3#/blah', undef, undef, undef, '3#', undef ],
[ '/Web.a<script>lah', undef, undef, undef, undef, 'a<script>lah' ],

# This next one works because of auto fix-up of lower case topic name
[ '/Blah/asdf', undef, 'Blah', 'Asdf', undef, undef ],
);
my $tn = 0;
foreach my $set (@paths) {
$tn++;
my $req = new Foswiki::Request( $set->[1] );
$req->pathInfo( $set->[0] );
$this->createNewFoswikiSession( 'AdminUser', $req );

#print STDERR $req->pathInfo() . " web "
# . ( ( defined $req->web() ) ? $req->web() : 'undef' )
# . " topic "
# . ( ( defined $req->topic() ) ? $req->topic() : 'undef' ) . "\n";

$this->assert_str_equals( $set->[0], $req->pathInfo,
"Test $tn: Wrong pathInfo value" );
$this->assert_equals( $set->[2], $req->web(),
"Test $tn: Incorrect web returned" );
$this->assert_equals( $set->[3], $req->topic(),
"Test $tn: Incorrect topic returned" );

$this->assert_equals( $set->[4], $req->invalidWeb(),
"Test $tn: Unexpected invalid web" );
$this->assert_equals( $set->[5], $req->invalidTopic(),
"Test $tn: Unexpected invalid topic" );
}
}

sub test_action {
my $this = shift;
my $req = new Foswiki::Request("");
Expand Down
26 changes: 6 additions & 20 deletions core/lib/Foswiki/Request.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,11 @@ sub parse {
print STDERR "Processing path ($query_path)\n" if TRACE;
my $topic_flag;

$query_path =~ s{/+}{/}g; # Remove duplicate slashes
$query_path =~ s{^/+}{/}g; # Remove duplicate leading slashes

# SMELL: The leading slash is *always* present in the pathInfo, but should
# not be there in the topic=blah query param. So if the leading slash is missing,
# then we assume we are parsing a topic= parameter, and not the URI.

if ( index( $query_path, '/' ) == 0 ) {
substr $query_path, 0, 1, ""; # remove first character
Expand All @@ -1026,25 +1030,7 @@ sub parse {
# trailingSlash Flag - hint that you want the web even if the topic exists
my $trailingSlash = ( $query_path =~ s/\/$// );

# Try the simple, split on dot, maybe it will work.
my ( $tweb, $ttopic ) = split( /\./, $query_path );
if ( defined $ttopic ) {

my $web = Foswiki::Sandbox::untaint( $tweb,
\&Foswiki::Sandbox::validateWebName );

my $topic = Foswiki::Sandbox::untaint( $ttopic,
\&Foswiki::Sandbox::validateTopicName );

my $resp = { web => $web, topic => $topic };
$resp->{invalidWeb} = $tweb unless defined $web;
$resp->{invalidTopic} = $ttopic unless defined $topic;

print STDERR Data::Dumper::Dumper( \$resp ) if TRACE;
return $resp;
}

my @parts = split( /\//, $query_path ); # split the path
my @parts = split( /[\/.]+/, $query_path ); # split the path, dot or slash

# Single component. It's a web unless the $topic_flag is set.
if ( scalar(@parts) eq 1 ) {
Expand Down

0 comments on commit 788f884

Please sign in to comment.