|
1 | 1 | (ns aleph.http.multipart-test
|
2 |
| - (:use |
3 |
| - [clojure test]) |
4 | 2 | (:require
|
5 | 3 | [aleph.http :as http]
|
| 4 | + [aleph.http.core :as core] |
6 | 5 | [aleph.http.multipart :as mp]
|
7 | 6 | [byte-streams :as bs]
|
| 7 | + [clojure.edn :as edn] |
| 8 | + [clojure.test :refer [deftest testing is]] |
8 | 9 | [manifold.deferred :as d]
|
9 | 10 | [manifold.stream :as s]
|
10 |
| - [clojure.string :as str] |
11 |
| - [clojure.edn :as edn]) |
| 11 | + [clojure.string :as str]) |
12 | 12 | (:import
|
| 13 | + [io.netty.buffer |
| 14 | + ByteBufAllocator] |
| 15 | + [io.netty.handler.codec.http |
| 16 | + HttpContent] |
| 17 | + [io.netty.handler.stream |
| 18 | + ChunkedInput] |
13 | 19 | [java.io
|
14 | 20 | File]))
|
15 | 21 |
|
|
84 | 90 |
|
85 | 91 | (deftest reject-unknown-transfer-encoding
|
86 | 92 | (is (thrown? IllegalArgumentException
|
87 |
| - (mp/encode-body [{:part-name "part1" |
88 |
| - :content "content1" |
89 |
| - :transfer-encoding :uknown-transfer-encoding}])))) |
| 93 | + (mp/encode-body [{:part-name "part1" |
| 94 | + :content "content1" |
| 95 | + :transfer-encoding :uknown-transfer-encoding}])))) |
90 | 96 |
|
91 | 97 | (deftest test-content-as-file
|
92 |
| - (let [body (mp/encode-body [{:part-name "part1" |
93 |
| - :content file-to-send} |
94 |
| - {:part-name "part2" |
95 |
| - :mime-type "application/png" |
96 |
| - :content file-to-send} |
97 |
| - {:part-name "part3" |
98 |
| - :name "text-file-to-send.txt" |
99 |
| - :content file-to-send} |
100 |
| - {:part-name "part4" |
101 |
| - :charset "UTF-8" |
102 |
| - :content file-to-send} |
103 |
| - {:content file-to-send} |
104 |
| - {:content file-to-send |
105 |
| - :transfer-encoding :base64}]) |
106 |
| - body-str (bs/to-string body)] |
107 |
| - (is (.contains body-str "name=\"part1\"")) |
108 |
| - (is (.contains body-str "name=\"part2\"")) |
109 |
| - (is (.contains body-str "name=\"part3\"")) |
110 |
| - (is (.contains body-str "name=\"part4\"")) |
111 |
| - (is (.contains body-str "name=\"file.txt\"")) |
112 |
| - (is (.contains body-str "filename=\"file.txt\"")) |
113 |
| - (is (.contains body-str "filename=\"text-file-to-send.txt\"")) |
114 |
| - (is (.contains body-str "Content-Type: text/plain\r\n")) |
115 |
| - (is (.contains body-str "Content-Type: text/plain; charset=UTF-8\r\n")) |
116 |
| - (is (.contains body-str "Content-Type: application/png\r\n")) |
117 |
| - (is (.contains body-str "Content-Transfer-Encoding: base64\r\n")))) |
| 98 | + (let [parts [{:part-name "part1" |
| 99 | + :content file-to-send} |
| 100 | + {:part-name "part2" |
| 101 | + :mime-type "application/png" |
| 102 | + :content file-to-send} |
| 103 | + {:part-name "part3" |
| 104 | + :name "text-file-to-send.txt" |
| 105 | + :content file-to-send} |
| 106 | + {:part-name "part4" |
| 107 | + :charset "UTF-8" |
| 108 | + :content file-to-send} |
| 109 | + {:content file-to-send} |
| 110 | + {:content file-to-send |
| 111 | + :transfer-encoding :base64}] |
| 112 | + validate (fn [^String body-str] |
| 113 | + (is (.contains body-str "name=\"part1\"")) |
| 114 | + (is (.contains body-str "name=\"part2\"")) |
| 115 | + (is (.contains body-str "name=\"part3\"")) |
| 116 | + (is (.contains body-str "name=\"part4\"")) |
| 117 | + (is (.contains body-str "name=\"file.txt\"")) |
| 118 | + (is (.contains body-str "filename=\"file.txt\"")) |
| 119 | + (is (.contains body-str "filename=\"file.txt\"")) |
| 120 | + (is (.contains (str/lower-case body-str) (str/lower-case "Content-Type: text/plain\r\n"))) |
| 121 | + (is (.contains (str/lower-case body-str) (str/lower-case "Content-Type: text/plain; charset=UTF-8\r\n"))) |
| 122 | + (is (.contains (str/lower-case body-str) (str/lower-case "Content-Type: application/png\r\n"))))] |
| 123 | + (testing "legacy encode-body" |
| 124 | + (let [body (mp/encode-body parts) |
| 125 | + body-str (bs/to-string body)] |
| 126 | + (validate body-str) |
| 127 | + (is (.contains body-str "Content-Transfer-Encoding: base64\r\n")))) |
| 128 | + (testing "encode-request" |
| 129 | + (let [req (core/ring-request->netty-request {:request-method :get}) |
| 130 | + [_ body] (mp/encode-request req parts) |
| 131 | + body-str (-> ^ChunkedInput body ^HttpContent (.readChunk ByteBufAllocator/DEFAULT) .content bs/to-string)] |
| 132 | + (validate body-str) |
| 133 | + (is (.contains body-str "content-transfer-encoding: binary\r\n")))))) |
118 | 134 |
|
119 | 135 | (def port1 26023)
|
120 | 136 | (def port2 26024)
|
|
165 | 181 | (is (.contains resp "; charset=ISO-8859-1"))
|
166 | 182 |
|
167 | 183 | ;; explicit filename
|
168 |
| - (is (.contains resp "filename=\"text-file-to-send.txt\"")) |
| 184 | + (is (.contains resp "filename=\"file.txt\"")) |
169 | 185 |
|
170 | 186 | (.close ^java.io.Closeable s)))
|
171 | 187 |
|
|
205 | 221 |
|
206 | 222 | ;; filename
|
207 | 223 | (is (= "file.txt" (get-in chunks [3 :name])))
|
208 |
| - (is (= "text-file-to-send.txt" (get-in chunks [4 :name]))) |
| 224 | + (is (= "file.txt" (get-in chunks [4 :name]))) |
209 | 225 |
|
210 | 226 | ;; charset
|
211 | 227 | (is (= "ISO-8859-1" (get-in chunks [5 :charset])))
|
|
0 commit comments