Class: Inferno::Entities::Request

Inherits:
Entity
  • Object
show all
Includes:
Attributes
Defined in:
lib/inferno/entities/request.rb

Overview

A Request represents a request and response issued during a test.

Constant Summary collapse

ATTRIBUTES =
[
  :id, :index, :verb, :url, :direction, :name, :status,
  :request_body, :response_body, :result_id, :test_session_id, :created_at,
  :updated_at, :headers
].freeze
SUMMARY_FIELDS =
[
  :id, :index, :url, :verb, :direction, :name, :status, :result_id, :created_at, :updated_at
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Request

Returns a new instance of Request.



45
46
47
48
49
50
# File 'lib/inferno/entities/request.rb', line 45

def initialize(params)
  super(params, ATTRIBUTES - [:headers, :name])

  @name = params[:name]&.to_sym
  @headers = params[:headers]&.map { |header| header.is_a?(Hash) ? Header.new(header) : header } || []
end

Instance Attribute Details

#created_atTime

Returns creation timestamp.

Returns:

  • (Time)

    creation timestamp



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/inferno/entities/request.rb', line 33

class Request < Entity
  ATTRIBUTES = [
    :id, :index, :verb, :url, :direction, :name, :status,
    :request_body, :response_body, :result_id, :test_session_id, :created_at,
    :updated_at, :headers
  ].freeze
  SUMMARY_FIELDS = [
    :id, :index, :url, :verb, :direction, :name, :status, :result_id, :created_at, :updated_at
  ].freeze

  include Attributes

  def initialize(params)
    super(params, ATTRIBUTES - [:headers, :name])

    @name = params[:name]&.to_sym
    @headers = params[:headers]&.map { |header| header.is_a?(Hash) ? Header.new(header) : header } || []
  end

  # @return [Hash<String, String>]
  def query_parameters
    Addressable::URI.parse(url).query_values || {}
  end

  # Find a response header
  #
  # @param name [String] the header name
  # @return [Inferno::Entities::RequestHeader, nil]
  def response_header(name)
    response_headers.find { |header| header.name.casecmp(name).zero? }
  end

  # Find a request header
  #
  # @param name [String] the header name
  # @return [Inferno::Entities::RequestHeader, nil]
  def request_header(name)
    request_headers.find { |header| header.name.casecmp(name).zero? }
  end

  # All of the request headers
  #
  # @return [Array<Inferno::Entities::RequestHeader>]
  def request_headers
    headers.select(&:request?)
  end

  # All of the response headers
  #
  # @return [Array<Inferno::Entities::RequestHeader>]
  def response_headers
    headers.select(&:response?)
  end

  # Return a hash of the request parameters
  #
  # @return [Hash]
  def request
    {
      verb: verb,
      url: url,
      headers: request_headers,
      body: request_body
    }
  end

  # Return a hash of the response parameters
  #
  # @return [Hash]
  def response
    {
      status: status,
      headers: response_headers,
      body: response_body
    }
  end

  # @private
  def to_hash
    {
      id: id,
      verb: verb,
      url: url,
      direction: direction,
      status: status,
      name: name,
      request_body: request_body,
      response_body: response_body,
      result_id: result_id,
      test_session_id: test_session_id,
      request_headers: request_headers.map(&:to_hash),
      response_headers: response_headers.map(&:to_hash),
      created_at: created_at,
      updated_at: updated_at
    }.compact
  end

  # Return the FHIR resource from the response body.
  #
  # @return [FHIR::Model]
  def resource
    FHIR.from_contents(response_body)
  end

  class << self
    # @private
    def from_rack_env(env, name: nil)
      rack_request = env['router.request'].rack_request
      url = "#{rack_request.base_url}#{rack_request.path}"
      url += "?#{rack_request.query_string}" if rack_request.query_string.present?
      request_headers =
        env
          .select { |key, _| key.start_with? 'HTTP_' }
          .transform_keys { |key| key.delete_prefix('HTTP_').tr('_', '-').downcase }
          .map { |header_name, value| Header.new(name: header_name, value: value, type: 'request') }

      new(
        verb: rack_request.request_method.downcase,
        url: url,
        direction: 'incoming',
        name: name,
        request_body: rack_request.body.string,
        headers: request_headers
      )
    end

    # @private
    def from_http_response(response, test_session_id:, direction: 'outgoing', name: nil)
      request_headers =
        response.env.request_headers
          .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'request') }
      response_headers =
        response.headers
          .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'response') }

      new(
        verb: response.env.method,
        url: response.env.url.to_s,
        direction: direction,
        name: name,
        status: response.status,
        request_body: response.env.request_body,
        response_body: response.body,
        test_session_id: test_session_id,
        headers: request_headers + response_headers
      )
    end

    # @private
    def from_fhir_client_reply(reply, test_session_id:, direction: 'outgoing', name: nil)
      request = reply.request
      response = reply.response
      request_headers = request[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'request') }
      response_headers = response[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'response') }
      request_body =
        if request.dig(:headers, 'Content-Type')&.include?('application/x-www-form-urlencoded')
          URI.encode_www_form(request[:payload])
        else
          request[:payload]
        end

      new(
        verb: request[:method],
        url: request[:url],
        direction: direction,
        name: name,
        status: response[:code].to_i,
        request_body: request_body,
        response_body: response[:body],
        test_session_id: test_session_id,
        headers: request_headers + response_headers
      )
    end
  end
end

#directionString

Returns incoming/outgoing.

Returns:

  • (String)

    incoming/outgoing



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/inferno/entities/request.rb', line 33

class Request < Entity
  ATTRIBUTES = [
    :id, :index, :verb, :url, :direction, :name, :status,
    :request_body, :response_body, :result_id, :test_session_id, :created_at,
    :updated_at, :headers
  ].freeze
  SUMMARY_FIELDS = [
    :id, :index, :url, :verb, :direction, :name, :status, :result_id, :created_at, :updated_at
  ].freeze

  include Attributes

  def initialize(params)
    super(params, ATTRIBUTES - [:headers, :name])

    @name = params[:name]&.to_sym
    @headers = params[:headers]&.map { |header| header.is_a?(Hash) ? Header.new(header) : header } || []
  end

  # @return [Hash<String, String>]
  def query_parameters
    Addressable::URI.parse(url).query_values || {}
  end

  # Find a response header
  #
  # @param name [String] the header name
  # @return [Inferno::Entities::RequestHeader, nil]
  def response_header(name)
    response_headers.find { |header| header.name.casecmp(name).zero? }
  end

  # Find a request header
  #
  # @param name [String] the header name
  # @return [Inferno::Entities::RequestHeader, nil]
  def request_header(name)
    request_headers.find { |header| header.name.casecmp(name).zero? }
  end

  # All of the request headers
  #
  # @return [Array<Inferno::Entities::RequestHeader>]
  def request_headers
    headers.select(&:request?)
  end

  # All of the response headers
  #
  # @return [Array<Inferno::Entities::RequestHeader>]
  def response_headers
    headers.select(&:response?)
  end

  # Return a hash of the request parameters
  #
  # @return [Hash]
  def request
    {
      verb: verb,
      url: url,
      headers: request_headers,
      body: request_body
    }
  end

  # Return a hash of the response parameters
  #
  # @return [Hash]
  def response
    {
      status: status,
      headers: response_headers,
      body: response_body
    }
  end

  # @private
  def to_hash
    {
      id: id,
      verb: verb,
      url: url,
      direction: direction,
      status: status,
      name: name,
      request_body: request_body,
      response_body: response_body,
      result_id: result_id,
      test_session_id: test_session_id,
      request_headers: request_headers.map(&:to_hash),
      response_headers: response_headers.map(&:to_hash),
      created_at: created_at,
      updated_at: updated_at
    }.compact
  end

  # Return the FHIR resource from the response body.
  #
  # @return [FHIR::Model]
  def resource
    FHIR.from_contents(response_body)
  end

  class << self
    # @private
    def from_rack_env(env, name: nil)
      rack_request = env['router.request'].rack_request
      url = "#{rack_request.base_url}#{rack_request.path}"
      url += "?#{rack_request.query_string}" if rack_request.query_string.present?
      request_headers =
        env
          .select { |key, _| key.start_with? 'HTTP_' }
          .transform_keys { |key| key.delete_prefix('HTTP_').tr('_', '-').downcase }
          .map { |header_name, value| Header.new(name: header_name, value: value, type: 'request') }

      new(
        verb: rack_request.request_method.downcase,
        url: url,
        direction: 'incoming',
        name: name,
        request_body: rack_request.body.string,
        headers: request_headers
      )
    end

    # @private
    def from_http_response(response, test_session_id:, direction: 'outgoing', name: nil)
      request_headers =
        response.env.request_headers
          .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'request') }
      response_headers =
        response.headers
          .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'response') }

      new(
        verb: response.env.method,
        url: response.env.url.to_s,
        direction: direction,
        name: name,
        status: response.status,
        request_body: response.env.request_body,
        response_body: response.body,
        test_session_id: test_session_id,
        headers: request_headers + response_headers
      )
    end

    # @private
    def from_fhir_client_reply(reply, test_session_id:, direction: 'outgoing', name: nil)
      request = reply.request
      response = reply.response
      request_headers = request[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'request') }
      response_headers = response[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'response') }
      request_body =
        if request.dig(:headers, 'Content-Type')&.include?('application/x-www-form-urlencoded')
          URI.encode_www_form(request[:payload])
        else
          request[:payload]
        end

      new(
        verb: request[:method],
        url: request[:url],
        direction: direction,
        name: name,
        status: response[:code].to_i,
        request_body: request_body,
        response_body: response[:body],
        test_session_id: test_session_id,
        headers: request_headers + response_headers
      )
    end
  end
