--command lein new app
--project deps :dependencies [[org.clojure/clojure "1.10.0"] [ring/ring-core "1.8.0"] [ring/ring-jetty-adapter "1.8.0"] [ring/ring-defaults "0.3.2"] [compojure "1.6.2"] [http-kit "2.5.0"] [liberator "0.15.3"]]
--core.clj (ns liberator-examples.core (:require [ring.adapter.jetty :as ring-jetty] [ring.middleware.defaults :as ring-defaults] [liberator-examples.hello-world.resource :as resource]) (:gen-class))
(defn start-server [] (ring-jetty/run-jetty (ring-defaults/wrap-defaults resource/make-routes ring-defaults/api-defaults) {:host "localhost" :port 3000 :join? false}))
(defn -main [& args] (start-server))
--resource (ns liberator-examples.hello-world.resource (:require [compojure.core :refer [GET defroutes]] [liberator.core :refer [resource defresource]]))
(defresource hello-world :available-media-types ["application/json"] :handle-ok {:data "Hello, world!"})
(defroutes make-routes (GET "/" [] hello-world))
--data {"data":"Hello, world!"}
--test (ns liberator-examples.core-test (:require [clojure.test :refer :all] [liberator-examples.core :refer :all] [org.httpkit.client :as http] [clojure.data.json :as json]))
(use-fixtures :once (fn [test] (let [server (start-server)] (try (test) (finally (.stop server))))))
(deftest hello-world-test (let [response @(http/get "http://localhost:3000")] (testing "Hello world resource returns 200 status code" (is (= (:status response) 200))) (testing "Hello world resource returns expected data" (is (= "Hello, world!" (-> response :body (json/read-str :key-fn keyword) :data))))))