(load-relative "../uri-match.scm") (import uri-match) (use test srfi-1 data-structures uri-common) (test-group "routes creation" (let ([routes (make-routes '(((/ "foo") (GET "bar") (POST "POSTed") ((/ "bar") (GET "nested")))))]) (test-assert (lset= '(GET POST) (map car routes))) (test-group "expansion" (test-assert (lset= '(("foo") ("foo" "bar")) (map car (alist-ref 'GET routes)))) (test-assert (lset= '(("foo")) (map car (alist-ref 'POST routes))))))) (test-group "basic matching" (test "this is the body" ((uri-match 'GET "/" (make-routes '(((/ "") (GET "this is the body"))))))) (test "against the path of a uri-reference" "something!" ((uri-match 'GET (uri-reference "http://foo/bar") (make-routes '(((/ "bar") (GET "something!"))))))) (test-assert (not (uri-match 'GET "/" (make-routes '(((/ "") (POST "won't reach me"))))))) (test-group "with nesting" (test "foo" ((uri-match 'POST "/foo/bar" (make-routes '(((/ "foo") ((/ "bar") (POST "foo")))))))) (test-group "with procedure body" (test 'something ((uri-match 'GET "/me" (make-routes `(((/ "me") (GET ,(lambda () 'something)))))))) (test 100 ((uri-match 'GET "/numbers/100" (make-routes `(((/ "numbers" "(\\d+)") (GET ,(lambda (n) (string->number n))))))))))) (test-group "with capture groups" (let ([routes (make-routes `(((/ "foo") ((/ "(\\d+)") ((/ "(\\d+)") (GET ,string-append))))))]) (test "105" ((uri-match 'GET "/foo/10/5" routes))) (test-assert (not (uri-match 'GET "/foo/bar/10" routes))))) (test-group "with irregex capture groups" (let ([routes (make-routes `(((/ "foo") ((/ (submatch (+ num))) ((/ (submatch (+ num))) (GET ,string-append))))))]) (test "105" ((uri-match 'GET "/foo/10/5" routes))) (test-assert (not (uri-match 'GET "/foo/bar/10" routes))))) (test-group "with irregex named capture groups" (let ([routes (make-routes `(((/ "foo") ((/ (submatch (+ num))) ((/ (or (submatch-named b (+ num)) (submatch-named c (+ alpha)))) (GET ,(lambda (a #!key (b "b") (c "c")) (string-append a b c))))))))]) (test "105c" ((uri-match 'GET "/foo/10/5" routes))) (test "10bx" ((uri-match 'GET "/foo/10/x" routes))) (test-assert (not (uri-match 'GET "/foo/bar/10" routes)))))) (test-group "matcher" (let ([matcher (make-uri-matcher '(((/ "") (GET "is")) ((/ "this") (POST "it") ((/ "or") (PUT "what?")))))]) (test "is" ((matcher 'GET "/"))) (test "it" ((matcher 'POST "/this"))) (test "what?" ((matcher 'PUT "/this/or"))))) (test-group "precedence" (let ([matcher (make-uri-matcher '(((/ "f..") (GET "first")) ((/ "foo") (GET "second"))))]) (test "first come, first serve" "first" ((matcher 'GET "/foo")))))