/
core_test.clj
264 lines (228 loc) · 9.62 KB
/
core_test.clj
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
(ns compojure.core-test
(:require [clojure.test :refer :all]
[ring.mock.request :as mock]
[compojure.response :as response]
[clout.core :as clout]
[compojure.core :refer :all]))
(deftest request-destructuring
(testing "vector arguments"
((GET "/foo" [x y]
(is (= x "bar"))
(is (= y "baz"))
nil)
(-> (mock/request :get "/foo")
(assoc :params {:x "bar", :y "baz"}))))
(testing "vector '& more' arguments"
((GET "/:x" [x y & more]
(is (= x "foo"))
(is (= y "bar"))
(is (= more {:z "baz"}))
nil)
(-> (mock/request :get "/foo")
(assoc :params {:y "bar", :z "baz"}))))
(testing "string parameter names"
((GET "/:x" [x y & more]
(is (= x "foo"))
(is (= y "bar"))
(is (= more {"z" "baz"}))
nil)
(-> (mock/request :get "/foo")
(assoc :params {"y" "bar", "z" "baz"}))))
(testing "vector ':as request' arguments"
(let [req (-> (mock/request :get "/foo")
(assoc :params {:y "bar"}))]
((GET "/:x" [x :as r]
(is (= x "foo"))
(is (= (dissoc r :params :route-params :compojure/route)
(dissoc req :params)))
nil)
req)))
(testing "map arguments"
((GET "/foo" {params :params}
(is (= params {:x "a", :y "b"}))
nil)
(-> (mock/request :get "/foo")
(assoc :params {:x "a", :y "b"}))))
(testing "* binding warning"
(is (= (with-out-str
(binding [*err* *out*]
(eval '(compojure.core/GET "/foo/*" [*] (str *)))))
"WARNING: * should not be used as a route binding.\n"))))
(deftest route-matching
(testing "_method parameter"
(let [req (-> (mock/request :post "/foo")
(assoc :form-params {"_method" "PUT"}))
resp {:status 200, :headers {}, :body "bar"}
route (PUT "/foo" [] resp)]
(is (= (route req) resp))))
(testing "_method parameter case-insenstive"
(let [req (-> (mock/request :post "/foo")
(assoc :form-params {"_method" "delete"}))
resp {:status 200, :headers {}, :body "bar"}
route (DELETE "/foo" [] resp)]
(is (= (route req) resp))))
(testing "_method parameter in multipart forms"
(let [req (-> (mock/request :post "/foo")
(assoc :multipart-params {"_method" "PUT"}))
resp {:status 200, :headers {}, :body "bar"}
route (PUT "/foo" [] resp)]
(is (= (route req) resp))))
(testing "HEAD requests"
(let [resp {:status 200, :headers {"X-Foo" "foo"}, :body "bar"}
route (GET "/foo" [] resp)]
(is (= (route (mock/request :head "/foo"))
(assoc resp :body nil)))))
(testing "custom regular expressions"
(let [route (GET ["/foo/:id" :id #"\d+"] [id] id)]
(is (nil? (route (mock/request :get "/foo/bar"))))
(is (nil? (route (mock/request :get "/foo/1.1"))))
(is (route (mock/request :get "/foo/10")))))
(testing "inline regular expressions"
(let [route (GET "/foo/:id{\\d+}" [id] id)]
(is (nil? (route (mock/request :get "/foo/bar"))))
(is (nil? (route (mock/request :get "/foo/1.1"))))
(is (route (mock/request :get "/foo/10"))))))
(deftest rfn-test
(testing "response rendering"
(is (= ((rfn [] "foo") (mock/request :get "/"))
(response/render "foo" {}))))
(testing "head requests"
(is (= ((rfn [] "foo") (mock/request :head "/"))
(assoc (response/render "foo" {}) :body nil))))
(testing "vector binding"
(is (= ((rfn [id] id) (assoc (mock/request :get "/") :params {"id" "bar"}))
(response/render "bar" {}))))
(testing "map binding"
(is (= ((rfn {x :x} x) (assoc (mock/request :get "/") :x "baz"))
(response/render "baz" {})))))
(deftest routing-test
(routing (mock/request :get "/bar")
(GET "/foo" [] (is false) nil)
(GET "/bar" [] (is true) nil)))
(deftest routes-test
((routes
(GET "/foo" [] (is false) nil)
(GET "/bar" [] (is true) nil))
(mock/request :get "/bar")))
(deftest context-test
(testing "keyword matching"
(let [handler (context "/foo/:id" [id] identity)]
(is (map? (handler (mock/request :get "/foo/10"))))
(is (nil? (handler (mock/request :get "/bar/10"))))))
(testing "regex matching"
(let [handler (context ["/foo/:id" :id #"\d+"] [id] identity)]
(is (map? (handler (mock/request :get "/foo/10"))))
(is (nil? (handler (mock/request :get "/foo/ab"))))))
(testing "symbol matching"
(let [path "/foo/:id"
handler (context path [id] identity)]
(is (map? (handler (mock/request :get "/foo/10"))))
(is (nil? (handler (mock/request :get "/bar/10"))))))
(testing "list matching"
(let [handler (context (str "/foo" "/:id") [id] identity)]
(is (map? (handler (mock/request :get "/foo/10"))))
(is (nil? (handler (mock/request :get "/bar/10"))))))
(testing "list with regex matching"
(let [handler (context [(str "/foo" "/:id") :id #"\d+"] [id] identity)]
(is (map? (handler (mock/request :get "/foo/10"))))
(is (nil? (handler (mock/request :get "/foo/ab"))))
(is (nil? (handler (mock/request :get "/bar/10"))))))
(testing "context key"
(let [handler (context "/foo/:id" [id] :context)]
(are [url ctx] (= (handler (mock/request :get url)) ctx)
"/foo/10" "/foo/10"
"/foo/10/bar" "/foo/10"
"/foo/10/b%20r" "/foo/10"
"/bar/10" nil)))
(testing "path-info key"
(let [handler (context "/foo/:id" [id] :path-info)]
(are [url ctx] (= (handler (mock/request :get url)) ctx)
"/foo/10" "/"
"/foo/10/bar" "/bar"
"/foo/10/b%20r" "/b%20r"
"/bar/10" nil)))
(testing "routes"
(let [handler (context "/foo/:id" [id]
(GET "/" [] "root")
(GET "/id" [] id)
(GET "/x/:x" [x] x))]
(are [url body] (= (:body (handler (mock/request :get url)))
body)
"/foo/10" "root"
"/foo/10/" "root"
"/foo/10/id" "10"
"/foo/1/x/2" "2")))
(testing "url decoding"
(let [handler (GET "/ip/:ip" [ip] ip)
cxt-handler (context "/ip/:ip" [ip] (GET "/" [] ip))
in-cxt-handler (context "/ip" [] (GET "/:ip" [ip] ip))
request (mock/request :get "/ip/0%3A0%3A0%3A0%3A0%3A0%3A0%3A1%250") ]
(is (= (-> request handler :body) "0:0:0:0:0:0:0:1%0"))
(is (= (-> request cxt-handler :body) "0:0:0:0:0:0:0:1%0"))
(is (= (-> request in-cxt-handler :body) "0:0:0:0:0:0:0:1%0"))))
(testing "url decoding with sensitive characters"
(let [handler (GET "/emote/:emote" [emote] emote)
cxt-handler (context "/emote/:emote" [emote] (GET "/" [] emote))
in-cxt-handler (context "/emote" [] (GET "/:emote" [emote] emote))
request (mock/request :get "/emote/%5C%3F%2F") ]
(is (= (-> request handler :body) "\\?/"))
(is (= (-> request cxt-handler :body) "\\?/"))
(is (= (-> request in-cxt-handler :body) "\\?/")))))
(deftest let-routes-test
(let [handler (let-routes [a "foo", b "bar"]
(GET "/foo" [] a)
(GET "/bar" [] b))]
(are [url body] (= (:body (handler (mock/request :get url))) body)
"/foo" "foo"
"/bar" "bar")))
(deftest make-route-test
(let [route (clout/route-compile "/foo/:id")
handler (make-route :get route #(-> % :params :id))]
(are [method url body] (= (:body (handler (mock/request method url))) body)
:get "/foo/10" "10"
:post "/foo/10" nil
:get "/bar/10" nil
:get "/foo" nil)))
(deftest route-middleware-test
(let [route (GET "/foo" [] "foo")
middleware (fn [_ s] #(response/render s %))
handler (wrap-routes route middleware "bar")]
(testing "route doesn't match"
(is (nil? (handler (mock/request :get "/bar")))))
(testing "route matches"
(is (= (:body (handler (mock/request :get "/foo"))) "bar"))))
(let [route (routes
(GET "/foo" [] "foo")
(GET "/bar" [] "bar"))
middleware (fn [_ s] #(response/render s %))
handler (wrap-routes route middleware "baz")]
(testing "combined routes don't match"
(is (nil? (handler (mock/request :get "/baz")))))
(testing "combined routes match"
(is (= (:body (handler (mock/request :get "/foo"))) "baz"))
(is (= (:body (handler (mock/request :get "/bar"))) "baz"))))
(testing "multiple middleware"
(let [route (GET "/" req (str (::a req) (::b req)))
mw-foo (fn [h] (fn [r] (h (assoc r ::a "foo"))))
mw-bar (fn [h] (fn [r] (h (assoc r ::b "bar"))))
handler (-> route (wrap-routes mw-foo) (wrap-routes mw-bar))]
(is (= (:body (handler (mock/request :get "/"))) "foobar"))))
(testing "middleware setup only once"
(let [counter (atom 0)
middleware (fn [h] (swap! counter inc) h)
route (GET "/foo" [] "foo")
handler (wrap-routes route middleware)]
(dotimes [_ 10]
(handler (mock/request :get "/foo")))
(is (= @counter 1)))))
(deftest route-information-test
(let [route (GET "/foo/:id" req req)
request (route (mock/request :get "/foo/1"))]
(testing "request has matched route information"
(is (= (request :compojure/route)
[:get "/foo/:id"]))))
(let [route (ANY "/foo/:id" req req)
request (route (mock/request :post "/foo/1" {}))]
(testing "ANY request has matched route information"
(is (= (request :compojure/route)
[:any "/foo/:id"])))))