Skip to content

Commit

Permalink
Improve resource tests: JSON vs. XML (Preparation for #2126)
Browse files Browse the repository at this point in the history
Make sure we are testing the same things in both cases.
  • Loading branch information
allentiak committed Jan 28, 2025
1 parent ed50177 commit d7ca8d4
Showing 1 changed file with 31 additions and 12 deletions.
43 changes: 31 additions & 12 deletions modules/rest-util/test/blaze/middleware/fhir/resource_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
[:body :issue 0 :diagnostics] := "Unrecognized token 'x': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')\n at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 1, column: 2]"))

(testing "body with no JSON object"
;; There is no XML analogy to this JSON test, since XML has no objects.
(given @(resource-handler
{:headers {"content-type" "application/fhir+json"}
:body (input-stream "1")})
Expand Down Expand Up @@ -115,7 +116,13 @@
[:body :issue 0 :severity] := #fhir/code"error"
[:body :issue 0 :code] := #fhir/code"invariant"
[:body :issue 0 :diagnostics] := "Error on value `{}`. Expected type is `code`, regex `[\\u0021-\\uFFFF]+([ \\t\\n\\r][\\u0021-\\uFFFF]+)*`."
[:body :issue 0 :expression] := ["entry[0].resource.gender"])))
[:body :issue 0 :expression] := ["entry[0].resource.gender"]))

(testing "long attribute values are allowed (JSON-wrapped Binary data)"
(given @(resource-handler
{:headers {"content-type" "application/fhir+json"}
:body (input-stream (str "{\"data\" : \"" (apply str (repeat (* 8 1024 1024) \a)) "\", \"resourceType\" : \"Binary\"}"))})
fhir-spec/fhir-type := :fhir/Binary)))

(deftest xml-test
(testing "possible content types"
Expand All @@ -127,22 +134,28 @@
fhir-spec/fhir-type := :fhir/Patient)
(is (true? @closed?)))))

(testing "long attribute values are allowed"
(testing "empty body"
(given @(resource-handler
{:headers {"content-type" "application/fhir+xml"}
:body (input-stream (str "<Binary xmlns=\"http://hl7.org/fhir\"><data value=\"" (apply str (repeat (* 8 1024 1024) \a)) "\"/></Binary>"))})
fhir-spec/fhir-type := :fhir/Binary))

(testing "body with invalid XML"
(given @(resource-handler
{:request-method :post
:headers {"content-type" "application/fhir+xml"}
:body (input-stream "<Patient xmlns=\"http://hl7.org/fhir\"><id value \"a_b\"/></Patient>")})
:body (input-stream "")})
:status := 400
[:body fhir-spec/fhir-type] := :fhir/OperationOutcome
[:body :issue 0 :severity] := #fhir/code"error"
[:body :issue 0 :code] := #fhir/code"invalid"
[:body :issue 0 :diagnostics] := "Unexpected character '\"' (code 34) expected '='\n at [row,col {unknown-source}]: [1,48]"))
[:body :issue 0 :diagnostics] := "Unexpected EOF in prolog\n at [row,col {unknown-source}]: [1,0]"))

(testing "body with invalid XML"
(doseq [[input-string diagnostics] [["1" "Unexpected character '1' (code 49) in prolog; expected '<'\n at [row,col {unknown-source}]: [1,1]"]
["<Patient xmlns=\"http://hl7.org/fhir\"><id value \"a_b\"/></Patient>" "Unexpected character '\"' (code 34) expected '='\n at [row,col {unknown-source}]: [1,48]"]]]
(given @(resource-handler
{:request-method :post
:headers {"content-type" "application/fhir+xml"}
:body (input-stream input-string)})
:status := 400
[:body fhir-spec/fhir-type] := :fhir/OperationOutcome
[:body :issue 0 :severity] := #fhir/code"error"
[:body :issue 0 :code] := #fhir/code"invalid"
[:body :issue 0 :diagnostics] := diagnostics)))

(testing "body with invalid resource"
(given @(resource-handler
Expand Down Expand Up @@ -172,7 +185,13 @@
[:body fhir-spec/fhir-type] := :fhir/OperationOutcome
[:body :issue 0 :severity] := #fhir/code"error"
[:body :issue 0 :code] := #fhir/code"invariant"
[:body :issue 0 :diagnostics] := "Error on value `a_b`. Expected type is `id`, regex `[A-Za-z0-9\\-\\.]{1,64}`.")))
[:body :issue 0 :diagnostics] := "Error on value `a_b`. Expected type is `id`, regex `[A-Za-z0-9\\-\\.]{1,64}`."))

(testing "long attribute values are allowed (XML-wrapped Binary data)"
(given @(resource-handler
{:headers {"content-type" "application/fhir+xml"}
:body (input-stream (str "<Binary xmlns=\"http://hl7.org/fhir\"><data value=\"" (apply str (repeat (* 8 1024 1024) \a)) "\"/></Binary>"))})
fhir-spec/fhir-type := :fhir/Binary)))

(def ^:private whitespace
(gen/fmap str/join (gen/vector (gen/elements [" " "\n" "\r" "\t"]))))
Expand Down

0 comments on commit d7ca8d4

Please sign in to comment.