File: test_conditionals.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 (222 lines) | stat: -rw-r--r-- 8,313 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
212
213
214
215
216
217
218
219
220
221
222
(ns test-conditionals
  (:use [liberator.core :only [resource request-method-in
                               with-console-logger]]
        midje.sweet
        checkers
        liberator.util
        [ring.mock.request :only [request header]]))

(defn if-modified-since [req value]
  (header req "if-modified-since" value))

(defn if-unmodified-since [req value]
  (header req "if-unmodified-since" value))

(defn if-match [req value]
  (header req "if-match" (if (= "*" value) value (str "\"" value "\""))))

(defn if-none-match [req value]
  (header req "if-none-match" (if (= "*" value) value (str "\"" value "\""))))


;; get requests

(facts "get requests"
  (facts "if-modified-since true"
    (let [resp ((resource :exists? true
                          :handle-ok "OK"
                          :last-modified (as-date 1001))
                (-> (request :get "/")
                    (if-modified-since (http-date (as-date 1000)))))]
      (fact resp => OK)
      (fact resp => (body "OK"))
      (fact resp => (header-value "Last-Modified" (http-date (as-date 1000))))))

  (facts "if-modified-since false"
    (let [resp ((resource :exists? true
                          :last-modified (as-date 1000))
                (-> (request :get "/")
                    (if-modified-since (http-date (as-date 1000)))))]
      (fact resp => NOT-MODIFIED)
      (fact resp => (no-body))
      (fact resp => (header-value "Last-Modified" (http-date (as-date 1000))))))

  (facts "if-unmodified-since true"
    (let [resp ((resource :exists? true
                          :last-modified (as-date 1000)
                          :handle-precondition-failed "precondition failed")
                (-> (request :get "/")
                    (if-unmodified-since (http-date (as-date 900)))))]
      (fact resp => PRECONDITION-FAILED)
      (fact resp => (body "precondition failed"))))

  (facts "if-unmodified-since false"
    (let [resp ((resource :exists? true
                          :last-modified (as-date 1000)
                          :handle-ok "OK")
                (-> (request :get "/")
                    (if-unmodified-since (http-date (as-date 1000)))))]
      (fact resp => OK)
      (fact resp => (body "OK"))))

 (facts "if-match true"
    (let [resp ((resource :exists? true
                          :etag (constantly "TAG1")
                          :handle-ok "OK")
                (-> (request :get "/")
                    (if-match "TAG1")))]
      (fact resp => OK)
      (fact resp => (body "OK"))
      (fact resp => (header-value "ETag" "\"TAG1\""))))

 (facts "if-match false"
    (let [resp ((resource :exists? true
                          :etag (constantly "TAG1")
                          :handle-ok "OK")
                (-> (request :get "/")
                    (if-match "TAG2")))]
      (fact resp => PRECONDITION-FAILED)))

 (facts "if-none-match true"
   (let [resp ((resource :exists? true
                         :etag (constantly "TAG1")
                         :handle-ok "OK")
               (-> (request :get "/")
                   (if-none-match "TAG1")))]
     (fact resp => NOT-MODIFIED)))

 (facts "if-none-match false"
   (let [resp ((resource :exists? true
                         :etag (constantly "TAG2")
                         :handle-ok "T2")
               (-> (request :get "/")
                   (if-none-match "TAG1")))]
     (fact resp => OK))))


;; put and post requests
(tabular
 (facts "conditional request for post and put"
   (facts "if-modified-since true"
     (let [resp ((resource :exists? true
                           :method-allowed? (request-method-in ?method)
                           :handle-created "CREATED"
                           :last-modified (as-date 1001))
                 (-> (request ?method "/")
                     (if-modified-since (http-date (as-date 1000)))))]
       (fact resp => CREATED)
       (fact resp => (body "CREATED"))))

   (facts "if-modified-since false"
     (let [resp ((resource :exists? true
                           :method-allowed? (request-method-in ?method)
                           :handle-not-modified "NM"
                           :last-modified (as-date 100000))
                 (-> (request ?method "/")
                     (if-modified-since (http-date (as-date 200000)))))]
       (fact resp => NOT-MODIFIED)
       (fact resp => (body "NM"))))

   (facts "if-unmodified-since false"
     (let [resp ((resource :exists? true
                           :method-allowed? (request-method-in ?method)
                           :handle-accepted "A"
                           :last-modified (as-date 200000))
                 (-> (request ?method "/")
                     (if-unmodified-since (http-date (as-date 100000)))))]
       (fact resp => PRECONDITION-FAILED)))

   (facts "if-unmodified-since true"
     (let [resp ((resource :exists? true
                           :method-allowed? (request-method-in ?method)
                           :handle-created "CREATED"
                           :last-modified (as-date 100000))
                 (-> (request ?method "/")
                     (if-unmodified-since (http-date (as-date 200000)))))]
       (fact resp => CREATED)
       (fact resp => (body "CREATED"))))

   (facts "if-unmodified-since with last-modified changes due do post"
          (let [resp ((resource :exists? true
                                :method-allowed? (request-method-in ?method)
                                :post! (fn [ctx] {::LM 1001})
                                :handle-created "CREATED"
                                :last-modified (fn [ctx] (as-date (get ctx ::LM 1000))))
                      (-> (request ?method "/")
                          (if-unmodified-since (http-date (as-date 1000)))))]
            (fact resp => CREATED)
            (fact resp => (body "CREATED"))
            (fact resp => (header-value "Last-Modified" (http-date (as-date 1001))))))

   (facts "if-match true"
     (let [resp ((resource :exists? true
                           :method-allowed? (request-method-in ?method)
                           :handle-created "CREATED"
                           :etag (constantly "TAG1"))
                 (-> (request ?method "/")
                     (if-match "TAG1")))]
       (fact resp => CREATED)
       (fact resp => (body "CREATED"))))

   (facts "if-match false"
     (let [resp ((resource :exists? true
                           :method-allowed? (request-method-in ?method)
                           :handle-precondition-failed "PF"
                           :etag (constantly "TAG1"))
                 (-> (request ?method "/")
                     (if-match "TAG2")))]
       (fact resp => PRECONDITION-FAILED)
       (fact resp => (body "PF"))))

   (facts "if-none-match true"
     (let [resp ((resource :exists? true
                           :method-allowed? (request-method-in ?method)
                           :handle-created "CREATED"
                           :etag (constantly "TAG1"))
                 (-> (request ?method "/")
                     (if-none-match "TAG1")))]
       (fact resp => PRECONDITION-FAILED)))

   (facts "if-none-match false"
     (let [resp ((resource :exists? true
                           :method-allowed? (request-method-in ?method)
                           :etag (constantly "TAG1"))
                 (-> (request ?method "/")
                     (if-none-match "TAG2")))]
       (fact resp => CREATED))))

 ?method
 :put
 :post)



(facts "if-match * false on unexisting"
  (tabular
   (let [resp ((resource :method-allowed? true
                         :exists? false
                         :etag (constantly "TAG1"))
               (-> (request ?method "/")
                   (if-match "*")))]
     (fact resp => PRECONDITION-FAILED))
   ?method
   :get
   :post
   :put
   :delete))

(facts "if-none-match * false on existing"
  (tabular
   (let [resp ((resource :method-allowed? true
                         :exists? true
                         :etag (constantly "TAG1"))
               (-> (request ?method "/")
                   (if-none-match "*")))]
     (if (= ?method :get)
       (fact resp => NOT-MODIFIED)
       (fact resp => PRECONDITION-FAILED)))
   ?method
   :get
   :post
   :put
   :delete))