basic named path part matching
[catagits/Web-Simple.git] / t / dispatch_parser.t
index b1bc859..7b1632b 100644 (file)
@@ -308,6 +308,52 @@ my $dp = Web::Dispatch::Parser->new;
 }
 
 {
+  my $match = '~';
+  my $sub = $dp->parse($match);
+
+  is_deeply(
+    [ $sub->({ PATH_INFO => '/foo' }) ],
+    [],
+    "$match does not match /foo"
+  );
+
+  is_deeply(
+    [ $sub->({ PATH_INFO => '' }) ],
+    [ {} ],
+    "$match matches empty path with empty env"
+  );
+}
+
+{
+  my $match = '/foo...';
+  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"
+  );
+
+  is_deeply(
+    [ $sub->({ PATH_INFO => '/foo/' }) ],
+    [ { PATH_INFO => '/', SCRIPT_NAME => '/foo' } ],
+    "$match matches /foo/ and strips to /"
+  );
+
+  is_deeply(
+    [ $sub->({ PATH_INFO => '/foo' }) ],
+    [ { PATH_INFO => '', SCRIPT_NAME => '/foo' } ],
+    "$match matches /foo and strips to empty path"
+  );
+}
+
+{
   my @dot_pairs = (
     [ '/one/*' => 'two' ],
     [ '/one/*.*' => 'two.three' ],
@@ -324,6 +370,24 @@ 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
 #