Skip to content

Instantly share code, notes, and snippets.

Created Aug 31, 2015
What would you like to do?
Stub HTTP server
(ns stubhttp.core
(:require [ring.adapter.jetty :as r]
[ring.middleware.json :as json]
[ring.util.response :as response]))
(def state (atom {}))
(defn register! [{uri "uri" {:strs [status headers body]} "response"}]
(swap! state assoc uri {:status status :headers headers :body body}))
;; POST /register
;; {"uri" "/api/echo" "response" {"status" 200 "headers" {} "body" "echo"}}
(defn handler [{:keys [request-method uri body] :as request}]
(if (and (= "/register" uri)
(= :post request-method))
(register! body)
(get @state uri (response/not-found "Not Found"))))
(defn -main [& args]
(r/run-jetty (json/wrap-json-body handler) {:port 3000}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment