mirror of
https://github.com/metosin/reitit.git
synced 2026-02-07 04:13:12 +00:00
Better tests for the static files
This commit is contained in:
parent
a99789216c
commit
80283dbbd2
1 changed files with 148 additions and 49 deletions
|
|
@ -3,7 +3,8 @@
|
||||||
[clojure.set :as set]
|
[clojure.set :as set]
|
||||||
[reitit.middleware :as middleware]
|
[reitit.middleware :as middleware]
|
||||||
[reitit.ring :as ring]
|
[reitit.ring :as ring]
|
||||||
[reitit.core :as r])
|
[reitit.core :as r]
|
||||||
|
[clojure.string :as str])
|
||||||
#?(:clj
|
#?(:clj
|
||||||
(:import (clojure.lang ExceptionInfo))))
|
(:import (clojure.lang ExceptionInfo))))
|
||||||
|
|
||||||
|
|
@ -267,56 +268,154 @@
|
||||||
|
|
||||||
#?(:clj
|
#?(:clj
|
||||||
(deftest resource-handler-test
|
(deftest resource-handler-test
|
||||||
(doseq [[test app] [["inside a router"
|
(let [redirect (fn [uri] {:status 302 :headers {"Location" uri}})
|
||||||
(ring/ring-handler
|
request (fn [uri] {:uri uri, :request-method :get})]
|
||||||
(ring/router
|
(testing "inside a router"
|
||||||
[["/ping" (constantly {:status 200, :body "pong"})]
|
|
||||||
["/files/*" (ring/create-resource-handler)]
|
|
||||||
["/*" (ring/create-resource-handler)]]
|
|
||||||
{:conflicts (constantly nil)})
|
|
||||||
(ring/create-default-handler))]
|
|
||||||
|
|
||||||
["outside of a router"
|
(testing "from root"
|
||||||
(ring/ring-handler
|
(let [app (ring/ring-handler
|
||||||
(ring/router
|
(ring/router
|
||||||
["/ping" (constantly {:status 200, :body "pong"})])
|
["/*" (ring/create-resource-handler)])
|
||||||
(ring/routes
|
(ring/create-default-handler))]
|
||||||
(ring/create-resource-handler {:path "/files"})
|
(testing test
|
||||||
(ring/create-resource-handler {:path "/"})
|
(testing "different file-types"
|
||||||
(ring/create-default-handler)))]]
|
(let [response (app (request "/hello.json"))]
|
||||||
prefix ["" "/" "/files" "/files/"]
|
(is (= "application/json" (get-in response [:headers "Content-Type"])))
|
||||||
:let [request (fn [uri] {:uri (str prefix uri), :request-method :get})]]
|
(is (get-in response [:headers "Last-Modified"]))
|
||||||
|
(is (= "{\"hello\": \"file\"}" (slurp (:body response)))))
|
||||||
|
(let [response (app (request "/hello.xml"))]
|
||||||
|
(is (= "text/xml" (get-in response [:headers "Content-Type"])))
|
||||||
|
(is (get-in response [:headers "Last-Modified"]))
|
||||||
|
(is (= "<xml><hello>file</hello></xml>\n" (slurp (:body response))))))
|
||||||
|
|
||||||
(testing test
|
(testing "index-files"
|
||||||
(testing "different file-types"
|
(let [response (app (request "/docs"))]
|
||||||
(let [response (app (request "/hello.json"))]
|
(is (= (redirect "/docs/index.html") response)))
|
||||||
(is (= "application/json" (get-in response [:headers "Content-Type"])))
|
(let [response (app (request "/docs/"))]
|
||||||
(is (get-in response [:headers "Last-Modified"]))
|
(is (= (redirect "/docs/index.html") response))))
|
||||||
(is (= "{\"hello\": \"file\"}" (slurp (:body response)))))
|
|
||||||
(let [response (app (request "/hello.xml"))]
|
|
||||||
(is (= "text/xml" (get-in response [:headers "Content-Type"])))
|
|
||||||
(is (get-in response [:headers "Last-Modified"]))
|
|
||||||
(is (= "<xml><hello>file</hello></xml>\n" (slurp (:body response))))))
|
|
||||||
|
|
||||||
(testing "index-files"
|
(testing "not found"
|
||||||
(let [response (app (request "/docs"))]
|
(let [response (app (request "/not-found"))]
|
||||||
(is (= "text/html" (get-in response [:headers "Content-Type"])))
|
(is (= 404 (:status response)))))
|
||||||
(is (get-in response [:headers "Last-Modified"]))
|
|
||||||
(is (= "<h1>hello</h1>\n" (slurp (:body response)))))
|
|
||||||
(let [response (app (request "/docs/"))]
|
|
||||||
(is (= "text/html" (get-in response [:headers "Content-Type"])))
|
|
||||||
(is (get-in response [:headers "Last-Modified"]))
|
|
||||||
(is (= "<h1>hello</h1>\n" (slurp (:body response))))))
|
|
||||||
|
|
||||||
(testing "not found"
|
(testing "3-arity"
|
||||||
(let [response (app (request "/not-found"))]
|
(let [result (atom nil)
|
||||||
(is (= 404 (:status response)))))
|
respond (partial reset! result)
|
||||||
|
raise ::not-called]
|
||||||
|
(app (request "/hello.xml") respond raise)
|
||||||
|
(is (= "text/xml" (get-in @result [:headers "Content-Type"])))
|
||||||
|
(is (get-in @result [:headers "Last-Modified"]))
|
||||||
|
(is (= "<xml><hello>file</hello></xml>\n" (slurp (:body @result)))))))))
|
||||||
|
|
||||||
(testing "3-arity"
|
(testing "from path"
|
||||||
(let [result (atom nil)
|
(let [app (ring/ring-handler
|
||||||
respond (partial reset! result)
|
(ring/router
|
||||||
raise ::not-called]
|
["/files/*" (ring/create-resource-handler)])
|
||||||
(app (request "/hello.xml") respond raise)
|
(ring/create-default-handler))
|
||||||
(is (= "text/xml" (get-in @result [:headers "Content-Type"])))
|
request #(request (str "/files" %))
|
||||||
(is (get-in @result [:headers "Last-Modified"]))
|
redirect #(redirect (str "/files" %))]
|
||||||
(is (= "<xml><hello>file</hello></xml>\n" (slurp (:body @result))))))))))
|
(testing test
|
||||||
|
(testing "different file-types"
|
||||||
|
(let [response (app (request "/hello.json"))]
|
||||||
|
(is (= "application/json" (get-in response [:headers "Content-Type"])))
|
||||||
|
(is (get-in response [:headers "Last-Modified"]))
|
||||||
|
(is (= "{\"hello\": \"file\"}" (slurp (:body response)))))
|
||||||
|
(let [response (app (request "/hello.xml"))]
|
||||||
|
(is (= "text/xml" (get-in response [:headers "Content-Type"])))
|
||||||
|
(is (get-in response [:headers "Last-Modified"]))
|
||||||
|
(is (= "<xml><hello>file</hello></xml>\n" (slurp (:body response))))))
|
||||||
|
|
||||||
|
(testing "index-files"
|
||||||
|
(let [response (app (request "/docs"))]
|
||||||
|
(is (= (redirect "/docs/index.html") response)))
|
||||||
|
(let [response (app (request "/docs/"))]
|
||||||
|
(is (= (redirect "/docs/index.html") response))))
|
||||||
|
|
||||||
|
(testing "not found"
|
||||||
|
(let [response (app (request "/not-found"))]
|
||||||
|
(is (= 404 (:status response)))))
|
||||||
|
|
||||||
|
(testing "3-arity"
|
||||||
|
(let [result (atom nil)
|
||||||
|
respond (partial reset! result)
|
||||||
|
raise ::not-called]
|
||||||
|
(app (request "/hello.xml") respond raise)
|
||||||
|
(is (= "text/xml" (get-in @result [:headers "Content-Type"])))
|
||||||
|
(is (get-in @result [:headers "Last-Modified"]))
|
||||||
|
(is (= "<xml><hello>file</hello></xml>\n" (slurp (:body @result))))))))))
|
||||||
|
|
||||||
|
(testing "outside a router"
|
||||||
|
|
||||||
|
(testing "from root"
|
||||||
|
(let [app (ring/ring-handler
|
||||||
|
(ring/router [])
|
||||||
|
(ring/routes
|
||||||
|
(ring/create-resource-handler {:path "/"})
|
||||||
|
(ring/create-default-handler)))]
|
||||||
|
(testing test
|
||||||
|
(testing "different file-types"
|
||||||
|
(let [response (app (request "/hello.json"))]
|
||||||
|
(is (= "application/json" (get-in response [:headers "Content-Type"])))
|
||||||
|
(is (get-in response [:headers "Last-Modified"]))
|
||||||
|
(is (= "{\"hello\": \"file\"}" (slurp (:body response)))))
|
||||||
|
(let [response (app (request "/hello.xml"))]
|
||||||
|
(is (= "text/xml" (get-in response [:headers "Content-Type"])))
|
||||||
|
(is (get-in response [:headers "Last-Modified"]))
|
||||||
|
(is (= "<xml><hello>file</hello></xml>\n" (slurp (:body response))))))
|
||||||
|
|
||||||
|
(testing "index-files"
|
||||||
|
(let [response (app (request "/docs"))]
|
||||||
|
(is (= (redirect "/docs/index.html") response)))
|
||||||
|
(let [response (app (request "/docs/"))]
|
||||||
|
(is (= (redirect "/docs/index.html") response))))
|
||||||
|
|
||||||
|
(testing "not found"
|
||||||
|
(let [response (app (request "/not-found"))]
|
||||||
|
(is (= 404 (:status response)))))
|
||||||
|
|
||||||
|
(testing "3-arity"
|
||||||
|
(let [result (atom nil)
|
||||||
|
respond (partial reset! result)
|
||||||
|
raise ::not-called]
|
||||||
|
(app (request "/hello.xml") respond raise)
|
||||||
|
(is (= "text/xml" (get-in @result [:headers "Content-Type"])))
|
||||||
|
(is (get-in @result [:headers "Last-Modified"]))
|
||||||
|
(is (= "<xml><hello>file</hello></xml>\n" (slurp (:body @result)))))))))
|
||||||
|
|
||||||
|
(testing "from path"
|
||||||
|
(let [app (ring/ring-handler
|
||||||
|
(ring/router [])
|
||||||
|
(ring/routes
|
||||||
|
(ring/create-resource-handler {:path "/files"})
|
||||||
|
(ring/create-default-handler)))
|
||||||
|
request #(request (str "/files" %))
|
||||||
|
redirect #(redirect (str "/files" %))]
|
||||||
|
(testing test
|
||||||
|
(testing "different file-types"
|
||||||
|
(let [response (app (request "/hello.json"))]
|
||||||
|
(is (= "application/json" (get-in response [:headers "Content-Type"])))
|
||||||
|
(is (get-in response [:headers "Last-Modified"]))
|
||||||
|
(is (= "{\"hello\": \"file\"}" (slurp (:body response)))))
|
||||||
|
(let [response (app (request "/hello.xml"))]
|
||||||
|
(is (= "text/xml" (get-in response [:headers "Content-Type"])))
|
||||||
|
(is (get-in response [:headers "Last-Modified"]))
|
||||||
|
(is (= "<xml><hello>file</hello></xml>\n" (slurp (:body response))))))
|
||||||
|
|
||||||
|
(testing "index-files"
|
||||||
|
(let [response (app (request "/docs"))]
|
||||||
|
(is (= (redirect "/docs/index.html") response)))
|
||||||
|
(let [response (app (request "/docs/"))]
|
||||||
|
(is (= (redirect "/docs/index.html") response))))
|
||||||
|
|
||||||
|
(testing "not found"
|
||||||
|
(let [response (app (request "/not-found"))]
|
||||||
|
(is (= 404 (:status response)))))
|
||||||
|
|
||||||
|
(testing "3-arity"
|
||||||
|
(let [result (atom nil)
|
||||||
|
respond (partial reset! result)
|
||||||
|
raise ::not-called]
|
||||||
|
(app (request "/hello.xml") respond raise)
|
||||||
|
(is (= "text/xml" (get-in @result [:headers "Content-Type"])))
|
||||||
|
(is (get-in @result [:headers "Last-Modified"]))
|
||||||
|
(is (= "<xml><hello>file</hello></xml>\n" (slurp (:body @result)))))))))))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue