forked from bblimke/webmock
/
request_pattern.rb
203 lines (161 loc) · 4.97 KB
/
request_pattern.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
module WebMock
class RequestPattern
def initialize(method, uri, options = {})
@method_pattern = MethodPattern.new(method)
@uri_pattern = create_uri_pattern(uri)
assign_options(options)
end
def with(options = {}, &block)
assign_options(options)
@with_block = block
self
end
def matches?(request_signature)
content_type = request_signature.headers['Content-Type'] if request_signature.headers
@method_pattern.matches?(request_signature.method) &&
@uri_pattern.matches?(request_signature.uri) &&
(@body_pattern.nil? || @body_pattern.matches?(request_signature.body, content_type || "")) &&
(@headers_pattern.nil? || @headers_pattern.matches?(request_signature.headers)) &&
(@with_block.nil? || @with_block.call(request_signature))
end
def to_s
string = "#{@method_pattern.to_s.upcase}"
string << " #{@uri_pattern.to_s}"
string << " with body #{@body_pattern.to_s}" if @body_pattern
string << " with headers #{@headers_pattern.to_s}" if @headers_pattern
string << " with given block" if @with_block
string
end
private
def assign_options(options)
@body_pattern = BodyPattern.new(options[:body]) if options.has_key?(:body)
@headers_pattern = HeadersPattern.new(options[:headers]) if options.has_key?(:headers)
@uri_pattern.add_query_params(options[:query]) if options.has_key?(:query)
end
def create_uri_pattern(uri)
if uri.is_a?(Regexp)
URIRegexpPattern.new(uri)
else
URIStringPattern.new(uri)
end
end
end
class MethodPattern
def initialize(pattern)
@pattern = pattern
end
def matches?(method)
@pattern == method || @pattern == :any
end
def to_s
@pattern.to_s
end
end
class URIPattern
def initialize(pattern)
@pattern = pattern.is_a?(Addressable::URI) ? pattern : WebMock::Util::URI.normalize_uri(pattern)
end
end
class URIRegexpPattern < URIPattern
def matches?(uri)
WebMock::Util::URI.variations_of_uri_as_strings(uri).any? { |u| u.match(@pattern) } &&
(@query_params.nil? || @query_params == uri.query_values)
end
def to_s
str = @pattern.inspect
str += " with query params #{@query_params.inspect}" if @query_params
str
end
def add_query_params(query_params)
@query_params = query_params.is_a?(Hash) ? query_params : Addressable::URI.parse('?' + query_params).query_values
end
end
class URIStringPattern < URIPattern
def matches?(uri)
if @pattern.is_a?(Addressable::URI)
uri === @pattern
else
false
end
end
def add_query_params(query_params)
if !query_params.is_a?(Hash)
query_params = Addressable::URI.parse('?' + query_params).query_values
end
@pattern.query_values = (@pattern.query_values || {}).merge(query_params)
end
def to_s
WebMock::Util::URI.strip_default_port_from_uri_string(@pattern.to_s)
end
end
class BodyPattern
BODY_FORMATS = {
'text/xml' => :xml,
'application/xml' => :xml,
'application/json' => :json,
'text/json' => :json,
'application/javascript' => :json,
'text/javascript' => :json,
'text/html' => :html,
'application/x-yaml' => :yaml,
'text/yaml' => :yaml,
'text/plain' => :plain
}
def initialize(pattern)
@pattern = pattern
if (@pattern).is_a?(Hash)
@pattern = normalize_hash(@pattern)
end
end
def matches?(body, content_type = "")
if (@pattern).is_a?(Hash)
return true if @pattern.empty?
case BODY_FORMATS[content_type]
when :json then
Crack::JSON.parse(body) == @pattern
when :xml then
Crack::XML.parse(body) == @pattern
else
Addressable::URI.parse('?' + body).query_values == @pattern
end
else
empty_string?(@pattern) && empty_string?(body) ||
@pattern == body ||
@pattern === body
end
end
def to_s
@pattern.inspect
end
private
def empty_string?(string)
string.nil? || string == ""
end
def normalize_hash(hash)
WebMock::Util::HashKeysStringifier.stringify_keys!(hash)
end
end
class HeadersPattern
def initialize(pattern)
@pattern = WebMock::Util::Headers.normalize_headers(pattern) || {}
end
def matches?(headers)
if empty_headers?(@pattern)
empty_headers?(headers)
else
return false if empty_headers?(headers)
@pattern.each do |key, value|
return false unless headers.has_key?(key) && value === headers[key]
end
true
end
end
def to_s
WebMock::Util::Headers.sorted_headers_string(@pattern)
end
private
def empty_headers?(headers)
headers.nil? || headers == {}
end
end
end