end

#headersArray<Inferno::Entities::Header>

Returns http request/response headers.

Returns:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/inferno/entities/request.rb', line 33

class Request < Entity
  ATTRIBUTES = [
    :id, :index, :verb, :url, :direction, :name, :status,
    :request_body, :response_body, :result_id, :test_session_id, :created_at,
    :updated_at, :headers
  ].freeze
  SUMMARY_FIELDS = [
    :id, :index, :url, :verb, :direction, :name, :status, :result_id, :created_at, :updated_at
  ].freeze

  include Attributes

  def initialize(params)
    super(params, ATTRIBUTES - [:headers, :name])

    @name = params[:name]&.to_sym
    @headers = params[:headers]&.map { |header| header.is_a?(Hash) ? Header.new(header) : header } || []
  end

  # @return [Hash<String, String>]
  def query_parameters
    Addressable::URI.parse(url).query_values || {}
  end

  # Find a response header
  #
  # @param name [String] the header name
  # @return [Inferno::Entities::RequestHeader, nil]
  def response_header(name)
    response_headers.find { |header| header.name.casecmp(name).zero? }
  end

  # Find a request header
  #
  # @param name [String] the header name
  # @return [Inferno::Entities::RequestHeader, nil]
  def request_header(name)
    request_headers.find { |header| header.name.casecmp(name).zero? }
  end

  # All of the request headers
  #
  # @return [Array<Inferno::Entities::RequestHeader>]
  def request_headers
    headers.select(&:request?)
  end

  # All of the response headers
  #
  # @return [Array<Inferno::Entities::RequestHeader>]
  def response_headers
    headers.select(&:response?)
  end

  # Return a hash of the request parameters
  #
  # @return [Hash]
  def request
    {
      verb: verb,
      url: url,
      headers: request_headers,
      body: request_body
    }
  end

  # Return a hash of the response parameters
  #
  # @return [Hash]
  def response
    {
      status: status,
      headers: response_headers,
      body: response_body
    }
  end

  # @private
  def to_hash
    {
      id: id,
      verb: verb,
      url: url,
      direction: direction,
      status: status,
      name: name,
      request_body: request_body,
      response_body: response_body,
      result_id: result_id,
      test_session_id: test_session_id,
      request_headers: request_headers.map(&:to_hash),
      response_headers: response_headers.map(&:to_hash),
      created_at: created_at,
      updated_at: updated_at
    }.compact
  end

  # Return the FHIR resource from the response body.
  #
  # @return [FHIR::Model]
  def resource
    FHIR.from_contents(response_body)
  end

  class << self
    # @private
    def from_rack_env(env, name: nil)
      rack_request = env['router.request'].rack_request
      url = "#{rack_request.base_url}#{rack_request.path}"
      url += "?#{rack_request.query_string}" if rack_request.query_string.present?
      request_headers =
        env
          .select { |key, _| key.start_with? 'HTTP_' }
          .transform_keys { |key| key.delete_prefix('HTTP_').tr('_', '-').downcase }
          .map { |header_name, value| Header.new(name: header_name, value: value, type: 'request') }

      new(
        verb: rack_request.request_method.downcase,
        url: url,
        direction: 'incoming',
        name: name,
        request_body: rack_request.body.string,
        headers: request_headers
      )
    end

    # @private
    def from_http_response(response, test_session_id:, direction: 'outgoing', name: nil)
      request_headers =
        response.env.request_headers
          .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'request') }
      response_headers =
        response.headers
          .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'response') }

      new(
        verb: response.env.method,
        url: response.env.url.to_s,
        direction: direction,
        name: name,
        status: response.status,
        request_body: response.env.request_body,
        response_body: response.body,
        test_session_id: test_session_id,
        headers: request_headers + response_headers
      )
    end

    # @private
    def from_fhir_client_reply(reply, test_session_id:, direction: 'outgoing', name: nil)
      request = reply.request
      response = reply.response
      request_headers = request[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'request') }
      response_headers = response[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'response') }
      request_body =
        if request.dig(:headers, 'Content-Type')&.include?('application/x-www-form-urlencoded')
          URI.encode_www_form(request[:payload])
        else
          request[:payload]
        end

      new(
        verb: request[:method],
        url: request[:url],
        direction: direction,
        name: name,
        status: response[:code].to_i,
        request_body: request_body,
        response_body: response[:body],
        test_session_id: test_session_id,
        headers: request_headers + response_headers
      )
    end
  end
end

#idString

Returns id of the request.

Returns:

  • (String)

    id of the request



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/inferno/entities/request.rb', line 33

class Request < Entity
  ATTRIBUTES = [
    :id, :index, :verb, :url, :direction, :name, :status,
    :request_body, :response_body, :result_id, :test_session_id, :created_at,
    :updated_at, :headers
  ].freeze
  SUMMARY_FIELDS = [
    :id, :index, :url, :verb, :direction, :name, :status, :result_id, :created_at, :updated_at
  ].freeze

  include Attributes

  def initialize(params)
    super(params, ATTRIBUTES - [:headers, :name])

    @name = params[:name]&.to_sym
    @headers = params[:headers]&.map { |header| header.is_a?(Hash) ? Header.new(header) : header } || []
  end

  # @return [Hash<String, String>]
  def query_parameters
    Addressable::URI.parse(url).query_values || {}
  end

  # Find a response header
  #
  # @param name [String] the header name
  # @return [Inferno::Entities::RequestHeader, nil]
  def response_header(name)
    response_headers.find { |header| header.name.casecmp(name).zero? }
  end

  # Find a request header
  #
  # @param name [String] the header name
  # @return [Inferno::Entities::RequestHeader, nil]
  def request_header(name)
    request_headers.find { |header| header.name.casecmp(name).zero? }
  end

  # All of the request headers
  #
  # @return [Array<Inferno::Entities::RequestHeader>]
  def request_headers
    headers.select(&:request?)
  end

  # All of the response headers
  #
  # @return [Array<Inferno::Entities::RequestHeader>]
  def response_headers
    headers.select(&:response?)
  end

  # Return a hash of the request parameters
  #
  # @return [Hash]
  def request
    {
      verb: verb,
      url: url,
      headers: request_headers,
      body: request_body
    }
  end

  # Return a hash of the response parameters
  #
  # @return [Hash]
  def response
    {
      status: status,
      headers: response_headers,
      body: response_body
    }
  end

  # @private
  def to_hash
    {
      id: id,
      verb: verb,
      url: url,
      direction: direction,
      status: status,
      name: name,
      request_body: request_body,
      response_body: response_body,
      result_id: result_id,
      test_session_id: test_session_id,
      request_headers: request_headers.map(&:to_hash),
      response_headers: response_headers.map(&:to_hash),
      created_at: created_at,
      updated_at: updated_at
    }.compact
  end

  # Return the FHIR resource from the response body.
  #
  # @return [FHIR::Model]
  def resource
    FHIR.from_contents(response_body)
  end

  class << self
    # @private
    def from_rack_env(env, name: nil)
      rack_request = env['router.request'].rack_request
      url = "#{rack_request.base_url}#{rack_request.path}"
      url += "?#{rack_request.query_string}" if rack_request.query_string.present?
      request_headers =
        env
          .select { |key, _| key.start_with? 'HTTP_' }
          .transform_keys { |key| key.delete_prefix('HTTP_').tr('_', '-').downcase }
          .map { |header_name, value| Header.new(name: header_name, value: value, type: 'request') }

      new(
        verb: rack_request.request_method.downcase,
        url: url,
        direction: 'incoming',
        name: name,
        request_body: rack_request.body.string,
        headers: request_headers
      )
    end

    # @private
    def from_http_response(response, test_session_id:, direction: 'outgoing', name: nil)
      request_headers =
        response.env.request_headers
          .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'request') }
      response_headers =
        response.headers
          .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'response') }

      new(
        verb: response.env.method,
        url: response.env.url.to_s,
        direction: direction,
        name: name,
        status: response.status,
        request_body: response.env.request_body,
        response_body: response.body,
        test_session_id: test_session_id,
        headers: request_headers + response_headers
      )
    end

    # @private
    def from_fhir_client_reply(reply, test_session_id:, direction: 'outgoing', name: nil)
      request = reply.request
      response = reply.response
      request_headers = request[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'request') }
      response_headers = response[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'response') }
      request_body =
        if request.dig(:headers, 'Content-Type')&.include?('application/x-www-form-urlencoded')
          URI.encode_www_form(request[:payload])
        else
          request[:payload]
        end

      new(
        verb: request[:method],
        url: request[:url],
        direction: direction,
        name: name,
        status: response[:code].to_i,
        request_body: request_body,
        response_body: response[:body],
        test_session_id: test_session_id,
        headers: request_headers + response_headers
      )
    end
  end
end

#indexString

Returns index of the request. Used for ordering.

Returns:

  • (String)

    index of the request. Used for ordering.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/inferno/entities/request.rb', line 33

class Request < Entity
  ATTRIBUTES = [
    :id, :index, :verb, :url, :direction, :name, :status,
    :request_body, :response_body, :result_id, :test_session_id, :created_at,
    :updated_at, :headers
  ].freeze
  SUMMARY_FIELDS = [
    :id, :index, :url, :verb, :direction, :name, :status, :result_id, :created_at, :updated_at
  ].freeze

  include Attributes

  def initialize(params)
    super(params, ATTRIBUTES - [:headers, :name])

    @name = params[:name]&.to_sym
    @headers = params[:headers]&.map { |header| header.is_a?(Hash) ? Header.new(header) : header } || []
  end

  # @return [Hash<String, String>]
  def query_parameters
    Addressable::URI.parse(url).query_values || {}
  end

  # Find a response header
  #
  # @param name [String] the header name
  # @return [Inferno::Entities::RequestHeader, nil]
  def response_header(name)
    response_headers.find { |header| header.name.casecmp(name).zero? }
  end

  # Find a request header
  #
  # @param name [String] the header name
  # @return [Inferno::Entities::RequestHeader, nil]
  def request_header(name)
    request_headers.find { |header| header.name.casecmp(name).zero? }
  end

  # All of the request headers
  #
  # @return [Array<Inferno::Entities::RequestHeader>]
  def request_headers
    headers.select(&:request?)
  end

  # All of the response headers
  #
  # @return [Array<Inferno::Entities::RequestHeader>]
  def response_headers
    headers.select(&:response?)
  end

  # Return a hash of the request parameters
  #
  # @return [Hash]
  def request
    {
      verb: verb,
      url: url,
      headers: request_headers,
      body: request_body
    }
  end

  # Return a hash of the response parameters
  #
  # @return [Hash]
  def response
    {
      status: status,
      headers: response_headers,
      body: response_body
    }
  end

  # @private
  def to_hash
    {
      id: id,
      verb: verb,
      url: url,
      direction: direction,
      status: status,
      name: name,
      request_body: request_body,
      response_body: response_body,
      result_id: result_id,
      test_session_id: test_session_id,
      request_headers: request_headers.map(&:to_hash),
      response_headers: response_headers.map(&:to_hash),
      created_at: created_at,
      updated_at: updated_at
    }.compact
  end

  # Return the FHIR resource from the response body.
  #
  # @return [FHIR::Model]
  def resource
    FHIR.from_contents(response_body)
  end

  class << self
    # @private
    def from_rack_env(env, name: nil)
      rack_request = env['router.request'].rack_request
      url = "#{rack_request.base_url}#{rack_request.path}"
      url += "?#{rack_request.query_string}" if rack_request.query_string.present?
      request_headers =
        env
          .select { |key, _| key.start_with? 'HTTP_' }
          .transform_keys { |key| key.delete_prefix('HTTP_').tr('_', '-').downcase }
          .map { |header_name, value| Header.new(name: header_name, value: value, type: 'request') }

      new(
        verb: rack_request.request_method.downcase,
        url: url,
        direction: 'incoming',
        name: name,
        request_body: rack_request.body.string,
        headers: request_headers
      )
    end

    # @private
    def from_http_response(response, test_session_id:, direction: 'outgoing', name: nil)
      request_headers =
        response.env.request_headers
          .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'request') }
      response_headers =
        response.headers
          .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'response') }

      new(
        verb: response.env.method,
        url: response.env.url.to_s,
        direction: direction,
        name: name,
        status: response.status,
        request_body: response.env.request_body,
        response_body: response.body,
        test_session_id: test_session_id,
        headers: request_headers + response_headers
      )
    end

    # @private
    def from_fhir_client_reply(reply, test_session_id:, direction: 'outgoing', name: nil)
      request = reply.request
      response = reply.response
      request_headers = request[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'request') }
      response_headers = response[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'response') }
      request_body =
        if request.dig(:headers, 'Content-Type')&.include?('application/x-www-form-urlencoded')
          URI.encode_www_form(request[:payload])
        else
          request[:payload]
        end

      new(
        verb: request[:method],
        url: request[:url],
        direction: direction,
        name: name,
        status: response[:code].to_i,
        request_body: request_body,
        response_body: response[:body],
        test_session_id: test_session_id,
        headers: request_headers + response_headers
      )
    end
  end
end

#nameString

Returns name for the request.

Returns:

  • (String)

    name for the request



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/inferno/entities/request.rb', line 33

