Skip to content

with routes macro

Johan Haleby edited this page Mar 25, 2016 · 10 revisions

with-routes! (found in stub-http.core) is a macro that is useful if you don't need to reuse the "server" instance returned by start!. You can pass both bindings and routes to the macro. The macro expose three bindings to the body of the macro: uri, port and server. In its simplest form it can be used by only specifying a route:

(with-routes!
      {"/something" {:status 200 :content-type "text/plan" :body "hello" }}
      (let [body (:body (client/get (str uri "/something")))]
        (is (= "hello" body))))

This will start the stub server on a free port (available via port). The to the server is generated by the macro and is available by uri (as seen in the example).

Another useful thing that with-routes! allows you to do is to declare bindings. This is useful to make the code more compact and avoid let statements. For example let's say you're reading a response from a file

Clone this wiki locally