Ring mock with GET parameters

391 views
Skip to first unread message

Jonathon McKitrick

unread,
Jul 12, 2014, 3:41:39 PM7/12/14
to clo...@googlegroups.com
I'm using ring.mock.request to test an API.  POST parameters work fine, but for GET requests, the parameters are not where Compojure expects to find them.

Here is the relevant part of the handler:

(GET "/outlines" [speaker :as r]
                (println (str "Request " r))
                (println (str "Param '" speaker "'"))
                )

Here is the test using the mock request:

(let [req (request :get "/api/outlines" {:speaker "Test Speaker 1"})]
        (is (= expected-outlines (api-routes req))))

Here is the value of the request and the 'speaker' parameter in the handler:

Request {:remote-addr "localhost", :scheme :http, :context "/api", :request-method :get, :query-string "speaker=Test+Speaker+1", :route-params {}, :path-info "/outlines", :uri "/api/outlines", :server-name "localhost", :params {}, :headers {"host" "localhost"}, :server-port 80}
Param ''

The mock library apparently puts the GET params into query-string, so Compojure does not see them.  Is there a way to work around this without changing ring.mock.request?

James Reeves

unread,
Jul 12, 2014, 6:24:28 PM7/12/14
to clo...@googlegroups.com
You need to make sure your handler has the wrap-params middleware applied to it in order for it to accept query parameters.

- James


--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jonathon McKitrick

unread,
Jul 12, 2014, 7:42:53 PM7/12/14
to clo...@googlegroups.com, ja...@booleanknot.com
Hmm, except I don't have any issues when accessing it via an http client.  Only with the mock.

Jonathon McKitrick

unread,
Jul 12, 2014, 7:43:32 PM7/12/14
to clo...@googlegroups.com, ja...@booleanknot.com
Here's my handler setup:

(def app
  (routes
   (-> (handler/api api-routes)
       (wrap-params)
       (wrap-restful-format)
       (wrap-reload))
   (-> (handler/site www-routes)
       (wrap-resource "public")
       (wrap-content-type)
       (wrap-reload))))

Jonathon McKitrick

unread,
Jul 12, 2014, 7:45:34 PM7/12/14
to clo...@googlegroups.com, ja...@booleanknot.com
Sorry, I just got it... I needed to use 'app' in the test, not 'api-routes.'


On Saturday, July 12, 2014 7:42:53 PM UTC-4, Jonathon McKitrick wrote:
Reply all
Reply to author
Forward
0 new messages