Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tobias
Created September 4, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobias/22a799e06969276e542e to your computer and use it in GitHub Desktop.
Save tobias/22a799e06969276e542e to your computer and use it in GitHub Desktop.
(deftest websocket-from-servlet
(let [path "/test-servlet"
events (atom [])
result (promise)
handler (fn [req]
(as-channel req
{:on-open (fn [_]
(swap! events conj :open))
:on-close (fn [_ {c :code}]
(deliver result (swap! events conj c)))
:on-message (fn [_ m]
(swap! events conj m))}))
servlet (create-servlet handler)]
(try
(run servlet {:path path})
(let [socket (ws/connect (str "ws://localhost:8080" path))]
(ws/send-msg socket "hello")
(ws/close socket))
(is (= [:open "hello" 1001] (deref result 2000 [:fail])))
(finally
(stop {:path path})))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment