From 4e2fbc1edff3ed89b604073b9f0a96ab6283ee7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20L=C3=B6tzsch?= Date: Sun, 23 Sep 2018 12:42:12 +0200 Subject: [PATCH 1/3] minor fix in documentation --- examples/ring-spec-swagger/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/ring-spec-swagger/README.md b/examples/ring-spec-swagger/README.md index 814545ef..3eb99d0c 100644 --- a/examples/ring-spec-swagger/README.md +++ b/examples/ring-spec-swagger/README.md @@ -11,7 +11,7 @@ To test the endpoints using [httpie](https://httpie.org/): ```bash http GET :3000/math/plus x==1 y==20 -http POST :3000/math/spec/plus x:=1 y:=20 +http POST :3000/math/plus x:=1 y:=20 http GET :3000/swagger.json ``` From 5ae17159cfba34ff6307571ded828242154d6b6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20L=C3=B6tzsch?= Date: Sun, 23 Sep 2018 20:23:03 +0200 Subject: [PATCH 2/3] test for examples/ring-spec-swagger --- .../test/example/server_test.clj | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 examples/ring-spec-swagger/test/example/server_test.clj diff --git a/examples/ring-spec-swagger/test/example/server_test.clj b/examples/ring-spec-swagger/test/example/server_test.clj new file mode 100644 index 00000000..a4496d2c --- /dev/null +++ b/examples/ring-spec-swagger/test/example/server_test.clj @@ -0,0 +1,31 @@ +(ns example.server-test + (:require [clojure.test :refer :all] + [example.server :refer [app]])) + +(deftest example-server + + (testing "GET" + (is (= (-> {:request-method :get :uri "/math/plus" :query-params {:x 20 :y 3}} + app :body slurp) + "{\"total\":23}"))) + + (testing "POST" + (is (= (-> {:request-method :post :uri "/math/plus" :body-params {:x 40 :y 2}} + app :body slurp) + "{\"total\":42}"))) + + (testing "Download" + (is (= (-> {:request-method :get :uri "/files/download"} + app :body (#(slurp % :encoding "ascii")) count) ;; binary + (.length (clojure.java.io/file "resources/reitit.png")) + 506325))) + + (testing "Upload" + (let [file (clojure.java.io/file "resources/reitit.png") + multipart-temp-file-part {:tempfile file + :size (.length file) + :filename (.getName file) + :content-type "image/png;"}] + (is (= (-> {:request-method :post :uri "/files/upload" :multipart-params {:file multipart-temp-file-part}} + app :body slurp) + "{\"name\":\"reitit.png\",\"size\":506325}"))))) From fb91499efdf62e46bf1bfc1c340c2a29e6fd4e59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20L=C3=B6tzsch?= Date: Sun, 23 Sep 2018 22:48:32 +0200 Subject: [PATCH 3/3] test whole stack (use query-string and json-body) --- examples/ring-spec-swagger/project.clj | 5 +++-- .../ring-spec-swagger/test/example/server_test.clj | 13 ++++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/examples/ring-spec-swagger/project.clj b/examples/ring-spec-swagger/project.clj index 98a21bf4..cfea302e 100644 --- a/examples/ring-spec-swagger/project.clj +++ b/examples/ring-spec-swagger/project.clj @@ -2,5 +2,6 @@ :description "Reitit Ring App with Swagger" :dependencies [[org.clojure/clojure "1.9.0"] [ring/ring-jetty-adapter "1.7.0-RC2"] - [metosin/reitit "0.2.2"]] - :repl-options {:init-ns example.server}) + [metosin/reitit "0.2.3-SNAPSHOT"]] + :profiles {:dev {:dependencies [[ring/ring-mock "0.3.2"]] + :repl-options {:init-ns example.server}}}) diff --git a/examples/ring-spec-swagger/test/example/server_test.clj b/examples/ring-spec-swagger/test/example/server_test.clj index a4496d2c..e35726b8 100644 --- a/examples/ring-spec-swagger/test/example/server_test.clj +++ b/examples/ring-spec-swagger/test/example/server_test.clj @@ -1,16 +1,23 @@ (ns example.server-test (:require [clojure.test :refer :all] - [example.server :refer [app]])) + [example.server :refer [app]] + [ring.mock.request :refer [request json-body]])) (deftest example-server (testing "GET" - (is (= (-> {:request-method :get :uri "/math/plus" :query-params {:x 20 :y 3}} + (is (= (-> (request :get "/math/plus?x=20&y=3") + app :body slurp) + (-> {:request-method :get :uri "/math/plus" :query-string "x=20&y=3"} + app :body slurp) + (-> {:request-method :get :uri "/math/plus" :query-params {:x 20 :y 3}} app :body slurp) "{\"total\":23}"))) (testing "POST" - (is (= (-> {:request-method :post :uri "/math/plus" :body-params {:x 40 :y 2}} + (is (= (-> (request :post "/math/plus") (json-body {:x 40 :y 2}) + app :body slurp) + (-> {:request-method :post :uri "/math/plus" :body-params {:x 40 :y 2}} app :body slurp) "{\"total\":42}")))