|
1 | 1 | (ns aleph.http-test
|
2 | 2 | (:require
|
3 |
| - [clojure.java.io :as io] |
4 |
| - [clojure.string :as str] |
5 |
| - [clojure.test :refer [deftest testing is]] |
6 |
| - [aleph |
7 |
| - [http :as http] |
8 |
| - [netty :as netty] |
9 |
| - [flow :as flow] |
10 |
| - [ssl :as ssl] |
11 |
| - [tcp :as tcp]] |
12 |
| - [clj-commons.byte-streams :as bs] |
13 |
| - [manifold.deferred :as d] |
14 |
| - [manifold.stream :as s] |
15 |
| - [manifold.time :as t]) |
| 3 | + [aleph.flow :as flow] |
| 4 | + [aleph.http :as http] |
| 5 | + [aleph.netty :as netty] |
| 6 | + [aleph.ssl :as ssl] |
| 7 | + [aleph.tcp :as tcp] |
| 8 | + [clj-commons.byte-streams :as bs] |
| 9 | + [clojure.java.io :as io] |
| 10 | + [clojure.string :as str] |
| 11 | + [clojure.test :refer [deftest is testing]] |
| 12 | + [manifold.deferred :as d] |
| 13 | + [manifold.stream :as s] |
| 14 | + [manifold.time :as t]) |
16 | 15 | (:import
|
17 |
| - (java.io |
18 |
| - File) |
19 |
| - (java.util.zip |
20 |
| - GZIPInputStream |
21 |
| - ZipException) |
22 |
| - (java.util.concurrent |
23 |
| - TimeoutException) |
24 |
| - (aleph.utils |
25 |
| - ConnectionTimeoutException |
26 |
| - RequestTimeoutException) |
27 |
| - (io.netty.channel |
28 |
| - ChannelHandlerContext |
29 |
| - ChannelOutboundHandlerAdapter |
30 |
| - ChannelPipeline |
31 |
| - ChannelPromise) |
32 |
| - (io.netty.handler.codec.http |
33 |
| - HttpMessage))) |
| 16 | + (aleph.utils ConnectionTimeoutException RequestTimeoutException) |
| 17 | + (io.netty.channel ChannelHandlerContext ChannelOutboundHandlerAdapter ChannelPipeline ChannelPromise) |
| 18 | + (io.netty.handler.codec.http HttpMessage) |
| 19 | + (java.io File) |
| 20 | + (java.util.concurrent TimeoutException) |
| 21 | + (java.util.zip GZIPInputStream ZipException))) |
34 | 22 |
|
35 | 23 | ;;;
|
36 | 24 |
|
|
69 | 57 | (def http-file-region-response (http/file filepath 5 4))
|
70 | 58 | (def stream-response "Stream!")
|
71 | 59 |
|
72 |
| -(defn string-handler [request] |
| 60 | +(defn string-handler [_request] |
73 | 61 | {:status 200
|
74 | 62 | :body string-response})
|
75 | 63 |
|
76 |
| -(defn seq-handler [request] |
| 64 | +(defn seq-handler [_request] |
77 | 65 | {:status 200
|
78 | 66 | :body seq-response})
|
79 | 67 |
|
80 |
| -(defn file-handler [request] |
| 68 | +(defn file-handler [_request] |
81 | 69 | {:status 200
|
82 | 70 | :body file-response})
|
83 | 71 |
|
84 |
| -(defn http-file-handler [request] |
| 72 | +(defn http-file-handler [_request] |
85 | 73 | {:status 200
|
86 | 74 | :body http-file-response})
|
87 | 75 |
|
88 |
| -(defn http-file-region-handler [request] |
| 76 | +(defn http-file-region-handler [_request] |
89 | 77 | {:status 200
|
90 | 78 | :body http-file-region-response})
|
91 | 79 |
|
92 |
| -(defn stream-handler [request] |
| 80 | +(defn stream-handler [_request] |
93 | 81 | {:status 200
|
94 | 82 | :body (bs/to-input-stream stream-response)})
|
95 | 83 |
|
96 |
| -(defn slow-handler [request] |
| 84 | +(defn slow-handler [_request] |
97 | 85 | {:status 200
|
98 | 86 | :body (cons "1" (lazy-seq (do (Thread/sleep 500) '("2"))))})
|
99 | 87 |
|
100 |
| -(defn manifold-handler [request] |
| 88 | +(defn manifold-handler [_request] |
101 | 89 | {:status 200
|
102 | 90 | :body (->> stream-response (map str) s/->source)})
|
103 | 91 |
|
|
109 | 97 | {:status 200
|
110 | 98 | :body (->> request :body bs/to-line-seq)})
|
111 | 99 |
|
112 |
| -(defn hello-handler [request] |
| 100 | +(defn hello-handler [_request] |
113 | 101 | {:status 200
|
114 | 102 | :body "hello"})
|
115 | 103 |
|
116 |
| -(defn big-handler [request] |
| 104 | +(defn big-handler [_request] |
117 | 105 | {:status 200
|
118 | 106 | :body (->> (s/periodically 0.1 #(byte-array 1024))
|
119 |
| - (s/transform (take 1e3)))}) |
| 107 | + (s/transform (take (long 1e3))))}) |
120 | 108 |
|
121 | 109 | (defn redirect-handler [{:keys [query-string] :as request}]
|
122 | 110 | (let [count (-> (.split #"[?=]" query-string) second Integer/parseInt)
|
|
152 | 140 | (try
|
153 | 141 | (deliver latch true) ;;this can be triggered more than once, sometimes
|
154 | 142 | (.close ^java.io.Closeable @browser-server)
|
155 |
| - (catch Exception e)))}) |
| 143 | + (catch Exception _)))}) |
156 | 144 |
|
157 | 145 | (defn print-vals [& args]
|
158 | 146 | (apply prn args)
|
|
215 | 203 |
|
216 | 204 | (deftest test-response-formats
|
217 | 205 | (with-handler basic-handler
|
218 |
| - (doseq [[index [path result]] (map-indexed vector expected-results)] |
219 |
| - (is |
220 |
| - (= result |
221 |
| - (bs/to-string |
222 |
| - (:body |
223 |
| - @(http-get (str "http://localhost:" port "/" path))))))))) |
| 206 | + (doseq [[path result] expected-results] |
| 207 | + (is (= result |
| 208 | + (bs/to-string |
| 209 | + (:body |
| 210 | + @(http-get (str "http://localhost:" port "/" path))))))))) |
224 | 211 |
|
225 | 212 | (deftest test-compressed-response
|
226 | 213 | (with-compressed-handler basic-handler
|
227 |
| - (doseq [[index [path result]] (map-indexed vector expected-results) |
| 214 | + (doseq [[path result] expected-results |
228 | 215 | :let [resp @(http-get (str "http://localhost:" port "/" path)
|
229 |
| - {:headers {:accept-encoding "gzip"}}) |
| 216 | + {:headers {:accept-encoding "gzip"}}) |
230 | 217 | unzipped (try
|
231 | 218 | (bs/to-string (GZIPInputStream. (:body resp)))
|
232 | 219 | (catch ZipException _ nil))]]
|
|
237 | 224 |
|
238 | 225 | (deftest test-ssl-response-formats
|
239 | 226 | (with-handler-options basic-handler default-ssl-options
|
240 |
| - (doseq [[index [path result]] (map-indexed vector expected-results)] |
| 227 | + (doseq [[path result] expected-results] |
241 | 228 | (is
|
242 | 229 | (= result
|
243 | 230 | (bs/to-string
|
|
297 | 284 | deref))))
|
298 | 285 |
|
299 | 286 | (deftest test-overly-long-url
|
300 |
| - (let [long-url (apply str "http://localhost:" port "/" (repeat 1e4 "a"))] |
| 287 | + (let [long-url (apply str "http://localhost:" port "/" (repeat (long 1e4) "a"))] |
301 | 288 | (with-handler basic-handler
|
302 | 289 | (is (= 414 (:status @(http-get long-url)))))))
|
303 | 290 |
|
304 | 291 | (deftest test-overly-long-header
|
305 | 292 | (let [url (str "http://localhost:" port)
|
306 |
| - long-header-value (apply str (repeat 1e5 "a")) |
| 293 | + long-header-value (apply str (repeat (long 1e5) "a")) |
307 | 294 | opts {:headers {"X-Long" long-header-value}}]
|
308 | 295 | (with-handler basic-handler
|
309 | 296 | (is (= 431 (:status @(http-get url opts)))))))
|
|
436 | 423 | (let [rsp (http/get (str "http://localhost:" port) {:connection-pool pool})]
|
437 | 424 | (is (= http/default-response-executor (.executor rsp))))))))
|
438 | 425 |
|
439 |
| -(defn echo-handler [req] |
440 |
| - {:status 200 |
441 |
| - :body (:body req)}) |
442 |
| - |
443 | 426 | (deftest test-trace-request-omitted-body
|
444 | 427 | (with-handler echo-handler
|
445 | 428 | (is (= "" (-> @(http/trace (str "http://localhost:" port) {:body "REQUEST"})
|
|
496 | 479 | []
|
497 | 480 | (let [body (s/stream 10)]
|
498 | 481 | (-> (d/loop [cnt 0]
|
499 |
| - (t/in 50 |
| 482 | + (t/in 50.0 |
500 | 483 | (fn []
|
501 | 484 | (d/chain' (s/put! body (str cnt))
|
502 | 485 | (fn [_]
|
|
533 | 516 |
|
534 | 517 | (deftest test-large-responses
|
535 | 518 | (with-handler basic-handler
|
536 |
| - (let [pool (http/connection-pool {:connection-options {:response-buffer-size 16}})] |
537 |
| - (dotimes [i 1 #_1e6] |
538 |
| - #_(when (zero? (rem i 1e2)) |
539 |
| - (prn i)) |
540 |
| - (-> @(http/get (str "http://localhost:" port "/big") |
541 |
| - {:as :byte-array}) |
| 519 | + (dotimes [_ 1 #_1e6] |
| 520 | + #_(when (zero? (rem i 1e2)) |
| 521 | + (prn i)) |
| 522 | + (-> @(http/get (str "http://localhost:" port "/big") |
| 523 | + {:as :byte-array}) |
542 | 524 | :body
|
543 |
| - count))))) |
| 525 | + count)))) |
544 | 526 |
|
545 | 527 | ;;;
|
546 | 528 |
|
|
0 commit comments