Module: DaVinciPDexTestKit::MockServer
Overview
Instance Method Summary
collapse
-
#absolute_reference(ref, entries, root_url) ⇒ Object
-
#binary_read_response(request, test = nil, test_result = nil) ⇒ Object
-
#claim_response(request, test = nil, test_result = nil) ⇒ Object
-
#everything_response(request, test = nil, test_result = nil) ⇒ Object
-
#export_response(request, test = nil, test_result = nil) ⇒ Object
-
#export_status_response(request, test = nil, test_result = nil) ⇒ Object
-
#extract_bearer_token(request) ⇒ Object
Header expected to be a bearer token of the form “Bearer: <token>”.
-
#extract_client_id(request) ⇒ Object
-
#extract_token_from_query_params(request) ⇒ Object
-
#fetch_all_bundled_resources(reply_handler: nil, max_pages: 0, additional_resource_types: [], resource_type: self.resource_type) ⇒ Object
-
#find_matching_entry(ref, entries, root_url = '') ⇒ Object
-
#get_metadata ⇒ Object
-
#match_request_to_expectation(endpoint, params) ⇒ Object
-
#member_match_response(request, test = nil, test_result = nil) ⇒ Object
-
#mock_operation_outcome_resource ⇒ Object
-
#new_link ⇒ Object
-
#read_next_page(request, test = nil, test_result = nil) ⇒ Object
-
#referenced_entities(resource, entries, root_url) ⇒ Object
-
#relative_reference?(ref) ⇒ Boolean
-
#remove_transfer_encoding_header(headers) ⇒ Object
-
#replace_bundle_urls(bundle) ⇒ Object
-
#replace_export_urls(export_status_output) ⇒ Object
-
#resource_endpoint(url) ⇒ Object
Pull resource type from url e.g.
-
#server_proxy ⇒ Object
-
#token_response(request, _test = nil, _test_result = nil) ⇒ Object
Methods included from URLs
#base_fhir_url, #base_url, #binary_url, #everything_url, #export_status_url, #export_url, #member_match_url, #metadata_url, #patient_url, #resume_clinical_data_url, #resume_fail_url, #resume_pass_url, #submit_url, #suite_id, #token_url
Instance Method Details
#absolute_reference(ref, entries, root_url) ⇒ Object
228
229
230
231
232
|
# File 'lib/davinci_pdex_test_kit/mock_server.rb', line 228
def absolute_reference(ref, entries, root_url)
url = find_matching_entry(ref&.reference, entries, root_url)&.fullUrl
ref.reference = url if url
ref
end
|
#binary_read_response(request, test = nil, test_result = nil) ⇒ Object
91
92
93
94
95
96
97
|
# File 'lib/davinci_pdex_test_kit/mock_server.rb', line 91
def binary_read_response(request, test = nil, test_result = nil)
binary_id = request.url.split('/').last
response = server_proxy.get('Binary/'+binary_id)
request.status = response.status
request. = response.
request.response_body = response.body
end
|
#claim_response(request, test = nil, test_result = nil) ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/davinci_pdex_test_kit/mock_server.rb', line 32
def claim_response(request, test = nil, test_result = nil)
endpoint = resource_endpoint(request.url)
params = match_request_to_expectation(endpoint, request.query_parameters)
if params
response = server_proxy.get(endpoint, params)
request.status = response.status
response_resource = replace_bundle_urls(FHIR.from_contents(response.body))
request. = (response.)
request.response_body = response_resource.to_json
request.("content-length").value = request.response_body.length
else
response = server_proxy.get('Patient', {_id: 999})
response_resource = FHIR.from_contents(response.body)
response_resource.entry = [{fullUrl: 'urn:uuid:2866af9c-137d-4458-a8a9-eeeec0ce5583', resource: mock_operation_outcome_resource, search: {mode: 'outcome'}}]
response_resource.link.first.url = request.url request.status = 400
request.response_body = response_resource.to_json
end
end
|
#everything_response(request, test = nil, test_result = nil) ⇒ Object
59
60
61
62
63
64
|
# File 'lib/davinci_pdex_test_kit/mock_server.rb', line 59
def everything_response(request, test = nil, test_result = nil)
response = server_proxy.get('Patient/999/$everything') request.status = response.status
request. = (response.)
request.response_body = replace_bundle_urls(FHIR.from_contents(response.body)).to_json
end
|
#export_response(request, test = nil, test_result = nil) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/davinci_pdex_test_kit/mock_server.rb', line 66
def export_response(request, test = nil, test_result = nil)
= request..map { || {"#{.name}": .value}}.reduce({}) { |reduced, curr| reduced.merge(curr)}
response = server_proxy.get do |req|
req.url 'Group/pdex-Group/$export' req. = .merge(server_proxy.)
end
request.status = response.status
request. = response.env.
request.("content-location").value.gsub!(/(.*)\?/, "#{new_link}/$export-poll-status?")
request.response_body = response.body
end
|
#export_status_response(request, test = nil, test_result = nil) ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/davinci_pdex_test_kit/mock_server.rb', line 78
def export_status_response(request, test = nil, test_result = nil)
= request..map { || {"#{.name}": .value}}.reduce({}) { |reduced, curr| reduced.merge(curr)}
response = server_proxy.get do |req|
req.url '$export-poll-status'
req.params = request.query_parameters
req. = .merge(server_proxy.)
end
request.status = response.status
request. = (response.env.)
request.response_body = response.status.to_i == 200 ? replace_export_urls(JSON.parse(response.body)).to_json : response.body
request.("content-length").value = request.response_body.length
end
|
Header expected to be a bearer token of the form “Bearer: <token>”
163
164
165
|
# File 'lib/davinci_pdex_test_kit/mock_server.rb', line 163
def (request)
request.('Authorization')&.value&.split&.last
end
|
158
159
160
|
# File 'lib/davinci_pdex_test_kit/mock_server.rb', line 158
def (request)
URI.decode_www_form(request.request_body).to_h['client_id']
end
|
167
168
169
|
# File 'lib/davinci_pdex_test_kit/mock_server.rb', line 167
def (request)
request.query_parameters['token']
end
|
#fetch_all_bundled_resources(reply_handler: nil, max_pages: 0, additional_resource_types: [], resource_type: self.resource_type) ⇒ Object
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
# File 'lib/davinci_pdex_test_kit/mock_server.rb', line 234
def fetch_all_bundled_resources(
reply_handler: nil,
max_pages: 0,
additional_resource_types: [],
resource_type: self.resource_type
)
page_count = 1
resources = []
bundle = resource
resources += bundle&.entry&.map { |entry| entry&.resource }
until bundle.nil? || (page_count == max_pages && max_pages != 0)
next_bundle_link = bundle&.link&.find { |link| link.relation == 'next' }&.url
reply_handler&.call(response)
break if next_bundle_link.blank?
reply = fhir_client.raw_read_url(next_bundle_link)
store_request('outgoing') { reply }
error_message = cant_resolve_next_bundle_message(next_bundle_link)
assert_response_status(200)
assert_valid_json(reply.body, error_message)
bundle = fhir_client.parse_reply(FHIR::Bundle, fhir_client.default_format, reply)
resources += bundle&.entry&.map { |entry| entry&.resource }
page_count += 1
end
valid_resource_types = [resource_type, 'OperationOutcome'].concat(additional_resource_types)
resources
end
|
#find_matching_entry(ref, entries, root_url = '') ⇒ Object
270
271
272
273
274
|
# File 'lib/davinci_pdex_test_kit/mock_server.rb', line 270
def find_matching_entry(ref, entries, root_url = '')
ref = "#{root_url}/#{ref}" if relative_reference?(ref) && root_url&.present?
entries&.find { |entry| entry&.fullUrl == ref }
end
|
130
131
132
|
# File 'lib/davinci_pdex_test_kit/mock_server.rb', line 130
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
|
#match_request_to_expectation(endpoint, params) ⇒ Object
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
# File 'lib/davinci_pdex_test_kit/mock_server.rb', line 142
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}
if matched_search
params.select {|key, value| matched_search.include?(key.to_s) || key == "_revInclude" || key == "_include"}
else
nil
end
end
|
#member_match_response(request, test = nil, test_result = nil) ⇒ Object
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/davinci_pdex_test_kit/mock_server.rb', line 99
def member_match_response(request, test = nil, test_result = nil)
original_request_as_hash = JSON.parse(request.request_body).to_h
request.request_body = original_request_as_hash.to_json
request.response_body = {
resourceType: "Parameters",
parameter: [
{
name: "MemberIdentifier",
valueIdentifier: {
type: {
coding: [
{
system: "http://terminology.hl7.org/CodeSystem/v2-0203",
code: "MB"
}
]
},
system: "https://github.com/inferno-framework/target-payer/identifiers/member",
value: "99999",
assigner: {
display: "Old Payer"
}
}
}
]
}.to_json
request.status = 200
end
|
#mock_operation_outcome_resource ⇒ Object
200
201
202
|
# File 'lib/davinci_pdex_test_kit/mock_server.rb', line 200
def mock_operation_outcome_resource
FHIR.from_contents(File.read("lib/davinci_pdex_test_kit/metadata/mock_operation_outcome_resource.json"))
end
|
#new_link ⇒ Object
223
224
225
|
# File 'lib/davinci_pdex_test_kit/mock_server.rb', line 223
def new_link
"#{Inferno::Application['base_url']}\/custom\/pdex_payer_client\/fhir"
end
|
#read_next_page(request, test = nil, test_result = nil) ⇒ Object
52
53
54
55
56
57
|
# File 'lib/davinci_pdex_test_kit/mock_server.rb', line 52
def read_next_page(request, test = nil, test_result = nil)
response = server_proxy.get('', request.query_parameters)
request.status = response.status
request. = (response.)
request.response_body = replace_bundle_urls(FHIR.from_contents(response.body)).to_json
end
|
#referenced_entities(resource, entries, root_url) ⇒ Object
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
# File 'lib/davinci_pdex_test_kit/mock_server.rb', line 181
def referenced_entities(resource, entries, root_url)
matches = []
attributes = resource&.source_hash&.keys
attributes.each do |attr|
value = resource.send(attr.to_sym)
if value.is_a?(FHIR::Reference) && value.reference.present?
match = find_matching_entry(value.reference, entries, root_url)
if match.present? && matches.none?(match)
value.reference = match.fullUrl
matches.concat([match], referenced_entities(match.resource, entries, root_url))
end
elsif value.is_a?(Array) && value.all? { |elmt| elmt.is_a?(FHIR::Model) }
value.each { |val| matches.concat(referenced_entities(val, entries, root_url)) }
end
end
matches
end
|
#relative_reference?(ref) ⇒ Boolean
277
278
279
|
# File 'lib/davinci_pdex_test_kit/mock_server.rb', line 277
def relative_reference?(ref)
ref&.count('/') == 1
end
|
134
135
136
137
138
139
140
|
# File 'lib/davinci_pdex_test_kit/mock_server.rb', line 134
def ()
if !["transfer-encoding"].nil?
.reject!{|key, value| key == "transfer-encoding"}
else
end
end
|
#replace_bundle_urls(bundle) ⇒ Object
204
205
206
207
208
209
210
211
212
213
214
|
# File 'lib/davinci_pdex_test_kit/mock_server.rb', line 204
def replace_bundle_urls(bundle)
reference_server_base = ENV.fetch('FHIR_REFERENCE_SERVER')
bundle&.link.map! {|link| {relation: link.relation, url: link.url.gsub(reference_server_base, new_link)}}
bundle&.entry&.map! do |bundled_resource|
{fullUrl: bundled_resource.fullUrl.gsub(reference_server_base, new_link),
resource: bundled_resource.resource,
search: bundled_resource.search
}
end
bundle
end
|
#replace_export_urls(export_status_output) ⇒ Object
216
217
218
219
220
221
|
# File 'lib/davinci_pdex_test_kit/mock_server.rb', line 216
def replace_export_urls(export_status_output)
reference_server_base = ENV.fetch('FHIR_REFERENCE_SERVER')
export_status_output['output'].map! { |binary| {type: binary["type"], url: binary["url"].gsub(reference_server_base, new_link)} }
export_status_output['request'] = new_link + '/Patient/$export'
export_status_output
end
|
#resource_endpoint(url) ⇒ Object
174
175
176
177
178
|
# File 'lib/davinci_pdex_test_kit/mock_server.rb', line 174
def resource_endpoint(url)
return unless url.start_with?('http://', 'https://')
/custom\/pdex_payer_client\/fhir\/(.*)\?/.match(url)[1]
end
|
#server_proxy ⇒ Object
16
17
18
19
20
21
22
23
24
|
# File 'lib/davinci_pdex_test_kit/mock_server.rb', line 16
def server_proxy
@server_proxy ||= Faraday.new(
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
|
#token_response(request, _test = nil, _test_result = nil) ⇒ Object
26
27
28
29
30
|
# File 'lib/davinci_pdex_test_kit/mock_server.rb', line 26
def token_response(request, _test = nil, _test_result = nil)
request.response_body = { access_token: SecureRandom.hex, token_type: 'bearer', expires_in: 300 }.to_json
request.status = 200
end
|