Class: TShield::RequestMatching
- Inherits:
-
Object
- Object
- TShield::RequestMatching
show all
- Includes:
- Matching::Filters
- Defined in:
- lib/tshield/request_matching.rb
Overview
Class to check request matching
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
build_query_hash, #filter_by_headers, #filter_by_method, #filter_by_path, #filter_by_query, #filter_stubs, #find_stub, include_headers, include_query
Constructor Details
#initialize(path, options = {}) ⇒ RequestMatching
Returns a new instance of RequestMatching.
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/tshield/request_matching.rb', line 15
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
|
Class Attribute Details
.stubs ⇒ Object
Returns the value of attribute stubs.
47
48
49
|
# File 'lib/tshield/request_matching.rb', line 47
def stubs
@stubs
end
|
Instance Attribute Details
#matched ⇒ Object
Returns the value of attribute matched.
13
14
15
|
# File 'lib/tshield/request_matching.rb', line 13
def matched
@matched
end
|
Class Method Details
.clear_stubs ⇒ Object
49
50
51
|
# File 'lib/tshield/request_matching.rb', line 49
def clear_stubs
@stubs = nil
end
|
.init_stub_session(stub) ⇒ Object
73
74
75
76
77
|
# File 'lib/tshield/request_matching.rb', line 73
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) ⇒ Object
83
84
85
86
|
# File 'lib/tshield/request_matching.rb', line 83
def load_item(item, session_name)
stubs[session_name][item['path']] ||= []
stubs[session_name][item['path']] << item
end
|
.load_items(items, session_name) ⇒ Object
79
80
81
|
# File 'lib/tshield/request_matching.rb', line 79
def load_items(items, session_name)
items.each { |item| load_item(item, session_name) }
end
|
.load_stub(file) ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/tshield/request_matching.rb', line 60
def load_stub(file)
content = JSON.parse File.open(file).read
content.each do |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
end
|
.load_stubs ⇒ Object
53
54
55
56
57
58
|
# File 'lib/tshield/request_matching.rb', line 53
def load_stubs
@stubs = {}
Dir.glob('matching/**/*.json').each do |entry|
load_stub(entry)
end
end
|
.read_body(content) ⇒ Object
88
89
90
91
92
93
|
# File 'lib/tshield/request_matching.rb', line 88
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) ⇒ Object
95
96
97
|
# File 'lib/tshield/request_matching.rb', line 95
def read_file_content(content)
File.open(File.join('matching', content.gsub('FILE://', '')), 'r').read
end
|
Instance Method Details
#current_response ⇒ Object
37
38
39
40
41
42
43
44
|
# File 'lib/tshield/request_matching.rb', line 37
def current_response
if matched.is_a? Array
index = @options[:call] % matched.size
return matched[index]
end
matched
end
|
#match_request ⇒ Object
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/tshield/request_matching.rb', line 26
def match_request
@matched = find_stub(self.class.stubs)
return unless matched
@matched = current_response
TShield::Response.new(self.class.read_body(matched['body']),
matched['headers'],
matched['status'])
end
|