Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

107 lines (100 sloc) 4.333 kb
(ns ring.util.test.servlet
(:use clojure.test
(defn- enumeration [coll]
(let [e (atom coll)]
(proxy [java.util.Enumeration] []
(hasMoreElements [] (not (empty? @e)))
(nextElement [] (let [f (first @e)] (swap! e rest) f)))))
(defn- servlet-request [request]
(let [attributes {"javax.servlet.request.X509Certificate"
[(request :ssl-client-cert)]}]
(proxy [javax.servlet.http.HttpServletRequest] []
(getServerPort [] (request :server-port))
(getServerName [] (request :server-name))
(getRemoteAddr [] (request :remote-addr))
(getRequestURI [] (request :uri))
(getQueryString [] (request :query-string))
(getScheme [] (name (request :scheme)))
(getMethod [] (-> request :request-method name .toUpperCase))
(getHeaderNames [] (enumeration (keys (request :headers))))
(getHeaders [name] (enumeration (get-in request [:headers name])))
(getContentType [] (request :content-type))
(getContentLength [] (or (request :content-length) -1))
(getCharacterEncoding [] (request :character-encoding))
(getAttribute [k] (attributes k))
(getInputStream [] (request :body)))))
(defn- servlet-response [response]
(proxy [javax.servlet.http.HttpServletResponse] []
(setStatus [status]
(swap! response assoc :status status))
(setHeader [name value]
(swap! response assoc-in [:headers name] value))
(setCharacterEncoding [value]
(swap! response assoc :character-encoding value))
(setContentType [value]
(swap! response assoc :content-type value))))
(defn- servlet-config []
(proxy [javax.servlet.ServletConfig] []
(getServletContext [] nil)))
(defn- run-servlet [handler request response]
(doto (servlet handler)
(.init (servlet-config))
(.service (servlet-request request)
(servlet-response response))))
(deftest servlet-test
(let [body (proxy [javax.servlet.ServletInputStream] [])
cert (proxy [] [])
request {:server-port 8080
:server-name "foobar"
:remote-addr ""
:uri "/foo"
:query-string "a=b"
:scheme :http
:request-method :get
:headers {"X-Client" ["Foo", "Bar"]
"X-Server" ["Baz"]}
:content-type "text/plain"
:content-length 10
:character-encoding "UTF-8"
:ssl-client-cert cert
:body body}
response (atom {})]
(testing "request"
(letfn [(handler [r]
(are [k v] (= (r k) v)
:server-port 8080
:server-name "foobar"
:remote-addr ""
:uri "/foo"
:query-string "a=b"
:scheme :http
:request-method :get
:headers {"x-client" "Foo,Bar"
"x-server" "Baz"}
:content-type "text/plain"
:content-length 10
:character-encoding "UTF-8"
:ssl-client-cert cert
:body body)
{:status 200, :headers {}})]
(run-servlet handler request response)))
(testing "response"
(letfn [(handler [r]
{:status 200
:headers {"Content-Type" "text/plain"
"X-Server" "Bar"}
:body nil})]
(run-servlet handler request response)
(is (= (@response :status) 200))
(is (= (@response :content-type) "text/plain"))
(is (= (@response :character-encoding) "UTF-8"))
(is (= (get-in @response [:headers "X-Server"]) "Bar"))))
(testing "response with character encoding"
(letfn [(handler [r]
{:status 200
:headers {"Content-Type" "text/plain; charset=utf-16"}
:body nil})]
(run-servlet handler request response)
(is (= (@response :content-type) "text/plain"))
(is (= (@response :character-encoding) "utf-16"))))))
Jump to Line
Something went wrong with that request. Please try again.