summaryrefslogtreecommitdiff
path: root/compojure-3.2/test/compojure/http/routes_test.clj
blob: 7d9b936139a50a69265104294f642bf8f6e1f335 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
(ns compojure.http.routes-test
  (:use compojure.http.routes
        clojure.contrib.test-is
        test.helpers))

(deftest fixed-path
  (are (match-uri (compile-uri-matcher _1) _1)
    "/"
    "/foo"
    "/foo/bar"
    "/foo/bar.html"))

(deftest nil-paths
  (is (match-uri (compile-uri-matcher "/") nil)))

(deftest keyword-paths
  (are (= (match-uri (compile-uri-matcher _1) _2) _3)
    "/:x"       "/foo"     {:x "foo"}
    "/foo/:x"   "/foo/bar" {:x "bar"}
    "/a/b/:c"   "/a/b/c"   {:c "c"}
    "/:a/b/:c"  "/a/b/c"   {:a "a", :c "c"}))

(deftest keywords-match-extensions
  (are (= (match-uri (compile-uri-matcher _1) _2) _3)
    "/foo.:ext" "/foo.txt" {:ext "txt"}
    "/:x.:y"    "/foo.txt" {:x "foo", :y "txt"}))

(deftest hyphen-keywords
  (are (= (match-uri (compile-uri-matcher _1) _2) _3)
    "/:foo-bar" "/baz" {:foo-bar "baz"}
    "/:foo-"    "/baz" {:foo- "baz"}))

(deftest urlencoded-keywords
  (are (= (match-uri (compile-uri-matcher _1) _2) _3)
    "/:x" "/foo%20bar" {:x "foo bar"}
    "/:x" "/foo+bar"   {:x "foo bar"}))

(deftest same-keyword-many-times
  (are (= (match-uri (compile-uri-matcher _1) _2) _3)
    "/:x/:x/:x" "/a/b/c" {:x ["a" "b" "c"]}
    "/:x/b/:x"  "/a/b/c" {:x ["a" "c"]}))

(deftest wildcard-paths
  (are (= (match-uri (compile-uri-matcher _1) _2) _3)
    "/*"     "/foo"         {:* "foo"}
    "/*"     "/foo.txt"     {:* "foo.txt"}
    "/*"     "/foo/bar"     {:* "foo/bar"}
    "/foo/*" "/foo/bar/baz" {:* "bar/baz"}
    "/a/*/d" "/a/b/c/d"     {:* "b/c"}))

(deftest url-paths
  (is (match-uri (compile-uri-matcher "http://localhost")
                 "http://localhost")))

(deftest url-port-paths
  (let [matcher (compile-uri-matcher "localhost:8080")]
    (is (match-uri matcher "localhost:8080"))
    (is (not (match-uri matcher "localhost:7070")))))

(deftest unmatched-paths
  (is (nil? (match-uri (compile-uri-matcher "/foo") "/bar"))))

(deftest regex-paths
  (is (match-uri #"/[A-Z][a-z]" "/Ab"))
  (is (not (match-uri  #"/[A-Z][a-z]" "/ab"))))

(deftest regex-path-params
  (are (= (match-uri _1 _2) _3)
    #"/foo/(\w+)"   "/foo/bar" ["bar"]
    #"/(\w+)/(\d+)" "/foo/10"  ["foo" "10"]))

(deftest assoc-route-map
  (is (= (assoc-route-params {:params {}} {"foo" "bar"})
         {:route-params {"foo" "bar"}, :params {"foo" "bar"}})))

(deftest assoc-route-vector
  (is (= (assoc-route-params {:params {}} ["foo" "bar"])
         {:route-params ["foo" "bar"], :params {}})))

(deftest route-response
  (let [route    (GET "/" "Lorem Ipsum")
        request  {:request-method :get, :uri "/"}
        response (route request)]
    (is (= response {:status 200,
                     :headers {"Content-Type" "text/html"},
                     :body "Lorem Ipsum"}))))

(defn- route-body
  [route method uri]
  (:body (route {:request-method method, :uri uri})))

(deftest route-methods
  (are (= (route-body _1 _2 "/") _3)
    (GET    "/" "a") :get    "a"
    (POST   "/" "b") :post   "b"
    (PUT    "/" "c") :put    "c"
    (HEAD   "/" "d") :head   "d"
    (DELETE "/" "e") :delete "e"))

(deftest route-any
  (are (= (route-body (ANY "/" _2) _1 "/") _2)
    :get    "a"
    :post   "b"
    :put    "c"
    :delete "d"))

(deftest route-var-paths
  (let [path "/foo/bar"]
    (is (= (route-body (GET path "pass") :get path)
           "pass"))))

(deftest route-not-match
  (let [route   (GET "/" "Lorem Ipsum")
        request {:request-method :get, :uri "/foo"}]
    (is (nil? (route request)))))

(deftest route-match-form-method
  (let [routes  (routes (DELETE "/foo" "body"))
        request {:request-method :post
                 :uri            "/foo"
                 :content-type   "application/x-www-form-urlencoded"
                 :body           (input-stream "_method=DELETE&a=1")}]
    (is (= (:status (routes request))
           200))))

(deftest route-not-match-form-method
  (let [routes  (routes (DELETE "/foo" "body"))
        request {:request-method :post
                 :uri            "/foo"
                 :content-type   "application/x-www-form-urlencoded"
                 :body           (input-stream "a=1")}]
    (is (nil? (routes request)))))

(deftest route-match-form-method-not-post
  (let [routes  (routes (POST "/foo" "post") (DELETE "/foo" "delete"))
        request {:request-method :post
                 :uri            "/foo"
                 :content-type   "application/x-www-form-urlencoded",
                 :body           (input-stream "_method=DELETE&a=1")}]
    (is (= (:body (routes request))
           "delete"))))

(deftest route-keywords
  (let [route (GET "/:foo"
                (is (= (:route-params request) {:foo "bar"}))
                "")]
    (route {:request-method :get, :uri "/bar"})))

(deftest combine-routes
  (let [r1 (fn [req] (if (= (:uri req) "/") {:body "x"}))
        r2 (fn [req] {:body "y"})
        rs (routes r1 r2)]
    (is (rs {:uri "/"}) "x")
    (is (rs {:uri "/foo"}) "y")))

(deftest route-params
  (let [site (routes
               (GET "/:route"
                 (is (= (params :route) "yes"))
                 (is (= (params :query) "yes"))
                 (is (= (params :form)  "yes"))
                 (is (request :params) params)
                 :next))]
    (site (merge
            {:request-method :get
             :uri "/yes"
             :query-string "query=yes"}
            (form-request "form=yes")))))