forked from bblimke/webmock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_stub_spec.rb
295 lines (230 loc) · 10.4 KB
/
request_stub_spec.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
require 'spec_helper'
describe WebMock::RequestStub do
before(:each) do
@request_stub = WebMock::RequestStub.new(:get, "www.example.com")
end
it "should have request pattern with method and uri" do
expect(@request_stub.request_pattern.to_s).to eq("GET http://www.example.com/")
end
it "should have response" do
expect(@request_stub.response).to be_a(WebMock::Response)
end
describe "requests" do
it "should have no requests" do
expect(@request_stub.requests).to eq([])
end
it "should have requests" do
signature = WebMock::RequestSignature.new(:get, "www.example.com")
WebMock::RequestRegistry.instance.requested_signatures.put(signature)
expect(@request_stub.requests).to eq([signature])
end
it "should only return matching requests" do
match = WebMock::RequestSignature.new(:get, "www.example.com")
WebMock::RequestRegistry.instance.requested_signatures.put(match)
non_match = WebMock::RequestSignature.new(:get, "www.example.org")
WebMock::RequestRegistry.instance.requested_signatures.put(non_match)
expect(@request_stub.requests).to eq([match])
end
it "should not combine identical requests" do
signature1 = WebMock::RequestSignature.new(:get, "www.example.com")
signature2 = WebMock::RequestSignature.new(:get, "www.example.com")
WebMock::RequestRegistry.instance.requested_signatures.put(signature1)
WebMock::RequestRegistry.instance.requested_signatures.put(signature2)
expect(@request_stub.requests).to eq([signature1, signature2])
end
it "test1" do
WebMock.allow_net_connect!
require 'net/http'
Net::HTTP.get('www.google.com', '/')
stub1 = stub_request(:get, "www.google.com").to_return(body: "abc")
Net::HTTP.get('www.google.com', '/')
stub2 = stub_request(:get, "www.google.com").to_return(body: "def")
Net::HTTP.get('www.google.com', '/')
WebMock.disable_net_connect!
expect(stub2).to(have_been_requested.once())
puts stub2.requests.size # will return 3
end
it "test2" do
WebMock.allow_net_connect!
require 'net/http'
stub1 = stub_request(:post, "www.google.com").to_return(body: "def")
stub2 = stub_request(:post, "www.google.com").with(:body => "param1=one").to_return(body: "abc")
uri = URI('http://www.google.com/')
Net::HTTP.post_form(uri, :param1 => 'one')
Net::HTTP.post_form(uri, :param1 => 'two')
WebMock.disable_net_connect!
expect(stub1).to(have_been_requested.once())
puts stub1.requests.size # will return 2, despite it only handled the last request
puts stub2.requests.size
end
end
describe "last_request" do
it "should be nil when there are no requests" do
expect(@request_stub.last_request).to be_nil
end
it "should be the last requests when there are multiple requests" do
first = WebMock::RequestSignature.new(:get, "www.example.com")
WebMock::RequestRegistry.instance.requested_signatures.put(first)
last = WebMock::RequestSignature.new(:get, "www.example.com")
WebMock::RequestRegistry.instance.requested_signatures.put(last)
expect(@request_stub.last_request).to eq(last)
end
end
describe "first_request" do
it "should be nil when there are no requests" do
expect(@request_stub.first_request).to be_nil
end
it "should be the first requests when there are multiple requests" do
first = WebMock::RequestSignature.new(:get, "www.example.com")
WebMock::RequestRegistry.instance.requested_signatures.put(first)
last = WebMock::RequestSignature.new(:get, "www.example.com")
WebMock::RequestRegistry.instance.requested_signatures.put(last)
expect(@request_stub.last_request).to eq(first)
end
end
describe "with" do
it "should assign body to request pattern" do
@request_stub.with(:body => "abc")
expect(@request_stub.request_pattern.to_s).to eq(WebMock::RequestPattern.new(:get, "www.example.com", :body => "abc").to_s)
end
it "should assign normalized headers to request pattern" do
@request_stub.with(:headers => {'A' => 'a'})
expect(@request_stub.request_pattern.to_s).to eq(
WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'A' => 'a'}).to_s
)
end
it "should assign given block to request profile" do
@request_stub.with { |req| req.body == "abc" }
expect(@request_stub.request_pattern.matches?(WebMock::RequestSignature.new(:get, "www.example.com", :body => "abc"))).to be_truthy
end
end
describe "to_return" do
it "should assign response with provided options" do
@request_stub.to_return(:body => "abc", :status => 500)
expect(@request_stub.response.body).to eq("abc")
expect(@request_stub.response.status).to eq([500, ""])
end
it "should assign responses with provided options" do
@request_stub.to_return([{:body => "abc"}, {:body => "def"}])
expect([@request_stub.response.body, @request_stub.response.body]).to eq(["abc", "def"])
end
end
describe "then" do
it "should return stub without any modifications, acting as syntactic sugar" do
expect(@request_stub.then).to eq(@request_stub)
end
end
describe "response" do
it "should return responses in a sequence passed as array" do
@request_stub.to_return([{:body => "abc"}, {:body => "def"}])
expect(@request_stub.response.body).to eq("abc")
expect(@request_stub.response.body).to eq("def")
end
it "should repeat returning last response" do
@request_stub.to_return([{:body => "abc"}, {:body => "def"}])
@request_stub.response
@request_stub.response
expect(@request_stub.response.body).to eq("def")
end
it "should return responses in a sequence passed as comma separated params" do
@request_stub.to_return({:body => "abc"}, {:body => "def"})
expect(@request_stub.response.body).to eq("abc")
expect(@request_stub.response.body).to eq("def")
end
it "should return responses declared in multiple to_return declarations" do
@request_stub.to_return({:body => "abc"}).to_return({:body => "def"})
expect(@request_stub.response.body).to eq("abc")
expect(@request_stub.response.body).to eq("def")
end
end
describe "to_raise" do
it "should assign response with exception to be thrown" do
@request_stub.to_raise(ArgumentError)
expect {
@request_stub.response.raise_error_if_any
}.to raise_error(ArgumentError, "Exception from WebMock")
end
it "should assign sequence of responses with response with exception to be thrown" do
@request_stub.to_return(:body => "abc").then.to_raise(ArgumentError)
expect(@request_stub.response.body).to eq("abc")
expect {
@request_stub.response.raise_error_if_any
}.to raise_error(ArgumentError, "Exception from WebMock")
end
it "should assign a list responses to be thrown in a sequence" do
@request_stub.to_raise(ArgumentError, IndexError)
expect {
@request_stub.response.raise_error_if_any
}.to raise_error(ArgumentError, "Exception from WebMock")
expect {
@request_stub.response.raise_error_if_any
}.to raise_error(IndexError, "Exception from WebMock")
end
it "should raise exceptions declared in multiple to_raise declarations" do
@request_stub.to_raise(ArgumentError).then.to_raise(IndexError)
expect {
@request_stub.response.raise_error_if_any
}.to raise_error(ArgumentError, "Exception from WebMock")
expect {
@request_stub.response.raise_error_if_any
}.to raise_error(IndexError, "Exception from WebMock")
end
end
describe "to_timeout" do
it "should assign response with timeout" do
@request_stub.to_timeout
expect(@request_stub.response.should_timeout).to be_truthy
end
it "should assign sequence of responses with response with timeout" do
@request_stub.to_return(:body => "abc").then.to_timeout
expect(@request_stub.response.body).to eq("abc")
expect(@request_stub.response.should_timeout).to be_truthy
end
it "should allow multiple timeouts to be declared" do
@request_stub.to_timeout.then.to_timeout.then.to_return(:body => "abc")
expect(@request_stub.response.should_timeout).to be_truthy
expect(@request_stub.response.should_timeout).to be_truthy
expect(@request_stub.response.body).to eq("abc")
end
end
describe "times" do
it "should give error if declared before any response declaration is declared" do
expect {
@request_stub.times(3)
}.to raise_error("Invalid WebMock stub declaration. times(N) can be declared only after response declaration.")
end
it "should repeat returning last declared response declared number of times" do
@request_stub.to_return({:body => "abc"}).times(2).then.to_return({:body => "def"})
expect(@request_stub.response.body).to eq("abc")
expect(@request_stub.response.body).to eq("abc")
expect(@request_stub.response.body).to eq("def")
end
it "should repeat raising last declared exception declared number of times" do
@request_stub.to_return({:body => "abc"}).times(2).then.to_return({:body => "def"})
expect(@request_stub.response.body).to eq("abc")
expect(@request_stub.response.body).to eq("abc")
expect(@request_stub.response.body).to eq("def")
end
it "should repeat returning last declared sequence of responses declared number of times" do
@request_stub.to_return({:body => "abc"}, {:body => "def"}).times(2).then.to_return({:body => "ghj"})
expect(@request_stub.response.body).to eq("abc")
expect(@request_stub.response.body).to eq("def")
expect(@request_stub.response.body).to eq("abc")
expect(@request_stub.response.body).to eq("def")
expect(@request_stub.response.body).to eq("ghj")
end
it "should return self" do
expect(@request_stub.to_return({:body => "abc"}).times(1)).to eq(@request_stub)
end
it "should raise error if argument is not integer" do
expect {
@request_stub.to_return({:body => "abc"}).times("not number")
}.to raise_error("times(N) accepts integers >= 1 only")
end
it "should raise error if argument is < 1" do
expect {
@request_stub.to_return({:body => "abc"}).times(0)
}.to raise_error("times(N) accepts integers >= 1 only")
end
end
end