|
9 | 9 | [byte-streams :as bs]
|
10 | 10 | [aleph.http :as http]
|
11 | 11 | [aleph.http.core :as http-core]
|
| 12 | + [aleph.http.server :as http-server] |
12 | 13 | [clojure.tools.logging :as log]))
|
13 | 14 |
|
14 | 15 | (netty/leak-detector-level! :paranoid)
|
|
33 | 34 | `(with-server (http/start-server ~handler {:port 8080, :compression? true})
|
34 | 35 | ~@body))
|
35 | 36 |
|
| 37 | +(defn connection-handler |
| 38 | + ([req] (connection-handler {} req)) |
| 39 | + ([options req] |
| 40 | + (if (http-server/websocket-upgrade-request? req) |
| 41 | + (-> (http/websocket-connection req options) |
| 42 | + (d/chain' #(s/connect % %)) |
| 43 | + (d/catch' |
| 44 | + (fn [^Throwable e] |
| 45 | + (log/error "upgrade to websocket conn failed" |
| 46 | + (.getMessage e)) |
| 47 | + {})))))) |
| 48 | + |
36 | 49 | (defn echo-handler
|
37 | 50 | ([req] (echo-handler {} req))
|
38 | 51 | ([options req]
|
|
53 | 66 | (.getMessage e))
|
54 | 67 | {}))))
|
55 | 68 |
|
| 69 | +(deftest test-connection-header |
| 70 | + (with-handler connection-handler |
| 71 | + (let [c @(http/websocket-client "ws://localhost:8080")] |
| 72 | + (is @(s/put! c "hello")) |
| 73 | + (is (= "hello" @(s/try-take! c 5e3))) |
| 74 | + (is (= "upgrade" (get-in (s/description c) [:sink :websocket-handshake-headers "connection"])))) |
| 75 | + (let [c @(http/websocket-client "ws://localhost:8080" {:headers {:connection "keep-alive, Upgrade"}})] |
| 76 | + (is @(s/put! c "hello")) |
| 77 | + (is (= "hello" @(s/try-take! c 5e3))) |
| 78 | + (is (= "upgrade" (get-in (s/description c) [:sink :websocket-handshake-headers "connection"])))) |
| 79 | + (is (= 204 (:status @(http/get "http://localhost:8080" |
| 80 | + {:throw-exceptions false}))))) |
| 81 | + ) |
| 82 | + |
56 | 83 | (deftest test-echo-handler
|
57 | 84 | (with-handler echo-handler
|
58 | 85 | (let [c @(http/websocket-client "ws://localhost:8080")]
|
|
0 commit comments