/
jetty.clj
70 lines (62 loc) · 2.71 KB
/
jetty.clj
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
(ns ring.adapter.test.jetty
(:use clojure.test
ring.adapter.jetty)
(:require [clj-http.client :as http])
(:import (org.eclipse.jetty.util.thread QueuedThreadPool)
(org.eclipse.jetty.server Server Request)
(org.eclipse.jetty.server.handler AbstractHandler)))
(defn- hello-world [request]
{:status 200
:headers {"Content-Type" "text/plain"}
:body "Hello World"})
(defn- content-type-handler [content-type]
(constantly
{:status 200
:headers {"Content-Type" content-type}
:body ""}))
(defmacro with-server [app options & body]
`(let [server# (run-jetty ~app ~(assoc options :join? false))]
(try
~@body
(finally (.stop server#)))))
(deftest test-run-jetty
(testing "HTTP server"
(with-server hello-world {:port 4347}
(let [response (http/get "http://localhost:4347")]
(is (= (:status response) 200))
(is (.startsWith (get-in response [:headers "content-type"])
"text/plain"))
(is (= (:body response) "Hello World")))))
(testing "HTTPS server"
(with-server hello-world {:port 4347
:ssl-port 4348
:keystore "test/keystore.jks"
:key-password "password"}
(let [response (http/get "https://localhost:4348" {:insecure? true})]
(is (= (:status response) 200))
(is (= (:body response) "Hello World")))))
(testing "configurator set to run last"
(let [max-threads 20
new-handler (proxy [AbstractHandler] []
(handle [_ ^Request base-request request response]))
threadPool (QueuedThreadPool. ({} :max-threads max-threads))
configurator (fn [server]
(.setThreadPool server threadPool)
(.setHandler server new-handler))
server (run-jetty hello-world
{:join? false :port 4347 :configurator configurator})]
(is (= (.getMaxThreads (.getThreadPool server)) max-threads))
(is (identical? new-handler (.getHandler server)))
(is (= 1 (count (.getHandlers server))))
(.stop server)))
(testing "default character encoding"
(with-server (content-type-handler "text/plain") {:port 4347}
(let [response (http/get "http://localhost:4347")]
(is (.contains
(get-in response [:headers "content-type"])
"text/plain")))))
(testing "custom content-type"
(with-server (content-type-handler "text/plain;charset=UTF-16;version=1") {:port 4347}
(let [response (http/get "http://localhost:4347")]
(is (= (get-in response [:headers "content-type"])
"text/plain;charset=UTF-16;version=1"))))))