|
| 1 | +(ns ring.middleware.authorization-test |
| 2 | + (:require [clojure.test :refer :all] |
| 3 | + [ring.middleware.authorization :refer :all])) |
| 4 | + |
| 5 | +(deftest test-authorization-request |
| 6 | + (testing "pre-existing authorization" |
| 7 | + (is (= "TEST" |
| 8 | + (-> {:headers {"authorization" "Basic"} |
| 9 | + :authorization "TEST"} |
| 10 | + authorization-request |
| 11 | + :authorization)))) |
| 12 | + (testing "no authorization" |
| 13 | + (is (nil? (-> {:headers {}} |
| 14 | + authorization-request |
| 15 | + :authorization)))) |
| 16 | + (testing "scheme without token" |
| 17 | + (is (= {:scheme "basic"} |
| 18 | + (-> {:headers {"authorization" "Basic"}} |
| 19 | + authorization-request |
| 20 | + :authorization)))) |
| 21 | + (testing "scheme with zero-length token" |
| 22 | + (is (= {:scheme "basic"} |
| 23 | + (-> {:headers {"authorization" "Basic "}} |
| 24 | + authorization-request |
| 25 | + :authorization)))) |
| 26 | + (testing "token68" |
| 27 | + (is (= {:scheme "basic" |
| 28 | + :token "dGVzdA=="} |
| 29 | + (-> {:headers {"authorization" "Basic dGVzdA=="}} |
| 30 | + authorization-request |
| 31 | + :authorization)))) |
| 32 | + (testing "auth-params, some malformed" |
| 33 | + (is (= {:scheme "digest" |
| 34 | + :params {"a" "B" |
| 35 | + "c" "d" |
| 36 | + "eeee" "dGVzdA==" |
| 37 | + "k" "1"}} |
| 38 | + (-> {:headers {"authorization" "Digest A=B, c=\"d\", |
| 39 | + eeee=\"dGVzdA==\", fparam=dGVzdA==, g, \"h\"=i, =j, = ,, , k=1"}} |
| 40 | + authorization-request |
| 41 | + :authorization))))) |
| 42 | + |
| 43 | +(deftest test-wrap-authorization-none |
| 44 | + (let [handler (wrap-authorization (fn [req respond _] (respond req))) |
| 45 | + request {:headers {}} |
| 46 | + response (promise) |
| 47 | + exception (promise)] |
| 48 | + (handler request response exception) |
| 49 | + (is (nil? (:authorization @response))) |
| 50 | + (is (not (realized? exception))))) |
| 51 | + |
| 52 | +(deftest test-wrap-authorization-scheme-only |
| 53 | + (let [handler (wrap-authorization (fn [req respond _] (respond req))) |
| 54 | + request {:headers {"authorization" "Basic"}} |
| 55 | + response (promise) |
| 56 | + exception (promise)] |
| 57 | + (handler request response exception) |
| 58 | + (is (= {:scheme "basic"} |
| 59 | + (:authorization @response))) |
| 60 | + (is (not (realized? exception))))) |
| 61 | + |
| 62 | +(deftest test-wrap-authorization-token68 |
| 63 | + (let [handler (wrap-authorization (fn [req respond _] (respond req))) |
| 64 | + request {:headers {"authorization" "Basic dGVzdA=="}} |
| 65 | + response (promise) |
| 66 | + exception (promise)] |
| 67 | + (handler request response exception) |
| 68 | + (is (= {:scheme "basic" |
| 69 | + :token "dGVzdA=="} |
| 70 | + (:authorization @response))) |
| 71 | + (is (not (realized? exception))))) |
| 72 | + |
| 73 | +(deftest test-wrap-authorization-auth-params |
| 74 | + (let [handler (wrap-authorization (fn [req respond _] (respond req))) |
| 75 | + request {:headers {"authorization" "Digest A=\"B\""}} |
| 76 | + response (promise) |
| 77 | + exception (promise)] |
| 78 | + (handler request response exception) |
| 79 | + (is (= {:params {"a" "B"} |
| 80 | + :scheme "digest"} |
| 81 | + (:authorization @response))) |
| 82 | + (is (not (realized? exception))))) |
| 83 | + |
| 84 | +(deftest test-wrap-authorization-synchronous |
| 85 | + (let [request (atom nil) |
| 86 | + handler (wrap-authorization (fn [req] (reset! request req)))] |
| 87 | + (handler {:headers {"authorization" "Basic A=\"B\""}}) |
| 88 | + (is (= {:params {"a" "B"} |
| 89 | + :scheme "basic"} |
| 90 | + (:authorization @request))))) |
0 commit comments