Skip to content

Commit

Permalink
Fix Failing Binary Emit case
Browse files Browse the repository at this point in the history
The body should contain a JSON payload with the OperationOutcome, not
the OperationOutcome itself.
We use JSON because it is our default.
  • Loading branch information
allentiak committed Dec 14, 2024
1 parent 78871ca commit 61c4925
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 4 deletions.
90 changes: 90 additions & 0 deletions .github/scripts/read-binary-content-via-json-invalid.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/bin/bash -e

# This script creates a binary resource with invalid data and verifies that,
# when trying to read its binary content, an error is generated.

BASE="http://localhost:8080/fhir"

# 10 KiB of random data, base64 encoded
ORIGINAL_DATA="$(openssl rand -base64 10240 | tr -d '\n')"


# Higher-order function to apply a transformation based on a condition
apply_generic_transformation() {
local data="$1"
local transform_function="$2"

local result=""

# Iterate over the data and apply the transform function
for i in $(seq 0 ${#data}-1); do
result+=$(eval "$transform_function \"${data:$i:1}\" $i")
done

echo "$result"
}

remove_parts_transform() {
local char="$1"
local index="$2"

# Randomly decide whether to remove this character (skip it)
if (( RANDOM % 10 == 0 )); then
echo "" # Skip (remove this character)
else
echo "$char" # Keep the character
fi
}

insert_non_base64_transform() {
local char="$1"
local index="$2"

# Every 5th character, insert a random non-base64 character
if (( index % 5 == 0 )); then
echo "$((RANDOM % 10))" # Insert a non-base64 digit
else
echo "$char" # Keep the character
fi
}

# Apply Remove Parts of the Data
FAULTY_DATA=$(apply_generic_transformation "$ORIGINAL_DATA" "remove_parts_transform")
# echo "Faulty Data (Removed Parts): $FAULTY_DATA"

# Apply Insert Non-Base64 Characters
FAULTY_DATA=$(apply_generic_transformation "$FAULTY_DATA" "insert_non_base64_transform")
# echo "Faulty Data (also with Non-Base64 Characters): $FAULTY_DATA"

DATA=$FAULTY_DATA


binary() {
cat <<END
{
"resourceType": "Binary",
"contentType": "application/pdf",
"data": "$DATA"
}
END
}

# Create a Binary resource that contains that faulty data, and get its ID (via JSON)
ID_VIA_JSON=$(curl -s -H 'Content-Type: application/fhir+json' -d "$(binary)" "$BASE/Binary" | jq -r '.id')

echo "Created Binary resource that contains the Random Data"
echo " - via JSON, with ID: $ID_VIA_JSON"


# Retrieve the Binary resource, and Base64 encode it so it can be safely handled by Bash (JSON)
BASE64_ENCODED_BINARY_RESOURCE_VIA_JSON=$(curl -s -H 'Accept: application/pdf' "$BASE/Binary/$ID_VIA_JSON" | base64 | tr -d '\n')


echo "Binary data retrieved. Verifying content... (JSON version)"

if [ "$DATA" = "$BASE64_ENCODED_BINARY_RESOURCE_VIA_JSON" ]; then
echo "✅ Base64 encoding of both the Original Data and the Retrieved Resource Data match (JSON)"
else
echo "🆘 Base64 encoding of both the Original Data and the Retrieved Resource Data are different (JSON)"
exit 1
fi
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,9 @@ jobs:
- name: Binary Content Download - not found
run: .github/scripts/read-binary-content-not-found.sh

- name: Binary Content Download - invalid (via JSON)
run: .github/scripts/read-binary-content-via-json-invalid.sh

- name: Binary Content Download - found (via JSON)
run: .github/scripts/read-binary-content-via-json-found.sh

Expand Down
2 changes: 1 addition & 1 deletion modules/rest-util/src/blaze/middleware/fhir/output.clj
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
(update response :body generate-binary**)
(catch Throwable e
(assoc response
:body (generate-error-payload identity e)
:body (generate-error-payload generate-json e)
:status 500))))

(defn- generate-binary [response]
Expand Down
6 changes: 3 additions & 3 deletions modules/rest-util/test/blaze/middleware/fhir/output_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,12 @@
[:headers "Content-Type"] := nil
:body := nil)))

(testing "failing binary emit (with invalid data)"
(testing "failing binary emit (with invalid data, using the JSON default)"
(given (call (binary-resource-handler-200 {:content-type "application/pdf" :data "MTANjECg=="}) {:headers {"accept" "text/plain"}})
:status := 500
[:headers "Content-Type"] := "application/pdf"
[:body :fhir/type] := :fhir/OperationOutcome
[:body :issue 0 :diagnostics] := "Input byte array has wrong 4-byte ending unit"))))
[:body parse-json :fhir/type] := :fhir/OperationOutcome
[:body parse-json :issue 0 :diagnostics] := "Input byte array has wrong 4-byte ending unit"))))

(deftest not-acceptable-test
(is (nil? (call resource-handler-200-with-patient {:headers {"accept" "text/plain"}}))))

0 comments on commit 61c4925

Please sign in to comment.