Skip to content

Instantly share code, notes, and snippets.

@jordanrobinson
Last active November 23, 2020 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jordanrobinson/facfae126370a6c26ced361fa8979ce8 to your computer and use it in GitHub Desktop.
Save jordanrobinson/facfae126370a6c26ced361fa8979ce8 to your computer and use it in GitHub Desktop.
blog data
(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))
(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))
{"data":"Hello, world!"}

--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))))))

lein new app
: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"]]
(ns liberator-examples.hello-world.resource
(:require
[compojure.core :refer [GET defroutes]]))
(defroutes make-routes)
(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))
(use-fixtures
:once (fn [test]
(let [server (start-server)]
(try
(test)
(finally (.stop server))))))
(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 expected data"
(is (= "Hello, world!"
(-> response
:body
(json/read-str :key-fn keyword)
:data))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment