File: test_flow.clj

package info (click to toggle)
liberator-clojure 0.15.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 436 kB
  • sloc: makefile: 18; sh: 2
file content (211 lines) | stat: -rw-r--r-- 10,075 bytes parent folder | download
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
(ns test-flow
  (:use liberator.core
        [liberator.representation :only (ring-response)]
        midje.sweet
        checkers
        [ring.mock.request :only [request header]]))

(facts "customize the initial context"
       (let [resp ((resource :initialize-context {::field "some initial context"}
                             :handle-ok ::field) (request :get "/"))]
         (fact resp => (body "some initial context"))))

(facts "GET Requests"
       (facts "get existing resource"
              (let [resp ((resource :exists? true :handle-ok "OK") (request :get "/"))]
                (fact resp => OK)
                (fact resp => (body "OK"))))

       (facts "get unexisting resource"
              (let [resp ((resource :exists? false :handle-not-found "NOT-FOUND") (request :get "/"))]
                (fact resp => NOT-FOUND)
                (fact resp => (body "NOT-FOUND"))))

       (facts "get on moved temporarily"
              (let [resp ((resource :exists? false
                                    :existed? true
                                    :moved-temporarily? {:location "http://new.example.com/"})
                          (request :get "/"))]
                (fact resp => (MOVED-TEMPORARILY "http://new.example.com/"))))

       (facts "get on moved temporarily with custom handler"
              (let [resp ((resource :exists? false
                                    :existed? true
                                    :moved-temporarily? {:location "http://new.example.com/"}
                                    :handle-moved-temporarily "Temporary redirection...")
                          (request :get "/"))]
                (fact resp => (MOVED-TEMPORARILY "http://new.example.com/"))
                (fact resp => (body "Temporary redirection..."))))

       (facts "get on moved permantently"
              (let [resp ((resource :exists? false :existed? true
                                    :moved-permanently? true
                                    :location "http://other.example.com/")
                          (request :get "/"))]
                (fact resp => (MOVED-PERMANENTLY "http://other.example.com/"))))

       (facts "get on moved permantently with custom response"
              (let [resp ((resource :exists? false :existed? true
                                    :moved-permanently? {:location "http://other.example.com/"}
                                    :handle-moved-permanently "Not here, there!")
                          (request :get "/"))]
                (fact resp => (MOVED-PERMANENTLY "http://other.example.com/"))
                (fact resp => (body "Not here, there!"))))

       (facts "get on moved permantently with custom response and explicit header"
              (let [resp ((resource :exists? false :existed? true
                                    :moved-permanently? true
                                    :handle-moved-permanently (ring-response {:body "Not here, there!"
                                                                              :headers {"Location" "http://other.example.com/"}}))
                          (request :get "/"))]
                (fact resp => (MOVED-PERMANENTLY "http://other.example.com/"))
                (fact resp => (body "Not here, there!"))))

       (facts "get on moved permantently with automatic response"
              (let [resp ((resource :exists? false :existed? true
                                    :moved-permanently? true
                                    :location "http://other.example.com/")
                          (request :get "/"))]
                (fact resp => (MOVED-PERMANENTLY "http://other.example.com/")))))

