module TShield::Controllers::Requests::Helpers

Requests Handler Helpers

Public Class Methods

build_headers(request) click to toggle source
# File lib/tshield/controllers/requests.rb, line 32
def self.build_headers(request)
  headers = request.env.select { |key, _value| key =~ /HTTP/ }
  headers['Content-Type'] = request.content_type || 'application/json'
  headers
end

Public Instance Methods

add_headers(options, path) click to toggle source
# File lib/tshield/controllers/requests.rb, line 93
def add_headers(options, path)
  (configuration.get_headers(domain(path)) || {}).each do |source, destiny|
    options[:headers][destiny] = request.env[source] if request.env[source]
  end
end
configuration() click to toggle source
# File lib/tshield/controllers/requests.rb, line 104
def configuration
  @configuration ||= TShield::Configuration.singleton
end
delay(path) click to toggle source
# File lib/tshield/controllers/requests.rb, line 112
def delay(path)
  delay_in_seconds = configuration.get_delay(domain(path), path) || 0
  logger.info("Response with delay of #{delay_in_seconds} seconds")
  sleep delay_in_seconds
end
domain(path) click to toggle source
# File lib/tshield/controllers/requests.rb, line 108
def domain(path)
  @domain ||= configuration.get_domain_for(path)
end
treat(params, request, _response) click to toggle source
# File lib/tshield/controllers/requests.rb, line 38
def treat(params, request, _response)
  path = params.fetch('captures', [])[0]
  callid = "#{path}?#{request.env['QUERY_STRING']}"

  method = request.request_method
  request_content_type = request.content_type

  session_name = TShield::Controllers::Helpers::SessionHelpers.current_session_name(request)
  secondary_sessions = TShield::Controllers::Helpers::SessionHelpers.secondary_sessions(request)
  session_call = TShield::Controllers::Helpers::SessionHelpers
                 .current_session_call(request, callid, method)

  options = {
    method: method,
    headers: Helpers.build_headers(request),
    raw_query: request.env['QUERY_STRING'],
    session: session_name,
    secondary_sessions: secondary_sessions,
    call: session_call,
    ip: request.ip
  }

  treat_headers_by_domain(options, path)

  if %w[POST PUT PATCH].include? method
    result = request.body.read.encode('UTF-8',
                                      invalid: :replace,
                                      undef: :replace,
                                      replace: '')
    options[:body] = result
  end
  api_response = TShield::RequestMatching.new(path, options.clone).match_request

  unless api_response
    add_headers(options, path)

    api_response ||= TShield::RequestVCR.new(path, options.clone).vcr_response
    api_response.headers.reject! do |key, _v|
      configuration.get_excluded_headers(domain(path)).include?(key)
    end
  end

  logger.info(
    "original=#{api_response.original} method=#{method} path=#{path} "\
    "content-type=#{request_content_type} "\
    "session=#{session_name} call=#{session_call}"
  )
  TShield::Controllers::Helpers::SessionHelpers.update_session_call(request, callid, method)

  delay(path)
  status api_response.status
  headers api_response.headers
  body api_response.body
end
treat_headers_by_domain(options, path) click to toggle source
# File lib/tshield/controllers/requests.rb, line 99
def treat_headers_by_domain(options, path)
  @send_header_content_type = configuration.send_header_content_type(domain(path))
  options[:headers].delete('Content-Type') unless @send_header_content_type
end