X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fdispatch_parser.t;h=b6e03172b520b77c77e322d446b201533ef4455c;hb=run_if_script_error;hp=8b6f4ab140ca58e154035687342053cc3fdb8a7c;hpb=a96dd5cb4a8216e60a9293aa537038c237819838;p=catagits%2FWeb-Simple.git diff --git a/t/dispatch_parser.t b/t/dispatch_parser.t index 8b6f4ab..b6e0317 100644 --- a/t/dispatch_parser.t +++ b/t/dispatch_parser.t @@ -214,6 +214,30 @@ my $dp = Web::Dispatch::Parser->new; } { + my $spec = '(GET+/foo)|(POST+/foo)'; + my $nest = $dp->parse($spec); + + for my $method (qw( GET POST )) { + is_deeply( + [ $nest->({ PATH_INFO => '/foo', REQUEST_METHOD => $method }) ], + [ {} ], + "$spec matches $method /foo" + ); + is_deeply( + [ $nest->({ PATH_INFO => '/bar', REQUEST_METHOD => $method }) ], + [], + "$spec does not match $method /bar" + ); + } + + is_deeply( + [ $nest->({ PATH_INFO => '/foo', REQUEST_METHOD => 'PUT' }) ], + [], + "$spec does not match PUT /foo" + ); +} + +{ local $@; ok( !eval { $dp->parse('/foo+(GET'); 1 }, @@ -329,6 +353,12 @@ my $dp = Web::Dispatch::Parser->new; my $sub = $dp->parse($match); is_deeply( + [ $sub->({ PATH_INFO => '/foobar' }) ], + [], + "$match does not match /foobar" + ); + + is_deeply( [ $sub->({ PATH_INFO => '/foo/bar' }) ], [ { PATH_INFO => '/bar', SCRIPT_NAME => '/foo' } ], "$match matches /foo/bar and strips to /bar" @@ -364,11 +394,30 @@ my $dp = Web::Dispatch::Parser->new; } } +{ + my @named = ( + [ '/foo/*:foo_id' => '/foo/1' => { foo_id => 1 } ], + [ '/foo/:foo_id' => '/foo/1' => { foo_id => 1 } ], + [ '/foo/:id/**:rest' => '/foo/id/rest/of/the/path.ext' + => { id => 'id', rest => 'rest/of/the/path' } ], + [ '/foo/:id/**.*:rest' => '/foo/id/rest/of/the/path.ext' + => { id => 'id', rest => 'rest/of/the/path.ext' } ], + ); + foreach my $n (@named) { + is_deeply( + [ $dp->parse($n->[0])->({ PATH_INFO => $n->[1] }) ], + [ {}, $n->[2] ], + "${\$n->[0]} matches ${\$n->[1]} with correct captures" + ); + } +} + # # query string # my $q = 'foo=FOO&bar=BAR1&baz=one+two&quux=QUUX1&quux=QUUX2' + .'&foo.bar=FOOBAR1&foo.bar=FOOBAR2&foo.baz=FOOBAZ' .'&bar=BAR2&quux=QUUX3&evil=%2F'; my %all_single = ( @@ -377,6 +426,8 @@ my %all_single = ( baz => 'one two', quux => 'QUUX3', evil => '/', + 'foo.baz' => 'FOOBAZ', + 'foo.bar' => 'FOOBAR2', ); my %all_multi = ( @@ -385,6 +436,8 @@ my %all_multi = ( baz => [ 'one two' ], quux => [ qw(QUUX1 QUUX2 QUUX3) ], evil => [ '/' ], + 'foo.baz' => [ 'FOOBAZ' ], + 'foo.bar' => [ qw(FOOBAR1 FOOBAR2) ], ); foreach my $lose ('?foo=','?:foo=','?@foo=','?:@foo=') { @@ -423,6 +476,12 @@ foreach my $win ( [ '?foo=&@*' => 'FOO', \%all_multi ], [ '?:foo=&@*' => { %all_multi, foo => 'FOO' } ], [ '?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ], + [ '?foo.baz=' => 'FOOBAZ' ], + [ '?:foo.baz=' => { 'foo.baz' => 'FOOBAZ' } ], + [ '?foo.bar=' => 'FOOBAR2' ], + [ '?:foo.bar=' => { 'foo.bar' => 'FOOBAR2' } ], + [ '?@foo.bar=' => [ qw(FOOBAR1 FOOBAR2) ] ], + [ '?:@foo.bar=' => { 'foo.bar' => [ qw(FOOBAR1 FOOBAR2) ] } ], ) { my ($spec, @res) = @$win; my $match = $dp->parse($spec); @@ -474,6 +533,12 @@ foreach my $win2 ( [ '/foo/bar/+?foo=&@*' => 'FOO', \%all_multi ], [ '/foo/bar/+?:foo=&@*' => { %all_multi, foo => 'FOO' } ], [ '/foo/bar/+?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ], + [ '/foo/bar/+?foo.baz=' => 'FOOBAZ' ], + [ '/foo/bar/+?:foo.baz=' => { 'foo.baz' => 'FOOBAZ' } ], + [ '/foo/bar/+?foo.bar=' => 'FOOBAR2' ], + [ '/foo/bar/+?:foo.bar=' => { 'foo.bar' => 'FOOBAR2' } ], + [ '/foo/bar/+?@foo.bar=' => [ qw(FOOBAR1 FOOBAR2) ] ], + [ '/foo/bar/+?:@foo.bar=' => { 'foo.bar' => [ qw(FOOBAR1 FOOBAR2) ] } ], ) { my ($spec, @res) = @$win2; my $match = $dp->parse($spec); @@ -525,6 +590,12 @@ foreach my $win3 ( [ '/foo/bar+?foo=&@*' => 'FOO', \%all_multi ], [ '/foo/bar+?:foo=&@*' => { %all_multi, foo => 'FOO' } ], [ '/foo/bar+?:@bar=&*' => { %all_single, bar => [ qw(BAR1 BAR2) ] } ], + [ '/foo/bar+?foo.baz=' => 'FOOBAZ' ], + [ '/foo/bar+?:foo.baz=' => { 'foo.baz' => 'FOOBAZ' } ], + [ '/foo/bar+?foo.bar=' => 'FOOBAR2' ], + [ '/foo/bar+?:foo.bar=' => { 'foo.bar' => 'FOOBAR2' } ], + [ '/foo/bar+?@foo.bar=' => [ qw(FOOBAR1 FOOBAR2) ] ], + [ '/foo/bar+?:@foo.bar=' => { 'foo.bar' => [ qw(FOOBAR1 FOOBAR2) ] } ], ) { my ($spec, @res) = @$win3; my $match = $dp->parse($spec);