[ANN] Juxt/Bidi routing library demo

69 views
Skip to first unread message

Alan Thompson

unread,
Jan 30, 2020, 2:36:16 AM1/30/20
to clojure
I just started maintaining a new codebase that uses the juxt/bidi routing library, which I've not used before.  In going through the code, the project uses some features which are not fully documented in the juxt/bidi README, so I wrote some demo code to clarify what is going on and the proper syntax to use. 


It is written as a sequence of unit tests, so one can verify that everything is OK by just running `lein test`:

~/io.tupelo.demo/bidi > lein clean ; lein test 

lein test _bootstrap
-------------------------------
   Clojure 1.10.1    Java 13
-------------------------------

lein test tst.demo.core

Ran 2 tests containing 17 assertions.
0 failures, 0 errors.

The demo tests start out simple:
(let [route ["/index.html" :index]]
(is= (bidi/match-route route "/index.html") {:handler :index}) ; found route
(is= (bidi/match-route route "/another.html") nil) ; did not find route
(is= "/index.html" (bidi/path-for route :index))) ; find URI for handler
but then dive into some lesser-known features like "Guards":
; short version to match GET "/index.html"
(let [route ["/index.html" {:get :index}]]
(is= (bidi/match-route route "/index.html" :request-method :get) {:handler :index, :request-method :get})
(is= (bidi/match-route route "/index.html" :request-method :put) nil))

; long version to match GET "/index.html"
(let [route ["" {
{:request-method :get} {"/index.html" :index}
}]]
(is= (bidi/match-route route "/index.html" :request-method :get) {:handler :index, :request-method :get})
(is= (bidi/match-route route "/index.html" :request-method :post) nil))
Enjoy!
Alan



Reply all
Reply to author
Forward
0 new messages