This repository was archived by the owner on Feb 28, 2024. It is now read-only.
forked from weavejester/compojure
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcore_test.clj
More file actions
491 lines (435 loc) · 18.4 KB
/
core_test.clj
File metadata and controls
491 lines (435 loc) · 18.4 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
(ns compojure.core-test
(:require [clojure.test :refer :all]
[ring.mock.request :as mock]
[compojure.response :as response]
[compojure.coercions :as coercions]
[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
:compojure/full-route)
(dissoc req :params)))
nil)
req)))
(testing "vector 'x :<< coercion' arguments"
(let [req (mock/request :get "/foo/10")]
((GET "/:x/:y" [x y :<< #(Integer/parseInt %)]
(is (= x "foo"))
(is (= y 10))
nil)
req)))
(testing "nil coercions"
(is (not (nil? ((GET "/foo/:x" [x] (str x))
(mock/request :get "/foo/bar")))))
(is (not (nil? ((GET "/foo/:x" [x :<< coercions/as-int] (str x))
(mock/request :get "/foo/100")))))
(is (not (nil? ((GET "/foo/:x" [x :<< #(Boolean/valueOf %)] (str x))
(mock/request :get "/foo/false")))))
(is (nil? ((GET "/foo/:x" [x :<< coercions/as-int] (str x))
(mock/request :get "/foo/bar")))))
(testing "nil coercions in contexts"
(is (not (nil? ((context "/foo/:x" [x] (GET "/" [] (str x)))
(mock/request :get "/foo/bar")))))
(is (not (nil? ((context "/foo/:x" [x :<< coercions/as-int] (GET "/" [] (str x)))
(mock/request :get "/foo/100")))))
(is (not (nil? ((context "/foo/:x" [x :<< #(Boolean/valueOf %)] (GET "/" [] (str x)))
(mock/request :get "/foo/false")))))
(is (nil? ((context "/foo/:x" [x :<< coercions/as-int] (GET "/" [] (str x)))
(mock/request :get "/foo/bar")))))
(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) "\\?/"))))
(testing "root context"
(let [handler (context "/" []
(GET "/" [] "root")
(GET "/foo" [] "foo")
(GET "/foo/:x" [x] x))]
(are [url body] (= (:body (handler (mock/request :get url)))
body)
"/" "root"
"/foo" "foo"
"/foo/2" "2"))))
(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))))
(testing "matched route available in request"
(let [route (GET "/foo" [] "foo")
matched (atom nil)
middleware (fn [h] (fn [r] (reset! matched (:compojure/route r)) (h r)))
handler (wrap-routes route middleware)
response (handler (mock/request :get "/foo"))]
(is (= @matched [:get "/foo"]))))
(testing "nested route-middlewares are applied in order"
(let [mw (fn [handler value]
(fn [req]
(let [resp (handler (update req :stack str value))]
(update resp :body str value))))
handler (wrap-routes
(routes
(wrap-routes (GET "/foo" req (:stack req)) mw "a")
(wrap-routes (GET "/bar" req (:stack req)) mw "b"))
mw
"1")]
(is (= "1aa1" (:body (handler (mock/request :get "/foo")))))
(is (= "1bb1" (:body (handler (mock/request :get "/bar"))))))))
(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"]))
(is (= (request :compojure/full-route)
"/foo/:id"))))
(let [route (context "/foo/:foo-id" [_] (GET "/bar/:bar-id" req req))
request (route (mock/request :get "/foo/1/bar/2"))]
(testing "request has matched route information with path prefix"
(is (= (request :compojure/route)
[:get "/bar/:bar-id"]))
(is (= (request :compojure/route-context)
"/foo/:foo-id"))
(is (= (request :compojure/full-route)
"/foo/:foo-id/bar/:bar-id"))))
(let [route (context "/foo/:foo-id" [_]
(context "/bar/:bar-id" [_]
(GET "/baz/:baz-id" req req)))
request (route (mock/request :get "/foo/1/bar/2/baz/3"))]
(testing "request has matched route information with multiple path prefix"
(is (= (request :compojure/route-context)
"/foo/:foo-id/bar/:bar-id"))
(is (= (request :compojure/full-route)
"/foo/:foo-id/bar/:bar-id/baz/:baz-id")))))
(deftest route-async-test
(testing "single route"
(let [route (GET "/hello/:name" [name] (str "hello " name))]
(testing "matching request"
(let [request (mock/request :get "/hello/world")
response (promise)
exception (promise)]
(route request response exception)
(is (not (realized? exception)))
(is (= @response
{:status 200
:headers {"Content-Type" "text/html; charset=utf-8"}
:body "hello world"}))))
(testing "not-matching request"
(let [request (mock/request :get "/goodbye/world")
response (promise)
exception (promise)]
(route request response exception)
(is (not (realized? exception)))
(is (nil? @response))))))
(testing "multiple routes"
(let [route (routes
(GET "/foo" [] "foo")
(GET "/bar" [] "bar")
(GET "/baz" [] "baz"))]
(testing "matching request"
(let [request (mock/request :get "/bar")
response (promise)
exception (promise)]
(route request response exception)
(is (not (realized? exception)))
(is (= (:body @response) "bar"))))
(testing "not-matching URI"
(let [request (mock/request :get "/quz")
response (promise)
exception (promise)]
(route request response exception)
(is (not (realized? exception)))
(is (nil? @response))))
(testing "not-matching method"
(let [request (mock/request :post "/bar")
response (promise)
exception (promise)]
(route request response exception)
(is (not (realized? exception)))
(is (nil? @response))))))
(testing "rfn"
(let [route (rfn [name] (str "hello " name))
request (-> (mock/request :get "/")
(assoc :params {:name "world"}))
response (promise)
exception (promise)]
(route request response exception)
(is (not (realized? exception)))
(is (= @response
{:status 200
:headers {"Content-Type" "text/html; charset=utf-8"}
:body "hello world"}))))
(testing "context"
(let [route (context "/:name" [name]
(GET "/bar" [] (str name "bar"))
(GET "/baz" [] (str name "baz")))]
(testing "matching request"
(let [request (mock/request :get "/foo/baz")
response (promise)
exception (promise)]
(route request response exception)
(is (not (realized? exception)))
(is (= (:body @response) "foobaz"))))
(testing "not-matching request"
(let [request (mock/request :get "/foo/quz")
response (promise)
exception (promise)]
(route request response exception)
(is (not (realized? exception)))
(is (nil? @response)))))
(testing "with coercion"
(let [route (context "/:id" [id :<< coercions/as-int]
(GET "/" [] (str id)))]
(testing "matching request"
(let [request (mock/request :get "/123")
response (promise)
exception (promise)]
(route request response exception)
(is (not (realized? exception)))
(is (= (:body @response) "123"))))
(testing "not-matching request"
(let [request (mock/request :get "/foo")
response (promise)
exception (promise)]
(route request response exception)
(is (not (realized? exception)))
(is (nil? @response)))))))
(testing "wrap-routes"
(let [route (wrap-routes
(GET "/foo" [] "foo")
(fn [handler]
(fn [request respond raise]
(handler request
#(respond (assoc % ::r (:compojure/route request)))
raise))))]
(testing "matching request"
(let [request (mock/request :get "/foo")
response (promise)
exception (promise)]
(route request response exception)
(is (not (realized? exception)))
(is (= (:body @response) "foo"))
(is (= (::r @response) [:get "/foo"]))))
(testing "not-matching request"
(let [request (mock/request :get "/bar")
response (promise)
exception (promise)]
(route request response exception)
(is (not (realized? exception)))
(is (nil? @response))))))
(testing "async response"
(let [route (GET "/" [] (fn [_ respond _] (respond "foobar")))
request (mock/request :get "/")
response (promise)
exception (promise)]
(route request response exception)
(is (not (realized? exception)))
(is (= @response
{:status 200
:headers {"Content-Type" "text/html; charset=utf-8"}
:body "foobar"})))))