|
1 | 1 | (ns blaze.rest-api.middleware.output-test
|
2 | 2 | (:require
|
| 3 | + [blaze.fhir.spec :as fhir-spec] |
3 | 4 | [blaze.fhir.spec-spec]
|
4 | 5 | [blaze.rest-api.middleware.output :refer [wrap-output]]
|
5 | 6 | [blaze.test-util :as tu]
|
6 | 7 | [blaze.test-util.ring :refer [call]]
|
| 8 | + [clojure.data.xml :as xml] |
| 9 | + [clojure.java.io :as io] |
7 | 10 | [clojure.spec.test.alpha :as st]
|
8 | 11 | [clojure.test :as test :refer [are deftest is testing]]
|
9 | 12 | [juxt.iota :refer [given]]
|
10 | 13 | [ring.util.response :as ring]
|
11 |
| - [taoensso.timbre :as log]) |
12 |
| - (:import |
13 |
| - [java.nio.charset StandardCharsets])) |
| 14 | + [taoensso.timbre :as log])) |
14 | 15 |
|
15 | 16 |
|
16 | 17 | (st/instrument)
|
|
34 | 35 | (respond (ring/response {:fhir/type :fhir/Patient :id "0"})))))
|
35 | 36 |
|
36 | 37 |
|
37 |
| -(defn- bytes->str [^bytes bs] |
38 |
| - (String. bs StandardCharsets/UTF_8)) |
| 38 | +(defn- parse-json [body] |
| 39 | + (fhir-spec/conform-json (fhir-spec/parse-json body))) |
39 | 40 |
|
40 | 41 |
|
41 | 42 | (deftest json-test
|
42 | 43 | (testing "JSON is the default"
|
43 | 44 | (testing "without accept header"
|
44 | 45 | (given (call resource-handler {})
|
45 |
| - [:body bytes->str] := "{\"id\":\"0\",\"resourceType\":\"Patient\"}")) |
| 46 | + [:headers "Content-Type"] := "application/fhir+json;charset=utf-8" |
| 47 | + [:body parse-json] := {:fhir/type :fhir/Patient :id "0"})) |
46 | 48 |
|
47 | 49 | (testing "with accept header"
|
48 |
| - (are [accept] (given (call resource-handler {:headers {"accept" accept}}) |
49 |
| - [:body bytes->str] := "{\"id\":\"0\",\"resourceType\":\"Patient\"}") |
50 |
| - "*/*" |
51 |
| - "application/*" |
52 |
| - "text/*"))) |
| 50 | + (are [accept content-type] |
| 51 | + (given (call resource-handler {:headers {"accept" accept}}) |
| 52 | + [:headers "Content-Type"] := content-type |
| 53 | + [:body parse-json] := {:fhir/type :fhir/Patient :id "0"}) |
| 54 | + "*/*" "application/fhir+json;charset=utf-8" |
| 55 | + "application/*" "application/fhir+json;charset=utf-8" |
| 56 | + "text/*" "text/json;charset=utf-8"))) |
53 | 57 |
|
54 | 58 | (testing "possible accept headers"
|
55 |
| - (are [accept] |
| 59 | + (are [accept content-type] |
56 | 60 | (given (call resource-handler {:headers {"accept" accept}})
|
57 |
| - [:body bytes->str] := "{\"id\":\"0\",\"resourceType\":\"Patient\"}") |
58 |
| - "application/fhir+json" |
59 |
| - "application/json" |
60 |
| - "text/json" |
61 |
| - "application/fhir+xml;q=0.9, application/fhir+json;q=1.0")) |
| 61 | + [:headers "Content-Type"] := content-type |
| 62 | + [:body parse-json] := {:fhir/type :fhir/Patient :id "0"}) |
| 63 | + "application/fhir+json" "application/fhir+json;charset=utf-8" |
| 64 | + "application/json" "application/json;charset=utf-8" |
| 65 | + "text/json" "text/json;charset=utf-8" |
| 66 | + "application/fhir+xml;q=0.9, application/fhir+json;q=1.0" "application/fhir+json;charset=utf-8")) |
62 | 67 |
|
63 | 68 | (testing "_format overrides"
|
64 |
| - (are [accept format] |
| 69 | + (are [accept format content-type] |
65 | 70 | (given (call resource-handler
|
66 | 71 | {:headers {"accept" accept}
|
67 | 72 | :query-params {"_format" format}})
|
68 |
| - [:body bytes->str] := "{\"id\":\"0\",\"resourceType\":\"Patient\"}") |
69 |
| - "application/fhir+xml" "application/fhir+json" |
70 |
| - "application/fhir+xml" "application/json" |
71 |
| - "application/fhir+xml" "text/json" |
72 |
| - "application/fhir+xml" "json" |
73 |
| - "*/*" "application/fhir+json" |
74 |
| - "*/*" "application/json" |
75 |
| - "*/*" "text/json" |
76 |
| - "*/*" "json"))) |
| 73 | + [:headers "Content-Type"] := content-type |
| 74 | + [:body parse-json] := {:fhir/type :fhir/Patient :id "0"}) |
| 75 | + "application/fhir+xml" |
| 76 | + "application/fhir+json" |
| 77 | + "application/fhir+json;charset=utf-8" |
| 78 | + |
| 79 | + "application/fhir+xml" |
| 80 | + "application/json" |
| 81 | + "application/json;charset=utf-8" |
| 82 | + |
| 83 | + "application/fhir+xml" |
| 84 | + "text/json" |
| 85 | + "text/json;charset=utf-8" |
| 86 | + |
| 87 | + "application/fhir+xml" |
| 88 | + "json" |
| 89 | + "application/fhir+json;charset=utf-8" |
| 90 | + |
| 91 | + "*/*" |
| 92 | + "application/fhir+json" |
| 93 | + "application/fhir+json;charset=utf-8" |
| 94 | + |
| 95 | + "*/*" |
| 96 | + "application/json" |
| 97 | + "application/json;charset=utf-8" |
| 98 | + |
| 99 | + "*/*" |
| 100 | + "text/json" |
| 101 | + "text/json;charset=utf-8" |
| 102 | + |
| 103 | + "*/*" |
| 104 | + "json" |
| 105 | + "application/fhir+json;charset=utf-8"))) |
| 106 | + |
| 107 | + |
| 108 | +(defn- parse-xml [body] |
| 109 | + (with-open [reader (io/reader body)] |
| 110 | + (fhir-spec/conform-xml (xml/parse reader)))) |
77 | 111 |
|
78 | 112 |
|
79 | 113 | (deftest xml-test
|
80 | 114 | (testing "possible accept headers"
|
81 |
| - (are [accept] |
| 115 | + (are [accept content-type] |
82 | 116 | (given (call resource-handler {:headers {"accept" accept}})
|
83 |
| - [:body bytes->str] := |
84 |
| - "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Patient xmlns=\"http://hl7.org/fhir\"><id value=\"0\"/></Patient>") |
| 117 | + [:headers "Content-Type"] := content-type |
| 118 | + [:body parse-xml] := {:fhir/type :fhir/Patient :id "0"}) |
85 | 119 | "application/fhir+xml"
|
| 120 | + "application/fhir+xml;charset=utf-8" |
| 121 | + |
86 | 122 | "application/xml"
|
| 123 | + "application/xml;charset=utf-8" |
| 124 | + |
87 | 125 | "text/xml"
|
88 |
| - "application/fhir+json;q=0.9, application/fhir+xml;q=1.0")) |
| 126 | + "text/xml;charset=utf-8" |
| 127 | + |
| 128 | + "application/fhir+json;q=0.9, application/fhir+xml;q=1.0" |
| 129 | + "application/fhir+xml;charset=utf-8" |
| 130 | + |
| 131 | + ;; Safari |
| 132 | + "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" |
| 133 | + "application/xml;charset=utf-8" |
| 134 | + |
| 135 | + ;; Chrome |
| 136 | + "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" |
| 137 | + "application/xml;charset=utf-8" |
| 138 | + |
| 139 | + ;; Edge |
| 140 | + "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" |
| 141 | + "application/xml;charset=utf-8" |
| 142 | + |
| 143 | + ;; Firefox |
| 144 | + "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8" |
| 145 | + "application/xml;charset=utf-8")) |
89 | 146 |
|
90 | 147 | (testing "_format overrides"
|
91 |
| - (are [accept format] |
| 148 | + (are [accept format content-type] |
92 | 149 | (given (call resource-handler
|
93 | 150 | {:headers {"accept" accept}
|
94 | 151 | :query-params {"_format" format}})
|
95 |
| - [:body bytes->str] := |
96 |
| - "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Patient xmlns=\"http://hl7.org/fhir\"><id value=\"0\"/></Patient>") |
97 |
| - "application/fhir+json" "application/fhir+xml" |
98 |
| - "application/fhir+json" "application/xml" |
99 |
| - "application/fhir+json" "text/xml" |
100 |
| - "application/fhir+json" "xml" |
101 |
| - "*/*" "application/fhir+xml" |
102 |
| - "*/*" "application/xml" |
103 |
| - "*/*" "text/xml" |
104 |
| - "*/*" "xml"))) |
| 152 | + [:headers "Content-Type"] := content-type |
| 153 | + [:body parse-xml] := {:fhir/type :fhir/Patient :id "0"}) |
| 154 | + "application/fhir+json" |
| 155 | + "application/fhir+xml" |
| 156 | + "application/fhir+xml;charset=utf-8" |
| 157 | + |
| 158 | + "application/fhir+json" |
| 159 | + "application/xml" |
| 160 | + "application/xml;charset=utf-8" |
| 161 | + |
| 162 | + "application/fhir+json" |
| 163 | + "text/xml" |
| 164 | + "text/xml;charset=utf-8" |
| 165 | + |
| 166 | + "application/fhir+json" |
| 167 | + "xml" |
| 168 | + "application/fhir+xml;charset=utf-8" |
| 169 | + |
| 170 | + "*/*" |
| 171 | + "application/fhir+xml" |
| 172 | + "application/fhir+xml;charset=utf-8" |
| 173 | + |
| 174 | + "*/*" |
| 175 | + "application/xml" |
| 176 | + "application/xml;charset=utf-8" |
| 177 | + |
| 178 | + "*/*" |
| 179 | + "text/xml" |
| 180 | + "text/xml;charset=utf-8" |
| 181 | + |
| 182 | + "*/*" |
| 183 | + "xml" |
| 184 | + "application/fhir+xml;charset=utf-8"))) |
105 | 185 |
|
106 | 186 |
|
107 | 187 | (deftest not-acceptable-test
|
|
0 commit comments