class Request < Entity
  ATTRIBUTES = [
    :id, :index, :verb, :url, :direction, :name, :status,
    :request_body, :response_body, :result_id, :test_session_id, :created_at,
    :updated_at, :headers
  ].freeze
  SUMMARY_FIELDS = [
    :id, :index, :url, :verb, :direction, :name, :status, :result_id, :created_at, :updated_at
  ].freeze

  include Attributes

  def initialize(params)
    super(params, ATTRIBUTES - [:headers, :name])

    @name = params[:name]&.to_sym
    @headers = params[:headers]&.map { |header| header.is_a?(Hash) ? Header.new(header) : header } || []
  end

  # @return [Hash<String, String>]
  def query_parameters
    Addressable::URI.parse(url).query_values || {}
  end

  # Find a response header
  #
  # @param name [String] the header name
  # @return [Inferno::Entities::RequestHeader, nil]
  def response_header(name)
    response_headers.find { |header| header.name.casecmp(name).zero? }
  end

  # Find a request header
  #
  # @param name [String] the header name
  # @return [Inferno::Entities::RequestHeader, nil]
  def request_header(name)
    request_headers.find { |header| header.name.casecmp(name).zero? }
  end

  # All of the request headers
  #
  # @return [Array<Inferno::Entities::RequestHeader>]
  def request_headers
    headers.select(&:request?)
  end

  # All of the response headers
  #
  # @return [Array<Inferno::Entities::RequestHeader>]
  def response_headers
    headers.select(&:response?)
  end

  # Return a hash of the request parameters
  #
  # @return [Hash]
  def request
    {
      verb: verb,
      url: url,
      headers: request_headers,
      body: request_body
    }
  end

  # Return a hash of the response parameters
  #
  # @return [Hash]
  def response
    {
      status: status,
      headers: response_headers,
      body: response_body
    }
  end

  # @private
  def to_hash
    {
      id: id,
      verb: verb,
      url: url,
      direction: direction,
      status: status,
      name: name,
      request_body: request_body,
      response_body: response_body,
      result_id: result_id,
      test_session_id: test_session_id,
      request_headers: request_headers.map(&:to_hash),
      response_headers: response_headers.map(&:to_hash),
      created_at: created_at,
      updated_at: updated_at
    }.compact
  end

  # Return the FHIR resource from the response body.
  #
  # @return [FHIR::Model]
  def resource
    FHIR.from_contents(response_body)
  end

  class << self
    # @private
    def from_rack_env(env, name: nil)
      rack_request = env['router.request'].rack_request
      url = "#{rack_request.base_url}#{rack_request.path}"
      url += "?#{rack_request.query_string}" if rack_request.query_string.present?
      request_headers =
        env
          .select { |key, _| key.start_with? 'HTTP_' }
          .transform_keys { |key| key.delete_prefix('HTTP_').tr('_', '-').downcase }
          .map { |header_name, value| Header.new(name: header_name, value: value, type: 'request') }

      new(
        verb: rack_request.request_method.downcase,
        url: url,
        direction: 'incoming',
        name: name,
        request_body: rack_request.body.string,
        headers: request_headers
      )
    end

    # @private
    def from_http_response(response, test_session_id:, direction: 'outgoing', name: nil)
      request_headers =
        response.env.request_headers
          .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'request') }
      response_headers =
        response.headers
          .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'response') }

      new(
        verb: response.env.method,
        url: response.env.url.to_s,
        direction: direction,
        name: name,
        status: response.status,
        request_body: response.env.request_body,
        response_body: response.body,
        test_session_id: test_session_id,
        headers: request_headers + response_headers
      )
    end

    # @private
    def from_fhir_client_reply(reply, test_session_id:, direction: 'outgoing', name: nil)
      request = reply.request
      response = reply.response
      request_headers = request[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'request') }
      response_headers = response[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'response') }
      request_body =
        if request.dig(:headers, 'Content-Type')&.include?('application/x-www-form-urlencoded')
          URI.encode_www_form(request[:payload])
        else
          request[:payload]
        end

      new(
        verb: request[:method],
        url: request[:url],
        direction: direction,
        name: name,
        status: response[:code].to_i,
        request_body: request_body,
        response_body: response[:body],
        test_session_id: test_session_id,
        headers: request_headers + response_headers
      )
    end
  end
end

#request_bodyString

Returns body of the http request.

Returns:

  • (String)

    body of the http request



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/inferno/entities/request.rb', line 33

class Request < Entity
  ATTRIBUTES = [
    :id, :index, :verb, :url, :direction, :name, :status,
    :request_body, :response_body, :result_id, :test_session_id, :created_at,
    :updated_at, :headers
  ].freeze
  SUMMARY_FIELDS = [
    :id, :index, :url, :verb, :direction, :name, :status, :result_id, :created_at, :updated_at
  ].freeze

  include Attributes

  def initialize(params)
    super(params, ATTRIBUTES - [:headers, :name])

    @name = params[:name]&.to_sym
    @headers = params[:headers]&.map { |header| header.is_a?(Hash) ? Header.new(header) : header } || []
  end

  # @return [Hash<String, String>]
  def query_parameters
    Addressable::URI.parse(url).query_values || {}
  end

  # Find a response header
  #
  # @param name [String] the header name
  # @return [Inferno::Entities::RequestHeader, nil]
  def response_header(name)
    response_headers.find { |header| header.name.casecmp(name).zero? }
  end

  # Find a request header
  #
  # @param name [String] the header name
  # @return [Inferno::Entities::RequestHeader, nil]
  def request_header(name)
    request_headers.find { |header| header.name.casecmp(name).zero? }
  end

  # All of the request headers
  #
  # @return [Array<Inferno::Entities::RequestHeader>]
  def request_headers
    headers.select(&:request?)
  end

  # All of the response headers
  #
  # @return [Array<Inferno::Entities::RequestHeader>]
  def response_headers
    headers.select(&:response?)
  end

  # Return a hash of the request parameters
  #
  # @return [Hash]
  def request
    {
      verb: verb,
      url: url,
      headers: request_headers,
      body: request_body
    }
  end

  # Return a hash of the response parameters
  #
  # @return [Hash]
  def response
    {
      status: status,
      headers: response_headers,
      body: response_body
    }
  end

  # @private
  def to_hash
    {
      id: id,
      verb: verb,
      url: url,
      direction: direction,
      status: status,
      name: name,
      request_body: request_body,
      response_body: response_body,
      result_id: result_id,
      test_session_id: test_session_id,
      request_headers: request_headers.map(&:to_hash),
      response_headers: response_headers.map(&:to_hash),
      created_at: created_at,
      updated_at: updated_at
    }.compact
  end

  # Return the FHIR resource from the response body.
  #
  # @return [FHIR::Model]
  def resource
    FHIR.from_contents(response_body)
  end

  class << self
    # @private
    def from_rack_env(env, name: nil)
      rack_request = env['router.request'].rack_request
      url = "#{rack_request.base_url}#{rack_request.path}"
      url += "?#{rack_request.query_string}" if rack_request.query_string.present?
      request_headers =
        env
          .select { |key, _| key.start_with? 'HTTP_' }
          .transform_keys { |key| key.delete_prefix('HTTP_').tr('_', '-').downcase }
          .map { |header_name, value| Header.new(name: header_name, value: value, type: 'request') }

      new(
        verb: rack_request.request_method.downcase,
        url: url,
        direction: 'incoming',
        name: name,
        request_body: rack_request.body.string,
        headers: request_headers
      )
    end

    # @private
    def from_http_response(response, test_session_id:, direction: 'outgoing', name: nil)
      request_headers =
        response.env.request_headers
          .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'request') }
      response_headers =
        response.headers
          .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'response') }

      new(
        verb: response.env.method,
        url: response.env.url.to_s,
        direction: direction,
        name: name,
        status: response.status,
        request_body: response.env.request_body,
        response_body: response.body,
        test_session_id: test_session_id,
        headers: request_headers + response_headers
      )
    end

    # @private
    def from_fhir_client_reply(reply, test_session_id:, direction: 'outgoing', name: nil)
      request = reply.request
      response = reply.response
      request_headers = request[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'request') }
      response_headers = response[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'response') }
      request_body =
        if request.dig(:headers, 'Content-Type')&.include?('application/x-www-form-urlencoded')
          URI.encode_www_form(request[:payload])
        else
          request[:payload]
        end

      new(
        verb: request[:method],
        url: request[:url],
        direction: direction,
        name: name,
        status: response[:code].to_i,
        request_body: request_body,
        response_body: response[:body],
        test_session_id: test_session_id,
        headers: request_headers + response_headers
      )
    end
  end
end

#response_bodyString

Returns body of the http response.

Returns:

  • (String)

    body of the http response



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/inferno/entities/request.rb', line 33

class Request < Entity
  ATTRIBUTES = [
    :id, :index, :verb, :url, :direction, :name, :status,
    :request_body, :response_body, :result_id, :test_session_id, :created_at,
    :updated_at, :headers
  ].freeze
  SUMMARY_FIELDS = [
    :id, :index, :url, :verb, :direction, :name, :status, :result_id, :created_at, :updated_at
  ].freeze

  include Attributes

  def initialize(params)
    super(params, ATTRIBUTES - [:headers, :name])

    @name = params[:name]&.to_sym
    @headers = params[:headers]&.map { |header| header.is_a?(Hash) ? Header.new(header) : header } || []
  end

  # @return [Hash<String, String>]
  def query_parameters
    Addressable::URI.parse(url).query_values || {}
  end

  # Find a response header
  #
  # @param name [String] the header name
  # @return [Inferno::Entities::RequestHeader, nil]
  def response_header(name)
    response_headers.find { |header| header.name.casecmp(name).zero? }
  end

  # Find a request header
  #
  # @param name [String] the header name
  # @return [Inferno::Entities::RequestHeader, nil]
  def request_header(name)
    request_headers.find { |header| header.name.casecmp(name).zero? }
  end

  # All of the request headers
  #
  # @return [Array<Inferno::Entities::RequestHeader>]
  def request_headers
    headers.select(&:request?)
  end

  # All of the response headers
  #
  # @return [Array<Inferno::Entities::RequestHeader>]
  def response_headers
    headers.select(&:response?)
  end

  # Return a hash of the request parameters
  #
  # @return [Hash]
  def request
    {
      verb: verb,
      url: url,
      headers: request_headers,
      body: request_body
    }
  end

  # Return a hash of the response parameters
  #
  # @return [Hash]
  def response
    {
      status: status,
      headers: response_headers,
      body: response_body
    }
  end

  # @private
  def to_hash
    {
      id: id,
      verb: verb,
      url: url,
      direction: direction,
      status: status,
      name: name,
      request_body: request_body,
      response_body: response_body,
      result_id: result_id,
      test_session_id: test_session_id,
      request_headers: request_headers.map(&:to_hash),
      response_headers: response_headers.map(&:to_hash),
      created_at: created_at,
      updated_at: updated_at
    }.compact
  end

  # Return the FHIR resource from the response body.
  #
  # @return [FHIR::Model]
  def resource
    FHIR.from_contents(response_body)
  end

  class << self
    # @private
    def from_rack_env(env, name: nil)
      rack_request = env['router.request'].rack_request
      url = "#{rack_request.base_url}#{rack_request.path}"
      url += "?#{rack_request.query_string}" if rack_request.query_string.present?
      request_headers =
        env
          .select { |key, _| key.start_with? 'HTTP_' }
          .transform_keys { |key| key.delete_prefix('HTTP_').tr('_', '-').downcase }
          .map { |header_name, value| Header.new(name: header_name, value: value, type: 'request') }

      new(
        verb: rack_request.request_method.downcase,
        url: url,
        direction: 'incoming',
        name: name,
        request_body: rack_request.body.string,
        headers: request_headers
      )
    end

    # @private
    def from_http_response(response, test_session_id:, direction: 'outgoing', name: nil)
      request_headers =
        response.env.request_headers
          .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'request') }
      response_headers =
        response.headers
          .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'response') }

      new(
        verb: response.env.method,
        url: response.env.url.to_s,
        direction: direction,
        name: name,
        status: response.status,
        request_body: response.env.request_body,
        response_body: response.body,
        test_session_id: test_session_id,
        headers: request_headers + response_headers
      )
    end

    # @private
    def from_fhir_client_reply(reply, test_session_id:, direction: 'outgoing', name: nil)
      request = reply.request
      response = reply.response
      request_headers = request[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'request') }
      response_headers = response[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'response') }
      request_body =
        if request.dig(:headers, 'Content-Type')&.include?('application/x-www-form-urlencoded')
          URI.encode_www_form(request[:payload])
        else
          request[:payload]
        end

      new(
        verb: request[:method],
        url: request[:url],
        direction: direction,
        name: name,
        status: response[:code].to_i,
        request_body: request_body,
        response_body: response[:body],
        test_session_id: test_session_id,
        headers: request_headers + response_headers
      )
    end
  end
end

#result_idString

Returns id of the result for this request.

Returns:

  • (String)

    id of the result for this request



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/inferno/entities/request.rb', line 33

class Request < Entity
  ATTRIBUTES = [
    :id, :index, :verb, :url, :direction, :name, :status,
    :request_body, :response_body, :result_id, :test_session_id, :created_at,
    :updated_at, :headers
  ].freeze
  SUMMARY_FIELDS = [
    :id, :index, :url, :verb, :direction, :name, :status, :result_id, :created_at, :updated_at
  ].freeze

  include Attributes

  def initialize(params)
    super(params, ATTRIBUTES - [:headers, :name])

    @name = params[:name]&.to_sym
    @headers = params[:headers]&.map { |header| header.is_a?(Hash) ? Header.new(header) : header } || []
  end

  # @return [Hash<String, String>]
  def query_parameters
    Addressable::URI.parse(url).query_values || {}
  end

  # Find a response header
  #
  # @param name [String] the header name
  # @return [Inferno::Entities::RequestHeader, nil]
  def response_header(name)
    response_headers.find { |header| header.name.casecmp(name).zero? }
  end

  # Find a request header
  #
  # @param name [String] the header name
  # @return [Inferno::Entities::RequestHeader, nil]
  def request_header(name)
    request_headers.find { |header| header.name.casecmp(name).zero? }
  end

  # All of the request headers
  #
  # @return [Array<Inferno::Entities::RequestHeader>]
  def request_headers
    headers.select(&:request?)
  end

  # All of the response headers
  #
  # @return [Array<Inferno::Entities::RequestHeader>]
  def response_headers
    headers.select(&:response?)
  end

  # Return a hash of the request parameters
  #
  # @return [Hash]
  def request
    {
      verb: verb,
      url: url,
      headers: request_headers,
      body: request_body
    }
  end

  # Return a hash of the response parameters
  #
  # @return [Hash]
  def response
    {
      status: status,
      headers: response_headers,
      body: response_body
    }
  end

  # @private
  def to_hash
    {
      id: id,
      verb: verb,
      url: url,
      direction: direction,
      status: status,
      name: name,
      request_body: request_body,
      response_body: response_body,
      result_id: result_id,
      test_session_id: test_session_id,
      request_headers: request_headers.map(&:to_hash),
      response_headers: response_headers.map(&:to_hash),
      created_at: created_at,
      updated_at: updated_at
    }.compact
  end

  # Return the FHIR resource from the response body.
  #
  # @return [FHIR::Model]
  def resource
    FHIR.from_contents(response_body)
  end

  class << self
    # @private
    def from_rack_env(env, name: nil)
      rack_request = env['router.request'].rack_request
      url = "#{rack_request.base_url}#{rack_request.path}"
      url += "?#{rack_request.query_string}" if rack_request.query_string.present?
      request_headers =
        env
          .select { |key, _| key.start_with? 'HTTP_' }
          .transform_keys { |key| key.delete_prefix('HTTP_').tr('_', '-').downcase }
          .map { |header_name, value| Header.new(name: header_name, value: value, type: 'request') }

      new(
        verb: rack_request.request_method.downcase,
        url: url,
        direction: 'incoming',
        name: name,
        request_body: rack_request.body.string,
        headers: request_headers
      )
    end

    # @private
    def from_http_response(response, test_session_id:, direction: 'outgoing', name: nil)
      request_headers =
        response.env.request_headers
          .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'request') }
      response_headers =
        response.headers
          .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'response') }

      new(
        verb: response.env.method,
        url: response.env.url.to_s,
        direction: direction,
        name: name,
        status: response.status,
        request_body: response.env.request_body,
        response_body: response.body,
        test_session_id: test_session_id,
        headers: request_headers + response_headers
      )
    end

    # @private
    def from_fhir_client_reply(reply, test_session_id:, direction: 'outgoing', name: nil)
      request = reply.request
      response = reply.response
      request_headers = request[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'request') }
      response_headers = response[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'response') }
      request_body =
        if request.dig(:headers, 'Content-Type')&.include?('application/x-www-form-urlencoded')
          URI.encode_www_form(request[:payload])
        else
          request[:payload]
        end

      new(
        verb: request[:method],
        url: request[:url],
        direction: direction,
        name: name,
        status: response[:code].to_i,
        request_body: request_body,
        response_body: response[:body],
        test_session_id: test_session_id,
        headers: request_headers + response_headers
      )
    end
  end
