-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_server.clj
More file actions
38 lines (35 loc) · 1.3 KB
/
test_server.clj
File metadata and controls
38 lines (35 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
(ns ^:skip-test java-http-clj.test-server
(:import org.eclipse.jetty.server.Server)
(:require [clojure.java.io :as io]
[clojure.test :refer :all]
[compojure.core :refer :all]
[compojure.route :as route]
[mount.core :as mount]
[ring.adapter.jetty9 :as jetty]
[ring.middleware.defaults :refer :all]))
(defroutes app
(GET "/" [] "ROOT")
(GET "/echo" [message] (or message "no message"))
(POST "/echo" r (slurp (io/reader (:body r))))
(PUT "/echo" r (slurp (io/reader (:body r))))
(DELETE "/echo" [] "deleted")
(GET "/redir" [] {:status 302 :headers {"Location" "/target"}})
(GET "/target" [] "did redirect"))
(def ws-handler {:on-connect (fn [ws])
:on-error (fn [ws e])
:on-close (fn [ws status-code reason])
:on-text
(fn [ws text-message]
(jetty/send! ws (str "SERVER: " text-message)))
:on-bytes
(fn [ws bs offset len]
(jetty/send! ws bs))})
(mount/defstate server
:start
(let [{:keys [port] :or {port 8080}} (mount/args)]
(jetty/run-jetty
(wrap-defaults app api-defaults)
{:port port
:websockets {"/ws" ws-handler}
:join? false}))
:stop (.stop ^Server server))