Skip to content
Johan Haleby edited this page Mar 26, 2016 · 30 revisions

Definitions

In order to stub HTTP endpoints you must conform to the route schema. Here's an example:

{ "/something" {:status 200 :body "hello"}
  "/something-else" {:status 201 } }

The key in this map is the request specification and the value is the response specification. The combination of key and value (i.e. request- and response specification) is called a route. You supply this route map to the with-routes! macro or the start! function. For example:

(with-routes! 
      { "/something" {:status 200 :body "hello"}
        "/something-else" {:status 201 } }
      (client/get ...))

Function

As an alternative to explicitly declaring the route map (that consists of one to many routes) you can provide it from a function that takes a server instance as a parameter. This is useful if you need access to some data from the server in order to build up your routes. For example:

(fn [server]
    (let [absolute-uri (str (:uri server) "/somewhere")]
      { "/somewhere" {:status 200 :body absolute-uri}}))

In this example we get the URI of the server (which may be autogenerated if using the macro for later use in our response specification. For example:

(with-routes! 
      (fn [server]
          (let [absolute-uri (str (:uri server) "/somewhere")]
               { "/somewhere" {:status 200 :body absolute-uri}}))
      (client/get ...))

Request Specification

The request specification (the key in the route map) can be created in three different ways:

  1. Path only

    This allows you to match a path in the request. For example:

    { "/something" { ... } }

    This will match requests (regardless of http method) whose path is equal to "/something". For example it'll match the URI http://localhost:8083/something but not http://localhost:8083/something2.

  2. Map

Clone this wiki locally