end

#statusString

Returns http response status code.

Returns:

  • (String)

    http response status code



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/inferno/entities/request.rb', line 33

class Request < Entity
  ATTRIBUTES = [
    :id, :index, :verb, :url, :direction, :name, :status,
    :request_body, :response_body, :result_id, :test_session_id, :created_at,
    :updated_at, :headers
  ].freeze
  SUMMARY_FIELDS = [
    :id, :index, :url, :verb, :direction, :name, :status, :result_id, :created_at, :updated_at
  ].freeze

  include Attributes

  def initialize(params)
    super(params, ATTRIBUTES - [:headers, :name])

    @name = params[:name]&.to_sym
    @headers = params[:headers]&.map { |header| header.is_a?(Hash) ? Header.new(header) : header } || []
  end

  # @return [Hash<String, String>]
  def query_parameters
    Addressable::URI.parse(url).query_values || {}
  end

  # Find a response header
  #
  # @param name [String] the header name
  # @return [Inferno::Entities::RequestHeader, nil]
  def response_header(name)
    response_headers.find { |header| header.name.casecmp(name).zero? }
  end

  # Find a request header
  #
  # @param name [String] the header name
  # @return [Inferno::Entities::RequestHeader, nil]
  def request_header(name)
    request_headers.find { |header| header.name.casecmp(name).zero? }
  end

  # All of the request headers
  #
  # @return [Array<Inferno::Entities::RequestHeader>]
  def request_headers
    headers.select(&:request?)
  end

  # All of the response headers
  #
  # @return [Array<Inferno::Entities::RequestHeader>]
  def response_headers
    headers.select(&:response?)
  end

  # Return a hash of the request parameters
  #
  # @return [Hash]
  def request
    {
      verb: verb,
      url: url,
      headers: request_headers,
      body: request_body
    }
  end

  # Return a hash of the response parameters
  #
  # @return [Hash]
  def response
    {
      status: status,
      headers: response_headers,
      body: response_body
    }
  end

  # @private
  def to_hash
    {
      id: id,
      verb: verb,
      url: url,
      direction: direction,
      status: status,
      name: name,
      request_body: request_body,
      response_body: response_body,
      result_id: result_id,
      test_session_id: test_session_id,
      request_headers: request_headers.map(&:to_hash),
      response_headers: response_headers.map(&:to_hash),
      created_at: created_at,
      updated_at: updated_at
    }.compact
  end

  # Return the FHIR resource from the response body.
  #
  # @return [FHIR::Model]
  def resource
    FHIR.from_contents(response_body)
  end

  class << self
    # @private
    def from_rack_env(env, name: nil)
      rack_request = env['router.request'].rack_request
      url = "#{rack_request.base_url}#{rack_request.path}"
      url += "?#{rack_request.query_string}" if rack_request.query_string.present?
      request_headers =
        env
          .select { |key, _| key.start_with? 'HTTP_' }
          .transform_keys { |key| key.delete_prefix('HTTP_').tr('_', '-').downcase }
          .map { |header_name, value| Header.new(name: header_name, value: value, type: 'request') }

      new(
        verb: rack_request.request_method.downcase,
        url: url,
        direction: 'incoming',
        name: name,
        request_body: rack_request.body.string,
        headers: request_headers
      )
    end

    # @private
    def from_http_response(response, test_session_id:, direction: 'outgoing', name: nil)
      request_headers =
        response.env.request_headers
          .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'request') }
      response_headers =
        response.headers
          .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'response') }

      new(
        verb: response.env.method,
        url: response.env.url.to_s,
        direction: direction,
        name: name,
        status: response.status,
        request_body: response.env.request_body,
        response_body: response.body,
        test_session_id: test_session_id,
        headers: request_headers + response_headers
      )
    end

    # @private
    def from_fhir_client_reply(reply, test_session_id:, direction: 'outgoing', name: nil)
      request = reply.request
      response = reply.response
      request_headers = request[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'request') }
      response_headers = response[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'response') }
      request_body =
        if request.dig(:headers, 'Content-Type')&.include?('application/x-www-form-urlencoded')
          URI.encode_www_form(request[:payload])
        else
          request[:payload]
        end

      new(
        verb: request[:method],
        url: request[:url],
        direction: direction,
        name: name,
        status: response[:code].to_i,
        request_body: request_body,
        response_body: response[:body],
        test_session_id: test_session_id,
        headers: request_headers + response_headers
      )
    end
  end
end

#test_session_idString

Returns id of the test session for this request.

Returns:

  • (String)

    id of the test session for this request



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/inferno/entities/request.rb', line 33

class Request < Entity
  ATTRIBUTES = [
    :id, :index, :verb, :url, :direction, :name, :status,
    :request_body, :response_body, :result_id, :test_session_id, :created_at,
    :updated_at, :headers
  ].freeze
  SUMMARY_FIELDS = [
    :id, :index, :url, :verb, :direction, :name, :status, :result_id, :created_at, :updated_at
  ].freeze

  include Attributes

  def initialize(params)
    super(params, ATTRIBUTES - [:headers, :name])

    @name = params[:name]&.to_sym
    @headers = params[:headers]&.map { |header| header.is_a?(Hash) ? Header.new(header) : header } || []
  end

  # @return [Hash<String, String>]
  def query_parameters
    Addressable::URI.parse(url).query_values || {}
  end

  # Find a response header
  #
  # @param name [String] the header name
  # @return [Inferno::Entities::RequestHeader, nil]
  def response_header(name)
    response_headers.find { |header| header.name.casecmp(name).zero? }
  end

  # Find a request header
  #
  # @param name [String] the header name
  # @return [Inferno::Entities::RequestHeader, nil]
  def request_header(name)
    request_headers.find { |header| header.name.casecmp(name).zero? }
  end

  # All of the request headers
  #
  # @return [Array<Inferno::Entities::RequestHeader>]
  def request_headers
    headers.select(&:request?)
  end

  # All of the response headers
  #
  # @return [Array<Inferno::Entities::RequestHeader>]
  def response_headers
    headers.select(&:response?)
  end

  # Return a hash of the request parameters
  #
  # @return [Hash]
  def request
    {
      verb: verb,
      url: url,
      headers: request_headers,
      body: request_body
    }
  end

  # Return a hash of the response parameters
  #
  # @return [Hash]
  def response
    {
      status: status,
      headers: response_headers,
      body: response_body
    }
  end

  # @private
  def to_hash
    {
      id: id,
      verb: verb,
      url: url,
      direction: direction,
      status: status,
      name: name,
      request_body: request_body,
      response_body: response_body,
      result_id: result_id,
      test_session_id: test_session_id,
      request_headers: request_headers.map(&:to_hash),
      response_headers: response_headers.map(&:to_hash),
      created_at: created_at,
      updated_at: updated_at
    }.compact
  end

  # Return the FHIR resource from the response body.
  #
  # @return [FHIR::Model]
  def resource
    FHIR.from_contents(response_body)
  end

  class << self
    # @private
    def from_rack_env(env, name: nil)
      rack_request = env['router.request'].rack_request
      url = "#{rack_request.base_url}#{rack_request.path}"
      url += "?#{rack_request.query_string}" if rack_request.query_string.present?
      request_headers =
        env
          .select { |key, _| key.start_with? 'HTTP_' }
          .transform_keys { |key| key.delete_prefix('HTTP_').tr('_', '-').downcase }
          .map { |header_name, value| Header.new(name: header_name, value: value, type: 'request') }

      new(
        verb: rack_request.request_method.downcase,
        url: url,
        direction: 'incoming',
        name: name,
        request_body: rack_request.body.string,
        headers: request_headers
      )
    end

    # @private
    def from_http_response(response, test_session_id:, direction: 'outgoing', name: nil)
      request_headers =
        response.env.request_headers
          .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'request') }
      response_headers =
        response.headers
          .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'response') }

      new(
        verb: response.env.method,
        url: response.env.url.to_s,
        direction: direction,
        name: name,
        status: response.status,
        request_body: response.env.request_body,
        response_body: response.body,
        test_session_id: test_session_id,
        headers: request_headers + response_headers
      )
    end

    # @private
    def from_fhir_client_reply(reply, test_session_id:, direction: 'outgoing', name: nil)
      request = reply.request
      response = reply.response
      request_headers = request[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'request') }
      response_headers = response[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'response') }
      request_body =
        if request.dig(:headers, 'Content-Type')&.include?('application/x-www-form-urlencoded')
          URI.encode_www_form(request[:payload])
        else
          request[:payload]
        end

      new(
        verb: request[:method],
        url: request[:url],
        direction: direction,
        name: name,
        status: response[:code].to_i,
        request_body: request_body,
        response_body: response[:body],
        test_session_id: test_session_id,
        headers: request_headers + response_headers
      )
    end
  end
