| 
1 | 1 | (ns io.staticweb.rate-limit.middleware-test  | 
2 |  | -  (:require [clj-time.coerce :as c]  | 
3 |  | -            [clojure.test :refer :all]  | 
4 |  | -            [io.staticweb.rate-limit.limits :as l]  | 
5 |  | -            [io.staticweb.rate-limit.middleware :refer :all]  | 
 | 2 | +  (:use clojure.test  | 
 | 3 | +        io.staticweb.rate-limit.middleware  | 
 | 4 | +        io.staticweb.rate-limit.test-utils)  | 
 | 5 | +  (:require [io.staticweb.rate-limit.limits :as l]  | 
6 | 6 |             [io.staticweb.rate-limit.responses :as r]  | 
7 |  | -            [io.staticweb.rate-limit.storage :as s]  | 
8 |  | -            [io.staticweb.rate-limit.test-utils :refer :all]))  | 
 | 7 | +            [io.staticweb.rate-limit.storage :as s])  | 
 | 8 | +  (:import java.time.Duration))  | 
9 | 9 | 
 
  | 
10 | 10 | (defrecord MockStorage [counters timeouts]  | 
11 | 11 |   s/Storage  | 
 | 
37 | 37 |   [counters & body]  | 
38 | 38 |   `(binding [*storage* (->MockStorage (atom {}) (atom {}))]  | 
39 | 39 |      (doseq [[k# v# e#] ~counters]  | 
40 |  | -       (set-counter k# v# (c/from-date e#)))  | 
 | 40 | +       (set-counter k# v# e#))  | 
41 | 41 |      ~@body))  | 
42 | 42 | 
 
  | 
43 | 43 | (defn make-request  | 
 | 
66 | 66 |         (is (= (s/counter-expiry *storage* :mock-limit-key) :mock-ttl)))))  | 
67 | 67 | 
 
  | 
68 | 68 |   (testing "with exhausted limit"  | 
69 |  | -    (with-counters [[:mock-limit-key 10 #inst "2014-12-31T12:34:56Z"]]  | 
 | 69 | +    (with-counters [[:mock-limit-key 10 (Duration/ofMinutes 5)]]  | 
70 | 70 |       (let [limit (->MockRateLimit 10 :mock-limit-key :mock-ttl)  | 
71 | 71 |             rsp (make-request wrap-stacking-rate-limit limit)]  | 
72 | 72 |         (is (= (:status rsp) 429))  | 
73 | 73 |         (is (= (::r/rate-limit-applied rsp) {:key :mock-limit-key  | 
74 | 74 |                                              :quota 10  | 
75 | 75 |                                              :remaining 0}))  | 
76 |  | -        (is (= (retry-after rsp) "Wed, 31 Dec 2014 12:34:56 GMT")))))  | 
 | 76 | +        (is (= (retry-after rsp) "300")))))  | 
77 | 77 | 
 
  | 
78 | 78 |   (testing "with custom 429 reponse"  | 
79 |  | -    (with-counters [[:mock-limit-key 10 #inst "2014-12-31T12:34:56Z"]]  | 
 | 79 | +    (with-counters [[:mock-limit-key 10 (Duration/ofSeconds 42)]]  | 
80 | 80 |       (let [limit (->MockRateLimit 10 :mock-limit-key :mock-ttl)  | 
81 | 81 |             custom-response-handler (fn [key retry-after]  | 
82 | 82 |                                       {:status 418  | 
 | 
111 | 111 |           (is (= (s/counter-expiry *storage* :first-limit-key) :first-ttl))))))  | 
112 | 112 | 
 
  | 
113 | 113 |   (testing "with exhausted first rate limit"  | 
114 |  | -    (with-counters [[:first-limit-key 1000 #inst "2014-12-31T12:34:56Z"]]  | 
 | 114 | +    (with-counters [[:first-limit-key 1000 (Duration/ofMillis 15000)]]  | 
115 | 115 |       (let [first-limit (->MockRateLimit 1000 :first-limit-key :first-ttl)  | 
116 | 116 |             second-limit (->MockRateLimit 10 :second-limit-key :second-ttl)  | 
117 | 117 |             handler (-> default-response  | 
 | 
125 | 125 |           (is (= (::r/rate-limit-applied rsp) {:key :first-limit-key  | 
126 | 126 |                                                :quota 1000  | 
127 | 127 |                                                :remaining 0}))  | 
128 |  | -          (is (= (retry-after rsp) "Wed, 31 Dec 2014 12:34:56 GMT"))  | 
 | 128 | +          (is (= (retry-after rsp) "15"))  | 
129 | 129 |           (is (= (s/get-count *storage* :first-limit-key) 1000))  | 
130 | 130 |           (is (= (s/get-count *storage* :second-limit-key) 0))))))  | 
131 | 131 | 
 
  | 
132 | 132 |   (testing "with exhausted second rate limit"  | 
133 |  | -    (with-counters [[:second-limit-key 10 #inst "2014-12-31T12:34:56Z"]]  | 
 | 133 | +    (with-counters [[:second-limit-key 10 (Duration/ofHours 3)]]  | 
134 | 134 |       (let [first-limit (->MockRateLimit 1000 :first-limit-key :first-ttl)  | 
135 | 135 |             second-limit (->MockRateLimit 10 :second-limit-key :second-ttl)  | 
136 | 136 |             handler (-> default-response  | 
 | 
144 | 144 |           (is (= (::r/rate-limit-applied rsp) {:key :second-limit-key  | 
145 | 145 |                                                :quota 10  | 
146 | 146 |                                                :remaining 0}))  | 
147 |  | -          (is (= (retry-after rsp) "Wed, 31 Dec 2014 12:34:56 GMT"))  | 
 | 147 | +          (is (= (retry-after rsp) "10800"))  | 
148 | 148 |           (is (= (s/get-count *storage* :first-limit-key) 0))  | 
149 | 149 |           (is (= (s/get-count *storage* :second-limit-key) 10)))))))  | 
0 commit comments