X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fdispatch_parser.t;h=b42bb61dbde91c08b34184af351f7fa133407e38;hb=ce69eb84aa493282c75d1be3f43c2b72d3e19aae;hp=334ceab8d81acb57b9a0a3c20dc2eec9ffa03b94;hpb=bccada1e140c0b193af37209ee5cd6584a4fea19;p=catagits%2FWeb-Simple.git diff --git a/t/dispatch_parser.t b/t/dispatch_parser.t index 334ceab..b42bb61 100644 --- a/t/dispatch_parser.t +++ b/t/dispatch_parser.t @@ -3,12 +3,12 @@ use warnings FATAL => 'all'; use Test::More qw(no_plan); -use Web::Simple::DispatchParser; +use Web::Dispatch::Parser; -my $dp = Web::Simple::DispatchParser->new; +my $dp = Web::Dispatch::Parser->new; { - my $get = $dp->parse_dispatch_specification('GET'); + my $get = $dp->parse('GET'); is_deeply( [ $get->({ REQUEST_METHOD => 'GET' }) ], @@ -23,17 +23,12 @@ my $dp = Web::Simple::DispatchParser->new; ); } -ok( - !eval { $dp->parse_dispatch_specification('GET POST'); 1; }, - "Don't yet allow two methods" -); - { - my $html = $dp->parse_dispatch_specification('.html'); + my $html = $dp->parse('.html'); is_deeply( [ $html->({ PATH_INFO => '/foo/bar.html' }) ], - [ { PATH_INFO => '/foo/bar' } ], + [ { } ], '.html matches' ); @@ -45,11 +40,11 @@ ok( } { - my $any_ext = $dp->parse_dispatch_specification('.*'); + my $any_ext = $dp->parse('.*'); is_deeply( [ $any_ext->({ PATH_INFO => '/foo/bar.html' }) ], - [ { PATH_INFO => '/foo/bar' }, 'html' ], + [ { }, 'html' ], '.html matches .* and extension returned' ); @@ -61,7 +56,7 @@ ok( } { - my $slash = $dp->parse_dispatch_specification('/'); + my $slash = $dp->parse('/'); is_deeply( [ $slash->({ PATH_INFO => '/' }) ], @@ -77,7 +72,7 @@ ok( } { - my $post = $dp->parse_dispatch_specification('/post/*'); + my $post = $dp->parse('/post/*'); is_deeply( [ $post->({ PATH_INFO => '/post/one' }) ], @@ -90,10 +85,32 @@ ok( [], '/post/one/ does not match' ); + + is_deeply( + [ $post->({ PATH_INFO => '/post/one.html' }) ], + [ {}, 'one' ], + '/post/one.html still parses out one' + ); } { - my $combi = $dp->parse_dispatch_specification('GET+/post/*'); + my $post = $dp->parse('/foo-bar/*'); + + is_deeply( + [ $post->({ PATH_INFO => '/foo-bar/one' }) ], + [ {}, 'one' ], + '/foo-bar/one parses out one' + ); + + is_deeply( + [ $post->({ PATH_INFO => '/foo-bar/one/' }) ], + [], + '/foo-bar/one/ does not match' + ); +} + +{ + my $combi = $dp->parse('GET+/post/*'); is_deeply( [ $combi->({ PATH_INFO => '/post/one', REQUEST_METHOD => 'GET' }) ], @@ -115,7 +132,7 @@ ok( } { - my $or = $dp->parse_dispatch_specification('GET|POST'); + my $or = $dp->parse('GET|POST'); foreach my $meth (qw(GET POST)) { @@ -134,7 +151,7 @@ ok( } { - my $or = $dp->parse_dispatch_specification('GET|POST|DELETE'); + my $or = $dp->parse('GET|POST|DELETE'); foreach my $meth (qw(GET POST DELETE)) { @@ -153,7 +170,7 @@ ok( } { - my $nest = $dp->parse_dispatch_specification('(GET+/foo)|POST'); + my $nest = $dp->parse('(GET+/foo)|POST'); is_deeply( [ $nest->({ PATH_INFO => '/foo', REQUEST_METHOD => 'GET' }) ], @@ -183,7 +200,7 @@ ok( { local $@; ok( - !eval { $dp->parse_dispatch_specification('/foo+(GET'); 1 }, + !eval { $dp->parse('/foo+(GET'); 1 }, 'Death with missing closing )' ); my $err = q{ @@ -199,11 +216,11 @@ ok( } { - my $not = $dp->parse_dispatch_specification('!.html+.*'); + my $not = $dp->parse('!.html+.*'); is_deeply( [ $not->({ PATH_INFO => '/foo.xml' }) ], - [ { PATH_INFO => '/foo' }, 'xml' ], + [ {}, 'xml' ], '!.html+.* matches /foo.xml' ); @@ -221,17 +238,17 @@ ok( } { - my $sub = $dp->parse_dispatch_specification('/foo/*/...'); + my $sub = $dp->parse('/foo/*/...'); is_deeply( [ $sub->({ PATH_INFO => '/foo/1/bar' }) ], - [ { PATH_INFO => '/bar' }, 1 ], + [ { PATH_INFO => '/bar', SCRIPT_NAME => '/foo/1' }, 1 ], '/foo/*/... matches /foo/1/bar and strips to /bar' ); is_deeply( [ $sub->({ PATH_INFO => '/foo/1/' }) ], - [ { PATH_INFO => '/' }, 1 ], + [ { PATH_INFO => '/', SCRIPT_NAME => '/foo/1' }, 1 ], '/foo/*/... matches /foo/1/bar and strips to /' ); @@ -242,68 +259,191 @@ ok( ); } +{ + my @dot_pairs = ( + [ '/one/*' => 'two' ], + [ '/one/*.*' => 'two.three' ], + [ '/**' => 'one/two' ], + [ '/**.*' => 'one/two.three' ], + ); + + foreach my $p (@dot_pairs) { + is_deeply( + [ $dp->parse($p->[0])->({ PATH_INFO => '/one/two.three' }) ], + [ {}, $p->[1] ], + "${\$p->[0]} matches /one/two.three and returns ${\$p->[1]}" + ); + } +} + +# +# query string +# + my $q = 'foo=FOO&bar=BAR1&baz=one+two&quux=QUUX1&quux=QUUX2' - .'&bar=BAR2&quux=QUUX3&evil=%2F'; + .'&bar=BAR2&quux=QUUX3&evil=%2F'; my %all_single = ( - foo => 'FOO', - bar => 'BAR2', - baz => 'one two', - quux => 'QUUX3', - evil => '/', + foo => 'FOO', + bar => 'BAR2', + baz => 'one two', + quux => 'QUUX3', + evil => '/', ); my %all_multi = ( - foo => [ 'FOO' ], - bar => [ qw(BAR1 BAR2) ], - baz => [ 'one two' ], - quux => [ qw(QUUX1 QUUX2 QUUX3) ], - evil => [ '/' ], + foo => [ 'FOO' ], + bar => [ qw(BAR1 BAR2) ], + baz => [ 'one two' ], + quux => [ qw(QUUX1 QUUX2 QUUX3) ], + evil => [ '/' ], ); foreach my $lose ('?foo=','?:foo=','?@foo=','?:@foo=') { - my $foo = $dp->parse_dispatch_specification($lose); + my $foo = $dp->parse($lose); + + is_deeply( + [ $foo->({ QUERY_STRING => '' }) ], + [], + "${lose} fails with no query" + ); + + is_deeply( + [ $foo->({ QUERY_STRING => 'bar=baz' }) ], + [], + "${lose} fails with query missing foo key" + ); +} - is_deeply( - [ $foo->({ QUERY_STRING => '' }) ], - [], - "${lose} fails with no query" - ); +foreach my $win ( + [ '?foo=' => 'FOO' ], + [ '?:foo=' => { foo => 'FOO' } ], + [ '?spoo~' => undef ], + [ '?:spoo~' => {} ], + [ '?@spoo~' => [] ], + [ '?:@spoo~' => { spoo => [] } ], + [ '?bar=' => 'BAR2' ], + [ '?:bar=' => { bar => 'BAR2' } ], + [ '?@bar=' => [ qw(BAR1 BAR2) ] ], + [ '?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ], + [ '?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ], + [ '?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ], + [ '?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ], + [ '?:baz=&:evil=' => { baz => 'one two', evil => '/' } ], + [ '?*' => \%all_single ], + [ '?@*' => \%all_multi ], + [ '?foo=&@*' => 'FOO', \%all_multi ], + [ '?:foo=&@*' => { %all_multi, foo => 'FOO' } ], + [ '?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ], +) { + my ($spec, @res) = @$win; + my $match = $dp->parse($spec); + #use Data::Dump::Streamer; warn Dump($match); + is_deeply( + [ $match->({ QUERY_STRING => $q }) ], + [ {}, @res ], + "${spec} matches correctly" + ); +} - is_deeply( - [ $foo->({ QUERY_STRING => 'bar=baz' }) ], - [], - "${lose} fails with query missing foo key" - ); +# +# /path/info/ + query string +# + +foreach my $lose2 ('/foo/bar/+?foo=','/foo/bar/+?:foo=','/foo/bar/+?@foo=','/foo/bar/+?:@foo=') { + my $foo = $dp->parse($lose2); + + is_deeply( + [ $foo->({ PATH_INFO => '/foo/bar/', QUERY_STRING => '' }) ], + [ ], + "${lose2} fails with no query" + ); + + is_deeply( + [ $foo->({ PATH_INFO => '/foo/bar/', QUERY_STRING => 'bar=baz' }) ], + [ ], + "${lose2} fails with query missing foo key" + ); } -foreach my $win ( - [ '?foo=' => 'FOO' ], - [ '?:foo=' => { foo => 'FOO' } ], - [ '?spoo~' => undef ], - [ '?:spoo~' => {} ], - [ '?@spoo~' => [] ], - [ '?:@spoo~' => { spoo => [] } ], - [ '?bar=' => 'BAR2' ], - [ '?:bar=' => { bar => 'BAR2' } ], - [ '?@bar=' => [ qw(BAR1 BAR2) ] ], - [ '?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ], - [ '?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ], - [ '?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ], - [ '?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ], - [ '?:baz=&:evil=' => { baz => 'one two', evil => '/' } ], - [ '?*' => \%all_single ], - [ '?@*' => \%all_multi ], - [ '?foo=&@*' => 'FOO', do { my %h = %all_multi; delete $h{foo}; \%h } ], - [ '?:foo=&@*' => { %all_multi, foo => 'FOO' } ], - [ '?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ], +foreach my $win2 ( + [ '/foo/bar/+?foo=' => 'FOO' ], + [ '/foo/bar/+?:foo=' => { foo => 'FOO' } ], + [ '/foo/bar/+?spoo~' => undef ], + [ '/foo/bar/+?:spoo~' => {} ], + [ '/foo/bar/+?@spoo~' => [] ], + [ '/foo/bar/+?:@spoo~' => { spoo => [] } ], + [ '/foo/bar/+?bar=' => 'BAR2' ], + [ '/foo/bar/+?:bar=' => { bar => 'BAR2' } ], + [ '/foo/bar/+?@bar=' => [ qw(BAR1 BAR2) ] ], + [ '/foo/bar/+?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ], + [ '/foo/bar/+?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ], + [ '/foo/bar/+?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ], + [ '/foo/bar/+?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ], + [ '/foo/bar/+?:baz=&:evil=' => { baz => 'one two', evil => '/' } ], + [ '/foo/bar/+?*' => \%all_single ], + [ '/foo/bar/+?@*' => \%all_multi ], + [ '/foo/bar/+?foo=&@*' => 'FOO', \%all_multi ], + [ '/foo/bar/+?:foo=&@*' => { %all_multi, foo => 'FOO' } ], + [ '/foo/bar/+?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ], ) { - my ($spec, @res) = @$win; - my $match = $dp->parse_dispatch_specification($spec); -#use Data::Dump::Streamer; warn Dump($match); - is_deeply( - [ $match->({ QUERY_STRING => $q }) ], - [ {}, @res ], - "${spec} matches correctly" - ); + my ($spec, @res) = @$win2; + my $match = $dp->parse($spec); + # use Data::Dump::Streamer; warn Dump($match); + is_deeply( + [ $match->({ PATH_INFO => '/foo/bar/', QUERY_STRING => $q }) ], + [ {}, @res ], + "${spec} matches correctly" + ); +} + +# +# /path/info + query string +# + +foreach my $lose3 ('/foo/bar+?foo=','/foo/bar+?:foo=','/foo/bar+?@foo=','/foo/bar+?:@foo=') { + my $foo = $dp->parse($lose3); + + is_deeply( + [ $foo->({ PATH_INFO => '/foo/bar', QUERY_STRING => '' }) ], + [ ], + "${lose3} fails with no query" + ); + + is_deeply( + [ $foo->({ PATH_INFO => '/foo/bar', QUERY_STRING => 'bar=baz' }) ], + [ ], + "${lose3} fails with query missing foo key" + ); +} + +foreach my $win3 ( + [ '/foo/bar+?foo=' => 'FOO' ], + [ '/foo/bar+?:foo=' => { foo => 'FOO' } ], + [ '/foo/bar+?spoo~' => undef ], + [ '/foo/bar+?:spoo~' => {} ], + [ '/foo/bar+?@spoo~' => [] ], + [ '/foo/bar+?:@spoo~' => { spoo => [] } ], + [ '/foo/bar+?bar=' => 'BAR2' ], + [ '/foo/bar+?:bar=' => { bar => 'BAR2' } ], + [ '/foo/bar+?@bar=' => [ qw(BAR1 BAR2) ] ], + [ '/foo/bar+?:@bar=' => { bar => [ qw(BAR1 BAR2) ] } ], + [ '/foo/bar+?foo=&@bar=' => 'FOO', [ qw(BAR1 BAR2) ] ], + [ '/foo/bar+?foo=&:@bar=' => 'FOO', { bar => [ qw(BAR1 BAR2) ] } ], + [ '/foo/bar+?:foo=&:@bar=' => { foo => 'FOO', bar => [ qw(BAR1 BAR2) ] } ], + [ '/foo/bar+?:baz=&:evil=' => { baz => 'one two', evil => '/' } ], + [ '/foo/bar+?*' => \%all_single ], + [ '/foo/bar+?@*' => \%all_multi ], + [ '/foo/bar+?foo=&@*' => 'FOO', \%all_multi ], + [ '/foo/bar+?:foo=&@*' => { %all_multi, foo => 'FOO' } ], + [ '/foo/bar+?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ], +) { + my ($spec, @res) = @$win3; + my $match = $dp->parse($spec); + # use Data::Dump::Streamer; warn Dump($match); + is_deeply( + [ $match->({ PATH_INFO => '/foo/bar', QUERY_STRING => $q }) ], + [ {}, @res ], + "${spec} matches correctly" + ); }