end

#updated_atTime

Returns update timestamp.

Returns:

  • (Time)

    update timestamp



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/inferno/entities/request.rb', line 33

class Request < Entity
  ATTRIBUTES = [
    :id, :index, :verb, :url, :direction, :name, :status,
    :request_body, :response_body, :result_id, :test_session_id, :created_at,
    :updated_at, :headers
  ].freeze
  SUMMARY_FIELDS = [
    :id, :index, :url, :verb, :direction, :name, :status, :result_id, :created_at, :updated_at
  ].freeze

  include Attributes

  def initialize(params)
    super(params, ATTRIBUTES - [:headers, :name])

    @name = params[:name]&.to_sym
    @headers = params[:headers]&.map { |header| header.is_a?(Hash) ? Header.new(header) : header } || []
  end

  # @return [Hash<String, String>]
  def query_parameters
    Addressable::URI.parse(url).query_values || {}
  end

  # Find a response header
  #
  # @param name [String] the header name
  # @return [Inferno::Entities::RequestHeader, nil]
  def response_header(name)
    response_headers.find { |header| header.name.casecmp(name).zero? }
  end

  # Find a request header
  #
  # @param name [String] the header name
  # @return [Inferno::Entities::RequestHeader, nil]
  def request_header(name)
    request_headers.find { |header| header.name.casecmp(name).zero? }
  end

  # All of the request headers
  #
  # @return [Array<Inferno::Entities::RequestHeader>]
  def request_headers
    headers.select(&:request?)
  end

  # All of the response headers
  #
  # @return [Array<Inferno::Entities::RequestHeader>]
  def response_headers
    headers.select(&:response?)
  end

  # Return a hash of the request parameters
  #
  # @return [Hash]
  def request
    {
      verb: verb,
      url: url,
      headers: request_headers,
      body: request_body
    }
  end

  # Return a hash of the response parameters
  #
  # @return [Hash]
  def response
    {
      status: status,
      headers: response_headers,
      body: response_body
    }
  end

  # @private
  def to_hash
    {
      id: id,
      verb: verb,
      url: url,
      direction: direction,
      status: status,
      name: name,
      request_body: request_body,
      response_body: response_body,
      result_id: result_id,
      test_session_id: test_session_id,
      request_headers: request_headers.map(&:to_hash),
      response_headers: response_headers.map(&:to_hash),
      created_at: created_at,
      updated_at: updated_at
    }.compact
  end

  # Return the FHIR resource from the response body.
  #
  # @return [FHIR::Model]
  def resource
    FHIR.from_contents(response_body)
  end

  class << self
    # @private
    def from_rack_env(env, name: nil)
      rack_request = env['router.request'].rack_request
      url = "#{rack_request.base_url}#{rack_request.path}"
      url += "?#{rack_request.query_string}" if rack_request.query_string.present?
      request_headers =
        env
          .select { |key, _| key.start_with? 'HTTP_' }
          .transform_keys { |key| key.delete_prefix('HTTP_').tr('_', '-').downcase }
          .map { |header_name, value| Header.new(name: header_name, value: value, type: 'request') }

      new(
        verb: rack_request.request_method.downcase,
        url: url,
        direction: 'incoming',
        name: name,
        request_body: rack_request.body.string,
        headers: request_headers
      )
    end

    # @private
    def from_http_response(response, test_session_id:, direction: 'outgoing', name: nil)
      request_headers =
        response.env.request_headers
          .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'request') }
      response_headers =
        response.headers
          .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'response') }

      new(
        verb: response.env.method,
        url: response.env.url.to_s,
        direction: direction,
        name: name,
        status: response.status,
        request_body: response.env.request_body,
        response_body: response.body,
        test_session_id: test_session_id,
        headers: request_headers + response_headers
      )
    end

    # @private
    def from_fhir_client_reply(reply, test_session_id:, direction: 'outgoing', name: nil)
      request = reply.request
      response = reply.response
      request_headers = request[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'request') }
      response_headers = response[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'response') }
      request_body =
        if request.dig(:headers, 'Content-Type')&.include?('application/x-www-form-urlencoded')
          URI.encode_www_form(request[:payload])
        else
          request[:payload]
        end

      new(
        verb: request[:method],
        url: request[:url],
        direction: direction,
        name: name,
        status: response[:code].to_i,
        request_body: request_body,
        response_body: response[:body],
        test_session_id: test_session_id,
        headers: request_headers + response_headers
      )
    end
  end
end

#urlString

Returns request url.

Returns:

  • (String)

    request url



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/inferno/entities/request.rb', line 33

class Request < Entity
  ATTRIBUTES = [
    :id, :index, :verb, :url, :direction, :name, :status,
    :request_body, :response_body, :result_id, :test_session_id, :created_at,
    :updated_at, :headers
  ].freeze
  SUMMARY_FIELDS = [
    :id, :index, :url, :verb, :direction, :name, :status, :result_id, :created_at, :updated_at
  ].freeze

  include Attributes

  def initialize(params)
    super(params, ATTRIBUTES - [:headers, :name])

    @name = params[:name]&.to_sym
    @headers = params[:headers]&.map { |header| header.is_a?(Hash) ? Header.new(header) : header } || []
  end

  # @return [Hash<String, String>]
  def query_parameters
    Addressable::URI.parse(url).query_values || {}
  end

  # Find a response header
  #
  # @param name [String] the header name
  # @return [Inferno::Entities::RequestHeader, nil]
  def response_header(name)
    response_headers.find { |header| header.name.casecmp(name).zero? }
  end

  # Find a request header
  #
  # @param name [String] the header name
  # @return [Inferno::Entities::RequestHeader, nil]
  def request_header(name)
    request_headers.find { |header| header.name.casecmp(name).zero? }
  end

  # All of the request headers
  #
  # @return [Array<Inferno::Entities::RequestHeader>]
  def request_headers
    headers.select(&:request?)
  end

  # All of the response headers
  #
  # @return [Array<Inferno::Entities::RequestHeader>]
  def response_headers
    headers.select(&:response?)
  end

  # Return a hash of the request parameters
  #
  # @return [Hash]
  def request
    {
      verb: verb,
      url: url,
      headers: request_headers,
      body: request_body
    }
  end

  # Return a hash of the response parameters
  #
  # @return [Hash]
  def response
    {
      status: status,
      headers: response_headers,
      body: response_body
    }
  end

  # @private
  def to_hash
    {
      id: id,
      verb: verb,
      url: url,
      direction: direction,
      status: status,
      name: name,
      request_body: request_body,
      response_body: response_body,
      result_id: result_id,
      test_session_id: test_session_id,
      request_headers: request_headers.map(&:to_hash),
      response_headers: response_headers.map(&:to_hash),
      created_at: created_at,
      updated_at: updated_at
    }.compact
  end

  # Return the FHIR resource from the response body.
  #
  # @return [FHIR::Model]
  def resource
    FHIR.from_contents(response_body)
  end

  class << self
    # @private
    def from_rack_env(env, name: nil)
      rack_request = env['router.request'].rack_request
      url = "#{rack_request.base_url}#{rack_request.path}"
      url += "?#{rack_request.query_string}" if rack_request.query_string.present?
      request_headers =
        env
          .select { |key, _| key.start_with? 'HTTP_' }
          .transform_keys { |key| key.delete_prefix('HTTP_').tr('_', '-').downcase }
          .map { |header_name, value| Header.new(name: header_name, value: value, type: 'request') }

      new(
        verb: rack_request.request_method.downcase,
        url: url,
        direction: 'incoming',
        name: name,
        request_body: rack_request.body.string,
        headers: request_headers
      )
    end

    # @private
    def from_http_response(response, test_session_id:, direction: 'outgoing', name: nil)
      request_headers =
        response.env.request_headers
          .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'request') }
      response_headers =
        response.headers
          .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'response') }

      new(
        verb: response.env.method,
        url: response.env.url.to_s,
        direction: direction,
        name: name,
        status: response.status,
        request_body: response.env.request_body,
        response_body: response.body,
        test_session_id: test_session_id,
        headers: request_headers + response_headers
      )
    end

    # @private
    def from_fhir_client_reply(reply, test_session_id:, direction: 'outgoing', name: nil)
      request = reply.request
      response = reply.response
      request_headers = request[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'request') }
      response_headers = response[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'response') }
      request_body =
        if request.dig(:headers, 'Content-Type')&.include?('application/x-www-form-urlencoded')
          URI.encode_www_form(request[:payload])
        else
          request[:payload]
        end

      new(
        verb: request[:method],
        url: request[:url],
        direction: direction,
        name: name,
        status: response[:code].to_i,
        request_body: request_body,
        response_body: response[:body],
        test_session_id: test_session_id,
        headers: request_headers + response_headers
      )
    end
  end
