| 1 |
(ns clout.core-test |
| 2 |
(:import [clojure.lang ExceptionInfo] |
| 3 |
[java.util.regex PatternSyntaxException]) |
| 4 |
(:require [clojure.test :refer :all] |
| 5 |
[ring.mock.request :refer [request]] |
| 6 |
[clout.core :refer :all])) |
| 7 |
|
| 8 |
(deftest fixed-path |
| 9 |
(are [path] (route-matches path (request :get path)) |
| 10 |
"/" |
| 11 |
"/foo" |
| 12 |
"/foo/bar" |
| 13 |
"/foo/bar.html")) |
| 14 |
|
| 15 |
(deftest keyword-paths |
| 16 |
(are [path uri params] (= (route-matches path (request :get uri)) params) |
| 17 |
"/:x" "/foo" {:x "foo"} |
| 18 |
"/foo/:x" "/foo/bar" {:x "bar"} |
| 19 |
"/a/b/:c" "/a/b/c" {:c "c"} |
| 20 |
"/:a/b/:c" "/a/b/c" {:a "a", :c "c"})) |
| 21 |
|
| 22 |
(deftest keywords-match-extensions |
| 23 |
(are [path uri params] (= (route-matches path (request :get uri)) params) |
| 24 |
"/foo.:ext" "/foo.txt" {:ext "txt"} |
| 25 |
"/:x.:y" "/foo.txt" {:x "foo", :y "txt"})) |
| 26 |
|
| 27 |
(deftest hyphen-keywords |
| 28 |
(are [path uri params] (= (route-matches path (request :get uri)) params) |
| 29 |
"/:foo-bar" "/baz" {:foo-bar "baz"} |
| 30 |
"/:foo-" "/baz" {:foo- "baz"})) |
| 31 |
|
| 32 |
(deftest underscore-keywords |
| 33 |
(are [path uri params] (= (route-matches path (request :get uri)) params) |
| 34 |
"/:foo_bar" "/baz" {:foo_bar "baz"} |
| 35 |
"/:_foo" "/baz" {:_foo "baz"})) |
| 36 |
|
| 37 |
(deftest urlencoded-keywords |
| 38 |
(are [path uri params] (= (route-matches path (request :get uri)) params) |
| 39 |
"/:x" "/foo%20bar" {:x "foo%20bar"} |
| 40 |
"/:x" "/foo+bar" {:x "foo+bar"} |
| 41 |
"/:x" "/foo%5Cbar" {:x "foo%5Cbar"})) |
| 42 |
|
| 43 |
(deftest same-keyword-many-times |
| 44 |
(are [path uri params] (= (route-matches path (request :get uri)) params) |
| 45 |
"/:x/:x/:x" "/a/b/c" {:x ["a" "b" "c"]} |
| 46 |
"/:x/b/:x" "/a/b/c" {:x ["a" "c"]})) |
| 47 |
|
| 48 |
(deftest non-ascii-keywords |
| 49 |
(are [path uri params] (= (route-matches path (request :get uri)) params) |
| 50 |
"/:äñßOÔ" "/abc" {:äñßOÔ "abc"} |
| 51 |
"/:ÁäñßOÔ" "/abc" {:ÁäñßOÔ "abc"} |
| 52 |
"/:ä/:ش" "/foo/bar" {:ä "foo" :ش "bar"} |
| 53 |
"/:ä/:ä" "/foo/bar" {:ä ["foo" "bar"]} |
| 54 |
"/:Ä-ü" "/baz" {:Ä-ü "baz"} |
| 55 |
"/:Ä_ü" "/baz" {:Ä_ü "baz"})) |
| 56 |
|
| 57 |
(deftest wildcard-paths |
| 58 |
(are [path uri params] (= (route-matches path (request :get uri)) params) |
| 59 |
"/*" "/foo" {:* "foo"} |
| 60 |
"/*" "/foo.txt" {:* "foo.txt"} |
| 61 |
"/*" "/foo/bar" {:* "foo/bar"} |
| 62 |
"/foo/*" "/foo/bar/baz" {:* "bar/baz"} |
| 63 |
"/a/*/d" "/a/b/c/d" {:* "b/c"})) |
| 64 |
|
| 65 |
(deftest escaped-chars |
| 66 |
(are [path uri params] (= (route-matches path (request :get uri)) params) |
| 67 |
"/\\:foo" "/foo" nil |
| 68 |
"/\\:foo" "/:foo" {})) |
| 69 |
|
| 70 |
(deftest inline-regexes |
| 71 |
(are [path uri params] (= (route-matches path (request :get uri)) params) |
| 72 |
"/:x{\\d+}" "/foo" nil |
| 73 |
"/:x{\\d+}" "/10" {:x "10"} |
| 74 |
"/:x{\\d{2}}" "/2" nil |
| 75 |
"/:x{\\d{2}}" "/20" {:x "20"} |
| 76 |
"/:x{\\d}/b" "/3/b" {:x "3"} |
| 77 |
"/:x{\\d}/b" "/a/b" nil |
| 78 |
"/a/:x{\\d}" "/a/4" {:x "4"} |
| 79 |
"/a/:x{\\d}" "/a/b" nil)) |
| 80 |
|
| 81 |
(deftest compiled-routes |
| 82 |
(is (= (route-matches (route-compile "/foo/:id") (request :get "/foo/bar")) |
| 83 |
{:id "bar"}))) |
| 84 |
|
| 85 |
(deftest url-paths |
| 86 |
(is (route-matches |
| 87 |
"http://localhost/" |
| 88 |
{:scheme :http |
| 89 |
:headers {"host" "localhost"} |
| 90 |
:uri "/"})) |
| 91 |
(is (route-matches |
| 92 |
"//localhost/" |
| 93 |
{:scheme :http |
| 94 |
:headers {"host" "localhost"} |
| 95 |
:uri "/"})) |
| 96 |
(is (route-matches |
| 97 |
"//localhost/" |
| 98 |
{:scheme :https |
| 99 |
:headers {"host" "localhost"} |
| 100 |
:uri "/"}))) |
| 101 |
|
| 102 |
(deftest url-port-paths |
| 103 |
(let [req (request :get "http://localhost:8080/")] |
| 104 |
(is (route-matches "http://localhost:8080/" req)) |
| 105 |
(is (not (route-matches "http://localhost:7070/" req))))) |
| 106 |
|
| 107 |
(deftest unmatched-paths |
| 108 |
(is (nil? (route-matches "/foo" (request :get "/bar"))))) |
| 109 |
|
| 110 |
(deftest path-info-matches |
| 111 |
(is (route-matches "/bar" (-> (request :get "/foo/bar") |
| 112 |
(assoc :path-info "/bar"))))) |
| 113 |
|
| 114 |
(deftest custom-matches |
| 115 |
(let [route (route-compile "/foo/:bar" {:bar #"\d+"})] |
| 116 |
(is (not (route-matches route (request :get "/foo/bar")))) |
| 117 |
(is (not (route-matches route (request :get "/foo/1x")))) |
| 118 |
(is (route-matches route (request :get "/foo/10"))))) |
| 119 |
|
| 120 |
(deftest unused-regex-keys |
| 121 |
(is (thrown? AssertionError (route-compile "/:foo" {:foa #"\d+"}))) |
| 122 |
(is (thrown? AssertionError (route-compile "/:foo" {:foo #"\d+" :bar #".*"})))) |
| 123 |
|
| 124 |
(deftest invalid-inline-patterns |
| 125 |
(is (thrown? ExceptionInfo (route-compile "/:foo{"))) |
| 126 |
(is (thrown? ExceptionInfo (route-compile "/:foo{\\d{2}"))) |
| 127 |
(is (thrown? PatternSyntaxException (route-compile "/:foo{[a-z}")))) |