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 71 72
|
(ns compojure.route-test
(:require [clojure.test :refer :all]
[ring.mock.request :as mock]
[compojure.route :as route]))
(deftest not-found-route
(testing "string body"
(let [response ((route/not-found "foo") (mock/request :get "/"))]
(is (= (:status response) 404))
(is (= (:body response) "foo"))))
(testing "response map body"
(let [response ((route/not-found {:status 200 :body "bar"})
(mock/request :get "/"))]
(is (= (:status response) 404))
(is (= (:body response) "bar"))))
(testing "async arity"
(let [handler (route/not-found "baz")
response (promise)
exception (promise)]
(handler (mock/request :get "/") response exception)
(is (not (realized? exception)))
(is (= (:status @response) 404))
(is (= (:body @response) "baz")))))
(deftest resources-route
(let [route (route/resources "/foo" {:root "test_files"})
response (route (mock/request :get "/foo/test.txt"))]
(is (= (:status response) 200))
(is (= (slurp (:body response)) "foobar\n"))
(is (= (get-in response [:headers "Content-Type"])
"text/plain"))))
(deftest files-route
(testing "text file"
(let [route (route/files "/foo" {:root "test/test_files"})
response (route (mock/request :get "/foo/test.txt"))]
(is (= (:status response) 200))
(is (= (slurp (:body response)) "foobar\n"))
(is (= (get-in response [:headers "Content-Type"])
"text/plain"))))
(testing "root"
(let [route (route/files "/" {:root "test/test_files"})
response (route (mock/request :get "/"))]
(is (= (:status response) 200))
(is (= (slurp (:body response)) "<!doctype html><title></title>\n"))
(is (= (get-in response [:headers "Content-Type"])
"text/html")))))
(deftest head-method
(testing "not found"
(let [response ((route/not-found {:status 200
:headers {"Content-Type" "text/plain"}
:body "bar"})
(mock/request :head "/"))]
(is (= (:status response) 404))
(is (nil? (:body response)))
(is (= (get-in response [:headers "Content-Type"])
"text/plain"))))
(testing "resources"
(let [route (route/resources "/foo" {:root "test_files"})
response (route (mock/request :head "/foo/test.txt"))]
(is (= (:status response) 200))
(is (nil? (:body response)))
(is (= (get-in response [:headers "Content-Type"])
"text/plain"))))
(testing "files"
(let [route (route/files "/foo" {:root "test/test_files"})
response (route (mock/request :head "/foo/test.txt"))]
(is (= (:status response) 200))
(is (nil? (:body response)))
(is (= (get-in response [:headers "Content-Type"])
"text/plain")))))
|