Skip to content

Instantly share code, notes, and snippets.

@ddeaguiar
Last active July 13, 2017 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ddeaguiar/bed92b8f34b7e93f15cd0e5548d783ae to your computer and use it in GitHub Desktop.
Save ddeaguiar/bed92b8f34b7e93f15cd0e5548d783ae to your computer and use it in GitHub Desktop.
Various ways to test Pedestal routes
(require '[io.pedestal.http.route :as route])
;; The following examples use a contrived expanded route structure. You can test your routes
;; by using the result of `(route/expand-routes my-routes)`, where `my-routes` is your route representation
;; created using one of the supported route syntaxes. If you are using the `pedestal-service` lein template,
;; you would use `(route/expand-routes service/routes)`.
(def contrived-expanded-routes [{:path "/bar" :method :get :interceptors []}])
;; You can test route matching with `try-routing-for`
(route/try-routing-for contrived-expanded-routes :map-tree "/bar" :get)
;; The router is exposed through an interceptor. You can create the router interceptor
;; and execute it's enter fn. The match result will be assoc'd to the context via the
;; :route key. This is what `try-routing-for` does under the covers.
;; https://github.com/pedestal/pedestal/blob/master/route/src/io/pedestal/http/route.clj#L514
(def rt (route/router contrived-expanded-routes))
;; Interceptor lifecycle fns expect a context so we create a simple context containing a request map
;; and pass it to the router interceptor's :enter fn.
((:enter rt) {:request {:path-info "/bar" :request-method :get}})
;; You can instantiate a router implementation directly and use the `find-route` Router protocol fn.
(require '[io.pedestal.http.route.map-tree :as map-tree]
'[io.pedestal.http.route.router :as router])
;; We need to create a concrete Router implementation so we'll use the map-tree router. This is the
;; default router used by Pedestal.
;; `find-route` expects a request map so create one and pass it to it.
(router/find-route (map-tree/router [{:path "/bar"}]) {:path-info "/bar" :request-method :get})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment