(let [fake-server (fake-server/start!) (fake-route! fake-server "/x" {:status 200 :content-type "application/json" :body (slurp (io/resource "my.json"))}) (fake-route! fake-server {:path "/y" :query {:q "something")}} {:status 200 :content-type "application/json" :body (slurp (io/resource "my2.json"))})] ; Do actual HTTP request (shutdown! fake-server))
(with-fake-routes!optional-server-instanceroute-map)
{"/x"
{:status 200 :content-type "application/json" :body (slurp (io/resource "my.json"))}
{:path "/y" :query {:q "something")}}
{:status 200 :content-type "application/json" :body (slurp (io/resource "my2.json"))}}
--
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.
I don't think you need a macro here. In any case, I'd avoid using a macro as late as possible. See how far you get with just functions, and then maybe at the end, add one macro if you absolutely need it to add just a touch of syntactic sugar.routes should clearly be some sort of data-structure, rather than side-effect setter functions. Maybe this:(with-fake-routes!optional-server-instanceroute-map)
Where optional-server-instance, if it exists is, an object returned by (fake-server/start!). If optional-server-instance is not passed in, then with-fake-routes! creates it's own and is free to call (shutdown!) on it automatically. And route-map is a Map of routes:{"/x"
{:status 200 :content-type "application/json" :body (slurp (io/resource "my.json"))}
{:path "/y" :query {:q "something")}}
{:status 200 :content-type "application/json" :body (slurp (io/resource "my2.json"))}}
Also, at the risk of scope creep, I could foresee wanting the response to be based on the input instead of just a static blob. So maybe the value of :body could be a string or a function of 1 arg, the route-- in your code test with (fn?).
Thanks for your feedback, exactly what I wanted.
On Tuesday, March 8, 2016 at 3:16:02 PM UTC+1, mlimotte wrote:I don't think you need a macro here. In any case, I'd avoid using a macro as late as possible. See how far you get with just functions, and then maybe at the end, add one macro if you absolutely need it to add just a touch of syntactic sugar.routes should clearly be some sort of data-structure, rather than side-effect setter functions. Maybe this:(with-fake-routes!optional-server-instanceroute-map)
You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/gieS5hQCUm4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.
(with-fake-routes! 9999 ...)
A macro(def the-uri (atom nil))(with-fake-routes! the-uri...(http/get @the-uri "/x"))
(with-fake-routes! [uri option-server-instance]
route-map
(http/get uri "/x"))
(def fake-server ...)
(with-fake-routes!
route-map
(http/get (:uri fake-server) "/x"))
Yes, I was assuming the HTTP calls happen inside the with-fake-routes! block.I missed the part about the random port. I se 3 options for that:Assign a port, rather than random(with-fake-routes! 9999 ...)But then, of course, you have to worry about port already in use.An atomA macro(def the-uri (atom nil))(with-fake-routes! the-uri...(http/get @the-uri "/x"))A common convention in Clojure would be to pass it a symbol (e.g. `uri` that is bound by the macro), rather implicitly creating `uri`.(with-fake-routes! [uri option-server-instance]route-map(http/get uri "/x"))
With the macro approach, they don't need to escape it.
I think Gary is right. with-open does exactly what you need, I should have thought of that, and you should probably use it. But if you want to get your version working, trying to understand what the with-open macro is doing. Your implementation can be simpler because you only have one explicit binding. Essentially you'll create a let as a backquoted form and then splice in the explicit symbol from the user:
`(let [~sym ...server-instance-or-uri...] ... )
marc