This repository has been archived by the owner on Aug 24, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
106 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
(ns ring.middleware.test.format | ||
(:use [clojure.test] | ||
[ring.middleware.format]) | ||
(:require [cheshire.core :as json] | ||
[clj-yaml.core :as yaml]) | ||
(:import [java.io ByteArrayInputStream])) | ||
|
||
(defn stream [s] | ||
(ByteArrayInputStream. (.getBytes s "UTF-8"))) | ||
|
||
(def restful-echo | ||
(wrap-restful-format (fn [req] (assoc req :body (vals (:body-params req)))))) | ||
|
||
(def restful-echo-json | ||
(wrap-restful-format (fn [req] (assoc req :body (vals (:body-params req)))) | ||
:format [:json])) | ||
|
||
(deftest test-restful-round-trip | ||
(let [ok-accept "application/edn" | ||
msg {:test :ok} | ||
ok-req {:headers {"accept" ok-accept} | ||
:content-type ok-accept | ||
:body (stream (pr-str msg))} | ||
r-trip (restful-echo ok-req)] | ||
(is (= (get-in r-trip [:headers "Content-Type"]) | ||
"application/edn; charset=utf-8")) | ||
(is (= (read-string (slurp (:body r-trip))) (vals msg))) | ||
(is (= (:params r-trip) msg)) | ||
(is (thrown? RuntimeException (restful-echo {:headers {"accept" "foo/bar"}})))) | ||
(let [ok-accept "application/json" | ||
msg {"test" "ok"} | ||
ok-req {:headers {"accept" ok-accept} | ||
:content-type ok-accept | ||
:body (stream (json/encode msg))} | ||
r-trip (restful-echo-json ok-req)] | ||
(is (= (get-in r-trip [:headers "Content-Type"]) | ||
"application/json; charset=utf-8")) | ||
(is (= (json/decode (slurp (:body r-trip))) (vals msg))) | ||
(is (= (:params r-trip) msg)) | ||
(is (nil? (:body-params (restful-echo-json {:headers {"accept" "application/edn"} | ||
:content-type "application/edn"})))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters