Class: Inferno::Entities::Request

Inherits:
Entity
  • Object
show all
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, :tags
].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

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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# 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, :tags
  ].freeze
  SUMMARY_FIELDS = [
    :id, :index, :url, :verb, :direction, :name, :status, :result_id, :created_at, :updated_at
  ].freeze

  include Attributes

  # @private
  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 } || []
    format_tags(params[:tags] || [])
  end

  def format_tags(raw_tags)
    @tags = raw_tags.map do |tag|
      case tag
      when Hash
        tag[:name]
      when String
        tag
      end
    end
  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::Header, 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::Header, nil]
  def request_header(name)
    request_headers.find { |header| header.name.casecmp(name).zero? }
  end

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

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

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

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

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

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

  class << self
    # @private
    def from_hanami_request(request, name: nil, tags: [])
      url = "#{request.base_url}#{request.path}"
      url += "?#{request.query_string}" if request.query_string.present?
      request_headers =
        request.params.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:, type: 'request') }

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

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

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

    # @private
    def from_fhir_client_reply(reply, test_session_id:, direction: 'outgoing', name: nil, tags: [])
      request = reply.request
      response = reply.response
      request_headers = request[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value:, type: 'request') }
      response_headers = response[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, 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:,
        name:,
        status: response[:code].to_i,
        request_body:,
        response_body: response[:body],
        test_session_id:,
        headers: request_headers + response_headers,
        tags:
      )
    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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# 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, :tags
  ].freeze
  SUMMARY_FIELDS = [
    :id, :index, :url, :verb, :direction, :name, :status, :result_id, :created_at, :updated_at
  ].freeze

  include Attributes

  # @private
  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 } || []
    format_tags(params[:tags] || [])
  end

  def format_tags(raw_tags)
    @tags = raw_tags.map do |tag|
      case tag
      when Hash
        tag[:name]
      when String
        tag
      end
    end
  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::Header, 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::Header, nil]
  def request_header(name)
    request_headers.find { |header| header.name.casecmp(name).zero? }
  end

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

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

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

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

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

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

  class << self
    # @private
    def from_hanami_request(request, name: nil, tags: [])
      url = "#{request.base_url}#{request.path}"
      url += "?#{request.query_string}" if request.query_string.present?
      request_headers =
        request.params.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:, type: 'request') }

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

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

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

    # @private
    def from_fhir_client_reply(reply, test_session_id:, direction: 'outgoing', name: nil, tags: [])
      request = reply.request
      response = reply.response
      request_headers = request[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value:, type: 'request') }
      response_headers = response[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, 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:,
        name:,
        status: response[:code].to_i,
        request_body:,
        response_body: response[:body],
        test_session_id:,
        headers: request_headers + response_headers,
        tags:
      )
    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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# 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, :tags
  ].freeze
  SUMMARY_FIELDS = [
    :id, :index, :url, :verb, :direction, :name, :status, :result_id, :created_at, :updated_at
  ].freeze

  include Attributes

  # @private
  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 } || []
    format_tags(params[:tags] || [])
  end

  def format_tags(raw_tags)
    @tags = raw_tags.map do |tag|
      case tag
      when Hash
        tag[:name]
      when String
        tag
      end
    end
  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::Header, 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::Header, nil]
  def request_header(name)
    request_headers.find { |header| header.name.casecmp(name).zero? }
  end

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

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

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

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

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

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

  class << self
    # @private
    def from_hanami_request(request, name: nil, tags: [])
      url = "#{request.base_url}#{request.path}"
      url += "?#{request.query_string}" if request.query_string.present?
      request_headers =
        request.params.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:, type: 'request') }

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

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

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

    # @private
    def from_fhir_client_reply(reply, test_session_id:, direction: 'outgoing', name: nil, tags: [])
      request = reply.request
      response = reply.response
      request_headers = request[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value:, type: 'request') }
      response_headers = response[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, 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:,
        name:,
        status: response[:code].to_i,
        request_body:,
        response_body: response[:body],
        test_session_id:,
        headers: request_headers + response_headers,
        tags:
      )
    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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# 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, :tags
  ].freeze
  SUMMARY_FIELDS = [
    :id, :index, :url, :verb, :direction, :name, :status, :result_id, :created_at, :updated_at
  ].freeze

  include Attributes

  # @private
  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 } || []
    format_tags(params[:tags] || [])
  end

  def format_tags(raw_tags)
    @tags = raw_tags.map do |tag|
      case tag
      when Hash
        tag[:name]
      when String
        tag
      end
    end
  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::Header, 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::Header, nil]
  def request_header(name)
    request_headers.find { |header| header.name.casecmp(name).zero? }
  end

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

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

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

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

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

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

  class << self
    # @private
    def from_hanami_request(request, name: nil, tags: [])
      url = "#{request.base_url}#{request.path}"
      url += "?#{request.query_string}" if request.query_string.present?
      request_headers =
        request.params.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:, type: 'request') }

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

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

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

    # @private
    def from_fhir_client_reply(reply, test_session_id:, direction: 'outgoing', name: nil, tags: [])
      request = reply.request
      response = reply.response
      request_headers = request[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value:, type: 'request') }
      response_headers = response[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, 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:,
        name:,
        status: response[:code].to_i,
        request_body:,
        response_body: response[:body],
        test_session_id:,
        headers: request_headers + response_headers,
        tags:
      )
    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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# 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, :tags
  ].freeze
  SUMMARY_FIELDS = [
    :id, :index, :url, :verb, :direction, :name, :status, :result_id, :created_at, :updated_at
  ].freeze

  include Attributes

  # @private
  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 } || []
    format_tags(params[:tags] || [])
  end

  def format_tags(raw_tags)
    @tags = raw_tags.map do |tag|
      case tag
      when Hash
        tag[:name]
      when String
        tag
      end
    end
  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::Header, 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::Header, nil]
  def request_header(name)
    request_headers.find { |header| header.name.casecmp(name).zero? }
  end

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

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

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

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

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

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

  class << self
    # @private
    def from_hanami_request(request, name: nil, tags: [])
      url = "#{request.base_url}#{request.path}"
      url += "?#{request.query_string}" if request.query_string.present?
      request_headers =
        request.params.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:, type: 'request') }

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

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

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

    # @private
    def from_fhir_client_reply(reply, test_session_id:, direction: 'outgoing', name: nil, tags: [])
      request = reply.request
      response = reply.response
      request_headers = request[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value:, type: 'request') }
      response_headers = response[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, 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:,
        name:,
        status: response[:code].to_i,
        request_body:,
        response_body: response[:body],
        test_session_id:,
        headers: request_headers + response_headers,
        tags:
      )
    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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# 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, :tags
  ].freeze
  SUMMARY_FIELDS = [
    :id, :index, :url, :verb, :direction, :name, :status, :result_id, :created_at, :updated_at
  ].freeze

  include Attributes

  # @private
  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 } || []
    format_tags(params[:tags] || [])
  end

  def format_tags(raw_tags)
    @tags = raw_tags.map do |tag|
      case tag
      when Hash
        tag[:name]
      when String
        tag
      end
    end
  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::Header, 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::Header, nil]
  def request_header(name)
    request_headers.find { |header| header.name.casecmp(name).zero? }
  end

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

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

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

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

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

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

  class << self
    # @private
    def from_hanami_request(request, name: nil, tags: [])
      url = "#{request.base_url}#{request.path}"
      url += "?#{request.query_string}" if request.query_string.present?
      request_headers =
        request.params.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:, type: 'request') }

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

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

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

    # @private
    def from_fhir_client_reply(reply, test_session_id:, direction: 'outgoing', name: nil, tags: [])
      request = reply.request
      response = reply.response
      request_headers = request[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value:, type: 'request') }
      response_headers = response[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, 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:,
        name:,
        status: response[:code].to_i,
        request_body:,
        response_body: response[:body],
        test_session_id:,
        headers: request_headers + response_headers,
        tags:
      )
    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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# 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, :tags
  ].freeze
  SUMMARY_FIELDS = [
    :id, :index, :url, :verb, :direction, :name, :status, :result_id, :created_at, :updated_at
  ].freeze

  include Attributes

  # @private
  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 } || []
    format_tags(params[:tags] || [])
  end

  def format_tags(raw_tags)
    @tags = raw_tags.map do |tag|
      case tag
      when Hash
        tag[:name]
      when String
        tag
      end
    end
  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::Header, 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::Header, nil]
  def request_header(name)
    request_headers.find { |header| header.name.casecmp(name).zero? }
  end

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

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

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

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

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

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

  class << self
    # @private
    def from_hanami_request(request, name: nil, tags: [])
      url = "#{request.base_url}#{request.path}"
      url += "?#{request.query_string}" if request.query_string.present?
      request_headers =
        request.params.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:, type: 'request') }

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

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

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

    # @private
    def from_fhir_client_reply(reply, test_session_id:, direction: 'outgoing', name: nil, tags: [])
      request = reply.request
      response = reply.response
      request_headers = request[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value:, type: 'request') }
      response_headers = response[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, 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:,
        name:,
        status: response[:code].to_i,
        request_body:,
        response_body: response[:body],
        test_session_id:,
        headers: request_headers + response_headers,
        tags:
      )
    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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# 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, :tags
  ].freeze
  SUMMARY_FIELDS = [
    :id, :index, :url, :verb, :direction, :name, :status, :result_id, :created_at, :updated_at
  ].freeze

  include Attributes

  # @private
  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 } || []
    format_tags(params[:tags] || [])
  end

  def format_tags(raw_tags)
    @tags = raw_tags.map do |tag|
      case tag
      when Hash
        tag[:name]
      when String
        tag
      end
    end
  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::Header, 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::Header, nil]
  def request_header(name)
    request_headers.find { |header| header.name.casecmp(name).zero? }
  end

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

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

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

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

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

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

  class << self
    # @private
    def from_hanami_request(request, name: nil, tags: [])
      url = "#{request.base_url}#{request.path}"
      url += "?#{request.query_string}" if request.query_string.present?
      request_headers =
        request.params.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:, type: 'request') }

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

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

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

    # @private
    def from_fhir_client_reply(reply, test_session_id:, direction: 'outgoing', name: nil, tags: [])
      request = reply.request
      response = reply.response
      request_headers = request[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value:, type: 'request') }
      response_headers = response[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, 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:,
        name:,
        status: response[:code].to_i,
        request_body:,
        response_body: response[:body],
        test_session_id:,
        headers: request_headers + response_headers,
        tags:
      )
    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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# 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, :tags
  ].freeze
  SUMMARY_FIELDS = [
    :id, :index, :url, :verb, :direction, :name, :status, :result_id, :created_at, :updated_at
  ].freeze

  include Attributes

  # @private
  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 } || []
    format_tags(params[:tags] || [])
  end

  def format_tags(raw_tags)
    @tags = raw_tags.map do |tag|
      case tag
      when Hash
        tag[:name]
      when String
        tag
      end
    end
  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::Header, 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::Header, nil]
  def request_header(name)
    request_headers.find { |header| header.name.casecmp(name).zero? }
  end

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

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

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

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

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

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

  class << self
    # @private
    def from_hanami_request(request, name: nil, tags: [])
      url = "#{request.base_url}#{request.path}"
      url += "?#{request.query_string}" if request.query_string.present?
      request_headers =
        request.params.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:, type: 'request') }

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

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

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

    # @private
    def from_fhir_client_reply(reply, test_session_id:, direction: 'outgoing', name: nil, tags: [])
      request = reply.request
      response = reply.response
      request_headers = request[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value:, type: 'request') }
      response_headers = response[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, 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:,
        name:,
        status: response[:code].to_i,
        request_body:,
        response_body: response[:body],
        test_session_id:,
        headers: request_headers + response_headers,
        tags:
      )
    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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# 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, :tags
  ].freeze
  SUMMARY_FIELDS = [
    :id, :index, :url, :verb, :direction, :name, :status, :result_id, :created_at, :updated_at
  ].freeze

  include Attributes

  # @private
  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 } || []
    format_tags(params[:tags] || [])
  end

  def format_tags(raw_tags)
    @tags = raw_tags.map do |tag|
      case tag
      when Hash
        tag[:name]
      when String
        tag
      end
    end
  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::Header, 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::Header, nil]
  def request_header(name)
    request_headers.find { |header| header.name.casecmp(name).zero? }
  end

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

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

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

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

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

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

  class << self
    # @private
    def from_hanami_request(request, name: nil, tags: [])
      url = "#{request.base_url}#{request.path}"
      url += "?#{request.query_string}" if request.query_string.present?
      request_headers =
        request.params.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:, type: 'request') }

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

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

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

    # @private
    def from_fhir_client_reply(reply, test_session_id:, direction: 'outgoing', name: nil, tags: [])
      request = reply.request
      response = reply.response
      request_headers = request[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value:, type: 'request') }
      response_headers = response[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, 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:,
        name:,
        status: response[:code].to_i,
        request_body:,
        response_body: response[:body],
        test_session_id:,
        headers: request_headers + response_headers,
        tags:
      )
    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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# 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, :tags
  ].freeze
  SUMMARY_FIELDS = [
    :id, :index, :url, :verb, :direction, :name, :status, :result_id, :created_at, :updated_at
  ].freeze

  include Attributes

  # @private
  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 } || []
    format_tags(params[:tags] || [])
  end

  def format_tags(raw_tags)
    @tags = raw_tags.map do |tag|
      case tag
      when Hash
        tag[:name]
      when String
        tag
      end
    end
  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::Header, 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::Header, nil]
  def request_header(name)
    request_headers.find { |header| header.name.casecmp(name).zero? }
  end

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

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

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

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

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

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

  class << self
    # @private
    def from_hanami_request(request, name: nil, tags: [])
      url = "#{request.base_url}#{request.path}"
      url += "?#{request.query_string}" if request.query_string.present?
      request_headers =
        request.params.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:, type: 'request') }

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

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

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

    # @private
    def from_fhir_client_reply(reply, test_session_id:, direction: 'outgoing', name: nil, tags: [])
      request = reply.request
      response = reply.response
      request_headers = request[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value:, type: 'request') }
      response_headers = response[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, 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:,
        name:,
        status: response[:code].to_i,
        request_body:,
        response_body: response[:body],
        test_session_id:,
        headers: request_headers + response_headers,
        tags:
      )
    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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# 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, :tags
  ].freeze
  SUMMARY_FIELDS = [
    :id, :index, :url, :verb, :direction, :name, :status, :result_id, :created_at, :updated_at
  ].freeze

  include Attributes

  # @private
  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 } || []
    format_tags(params[:tags] || [])
  end

  def format_tags(raw_tags)
    @tags = raw_tags.map do |tag|
      case tag
      when Hash
        tag[:name]
      when String
        tag
      end
    end
  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::Header, 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::Header, nil]
  def request_header(name)
    request_headers.find { |header| header.name.casecmp(name).zero? }
  end

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

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

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

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

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

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

  class << self
    # @private
    def from_hanami_request(request, name: nil, tags: [])
      url = "#{request.base_url}#{request.path}"
      url += "?#{request.query_string}" if request.query_string.present?
      request_headers =
        request.params.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:, type: 'request') }

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

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

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

    # @private
    def from_fhir_client_reply(reply, test_session_id:, direction: 'outgoing', name: nil, tags: [])
      request = reply.request
      response = reply.response
      request_headers = request[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value:, type: 'request') }
      response_headers = response[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, 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:,
        name:,
        status: response[:code].to_i,
        request_body:,
        response_body: response[:body],
        test_session_id:,
        headers: request_headers + response_headers,
        tags:
      )
    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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# 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, :tags
  ].freeze
  SUMMARY_FIELDS = [
    :id, :index, :url, :verb, :direction, :name, :status, :result_id, :created_at, :updated_at
  ].freeze

  include Attributes

  # @private
  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 } || []
    format_tags(params[:tags] || [])
  end

  def format_tags(raw_tags)
    @tags = raw_tags.map do |tag|
      case tag
      when Hash
        tag[:name]
      when String
        tag
      end
    end
  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::Header, 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::Header, nil]
  def request_header(name)
    request_headers.find { |header| header.name.casecmp(name).zero? }
  end

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

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

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

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

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

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

  class << self
    # @private
    def from_hanami_request(request, name: nil, tags: [])
      url = "#{request.base_url}#{request.path}"
      url += "?#{request.query_string}" if request.query_string.present?
      request_headers =
        request.params.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:, type: 'request') }

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

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

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

    # @private
    def from_fhir_client_reply(reply, test_session_id:, direction: 'outgoing', name: nil, tags: [])
      request = reply.request
      response = reply.response
      request_headers = request[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value:, type: 'request') }
      response_headers = response[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, 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:,
        name:,
        status: response[:code].to_i,
        request_body:,
        response_body: response[:body],
        test_session_id:,
        headers: request_headers + response_headers,
        tags:
      )
    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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# 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, :tags
  ].freeze
  SUMMARY_FIELDS = [
    :id, :index, :url, :verb, :direction, :name, :status, :result_id, :created_at, :updated_at
  ].freeze

  include Attributes

  # @private
  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 } || []
    format_tags(params[:tags] || [])
  end

  def format_tags(raw_tags)
    @tags = raw_tags.map do |tag|
      case tag
      when Hash
        tag[:name]
      when String
        tag
      end
    end
  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::Header, 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::Header, nil]
  def request_header(name)
    request_headers.find { |header| header.name.casecmp(name).zero? }
  end

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

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

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

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

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

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

  class << self
    # @private
    def from_hanami_request(request, name: nil, tags: [])
      url = "#{request.base_url}#{request.path}"
      url += "?#{request.query_string}" if request.query_string.present?
      request_headers =
        request.params.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:, type: 'request') }

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

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

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

    # @private
    def from_fhir_client_reply(reply, test_session_id:, direction: 'outgoing', name: nil, tags: [])
      request = reply.request
      response = reply.response
      request_headers = request[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, value:, type: 'request') }
      response_headers = response[:headers]
        .map { |header_name, value| Header.new(name: header_name.downcase, 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:,
        name:,
        status: response[:code].to_i,
        request_body:,
        response_body: response[:body],
        test_session_id:,
        headers: request_headers + response_headers,
        tags:
      )
    end
  end
end

Instance Method Details

#format_tags(raw_tags) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/inferno/entities/request.rb', line 54

def format_tags(raw_tags)
  @tags = raw_tags.map do |tag|
    case tag
    when Hash
      tag[:name]
    when String
      tag
    end
  end
end

#query_parametersHash<String, String>

Returns:

  • (Hash<String, String>)


66
67
68
# File 'lib/inferno/entities/request.rb', line 66

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

#requestHash

Return a hash of the request parameters

Returns:

  • (Hash)

    A Hash with :verb, :url, :headers, and :body fields



104
105
106
107
108
109
110
111
# File 'lib/inferno/entities/request.rb', line 104

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

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

Find a request header

Parameters:

  • name (String)

    the header name.

Returns:



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

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

#request_headersArray<Inferno::Entities::Header>

All of the request headers

Returns:



89
90
91
# File 'lib/inferno/entities/request.rb', line 89

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

#resourceFHIR::Model?

Return the FHIR resource from the response body.

Returns:



148
149
150
# File 'lib/inferno/entities/request.rb', line 148

def resource
  FHIR.from_contents(response_body)
end

#responseHash

Return a hash of the response parameters

Returns:

  • (Hash)

    A Hash with :status, :headers, and :body fields



116
117
118
119
120
121
122
# File 'lib/inferno/entities/request.rb', line 116

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

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

Find a response header

Parameters:

  • name (String)

    the header name

Returns:



74
75
76
# File 'lib/inferno/entities/request.rb', line 74

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

#response_headersArray<Inferno::Entities::Header>

All of the response headers

Returns:



96
97
98
# File 'lib/inferno/entities/request.rb', line 96

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