Test that (GET+/foo)|(POST+/foo) works
[catagits/Web-Simple.git] / t / dispatch_parser.t
index 6cfcb9e..b6e0317 100644 (file)
@@ -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 },
@@ -370,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 = (
@@ -383,6 +426,8 @@ my %all_single = (
   baz => 'one two',
   quux => 'QUUX3',
   evil => '/',
+  'foo.baz' => 'FOOBAZ',
+  'foo.bar' => 'FOOBAR2',
 );
 
 my %all_multi = (
@@ -391,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=') {
@@ -429,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);
@@ -480,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);
@@ -531,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);