Skip to content

Commit

Permalink
make sure charset parameter is included with fhir mime types
Browse files Browse the repository at this point in the history
  • Loading branch information
radamson committed Mar 20, 2019
1 parent 8fd1cbd commit 18f76d7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
3 changes: 3 additions & 0 deletions lib/fhir_client/resource_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ def self.fhir_headers(headers = {}, additional_headers = {}, format = DEFAULT_CO
# ,but an accept header is explicitly supplied then it will be used (or override the existing)
fhir_headers.merge!(headers) unless headers.empty?
fhir_headers.merge!(additional_headers)
if /\Aapplication\/(fhir\+xml|xml\+fhir|fhir\+json|json\+fhir)\z/.match? fhir_headers['Content-Type']
fhir_headers['Content-Type'] << ";charset=#{DEFAULT_CHARSET}"
end
fhir_headers
end

Expand Down
28 changes: 25 additions & 3 deletions test/unit/client_interface_sections/create_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ def test_create_response_properly_parsed_xml
patient = FHIR::Patient.new({'gender'=>'female', 'active'=>true, 'deceasedBoolean'=>false})
outcome = FHIR::OperationOutcome.new({'issue'=>[{'code'=>'informational', 'severity'=>'information', 'diagnostics'=>'Successfully created "Patient/foo" in 0 ms'}]})

stub_request(:post, /create-test/).to_return(status: 201, body: outcome.to_xml, headers: {'Content-Type'=>'application/fhir+xml', 'Location'=>'http:https://create-test/Patient/foo/_history/0', 'ETag'=>'W/"foo"', 'Last-Modified'=>Time.now.strftime("%a, %e %b %Y %T %Z")})
stub_request(:post, /create-test/)
.with(headers: {'Content-Type'=>'application/fhir+xml;charset=utf-8'})
.to_return(status: 201,
body: outcome.to_xml,
headers: {'Content-Type'=>'application/fhir+xml',
'Location'=>'http:https://create-test/Patient/foo/_history/0',
'ETag'=>'W/"foo"',
'Last-Modified'=>Time.now.strftime("%a, %e %b %Y %T %Z")})
client.default_xml
reply = client.create(patient)
assert reply.resource.is_a?(FHIR::OperationOutcome)
assert reply.resource_class == FHIR::Patient
Expand All @@ -22,7 +30,15 @@ def test_create_response_properly_parsed_json
patient = FHIR::Patient.new({'gender'=>'female', 'active'=>true, 'deceasedBoolean'=>false})
outcome = FHIR::OperationOutcome.new({'issue'=>[{'code'=>'informational', 'severity'=>'information', 'diagnostics'=>'Successfully created "Patient/foo" in 0 ms'}]})

stub_request(:post, /create-test/).to_return(status: 201, body: outcome.to_json, headers: {'Content-Type'=>'application/fhir+json', 'Location'=>'http:https://create-test/Patient/foo/_history/0', 'ETag'=>'W/"foo"', 'Last-Modified'=>Time.now.strftime("%a, %e %b %Y %T %Z")})
stub_request(:post, /create-test/)
.with(headers: {'Content-Type'=>'application/fhir+json;charset=utf-8'})
.to_return(status: 201,
body: outcome.to_json,
headers: {'Content-Type'=>'application/fhir+json',
'Location'=>'http:https://create-test/Patient/foo/_history/0',
'ETag'=>'W/"foo"',
'Last-Modified'=>Time.now.strftime("%a, %e %b %Y %T %Z")})
client.default_json
reply = client.create(patient)
assert reply.resource.is_a?(FHIR::OperationOutcome)
assert reply.resource_class == FHIR::Patient
Expand All @@ -34,7 +50,13 @@ def test_create_response_properly_parsed_json
def test_create_response_blank
patient = FHIR::Patient.new({'gender'=>'female', 'active'=>true, 'deceasedBoolean'=>false})

stub_request(:post, /create-test/).to_return(status: 201, headers: {'Location'=>'http:https://create-test/Patient/foo/_history/0', 'ETag'=>'W/"foo"', 'Last-Modified'=>Time.now.strftime("%a, %e %b %Y %T %Z")})
stub_request(:post, /create-test/)
.with(headers: {'Content-Type'=>'application/fhir+xml;charset=utf-8'})
.to_return(status: 201,
headers: {'Location'=>'http:https://create-test/Patient/foo/_history/0',
'ETag'=>'W/"foo"',
'Last-Modified'=>Time.now.strftime("%a, %e %b %Y %T %Z")})
client.default_xml
reply = client.create(patient)
assert reply.resource.is_a?(FHIR::Patient)
assert reply.resource_class == FHIR::Patient
Expand Down
2 changes: 1 addition & 1 deletion test/unit/client_interface_sections/update_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_class_partial_update
def test_update
FHIR::Model.client = client
patient = FHIR::Patient.new({'id' => 'foo', 'active'=>true})
stub_request(:put, /update-test\/Patient\/foo/).with(headers: {'Content-Type'=>'application/json+fhir'}).to_return(status: 200)
stub_request(:put, /update-test\/Patient\/foo/).with(headers: {'Content-Type'=>'application/json+fhir;charset=utf-8'}).to_return(status: 200)
reply = client.update(patient, 'foo')
check_header_keys reply
assert reply.code == 200
Expand Down

0 comments on commit 18f76d7

Please sign in to comment.