(facts "POST Requests"
       (let [r (resource :allowed-methods [:post]
                         :exists? true
                         :handle-created "Created")
             resp (r (request :post "/"))]
         (fact "Post to existing" resp => CREATED)
         (fact "Body of 201" resp => (body "Created")))

       (let [r (resource :allowed-methods [:post]
                         :exists? true
                         :post-enacted? true
                         :post-redirect? {:location "http://example.com/foo"})
             resp (r (request :post "/"))]
         (fact "Post completed to existing resource and redirect" resp => (SEE-OTHER "http://example.com/foo")))

       (let [r (resource :allowed-methods [:post]
                         :exists? true
                         :post-enacted? true)
             resp (r (request :post "/"))]
         (fact "Post completed to existing resource" resp => CREATED))

       (let [r (resource :allowed-methods [:post]
                         :exists? true
                         :post-enacted? true
                         :new? false
                         :respond-with-entity? false)
             resp (r (request :post "/"))]
         (fact "Post completed to existing resource with new? and respond-with-entity? as false" resp => NO-CONTENT))

       (let [r (resource :allowed-methods [:post]
                         :exists? true
                         :post-enacted? false)
             resp (r (request :post "/"))]
         (fact "Post in progress to existing resource" resp => ACCEPTED))

       (let [r (resource :allowed-methods [:post]
                         :exists? true
                         :conflict? true)
             resp (r (request :post "/"))]
         (fact "Post to existing with conflict" resp => CONFLICT))

       (let [r (resource :allowed-methods [:post]
                         :exists? true
                         :post-redirect? {:location "http://example.com/foo"})
             resp (r (request :post "/")) ]
         (fact "Post to existing resource and redirect" resp => (SEE-OTHER  "http://example.com/foo")))

       (let [r (resource :allowed-methods [:post]
                         :exists? false
                         :post-redirect? true
                         :can-post-to-missing? true
                         :location "http://example.com/foo")
             resp (r (request :post "/")) ]
         (fact "Post to missing can redirect" resp => (SEE-OTHER  "http://example.com/foo")))

       (let [r (resource :allowed-methods [:post]
                         :exists? false
                         :location "foo"
                         :can-post-to-missing? true)
             resp (r (request :post "/")) ]
         (fact "Post to missing if post to missing is allowed" resp => CREATED)
         (fact "Location is set" resp => (contains {:headers (contains {"Location" "foo"})})))

       (let [r (resource :allowed-methods [:post]
                         :exists? false
                         :can-post-to-missing? false
                         :handle-not-found "not-found")
             resp (r (request :post "/")) ]
         (fact "Post to missing can give 404" resp => NOT-FOUND)
         (fact "Body of 404" resp => (body "not-found")))

       (let [r (resource :allowed-methods [:post]
                         :exists? true
                         :can-post-to-missing? false)
             resp (r (request :post "/")) ]
         (fact "Post to existing if post to missing forbidden is allowed" resp => CREATED)))

(facts "PUT requests"
       (let [r (resource :allowed-methods [:put]
                         :exists? false
                         :can-put-to-missing? false)
             resp (r (request :put "/"))]
         (fact "Put to missing can give 501" resp => NOT-IMPLEMENTED))

       (let [r (resource :allowed-methods [:put]
                         :exists? false
                         :can-put-to-missing? true)
             resp (r (request :put "/"))]
         (fact "Put to missing can give 201" resp => CREATED))

       (let [r (resource :allowed-methods [:put]
                         :exists? true
                         :conflict? true)
             resp (r (request :put "/"))]
         (fact "Put to existing with conflict" resp => CONFLICT))
       
       (let [r (resource :allowed-methods [:put]
                         :processable? false)
             resp (r (request :put "/"))]
         (fact "Unprocessable can give 422" resp => UNPROCESSABLE))

       (let [r (resource :allowed-methods [:put]
                         :exists? true
                         :put-enacted? true)
             resp (r (request :put "/"))]
         (fact "Put to existing completed" resp => CREATED))

       (let [r (resource :allowed-methods [:put]
                         :exists? true
                         :put-enacted? true
                         :new? false
                         :respond-with-entity? false)
             resp (r (request :put "/"))]
         (fact "Put to existing resource with new? and respond-with-entity? as false" resp => NO-CONTENT))

       (let [r (resource :allowed-methods [:put]
                         :exists? true
                         :put-enacted? false)
             resp (r (request :put "/"))]
         (fact "Put in progress to existing resource" resp => ACCEPTED)))

(facts "HEAD requests"
       (facts "on existing resource"
              (let [resp ((resource :exists? true :handle-ok "OK") (request :head "/"))]
                (fact resp => OK)
                (fact resp => (content-type "text/plain;charset=UTF-8"))
                (fact resp => (no-body))))

       (facts "unexisting resource"
              (let [resp ((resource :exists? false :handle-not-found "NOT-FOUND") (request :head "/"))]
                (fact resp => NOT-FOUND)
                (fact resp => (no-body))))

       (facts "on moved temporarily"
              (let [resp ((resource :exists? false
                                    :existed? true
                                    :moved-temporarily? true
                                    :location "http://new.example.com/")
                          (request :get "/"))]
                (fact resp => (MOVED-TEMPORARILY "http://new.example.com/"))
                (fact resp => (no-body)))))