Skip to content

Commit

Permalink
Add support for zipped payloads in export (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
360dgries authored Jul 9, 2024
1 parent 1247aab commit 16cb96a
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions lib/davinci_pdex_test_kit/mock_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ def server_proxy
url: ENV.fetch('FHIR_REFERENCE_SERVER'),
params: {},
headers: {'Content-Type' => 'application/json', 'Authorization' => 'Bearer SAMPLE_TOKEN', 'Host' => ENV.fetch('HOST_HEADER')},
)
) do |proxy|
proxy.use FaradayMiddleware::Gzip
end
end

def token_response(request, _test = nil, _test_result = nil)
Expand All @@ -34,8 +36,9 @@ def claim_response(request, test = nil, test_result = nil)
response = server_proxy.get(endpoint, params)
request.status = response.status
response_resource = replace_bundle_urls(FHIR.from_contents(response.body))
request.response_headers = response.headers.reject!{|key, value| key == "transfer-encoding"} # chunked causes problems for client
request.response_headers = remove_transfer_encoding_header(response.headers)
request.response_body = response_resource.to_json
request.response_header("content-length").value = request.response_body.length
else
response = server_proxy.get('Patient', {_id: 999})
response_resource = FHIR.from_contents(response.body)
Expand All @@ -49,14 +52,14 @@ def claim_response(request, test = nil, test_result = nil)
def read_next_page(request, test = nil, test_result = nil)
response = server_proxy.get('', request.query_parameters)
request.status = response.status
request.response_headers = response.headers.reject!{|key, value| key == "transfer-encoding"}
request.response_headers = remove_transfer_encoding_header(response.headers)
request.response_body = replace_bundle_urls(FHIR.from_contents(response.body)).to_json
end

def everything_response(request, test = nil, test_result = nil)
response = server_proxy.get('Patient/999/$everything') #TODO: Change from static response
request.status = response.status
request.response_headers = response.headers.reject!{|key, value| key == "transfer-encoding"}
request.response_headers = remove_transfer_encoding_header(response.headers)
request.response_body = replace_bundle_urls(FHIR.from_contents(response.body)).to_json
end

Expand All @@ -80,7 +83,7 @@ def export_status_response(request, test = nil, test_result = nil)
req.headers = headers_as_hash.merge(server_proxy.headers)
end
request.status = response.status
request.response_headers = response.env.response_headers
request.response_headers = remove_transfer_encoding_header(response.env.response_headers)
request.response_body = response.status.to_i == 200 ? replace_export_urls(JSON.parse(response.body)).to_json : response.body
request.response_header("content-length").value = request.response_body.length
end
Expand Down Expand Up @@ -128,6 +131,14 @@ def get_metadata
proc { [200, {'Content-Type' => 'application/fhir+json;charset=utf-8'}, [File.read("lib/davinci_pdex_test_kit/metadata/mock_capability_statement.json")]] }
end

def remove_transfer_encoding_header(headers)
if !headers["transfer-encoding"].nil?
headers.reject!{|key, value| key == "transfer-encoding"}
else
headers
end
end

def match_request_to_expectation(endpoint, params)
matched_search = SEARCHES_BY_PRIORITY[endpoint.to_sym].find {|expectation| (params.keys.map{|key| key.to_s} & expectation).sort == expectation}
# matched_search_without_patient = SEARCHES_BY_PRIORITY[endpoint.to_sym].find {|expectation| (params.keys.map{|key| key.to_s} << "patient" & expectation) == expectation}
Expand Down

0 comments on commit 16cb96a

Please sign in to comment.