|
1 | 1 | (ns clj-http.test.multipart-test |
2 | 2 | (:require [clj-http.multipart :refer :all] |
3 | 3 | [clojure.test :refer :all]) |
4 | | - (:import (java.io File) |
5 | | - (org.apache.http.entity.mime.content FileBody))) |
6 | | - |
7 | | -(deftest test-make-file-body |
8 | | - (testing "no content throws exception" |
9 | | - (is (thrown? Exception (make-file-body {})))) |
10 | | - |
11 | | - (testing "can create FileBody with content only" |
12 | | - (let [testfile (File. "testfile") |
13 | | - file-body (make-file-body {:content (File. "testfile")})] |
14 | | - (is (= (.getFile ^FileBody file-body) testfile)))) |
15 | | - |
16 | | - (testing "can create FileBody with content and mime-type" |
17 | | - (let [testfile (File. "testfile") |
18 | | - file-body (make-file-body {:content (File. "testfile") |
19 | | - :mime-type "application/octet-stream"})] |
20 | | - (is (= (.getFile ^FileBody file-body) testfile)))) |
21 | | - |
22 | | - (testing "can create FileBody with content and mime-type and name" |
23 | | - (let [testfile (File. "testfile") |
24 | | - file-body (make-file-body {:content (File. "testfile") |
25 | | - :mime-type "application/octet-stream" |
26 | | - :name "testname"})] |
27 | | - (is (= (.getFile ^FileBody file-body) testfile)) |
28 | | - (is (= (.getFilename ^FileBody file-body) "testname")))) |
29 | | - |
30 | | - (testing "can create FileBody with content and mime-type and encoding" |
31 | | - (let [testfile (File. "testfile") |
32 | | - file-body (make-file-body {:content (File. "testfile") |
33 | | - :mime-type "application/octet-stream" |
34 | | - :encoding "utf-8"})] |
35 | | - (is (= (.getFile ^FileBody file-body) testfile)))) |
36 | | - |
37 | | - (testing "can create FileBody with content, mime-type, encoding, and name" |
38 | | - (let [testfile (File. "testfile") |
39 | | - file-body (make-file-body {:content (File. "testfile") |
40 | | - :mime-type "application/octet-stream" |
41 | | - :encoding "utf-8" |
42 | | - :name "testname"})] |
43 | | - (is (= (.getFile ^FileBody file-body) testfile)) |
44 | | - (is (= (.getFilename ^FileBody file-body) "testname"))))) |
| 4 | + (:import (java.io File ByteArrayOutputStream ByteArrayInputStream) |
| 5 | + (org.apache.http.entity.mime.content FileBody StringBody ContentBody ByteArrayBody InputStreamBody) |
| 6 | + (java.nio.charset Charset))) |
| 7 | + |
| 8 | +(defn body-str [^StringBody body] |
| 9 | + (-> body .getReader slurp)) |
| 10 | + |
| 11 | +(defn body-bytes [^ContentBody body] |
| 12 | + (let [buf (ByteArrayOutputStream.)] |
| 13 | + (.writeTo body buf) |
| 14 | + (.toByteArray buf))) |
| 15 | + |
| 16 | +(defn body-charset [^ContentBody body] |
| 17 | + (-> body .getContentType .getCharset)) |
| 18 | + |
| 19 | +(defn body-mime-type [^ContentBody body] |
| 20 | + (-> body .getContentType .getMimeType)) |
| 21 | + |
| 22 | +(defn make-input-stream [& bytes] |
| 23 | + (ByteArrayInputStream. (byte-array bytes))) |
| 24 | + |
| 25 | +(deftest test-multipart-body |
| 26 | + (testing "nil content throws exception" |
| 27 | + (is (thrown-with-msg? Exception #"Multipart content cannot be nil" |
| 28 | + (make-multipart-body {:content nil})))) |
| 29 | + |
| 30 | + (testing "unsupported content type throws exception" |
| 31 | + (is (thrown-with-msg? Exception #"Unsupported type for multipart content: class java.lang.Object" |
| 32 | + (make-multipart-body {:content (Object.)})))) |
| 33 | + |
| 34 | + (testing "ContentBody content direct usage" |
| 35 | + (let [contentBody (StringBody. "abc")] |
| 36 | + (is (identical? contentBody (make-multipart-body {:content contentBody}))))) |
| 37 | + |
| 38 | + (testing "StringBody" |
| 39 | + |
| 40 | + (testing "can create StringBody with content only" |
| 41 | + (let [body (make-multipart-body {:content "abc"})] |
| 42 | + (is (instance? StringBody body)) |
| 43 | + (is (= "abc" (body-str body))))) |
| 44 | + |
| 45 | + (testing "can create StringBody with content and encoding" |
| 46 | + (let [body (make-multipart-body {:content "abc" :encoding "ascii"})] |
| 47 | + (is (instance? StringBody body)) |
| 48 | + (is (= "abc" (body-str body))) |
| 49 | + (is (= (Charset/forName "ascii") (body-charset body))))) |
| 50 | + |
| 51 | + (testing "can create StringBody with content and mime-type and encoding" |
| 52 | + (let [body (make-multipart-body {:content "abc" :mime-type "stream-body" :encoding "ascii"})] |
| 53 | + (is (instance? StringBody body)) |
| 54 | + (is (= "abc" (body-str body))) |
| 55 | + (is (= (Charset/forName "ascii") (body-charset body))) |
| 56 | + (is (= "stream-body" (body-mime-type body)))))) |
| 57 | + |
| 58 | + (testing "ByteArrayBody" |
| 59 | + |
| 60 | + (testing "exception thrown on missing name" |
| 61 | + (is (thrown-with-msg? Exception #"Multipart byte array body must contain at least :content and :name" |
| 62 | + (make-multipart-body {:content (byte-array [0 1 2])})))) |
| 63 | + |
| 64 | + (testing "can create ByteArrayBody with name only" |
| 65 | + (let [body (make-multipart-body {:content (byte-array [0 1 2]) :name "testname"})] |
| 66 | + (is (instance? ByteArrayBody body)) |
| 67 | + (is (= "testname" (.getFilename body))) |
| 68 | + (is (= [0 1 2] (vec (body-bytes body)))))) |
| 69 | + |
| 70 | + (testing "can create ByteArrayBody with name and mime-type" |
| 71 | + (let [body (make-multipart-body {:content (byte-array [0 1 2]) |
| 72 | + :name "testname" |
| 73 | + :mime-type "byte-body"})] |
| 74 | + (is (instance? ByteArrayBody body)) |
| 75 | + (is (= "testname" (.getFilename body))) |
| 76 | + (is (= "byte-body" (body-mime-type body))) |
| 77 | + (is (= [0 1 2] (vec (body-bytes body))))))) |
| 78 | + |
| 79 | + (testing "InputStreamBody" |
| 80 | + |
| 81 | + (testing "exception thrown on missing name" |
| 82 | + (is (thrown-with-msg? |
| 83 | + Exception |
| 84 | + #"Multipart input stream body must contain at least :content and :name" |
| 85 | + (make-multipart-body {:content (ByteArrayInputStream. (byte-array [0 1 2]))})))) |
| 86 | + |
| 87 | + (testing "can create InputStreamBody with name and content" |
| 88 | + (let [input-stream (make-input-stream 1 2 3) |
| 89 | + body (make-multipart-body {:content input-stream |
| 90 | + :name "testname"})] |
| 91 | + (is (instance? InputStreamBody body)) |
| 92 | + (is (= "testname" (.getFilename body))) |
| 93 | + (is (identical? input-stream (.getInputStream body))))) |
| 94 | + |
| 95 | + (testing "can create InputStreamBody with name, content and mime-type" |
| 96 | + (let [input-stream (make-input-stream 1 2 3) |
| 97 | + body (make-multipart-body {:content input-stream |
| 98 | + :name "testname" |
| 99 | + :mime-type "input-stream-body"})] |
| 100 | + (is (instance? InputStreamBody body)) |
| 101 | + (is (= "testname" (.getFilename body))) |
| 102 | + (is (= "input-stream-body" (body-mime-type body))) |
| 103 | + (is (identical? input-stream (.getInputStream body))))) |
| 104 | + |
| 105 | + (testing "can create input InputStreamBody name, content, mime-type and length" |
| 106 | + (let [input-stream (make-input-stream 1 2 3) |
| 107 | + body (make-multipart-body {:content input-stream |
| 108 | + :name "testname" |
| 109 | + :mime-type "input-stream-body" |
| 110 | + :length 42})] |
| 111 | + (is (instance? InputStreamBody body)) |
| 112 | + (is (= "testname" (.getFilename body))) |
| 113 | + (is (= "input-stream-body" (body-mime-type body))) |
| 114 | + (is (identical? input-stream (.getInputStream body))) |
| 115 | + (is (= 42 (.getContentLength body)))))) |
| 116 | + |
| 117 | + (testing "FileBody" |
| 118 | + |
| 119 | + (testing "can create FileBody with content only" |
| 120 | + (let [test-file (File. "testfile") |
| 121 | + body (make-multipart-body {:content test-file})] |
| 122 | + (is (instance? FileBody body)) |
| 123 | + (is (= test-file (.getFile body))))) |
| 124 | + |
| 125 | + (testing "can create FileBody with content and mime-type" |
| 126 | + (let [test-file (File. "testfile") |
| 127 | + body (make-multipart-body {:content test-file |
| 128 | + :mime-type "file-body"})] |
| 129 | + (is (instance? FileBody body)) |
| 130 | + (is (= "file-body" (body-mime-type body))) |
| 131 | + (is (= test-file (.getFile body))))) |
| 132 | + |
| 133 | + (testing "can create FileBody with content, mime-type and name" |
| 134 | + (let [test-file (File. "testfile") |
| 135 | + body (make-multipart-body {:content test-file |
| 136 | + :mime-type "file-body" |
| 137 | + :name "testname"})] |
| 138 | + (is (instance? FileBody body)) |
| 139 | + (is (= "file-body" (body-mime-type body))) |
| 140 | + (is (= test-file (.getFile body))) |
| 141 | + (is (= "testname" (.getFilename body))))) |
| 142 | + |
| 143 | + (testing "can create FileBody with content and mime-type and encoding" |
| 144 | + (let [test-file (File. "testfile") |
| 145 | + body (make-multipart-body {:content test-file |
| 146 | + :mime-type "file-body" |
| 147 | + :encoding "ascii"})] |
| 148 | + (is (instance? FileBody body)) |
| 149 | + (is (= "file-body" (body-mime-type body))) |
| 150 | + (is (= (Charset/forName "ascii") (body-charset body))) |
| 151 | + (is (= test-file (.getFile body))))) |
| 152 | + |
| 153 | + (testing "can create FileBody with content, mime-type, encoding and name" |
| 154 | + (let [test-file (File. "testfile") |
| 155 | + body (make-multipart-body {:content test-file |
| 156 | + :mime-type "file-body" |
| 157 | + :encoding "ascii" |
| 158 | + :name "testname"})] |
| 159 | + (is (instance? FileBody body)) |
| 160 | + (is (= "file-body" (body-mime-type body))) |
| 161 | + (is (= (Charset/forName "ascii") (body-charset body))) |
| 162 | + (is (= test-file (.getFile body) )) |
| 163 | + (is (= "testname" (.getFilename body))))))) |
0 commit comments