Skip to content

Commit 73a1c16

Browse files
committed
Add tests to :content-type, release 1.1.6
* fixes also #236
1 parent 79d959d commit 73a1c16

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

CHANGELOG.md

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
## 1.1.6 (1.8.2016)
2+
3+
* `:content-type` of user-defined formats are pushed into Swagger `:produces` and `:consumes`, thanks to [Waldemar](https://github.com/Velrok).
4+
5+
```clj
6+
(def custom-json-format
7+
(ring.middleware.format-response/make-encoder cheshire.core/generate-string "application/vnd.vendor.v1+json"))
8+
9+
(api
10+
{:format {:formats [custom-json-format :json :edn]}}
11+
...)
12+
```
13+
114
## 1.1.5 (27.7.2016)
215

316
* New api-options `[:api :disable-api-middleware?]` to disable the api-middleware completely. With this set, `api` only produces the (reverse) route-tree + set's swagger stuff and sets schema coercions for the api.

project.clj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(defproject metosin/compojure-api "1.1.5"
1+
(defproject metosin/compojure-api "1.1.6"
22
:description "Compojure Api"
33
:url "https://github.com/metosin/compojure-api"
44
:license {:name "Eclipse Public License"

test/compojure/api/integration_test.clj

+27-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
[compojure.api.middleware :as mw]
1212
[ring.swagger.middleware :as rsm]
1313
[compojure.api.validator :as validator]
14-
[compojure.api.routes :as routes]))
14+
[compojure.api.routes :as routes]
15+
16+
[ring.middleware.format-response :as format-response]))
1517

1618
;;
1719
;; Data
@@ -1440,3 +1442,27 @@
14401442

14411443
(fact "exceptions are not caught"
14421444
(raw-get* app "/throw") => throws)))
1445+
1446+
(facts "custom formats contribute to Swagger :consumes & :produces"
1447+
(let [custom-json (format-response/make-encoder json "application/vnd.vendor.v1+json")
1448+
app (api
1449+
{:swagger {:spec "/swagger.json"}
1450+
:format {:formats [custom-json :json]}}
1451+
(POST "/echo" []
1452+
:body [data {:kikka s/Str}]
1453+
(ok data)))]
1454+
1455+
(fact "it works"
1456+
(let [response (app {:uri "/echo"
1457+
:request-method :post
1458+
:body (json-stream {:kikka "kukka"})
1459+
:headers {"content-type" "application/vnd.vendor.v1+json"
1460+
"accept" "application/vnd.vendor.v1+json"}})]
1461+
1462+
(-> response :body slurp) => (json {:kikka "kukka"})
1463+
(-> response :headers) => (contains {"Content-Type" "application/vnd.vendor.v1+json; charset=utf-8"})))
1464+
1465+
(fact "spec is correct"
1466+
(get-spec app) => (contains
1467+
{:produces ["application/vnd.vendor.v1+json" "application/json"]
1468+
:consumes ["application/vnd.vendor.v1+json" "application/json"]}))))

test/compojure/api/test_utils.clj

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
(:require [cheshire.core :as cheshire]
33
[clojure.string :as str]
44
[peridot.core :as p]
5+
[clojure.java.io :as io]
56
[compojure.api.routes :as routes])
67
(:import [java.io InputStream]))
78

@@ -34,6 +35,8 @@
3435

3536
(defn json [x] (cheshire/generate-string x))
3637

38+
(defn json-stream [x] (io/input-stream (.getBytes (json x))))
39+
3740
(defn follow-redirect [state]
3841
(if (some-> state :response :headers (get "Location"))
3942
(p/follow-redirect state)

0 commit comments

Comments
 (0)