|
153 | 153 | "{\"baz\":[\"qu\",\"qi\",\"qo\"]}")) |
154 | 154 | (is (= (:content-length resp) 24))))) |
155 | 155 |
|
| 156 | +(defn- get-boundary [{:keys [content-type]}] |
| 157 | + (re-find #"(?<=boundary=)[^;]*" content-type)) |
| 158 | + |
| 159 | +(deftest test-multipart-body |
| 160 | + (testing "string values" |
| 161 | + (let [response (multipart-body {} {:foo "a" |
| 162 | + :bar {:value "b"} |
| 163 | + :baz {:value "<html></html>" |
| 164 | + :content-type "text/html"}}) |
| 165 | + boundary (get-boundary response)] |
| 166 | + (is (= (:content-length response) |
| 167 | + (+ 284 (* 4 (count boundary))))) |
| 168 | + (is (= (:content-type response) |
| 169 | + (str "multipart/form-data; charset=ISO-8859-1; " |
| 170 | + "boundary=" boundary))) |
| 171 | + (is (= (slurp (:body response)) |
| 172 | + (str "--" boundary "\r\n" |
| 173 | + "Content-Disposition: form-data; name=\"foo\"\r\n" |
| 174 | + "Content-Type: text/plain; charset=UTF-8\r\n\r\n" |
| 175 | + "a\r\n" |
| 176 | + "--" boundary "\r\n" |
| 177 | + "Content-Disposition: form-data; name=\"bar\"\r\n" |
| 178 | + "Content-Type: text/plain; charset=UTF-8\r\n\r\n" |
| 179 | + "b\r\n" |
| 180 | + "--" boundary "\r\n" |
| 181 | + "Content-Disposition: form-data; name=\"baz\"\r\n" |
| 182 | + "Content-Type: text/html\r\n\r\n" |
| 183 | + "<html></html>\r\n" |
| 184 | + "--" boundary "--\r\n"))))) |
| 185 | + (testing "byte array values" |
| 186 | + (let [response (multipart-body {} {:foo (.getBytes "a" "UTF-8") |
| 187 | + :bar {:value (.getBytes "b" "UTF-8") |
| 188 | + :content-type "image/png" |
| 189 | + :filename "bee.png"}}) |
| 190 | + boundary (get-boundary response)] |
| 191 | + (is (= (:content-length response) |
| 192 | + (+ 197 (* 3 (count boundary))))) |
| 193 | + (is (= (:content-type response) |
| 194 | + (str "multipart/form-data; charset=ISO-8859-1; " |
| 195 | + "boundary=" boundary))) |
| 196 | + (is (= (slurp (:body response)) |
| 197 | + (str "--" boundary "\r\n" |
| 198 | + "Content-Disposition: form-data; name=\"foo\"\r\n" |
| 199 | + "Content-Type: application/octet-stream\r\n\r\n" |
| 200 | + "a\r\n" |
| 201 | + "--" boundary "\r\n" |
| 202 | + "Content-Disposition: form-data; name=\"bar\"; " |
| 203 | + "filename=\"bee.png\"\r\n" |
| 204 | + "Content-Type: image/png\r\n\r\n" |
| 205 | + "b\r\n" |
| 206 | + "--" boundary "--\r\n"))))) |
| 207 | + (testing "file values" |
| 208 | + (let [test-file (io/file (io/resource "ring/mock/test.txt")) |
| 209 | + response (multipart-body {} {:foo test-file |
| 210 | + :bar {:value test-file |
| 211 | + :content-type "text/html" |
| 212 | + :filename "test.html"}}) |
| 213 | + boundary (get-boundary response)] |
| 214 | + (is (= (:content-length response) |
| 215 | + (+ 208 (* 3 (count boundary))))) |
| 216 | + (is (= (:content-type response) |
| 217 | + (str "multipart/form-data; charset=ISO-8859-1; " |
| 218 | + "boundary=" boundary))) |
| 219 | + (is (= (slurp (:body response)) |
| 220 | + (str "--" boundary "\r\n" |
| 221 | + "Content-Disposition: form-data; name=\"foo\"; " |
| 222 | + "filename=\"test.txt\"\r\n" |
| 223 | + "Content-Type: text/plain\r\n\r\n" |
| 224 | + "a\n\r\n" |
| 225 | + "--" boundary "\r\n" |
| 226 | + "Content-Disposition: form-data; name=\"bar\"; " |
| 227 | + "filename=\"test.html\"\r\n" |
| 228 | + "Content-Type: text/html\r\n\r\n" |
| 229 | + "a\n\r\n" |
| 230 | + "--" boundary "--\r\n")))))) |
| 231 | + |
156 | 232 | (defmacro when-clojure-spec |
157 | 233 | [& body] |
158 | 234 | (when (try |
|
0 commit comments