end

#verbString

Returns http verb.

Returns:

  • (String)

    http verb



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/inferno/entities/request.rb', line 33

class Request < Entity
  ATTRIBUTES = [
    :id, :index, :verb, :url, :direction, :name, :status,
    :request_body, :response_body, :result_id, :test_session_id, :created_at,
    :updated_at, :headers
  ].freeze
  SUMMARY_FIELDS = [
    :id, :index, :url, :verb, :direction, :name, :status, :result_id, :created_at, :updated_at
  ].freeze

  include Attributes

  def initialize(params)
    super(params, ATTRIBUTES - [:headers, :name])

    @name = params[:name]&.to_sym
    @headers = params[:headers]&.map { |header| header.is_a?(Hash) ? Header.new(header) : header } || []
  end

  # @return [Hash<String, String>]
  def query_parameters
    Addressable::URI.parse(url).query_values || {}
  end

  # Find a response header
  #
  # @param name [String] the header name
  # @return [Inferno::Entities::RequestHeader, nil]
  def response_header(name)
    response_headers.find { |header| header.name.casecmp(name).zero? }
  end

  # Find a request header
  #
  # @param name [String] the header name
  # @return [Inferno::Entities::RequestHeader, nil]
  def request_header(name)
    request_headers.find { |header| header.name.casecmp(name).zero? }
  end

  # All of the request headers
  #
  # @return [Array<Inferno::Entities::RequestHeader>]
  def request_headers
    headers.select(&:request?)
  end

  # All of the response headers
  #
  # @return [Array<Inferno::Entities::RequestHeader>]
  def response_headers
    headers.select(&:response?)
  end

  # Return a hash of the request parameters
  #
  # @return [Hash]
  def request
    {
      verb: verb,
      url: url,
      headers: request_headers,
      body: request_body
    }
  end

  # Return a hash of the response parameters
  #
  # @return [Hash]
  def response
    {
      status: status,
      headers: response_headers,
      body: response_body
    }
  end

  # @private
  def to_hash
    {
      id: id,
      verb: verb,
      url: url,
      direction: direction,
      status: status,
      name: name,
      request_body: request_body,
      response_body: response_body,
      result_id: result_id,
      test_session_id: test_session_id,
      request_headers: request_headers.map(&:to_hash),
      response_headers: response_headers.map(&:to_hash),
      created_at: created_at,
      updated_at: updated_at
    }.compact
  end

  # Return the FHIR resource from the response body.
  #
  # @return [FHIR::Model]
  def resource
    FHIR.from_contents(response_body)
  end

  class << self
    # @private
    def from_rack_env(env, name: nil)
      rack_request = env['router.request'].rack_request
      url = "#{rack_request.base_url}#{rack_request.path}"
      url += "?#{rack_request.query_string}" if rack_request.query_string.present?
      request_headers =
        env
          .select { |key, _| key.start_with? 'HTTP_' }
          .transform_keys { |key| key.delete_prefix('HTTP_').tr('_', '-').downcase }
          .map { |header_name, value| Header.new(name: header_name, value: value, type: 'request') }

      new(
        verb: rack_request.request_method.downcase,
        url: url,
        direction: 'incoming',
        name: name,
        request_body: rack_request.body.string,
        headers: request_headers
      )
    end

    # @private
    def from_http_response(response, test_session_id:, direction: 'outgoing', name: nil)
      request_headers =
        response.env.request_headers
          .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'request') }
      response_headers =
        response.headers
          .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'response') }

      new(
        verb: response.env.method,
        url: response.env.url.to_s,
        direction: direction,
        name: name,
        status: response.status,
        request_body: response.env.request_body,
        response_body: response.body,
        test_session_id: test_session_id,
        headers: request_headers + response_headers
      )
    end

    # @private
    def from_fhir_client_reply(reply, test_session_id:, direction: 'outgoing', name: nil)
      request = reply.request
      response = reply.response
      request_headers = request[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'request') }
      response_headers = response[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value: value, type: 'response') }
      request_body =
        if request.dig(:headers, 'Content-Type')&.include?('application/x-www-form-urlencoded')
          URI.encode_www_form(request[:payload])
        else
          request[:payload]
        end

      new(
        verb: request[:method],
        url: request[:url],
        direction: direction,
        name: name,
        status: response[:code].to_i,
        request_body: request_body,
        response_body: response[:body],
        test_session_id: test_session_id,
        headers: request_headers + response_headers
      )
    end
  end
end

Instance Method Details

#query_parametersHash<String, String>

Returns:

  • (Hash<String, String>)


53
54
55
# File 'lib/inferno/entities/request.rb', line 53

def query_parameters
  Addressable::URI.parse(url).query_values || {}
end

#requestHash

Return a hash of the request parameters

Returns:

  • (Hash)


90
91
92
93
94
95
96
97
# File 'lib/inferno/entities/request.rb', line 90

def request
  {
    verb: verb,
    url: url,
    headers: request_headers,
    body: request_body
  }
end

#request_header(name) ⇒ Inferno::Entities::RequestHeader?

Find a request header

Parameters:

  • name (String)

    the header name

Returns:

  • (Inferno::Entities::RequestHeader, nil)


69
70
71
# File 'lib/inferno/entities/request.rb', line 69

def request_header(name)
  request_headers.find { |header| header.name.casecmp(name).zero? }
end

#request_headersArray<Inferno::Entities::RequestHeader>

All of the request headers

Returns:

  • (Array<Inferno::Entities::RequestHeader>)


76
77
78
# File 'lib/inferno/entities/request.rb', line 76

def request_headers
  headers.select(&:request?)
end

#resourceFHIR::Model

Return the FHIR resource from the response body.

Returns:



133
134
135
# File 'lib/inferno/entities/request.rb', line 133

def resource
  FHIR.from_contents(response_body)
end

#responseHash

Return a hash of the response parameters

Returns:

  • (Hash)


102
103
104
105
106
107
108
# File 'lib/inferno/entities/request.rb', line 102

def response
  {
    status: status,
    headers: response_headers,
    body: response_body
  }
end

#response_header(name) ⇒ Inferno::Entities::RequestHeader?

Find a response header

Parameters:

  • name (String)

    the header name

Returns:

  • (Inferno::Entities::RequestHeader, nil)


61
62
63
# File 'lib/inferno/entities/request.rb', line 61

def response_header(name)
  response_headers.find { |header| header.name.casecmp(name).zero? }
end

#response_headersArray<Inferno::Entities::RequestHeader>

All of the response headers

Returns:

  • (Array<Inferno::Entities::RequestHeader>)


83
84
85
# File 'lib/inferno/entities/request.rb', line 83

def response_headers
  headers.select(&:response?)
end