class Inferno::Entities::Request

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

@attr_accessor [String] id of the request @attr_accessor [String] index of the request. Used for ordering. @attr_accessor [String] verb http verb @attr_accessor [String] url request url @attr_accessor [String] direction incoming/outgoing @attr_accessor [String] name name for the request @attr_accessor [String] status http response status code @attr_accessor [String] request_body body of the http request @attr_accessor [String] response_body body of the http response @attr_accessor [Array<Inferno::Entities::Header>] headers http

request/response headers

@attr_accessor [String] result_id id of the result for this request @attr_accessor [String] test_session_id id of the test session for this request @attr_accessor [Time] created_at creation timestamp @attr_accessor [Time] updated_at update timestamp

Constants

ATTRIBUTES
SUMMARY_FIELDS

Public Class Methods

from_fhir_client_reply(reply, test_session_id:, direction: 'outgoing', name: nil) click to toggle source

@api private

# File lib/inferno/entities/request.rb, line 169
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') }

  new(
    verb: request[:method],
    url: request[:url],
    direction: direction,
    name: name,
    status: response[:code].to_i,
    request_body: request[:payload],
    response_body: response[:body],
    test_session_id: test_session_id,
    headers: request_headers + response_headers
  )
end
from_http_response(response, test_session_id:, direction: 'outgoing', name: nil) click to toggle source

@api private

# File lib/inferno/entities/request.rb, line 147
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
from_rack_env(env, name: nil) click to toggle source

@api private

# File lib/inferno/entities/request.rb, line 126
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
new(params) click to toggle source
Calls superclass method Inferno::Entities::Entity::new
# File lib/inferno/entities/request.rb, line 32
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

Public Instance Methods

query_parameters() click to toggle source

@return [Hash<String, String>]

# File lib/inferno/entities/request.rb, line 40
def query_parameters
  Addressable::URI.parse(url).query_values || {}
end
request() click to toggle source

Return a hash of the request parameters

@return [Hash]

# File lib/inferno/entities/request.rb, line 77
def request
  {
    verb: verb,
    url: url,
    headers: request_headers,
    body: request_body
  }
end
request_header(name) click to toggle source

Find a request header

@param name [String] the header name @return [Inferno::Entities::RequestHeader, nil]

# File lib/inferno/entities/request.rb, line 56
def request_header(name)
  request_headers.find { |header| header.name == name.downcase }
end
request_headers() click to toggle source

All of the request headers

@return [Array<Inferno::Entities::RequestHeader>]

# File lib/inferno/entities/request.rb, line 63
def request_headers
  headers.select(&:request?)
end
resource() click to toggle source

Return the FHIR resource from the response body.

@return [FHIR::Model]

# File lib/inferno/entities/request.rb, line 120
def resource
  FHIR.from_contents(response_body)
end
response() click to toggle source

Return a hash of the response parameters

@return [Hash]

# File lib/inferno/entities/request.rb, line 89
def response
  {
    status: status,
    headers: response_headers,
    body: response_body
  }
end
response_header(name) click to toggle source

Find a response header

@param name [String] the header name @return [Inferno::Entities::RequestHeader, nil]

# File lib/inferno/entities/request.rb, line 48
def response_header(name)
  response_headers.find { |header| header.name == name.downcase }
end
response_headers() click to toggle source

All of the response headers

@return [Array<Inferno::Entities::RequestHeader>]

# File lib/inferno/entities/request.rb, line 70
def response_headers
  headers.select(&:response?)
end
to_hash() click to toggle source

@api private

# File lib/inferno/entities/request.rb, line 98
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