linear-router serves unnamed catch-all paths

This commit is contained in:
Tommi Reiman 2018-05-14 08:21:03 +03:00
parent 027d46508b
commit e137b8baec
2 changed files with 13 additions and 8 deletions

View file

@ -56,7 +56,7 @@
(update-in [:path-parts] conj key) (update-in [:path-parts] conj key)
(update-in [:path-params] conj key) (update-in [:path-params] conj key)
(assoc-in [:path-constraints key] "([^/]+)")))) (assoc-in [:path-constraints key] "([^/]+)"))))
#"^\*(.+)$" :>> (fn [[_ token]] #"^\*(.*)$" :>> (fn [[_ token]]
(let [key (keyword token)] (let [key (keyword token)]
(-> out (-> out
(update-in [:path-parts] conj key) (update-in [:path-parts] conj key)

View file

@ -271,7 +271,9 @@
(ring/ring-handler (ring/ring-handler
(ring/router (ring/router
[["/ping" (constantly {:status 200, :body "pong"})] [["/ping" (constantly {:status 200, :body "pong"})]
["/files/*" (ring/create-resource-handler)]]) ["/files/*" (ring/create-resource-handler)]
["/*" (ring/create-resource-handler)]]
{:conflicts (constantly nil)})
(ring/create-default-handler))] (ring/create-default-handler))]
["outside of a router" ["outside of a router"
@ -280,34 +282,37 @@
["/ping" (constantly {:status 200, :body "pong"})]) ["/ping" (constantly {:status 200, :body "pong"})])
(ring/routes (ring/routes
(ring/create-resource-handler {:path "/files"}) (ring/create-resource-handler {:path "/files"})
(ring/create-default-handler)))]]] (ring/create-resource-handler {:path "/"})
(ring/create-default-handler)))]]
prefix ["/" "/files"]
:let [request (fn [uri] {:uri (str prefix uri), :request-method :get})]]
(testing test (testing test
(testing "different file-types" (testing "different file-types"
(let [response (app {:uri "/files/hello.json", :request-method :get})] (let [response (app (request "/hello.json"))]
(is (= "application/json" (get-in response [:headers "Content-Type"]))) (is (= "application/json" (get-in response [:headers "Content-Type"])))
(is (get-in response [:headers "Last-Modified"])) (is (get-in response [:headers "Last-Modified"]))
(is (= "{\"hello\": \"file\"}" (slurp (:body response))))) (is (= "{\"hello\": \"file\"}" (slurp (:body response)))))
(let [response (app {:uri "/files/hello.xml", :request-method :get})] (let [response (app (request "/hello.xml"))]
(is (= "text/xml" (get-in response [:headers "Content-Type"]))) (is (= "text/xml" (get-in response [:headers "Content-Type"])))
(is (get-in response [:headers "Last-Modified"])) (is (get-in response [:headers "Last-Modified"]))
(is (= "<xml><hello>file</hello></xml>\n" (slurp (:body response)))))) (is (= "<xml><hello>file</hello></xml>\n" (slurp (:body response))))))
(testing "index-files" (testing "index-files"
(let [response (app {:uri "/files/docs", :request-method :get})] (let [response (app (request "/docs"))]
(is (= "text/html" (get-in response [:headers "Content-Type"]))) (is (= "text/html" (get-in response [:headers "Content-Type"])))
(is (get-in response [:headers "Last-Modified"])) (is (get-in response [:headers "Last-Modified"]))
(is (= "<h1>hello</h1>\n" (slurp (:body response)))))) (is (= "<h1>hello</h1>\n" (slurp (:body response))))))
(testing "not found" (testing "not found"
(let [response (app {:uri "/files/not-found", :request-method :get})] (let [response (app (request "/not-found"))]
(is (= 404 (:status response))))) (is (= 404 (:status response)))))
(testing "3-arity" (testing "3-arity"
(let [result (atom nil) (let [result (atom nil)
respond (partial reset! result) respond (partial reset! result)
raise ::not-called] raise ::not-called]
(app {:uri "/files/hello.xml", :request-method :get} respond raise) (app (request "/hello.xml") respond raise)
(is (= "text/xml" (get-in @result [:headers "Content-Type"]))) (is (= "text/xml" (get-in @result [:headers "Content-Type"])))
(is (get-in @result [:headers "Last-Modified"])) (is (get-in @result [:headers "Last-Modified"]))
(is (= "<xml><hello>file</hello></xml>\n" (slurp (:body @result)))))))))) (is (= "<xml><hello>file</hello></xml>\n" (slurp (:body @result))))))))))