blob: b829cec3498b740bf985cdaaf5420584b3715abe (
about) (
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 abra.core-test
(:use clojure.test
abra.core
[compojure.core :only [GET]]))
(defn make-request [handler uri]
;; Technically most of this map is required by the ring spec
;; I'm fairly sure we could get away with not having most of it
(-> {:request-method :get
:scheme :http
:uri uri
:remote-addr "127.0.0.1"
:server-port 8080
:server-name "something"}
handler
:body))
(deftest test-basic-route
(let [handler (-> (register-route :user
(GET "/succeed" [id] (url-for :user 10)))
wrap-reverse-routing)]
(is (= (make-request handler "/succeed") "/user/10"))))
(-> (routes
(register-route :user
(GET "/user/:id" [id] (str "user " id)))
(GET "/succeed" [] (url-for :user 10))
(GET "/fail1" [] (url-for :user))
(GET "/fail2" [] (url-for :user 10 20)))
wrap-reverse-routing
(make-request "/succeed"))
(deftest test-basic-route
(let [handler (-> (routes
(register-route :user
(GET "/user/:id" [id] (str "user " id)))
(GET "/succeed" [] (url-for :user 10))
(GET "/fail1" [] (url-for :user))
(GET "/fail2" [] (url-for :user 10 20)))
wrap-reverse-routing)]
(is (= (make-request handler "/succeed") "/user/10"))
(is (nil? (make-request handler "/fail1")))
(is (nil? (make-request handler "/fail2")))))
(-> (routes
(register-route :user
(GET "/user/" [id] (str "user list")))
(context "/user/:id" []
(register-route :user
(GET "/" [id] (str "user " id)))
(register-route :edit-user
(GET "/edit" [id] (str "edit user " id))))
(GET "/succeed1" [] (url-for :user))
(GET "/succeed2" [] (url-for :user 10))
(GET "/succeed3" [] (url-for :edit-user 10))
(GET "/fail1" [] (url-for :user 10 20))
(GET "/fail2" [] (url-for :edit-user))
(GET "/fail3" [] (url-for :edit-user 10 20)))
wrap-reverse-routing
(make-request "/fail2"))
(deftest test-context-routes
(let [handler (-> (routes
(register-route :user
(GET "/user/" [id] (str "user list")))
(context "/user/:id" []
(register-route :user
(GET "/" [id] (str "user " id)))
(register-route :edit-user
(GET "/edit" [id] (str "edit user " id))))
(GET "/succeed1" [] (url-for :user))
(GET "/succeed2" [] (url-for :user 10))
(GET "/succeed3" [] (url-for :edit-user 10))
(GET "/fail1" [] (url-for :user 10 20))
(GET "/fail2" [] (url-for :edit-user))
(GET "/fail3" [] (url-for :edit-user 10 20)))
wrap-reverse-routing)]
(is (= (make-request handler "/succeed1") "/user/"))
(is (= (make-request handler "/succeed2") "/user/10/"))
(is (= (make-request handler "/succeed3") "/user/10/edit"))
(is (nil? (make-request handler "/fail1")))
(is (nil? (make-request handler "/fail2")))
(is (nil? (make-request handler "/fail3")))))
(deftest test-context-in-context-routes
(let [handler (-> (routes
(context "/user" []
(register-route :user
(GET "/" [id] (str "user list")))
(context "/:id" [id]
(register-route :user
(GET "/" [] (str "user " id)))
(register-route :edit-user
(GET "/edit" [] (str "edit user " id)))))
(GET "/succeed1" [] (url-for :user))
(GET "/succeed2" [] (url-for :user 10))
(GET "/succeed3" [] (url-for :edit-user 10))
(GET "/fail1" [] (url-for :user 10 20))
(GET "/fail2" [] (url-for :edit-user))
(GET "/fail3" [] (url-for :edit-user 10 20)))
wrap-reverse-routing)]
(is (= (make-request handler "/succeed1") "/user/"))
(is (= (make-request handler "/succeed2") "/user/10/"))
(is (= (make-request handler "/succeed3") "/user/10/edit"))
(is (nil? (make-request handler "/fail1")))
(is (nil? (make-request handler "/fail2")))
(is (nil? (make-request handler "/fail3")))))
(deftest test-with-rebinding-vars
(with-local-vars [subroutes (register-route :user
(GET "/user" [] (str "user")))]
(let [handler (-> (routes
subroutes
(GET "/succeed" [] (url-for :user))
(GET "/fail" [] (url-for :user 10)))
wrap-reverse-routing)]
(is (= (make-request handler "/succeed") "/user"))
(is (nil? (make-request handler "/fail")))
(var-set subroutes (register-route :user
(GET "/not-user" [] (str "user"))))
(is (= (make-request handler "/succeed") "/not-user"))
(is (nil? (make-request handler "/fail"))))))
(deftest top-level-is-a-var
(with-local-vars [bare-handler (routes
(register-route :user
(GET "/user/:id" [id] (str "user " id)))
(GET "/succeed" [] (url-for :user 10))
(GET "/fail1" [] (url-for :user))
(GET "/fail2" [] (url-for :user 10 20)))]
(let [handler (wrap-reverse-routing bare-handler)]
(is (= (make-request handler "/succeed") "/user/10"))
(is (nil? (make-request handler "/fail1")))
(is (nil? (make-request handler "/fail2"))))))
(deftest context-requiring-not-nil-value
(let [handler (-> (routes
(context "/:type" [type]
(if (not= type nil)
(register-route :get-object
(GET "/:id" [id]
(str [type id])))))
(GET "/succeed" [] (url-for :get-object "user" 10))
(GET "/fail" [] (url-for :get-object nil 10)))
wrap-reverse-routing)]
(is (= (make-request handler "/succeed"), "/user/10"))
(is (nil? (make-request handler "/fail")))))
(deftest context-with-limited-options
(let [valid-type? #{"user"}
handler (-> (routes
(context "/:type" [type]
(when-routes (valid-type? type)
(register-route :get-object
(GET "/:id" [id]
(str [type id])))))
(GET "/succeed" [] (url-for :get-object "user" 10))
(GET "/fail1" [] (url-for :get-object nil 10))
(GET "/fail2" [] (url-for :get-object "person" 10)))
wrap-reverse-routing)]
(is (= (make-request handler "/succeed"), "/user/10"))
(is (nil? (make-request handler "/fail1")))
(is (nil? (make-request handler "/fail2")))))
|