File tree Expand file tree Collapse file tree 2 files changed +15
-2
lines changed
test/ring/middleware/test Expand file tree Collapse file tree 2 files changed +15
-2
lines changed Original file line number Diff line number Diff line change 3
3
(:require [ring.util.mime-type :refer [ext-mime-type]]
4
4
[ring.util.response :refer [content-type get-header]]))
5
5
6
+ (defn- guess-mime-type [{:keys [uri]} {:keys [body]} mime-types]
7
+ (or (ext-mime-type uri mime-types)
8
+ (when (instance? java.io.File body)
9
+ (ext-mime-type (str body) mime-types))))
10
+
6
11
(defn content-type-response
7
12
" Adds a content-type header to response. See: wrap-content-type."
8
13
{:added " 1.2" }
12
17
(if response
13
18
(if (get-header response " Content-Type" )
14
19
response
15
- (let [mime-type (ext -mime-type ( :uri request) (:mime-types options))]
20
+ (let [mime-type (guess -mime-type request response (:mime-types options))]
16
21
(content-type response (or mime-type " application/octet-stream" )))))))
17
22
18
23
(defn wrap-content-type
Original file line number Diff line number Diff line change 39
39
(let [response {:headers {" CoNteNt-typE" " application/x-overridden" }}
40
40
handler (wrap-content-type (constantly response))]
41
41
(is (= (handler {:uri " /foo/bar.png" })
42
- {:headers {" CoNteNt-typE" " application/x-overridden" }})))))
42
+ {:headers {" CoNteNt-typE" " application/x-overridden" }}))))
43
+
44
+ (testing " fallback on response file extension"
45
+ (let [response {:body (java.io.File. " test/ring/assets/index.html" )}
46
+ handler (wrap-content-type (constantly response))]
47
+ (is (= (-> (handler {:uri " /" }) (dissoc :body ))
48
+ {:headers {" Content-Type" " text/html" }}))
49
+ (is (= (-> (handler {:uri " /index.txt" }) (dissoc :body ))
50
+ {:headers {" Content-Type" " text/plain" }})))))
43
51
44
52
(deftest wrap-content-type-cps-test
45
53
(testing " response without content-type"
You can’t perform that action at this time.
0 commit comments