class TShield::RequestMatching

Class to check request matching

Attributes

stubs[R]
matched[R]

Public Class Methods

clear_stubs() click to toggle source
# File lib/tshield/request_matching.rb, line 51
def clear_stubs
  @stubs = nil
end
init_stub_session(stub) click to toggle source
# File lib/tshield/request_matching.rb, line 98
def init_stub_session(stub)
  stub_session_name = stub['session'] || DEFAULT_SESSION
  stubs[stub_session_name] ||= {}
  stub_session_name
end
load_item(item, session_name) click to toggle source
# File lib/tshield/request_matching.rb, line 108
def load_item(item, session_name)
  stubs[session_name][item['path']] ||= []
  stubs[session_name][item['path']] << item
end
load_items(items, session_name) click to toggle source
# File lib/tshield/request_matching.rb, line 104
def load_items(items, session_name)
  items.each { |item| load_item(item, session_name) }
end
load_stub(file) click to toggle source
# File lib/tshield/request_matching.rb, line 62
def load_stub(file)
  content = read_stub_file(file)
  content.each do |stub|
    next unless valid_stub?(file, stub)

    load_valid_stub(stub)
  end
end
load_stubs() click to toggle source
# File lib/tshield/request_matching.rb, line 55
def load_stubs
  @stubs = {}
  Dir.glob('matching/**/*.json').each do |entry|
    load_stub(entry)
  end
end
load_valid_stub(stub) click to toggle source
# File lib/tshield/request_matching.rb, line 88
def load_valid_stub(stub)
  stub_session_name = init_stub_session(stub)

  if stub['stubs']
    load_items(stub['stubs'] || [], stub_session_name)
  else
    load_item(stub, stub_session_name)
  end
end
mandatory_attributes?(stub) click to toggle source
# File lib/tshield/request_matching.rb, line 84
def mandatory_attributes?(stub)
  (stub['method'] && stub['path'] && stub['response']) || (stub['session'] && stub['stubs'])
end
new(path, options = {}) click to toggle source
Calls superclass method
# File lib/tshield/request_matching.rb, line 16
def initialize(path, options = {})
  super()
  @path = path
  @options = options
  @options[:session] ||= DEFAULT_SESSION
  @options[:method] ||= 'GET'

  klass = self.class
  klass.load_stubs unless klass.stubs
end
read_body(content) click to toggle source
# File lib/tshield/request_matching.rb, line 113
def read_body(content)
  return content.to_json if content.is_a? Hash
  return read_file_content(content) if content =~ %r{^FILE://}

  content
end
read_file_content(content) click to toggle source
# File lib/tshield/request_matching.rb, line 120
def read_file_content(content)
  File.open(File.join('matching', content.gsub('FILE://', '')), 'r').read
end
read_stub_file(file) click to toggle source
# File lib/tshield/request_matching.rb, line 71
def read_stub_file(file)
  JSON.parse File.open(file).read
rescue StandardError
  TShield.logger.error "error in loading matching file #{file}"
  []
end
valid_stub?(file, stub) click to toggle source
# File lib/tshield/request_matching.rb, line 78
def valid_stub?(file, stub)
  is_valid = stub.is_a?(Hash) && mandatory_attributes?(stub)
  TShield.logger.info "loading matching file #{file}" if is_valid
  is_valid
end

Public Instance Methods

current_response() click to toggle source
# File lib/tshield/request_matching.rb, line 39
def current_response
  if matched.is_a? Array
    index = @options[:call] % matched.size
    return matched[index]
  end

  matched
end
match_request() click to toggle source
# File lib/tshield/request_matching.rb, line 27
def match_request
  @matched = find_stub(self.class.stubs)
  return unless matched

  @matched = current_response

  sleep matched['delay'] || 0
  TShield::Response.new(self.class.read_body(matched['body']),
                        matched['headers'],
                        matched['status'])
end