|
4 | 4 | [clj-http.conn-mgr :as conn] |
5 | 5 | [clj-http.test.core-test :refer [run-server]] |
6 | 6 | [clj-http.util :as util] |
7 | | - [clojure.java.io :refer [resource]] |
| 7 | + [clojure.java.io :as io :refer [resource]] |
8 | 8 | [clojure.string :as str] |
9 | 9 | [clojure.test :refer :all] |
10 | 10 | [cognitect.transit :as transit] |
|
16 | 16 | java.io.PipedOutputStream |
17 | 17 | java.net.UnknownHostException |
18 | 18 | org.apache.http.HttpEntity |
| 19 | + org.apache.http.HttpMessage |
19 | 20 | org.apache.logging.log4j.LogManager)) |
20 | 21 |
|
21 | 22 | (defonce logger (LogManager/getLogger "clj-http.test.client-test")) |
|
146 | 147 | "Verify that we went through the failure path, not the success") |
147 | 148 | (is (= @fail-p expected-var *test-dynamic-var*)))))))) |
148 | 149 |
|
| 150 | +(defn retrieve-http-request-content-type-header |
| 151 | + [response] |
| 152 | + (let [http-req (get-in response [:request :http-req])] |
| 153 | + (->> (.getAllHeaders ^HttpMessage http-req) |
| 154 | + (map str) |
| 155 | + (some #(when (str/starts-with? (str/lower-case %) "content-type") %))))) |
| 156 | + |
149 | 157 | (deftest ^:integration multipart-async |
150 | 158 | (run-server) |
151 | | - (let [resp (promise) |
152 | | - exception (promise) |
153 | | - _ (request {:uri "/post" :method :post |
154 | | - :async? true |
155 | | - :multipart [{:name "title" :content "some-file"} |
156 | | - {:name "Content/Type" :content "text/plain"} |
157 | | - {:name "file" |
158 | | - :content (clojure.java.io/file |
159 | | - "test-resources/m.txt")}]} |
160 | | - resp |
161 | | - exception |
162 | | - )] |
163 | | - (is (= 200 (:status @resp))) |
164 | | - (is (not (realized? exception))) |
165 | | - #_(when (realized? exception) (prn @exception))) |
| 159 | + |
| 160 | + (testing "basics" |
| 161 | + (let [resp (promise) |
| 162 | + exception (promise) |
| 163 | + _ (request {:uri "/post" :method :post |
| 164 | + :async? true |
| 165 | + :multipart [{:name "title" :content "some-file"} |
| 166 | + {:name "Content/Type" :content "text/plain"} |
| 167 | + {:name "file" |
| 168 | + :content (io/file "test-resources/m.txt")}]} |
| 169 | + resp |
| 170 | + exception)] |
| 171 | + (is (= 200 (:status (deref resp 500 :failed)))) |
| 172 | + (is (not (realized? exception))) |
| 173 | + #_(when (realized? exception) (prn @exception)))) |
166 | 174 |
|
167 | 175 | ;; Regression Testing https://github.com/dakrone/clj-http/issues/560 |
168 | 176 | (testing "multipart uploads larger than 25kb" |
169 | 177 | (let [resp (promise) |
170 | 178 | exception (promise) |
171 | 179 | ;; assumption: file > 5kb |
172 | | - file (clojure.java.io/file "test-resources/big_array_json.json") |
| 180 | + file (io/file "test-resources/big_array_json.json") |
173 | 181 |
|
174 | 182 | _ (request {:uri "/post" :method :post |
175 | 183 | :async? true |
|
181 | 189 | resp |
182 | 190 | exception)] |
183 | 191 | (is (= 200 (:status (deref resp 500 :failed)))) |
184 | | - (is (not (realized? exception)))))) |
| 192 | + (is (not (realized? exception))))) |
| 193 | + |
| 194 | + ;; Find the details in https://github.com/dakrone/clj-http/pull/654 |
| 195 | + (testing "existing \"Content-Type\" request header is discarded" |
| 196 | + (let [resp (request {:uri "/post" :method :post |
| 197 | + :headers {"content-type" "multipart/form-data"} |
| 198 | + :multipart [{:name "some" :content "thing"}] |
| 199 | + :save-request? true}) |
| 200 | + content-type (retrieve-http-request-content-type-header resp)] |
| 201 | + (is (= 200 (:status resp))) |
| 202 | + (is (not= "multipart/form-data" content-type)) |
| 203 | + (is (nil? content-type))) |
| 204 | + (let [resp (request {:uri "/post" :method :post |
| 205 | + :headers {"Content-Type" "multipart/form-data"} |
| 206 | + :multipart [{:name "some" :content "thing"}] |
| 207 | + :save-request? true}) |
| 208 | + content-type (retrieve-http-request-content-type-header resp)] |
| 209 | + (is (= 200 (:status resp))) |
| 210 | + (is (not= "multipart/form-data" content-type)) |
| 211 | + (is (nil? content-type))))) |
185 | 212 |
|
186 | 213 | (deftest ^:integration nil-input |
187 | 214 | (is (thrown-with-msg? Exception #"Host URL cannot be nil" |
|
0 commit comments