|
8 | 8 |
|
9 | 9 | (deftest wrap-file-no-directory
|
10 | 10 | (is (thrown-with-msg? Exception #".*Directory does not exist.*"
|
11 |
| - (wrap-file (constantly test-response) "not_here")))) |
| 11 | + (wrap-file (constantly test-response) "not_here")))) |
12 | 12 |
|
13 | 13 | (def public-dir "test/ring/assets")
|
14 | 14 | (def index-html (File. ^String public-dir "index.html"))
|
|
93 | 93 | (is (= 200 status))
|
94 | 94 | (is (= (into #{} (keys headers)) #{"Content-Length" "Last-Modified"}))
|
95 | 95 | (is (= foo-html body)))))
|
| 96 | + |
| 97 | +(defn- prefer-foo-handler |
| 98 | + ([request] |
| 99 | + (if (= (:uri request) "/foo.html") |
| 100 | + {:status 200, :headers {}, :body "override"} |
| 101 | + {:status 404, :headers {}, :body "not found"})) |
| 102 | + ([request respond raise] |
| 103 | + (respond (prefer-foo-handler request)))) |
| 104 | + |
| 105 | +(deftest test-wrap-file-with-prefer-handler |
| 106 | + (let [handler (wrap-file prefer-foo-handler |
| 107 | + (File. public-dir) |
| 108 | + {:prefer-handler? true})] |
| 109 | + |
| 110 | + (testing "middleware serves file (synchronously)" |
| 111 | + (let [response (handler {:request-method :get, :uri "/index.html"})] |
| 112 | + (is (= 200 (:status response))) |
| 113 | + (is (= index-html (:body response))))) |
| 114 | + |
| 115 | + (testing "middleware serves file (asynchronously)" |
| 116 | + (let [response (promise) |
| 117 | + error (promise)] |
| 118 | + (handler {:request-method :get, :uri "/index.html"} response error) |
| 119 | + (is (= 200 (:status @response))) |
| 120 | + (is (= index-html (:body @response))) |
| 121 | + (is (not (realized? error))))) |
| 122 | + |
| 123 | + (testing "handler serves file (synchronously)" |
| 124 | + (let [response (handler {:request-method :get, :uri "/foo.html"})] |
| 125 | + (is (= 200 (:status response))) |
| 126 | + (is (= "override" (:body response))))) |
| 127 | + |
| 128 | + (testing "handler serves file (asynchronously)" |
| 129 | + (let [response (promise) |
| 130 | + error (promise)] |
| 131 | + (handler {:request-method :get, :uri "/foo.html"} response error) |
| 132 | + (is (= 200 (:status @response))) |
| 133 | + (is (= "override" (:body @response))) |
| 134 | + (is (not (realized? error))))))) |
0 commit comments