webmock 0.8.2 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +113 -0
- data/README.md +84 -18
- data/VERSION +1 -1
- data/lib/webmock.rb +1 -0
- data/lib/webmock/adapters/rspec/webmock_matcher.rb +2 -2
- data/lib/webmock/http_lib_adapters/net_http.rb +11 -13
- data/lib/webmock/request.rb +1 -1
- data/lib/webmock/request_profile.rb +11 -2
- data/lib/webmock/request_signature.rb +8 -2
- data/lib/webmock/request_stub.rb +39 -11
- data/lib/webmock/response.rb +32 -1
- data/lib/webmock/responses_sequence.rb +40 -0
- data/lib/webmock/util/headers.rb +1 -1
- data/lib/webmock/webmock.rb +4 -4
- data/spec/example_curl_output.txt +20 -0
- data/spec/httpclient_spec_helper.rb +4 -0
- data/spec/net_http_spec.rb +8 -0
- data/spec/net_http_spec_helper.rb +10 -1
- data/spec/request_profile_spec.rb +5 -0
- data/spec/request_registry_spec.rb +4 -4
- data/spec/request_signature_spec.rb +35 -0
- data/spec/request_stub_spec.rb +123 -0
- data/spec/response_spec.rb +72 -7
- data/spec/spec_helper.rb +27 -0
- data/spec/webmock_spec.rb +674 -330
- data/test/test_webmock.rb +2 -1
- data/webmock.gemspec +5 -3
- metadata +5 -3
- data/CHANGELOG +0 -36
data/spec/response_spec.rb
CHANGED
@@ -36,11 +36,11 @@ describe Response do
|
|
36
36
|
it "should not raise error if no error assigned" do
|
37
37
|
@response.raise_error_if_any
|
38
38
|
end
|
39
|
-
|
39
|
+
|
40
40
|
end
|
41
41
|
|
42
42
|
describe "body" do
|
43
|
-
|
43
|
+
|
44
44
|
it "should return empty body by default" do
|
45
45
|
@response.body.should == ''
|
46
46
|
end
|
@@ -49,23 +49,88 @@ describe Response do
|
|
49
49
|
@response = Response.new(:body => "abc")
|
50
50
|
@response.body.should == "abc"
|
51
51
|
end
|
52
|
-
|
52
|
+
|
53
53
|
it "should report string even if existing file path was provided" do
|
54
54
|
@response = Response.new(:body => __FILE__)
|
55
55
|
@response.body.should == __FILE__
|
56
56
|
end
|
57
|
-
|
57
|
+
|
58
58
|
it "should report content of a IO object if provided" do
|
59
59
|
@response = Response.new(:body => File.new(__FILE__))
|
60
60
|
@response.body.should == File.new(__FILE__).read
|
61
61
|
end
|
62
|
-
|
62
|
+
|
63
63
|
it "should report many times content of a IO object if provided" do
|
64
64
|
@response = Response.new(:body => File.new(__FILE__))
|
65
65
|
@response.body.should == File.new(__FILE__).read
|
66
66
|
@response.body.should == File.new(__FILE__).read
|
67
67
|
end
|
68
|
-
|
68
|
+
|
69
69
|
end
|
70
|
-
|
70
|
+
|
71
|
+
describe "from raw response" do
|
72
|
+
|
73
|
+
describe "when input is IO" do
|
74
|
+
before(:each) do
|
75
|
+
@file = File.new(File.expand_path(File.dirname(__FILE__)) + "/example_curl_output.txt")
|
76
|
+
@response = Response.new(@file)
|
77
|
+
end
|
78
|
+
|
79
|
+
|
80
|
+
it "should read status" do
|
81
|
+
@response.status.should == 200
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should read headers" do
|
85
|
+
@response.headers.should == {
|
86
|
+
"Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
|
87
|
+
"Content-Type"=>"text/html; charset=UTF-8",
|
88
|
+
"Content-Length"=>"438",
|
89
|
+
"Connection"=>"Keep-Alive"
|
90
|
+
}
|
91
|
+
end
|
92
|
+
|
93
|
+
it "should read body" do
|
94
|
+
@response.body.size.should == 438
|
95
|
+
end
|
96
|
+
|
97
|
+
it "should close IO" do
|
98
|
+
@file.should be_closed
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
describe "when input is String" do
|
104
|
+
before(:each) do
|
105
|
+
@input = File.new(File.expand_path(File.dirname(__FILE__)) + "/example_curl_output.txt").read
|
106
|
+
@response = Response.new(@input)
|
107
|
+
end
|
108
|
+
|
109
|
+
it "should read status" do
|
110
|
+
@response.status.should == 200
|
111
|
+
end
|
112
|
+
|
113
|
+
it "should read headers" do
|
114
|
+
@response.headers.should == {
|
115
|
+
"Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
|
116
|
+
"Content-Type"=>"text/html; charset=UTF-8",
|
117
|
+
"Content-Length"=>"438",
|
118
|
+
"Connection"=>"Keep-Alive"
|
119
|
+
}
|
120
|
+
end
|
121
|
+
|
122
|
+
it "should read body" do
|
123
|
+
@response.body.size.should == 438
|
124
|
+
end
|
125
|
+
|
126
|
+
it "should work with transfer-encoding set to chunked" do
|
127
|
+
@input.gsub!("Content-Length: 438", "Transfer-Encoding: chunked")
|
128
|
+
@response = Response.new(@input)
|
129
|
+
@response.body.size.should == 438
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
133
|
+
|
134
|
+
end
|
135
|
+
|
71
136
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -7,6 +7,8 @@ require 'spec/autorun'
|
|
7
7
|
|
8
8
|
require 'webmock/rspec'
|
9
9
|
|
10
|
+
require 'json'
|
11
|
+
|
10
12
|
include WebMock
|
11
13
|
|
12
14
|
def fail()
|
@@ -31,3 +33,28 @@ def setup_expectations_for_real_example_com_request(options = {})
|
|
31
33
|
setup_expectations_for_real_request(defaults.merge(options))
|
32
34
|
end
|
33
35
|
|
36
|
+
def client_specific_request_string(string)
|
37
|
+
method = string.gsub(/.*Unregistered request: ([^ ]+).+/, '\1')
|
38
|
+
has_body = string.include?(" with body")
|
39
|
+
default_headers = default_client_request_headers(method, has_body)
|
40
|
+
if default_headers
|
41
|
+
default_headers_string = WebMock::Util::Headers.normalize_headers(default_headers).inspect.gsub("\"","'")
|
42
|
+
default_headers_string.gsub!(/[{}]/, "")
|
43
|
+
if string.include?(" with headers")
|
44
|
+
current_headers = JSON.parse(string.gsub(/.*with headers (\{[^}]+\}).*/, '\1').gsub("=>",":").gsub("'","\""))
|
45
|
+
default_headers = WebMock::Util::Headers.normalize_headers(default_headers)
|
46
|
+
default_headers.reject! {|k,v| current_headers.has_key?(k) }
|
47
|
+
default_headers_string = default_headers.inspect.gsub("\"","'").gsub!(/[{}]/, "")
|
48
|
+
string.gsub!(/(.*)(with headers \{[^}]*)(\}.*)/, '\1\2' + ", #{default_headers_string}}") if !default_headers_string.empty?
|
49
|
+
string
|
50
|
+
else
|
51
|
+
headers_string =
|
52
|
+
" with headers #{WebMock::Util::Headers.normalize_headers(default_headers).inspect.gsub("\"","'")}"
|
53
|
+
string << headers_string
|
54
|
+
end
|
55
|
+
end
|
56
|
+
string
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
|
data/spec/webmock_spec.rb
CHANGED
@@ -6,6 +6,8 @@ unless defined? SAMPLE_HEADERS
|
|
6
6
|
NOT_ESCAPED_PARAMS = "z='Stop!' said Fred&x=ab c"
|
7
7
|
end
|
8
8
|
|
9
|
+
class MyException < StandardError; end;
|
10
|
+
|
9
11
|
describe "WebMock", :shared => true do
|
10
12
|
before(:each) do
|
11
13
|
WebMock.disable_net_connect!
|
@@ -50,7 +52,7 @@ describe "WebMock", :shared => true do
|
|
50
52
|
it "should raise exception if request was not stubbed" do
|
51
53
|
lambda {
|
52
54
|
http_request(:get, "http://www.example.com/")
|
53
|
-
}.should fail_with("Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/")
|
55
|
+
}.should fail_with(client_specific_request_string("Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/"))
|
54
56
|
end
|
55
57
|
end
|
56
58
|
|
@@ -89,7 +91,8 @@ describe "WebMock", :shared => true do
|
|
89
91
|
http_request(:get, "http://www.example.com/").status.should == "200"
|
90
92
|
lambda {
|
91
93
|
http_request(:post, "http://www.example.com/")
|
92
|
-
}.should fail_with(
|
94
|
+
}.should fail_with(client_specific_request_string(
|
95
|
+
"Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/")
|
93
96
|
)
|
94
97
|
end
|
95
98
|
|
@@ -98,24 +101,44 @@ describe "WebMock", :shared => true do
|
|
98
101
|
describe "on body" do
|
99
102
|
|
100
103
|
it "should match requests if body is the same" do
|
101
|
-
stub_http_request(:
|
104
|
+
stub_http_request(:post, "www.example.com").with(:body => "abc")
|
102
105
|
http_request(
|
103
|
-
:
|
106
|
+
:post, "http://www.example.com/",
|
104
107
|
:body => "abc").status.should == "200"
|
105
108
|
end
|
106
109
|
|
107
110
|
it "should match requests if body is not set in the stub" do
|
108
|
-
stub_http_request(:
|
111
|
+
stub_http_request(:post, "www.example.com")
|
109
112
|
http_request(
|
110
|
-
:
|
113
|
+
:post, "http://www.example.com/",
|
111
114
|
:body => "abc").status.should == "200"
|
112
115
|
end
|
113
116
|
|
114
117
|
it "should not match requests if body is different" do
|
115
|
-
stub_http_request(:
|
118
|
+
stub_http_request(:post, "www.example.com").with(:body => "abc")
|
116
119
|
lambda {
|
117
|
-
http_request(:
|
118
|
-
}.should fail_with(
|
120
|
+
http_request(:post, "http://www.example.com/", :body => "def")
|
121
|
+
}.should fail_with(client_specific_request_string(
|
122
|
+
"Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/ with body 'def'"))
|
123
|
+
end
|
124
|
+
|
125
|
+
describe "with regular expressions" do
|
126
|
+
|
127
|
+
it "should match requests if body matches regexp" do
|
128
|
+
stub_http_request(:post, "www.example.com").with(:body => /\d+abc$/)
|
129
|
+
http_request(
|
130
|
+
:post, "http://www.example.com/",
|
131
|
+
:body => "123abc").status.should == "200"
|
132
|
+
end
|
133
|
+
|
134
|
+
it "should not match requests if body doesn't match regexp" do
|
135
|
+
stub_http_request(:post, "www.example.com").with(:body => /^abc/)
|
136
|
+
lambda {
|
137
|
+
http_request(:post, "http://www.example.com/", :body => "xabc")
|
138
|
+
}.should fail_with(client_specific_request_string(
|
139
|
+
"Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/ with body 'xabc'"))
|
140
|
+
end
|
141
|
+
|
119
142
|
end
|
120
143
|
|
121
144
|
end
|
@@ -143,17 +166,41 @@ describe "WebMock", :shared => true do
|
|
143
166
|
http_request(
|
144
167
|
:get, "http://www.example.com/",
|
145
168
|
:headers => { 'Content-Length' => '9999'})
|
146
|
-
}.should fail_with(
|
169
|
+
}.should fail_with(client_specific_request_string(
|
170
|
+
%q(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers {'Content-Length'=>'9999'})))
|
147
171
|
end
|
148
|
-
|
172
|
+
|
149
173
|
it "should not match if accept header is different" do
|
150
174
|
stub_http_request(:get, "www.example.com").
|
151
175
|
with(:headers => { 'Accept' => 'application/json'})
|
152
176
|
lambda {
|
153
177
|
http_request(
|
154
178
|
:get, "http://www.example.com/",
|
155
|
-
|
156
|
-
}.should fail_with(
|
179
|
+
:headers => { 'Accept' => 'application/xml'})
|
180
|
+
}.should fail_with(client_specific_request_string(
|
181
|
+
%q(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers {'Accept'=>'application/xml'})))
|
182
|
+
end
|
183
|
+
|
184
|
+
describe "with regular expressions" do
|
185
|
+
|
186
|
+
it "should match requests if header values match regular expression" do
|
187
|
+
stub_http_request(:get, "www.example.com").with(:headers => { :user_agent => /^MyAppName$/ })
|
188
|
+
http_request(
|
189
|
+
:get, "http://www.example.com/",
|
190
|
+
:headers => { 'user-agent' => 'MyAppName' }).status.should == "200"
|
191
|
+
end
|
192
|
+
|
193
|
+
it "should not match requests if headers values do not match regular expression" do
|
194
|
+
stub_http_request(:get, "www.example.com").with(:headers => { :user_agent => /^MyAppName$/ })
|
195
|
+
|
196
|
+
lambda {
|
197
|
+
http_request(
|
198
|
+
:get, "http://www.example.com/",
|
199
|
+
:headers => { 'user-agent' => 'xMyAppName' })
|
200
|
+
}.should fail_with(client_specific_request_string(
|
201
|
+
%q(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers {'User-Agent'=>'xMyAppName'})))
|
202
|
+
end
|
203
|
+
|
157
204
|
end
|
158
205
|
end
|
159
206
|
|
@@ -168,415 +215,712 @@ describe "WebMock", :shared => true do
|
|
168
215
|
stub_http_request(:get, "user:pass@www.example.com")
|
169
216
|
lambda {
|
170
217
|
http_request(:get, "http://user:pazz@www.example.com/").status.should == "200"
|
171
|
-
}.should fail_with(
|
218
|
+
}.should fail_with(client_specific_request_string(
|
219
|
+
%q(Real HTTP connections are disabled. Unregistered request: GET http://user:pazz@www.example.com/)))
|
172
220
|
end
|
173
221
|
|
174
222
|
it "should not match if credentials are stubbed but not provided in the request" do
|
175
223
|
stub_http_request(:get, "user:pass@www.example.com")
|
176
224
|
lambda {
|
177
225
|
http_request(:get, "http://www.example.com/").status.should == "200"
|
178
|
-
}.should fail_with(
|
226
|
+
}.should fail_with(client_specific_request_string(
|
227
|
+
%q(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/)))
|
179
228
|
end
|
180
229
|
|
181
230
|
it "should not match if credentials are not stubbed but exist in the request" do
|
182
231
|
stub_http_request(:get, "www.example.com")
|
183
232
|
lambda {
|
184
233
|
http_request(:get, "http://user:pazz@www.example.com/").status.should == "200"
|
185
|
-
}.should fail_with(
|
234
|
+
}.should fail_with(client_specific_request_string(
|
235
|
+
%q(Real HTTP connections are disabled. Unregistered request: GET http://user:pazz@www.example.com/)))
|
186
236
|
end
|
187
237
|
|
188
238
|
end
|
239
|
+
|
240
|
+
describe "with block" do
|
241
|
+
|
242
|
+
it "should match if block returns true" do
|
243
|
+
stub_http_request(:get, "www.example.com").with { |request| true }
|
244
|
+
http_request(:get, "http://www.example.com/").status.should == "200"
|
245
|
+
end
|
246
|
+
|
247
|
+
it "should not match if block returns false" do
|
248
|
+
stub_http_request(:get, "www.example.com").with { |request| false }
|
249
|
+
lambda {
|
250
|
+
http_request(:get, "http://www.example.com/")
|
251
|
+
}.should fail_with(client_specific_request_string(
|
252
|
+
"Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/"))
|
253
|
+
end
|
254
|
+
|
255
|
+
it "should pass the request to the block" do
|
256
|
+
stub_http_request(:post, "www.example.com").with { |request| request.body == "wadus" }
|
257
|
+
http_request(
|
258
|
+
:post, "http://www.example.com/",
|
259
|
+
:body => "wadus").status.should == "200"
|
260
|
+
lambda {
|
261
|
+
http_request(:post, "http://www.example.com/", :body => "jander")
|
262
|
+
}.should fail_with(client_specific_request_string(
|
263
|
+
"Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/ with body 'jander'"))
|
264
|
+
end
|
265
|
+
|
266
|
+
end
|
189
267
|
|
190
268
|
end
|
191
269
|
|
192
270
|
describe "raising stubbed exceptions" do
|
193
271
|
|
194
|
-
|
195
|
-
class MyException < StandardError; end;
|
272
|
+
it "should raise exception if declared in a stubbed response" do
|
196
273
|
stub_http_request(:get, "www.example.com").to_raise(MyException)
|
197
274
|
lambda {
|
198
275
|
http_request(:get, "http://www.example.com/")
|
199
276
|
}.should raise_error(MyException, "Exception from WebMock")
|
200
277
|
end
|
201
278
|
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
stub_http_request(:get, "www.example.com").to_return(:body => "abc")
|
209
|
-
http_request(:get, "http://www.example.com/").body.should == "abc"
|
210
|
-
end
|
211
|
-
|
212
|
-
it "should return declared headers" do
|
213
|
-
stub_http_request(:get, "www.example.com").to_return(:headers => SAMPLE_HEADERS)
|
214
|
-
response = http_request(:get, "http://www.example.com/")
|
215
|
-
response.headers["Content-Length"].should == "8888"
|
216
|
-
end
|
217
|
-
|
218
|
-
it "should return declared status" do
|
219
|
-
stub_http_request(:get, "www.example.com").to_return(:status => 500)
|
220
|
-
http_request(:get, "http://www.example.com/").status.should == "500"
|
221
|
-
end
|
222
|
-
|
223
|
-
it "should return body declared as IO" do
|
224
|
-
stub_http_request(:get, "www.example.com").to_return(:body => File.new(__FILE__))
|
225
|
-
http_request(:get, "http://www.example.com/").body.should == File.new(__FILE__).read
|
226
|
-
end
|
227
|
-
|
228
|
-
it "should return body declared as IO if requested many times" do
|
229
|
-
stub_http_request(:get, "www.example.com").to_return(:body => File.new(__FILE__))
|
230
|
-
2.times do
|
231
|
-
http_request(:get, "http://www.example.com/").body.should == File.new(__FILE__).read
|
279
|
+
it "should raise exception if declared in a stubbed response after returning declared response" do
|
280
|
+
stub_http_request(:get, "www.example.com").to_return(:body => "abc").then.to_raise(MyException)
|
281
|
+
http_request(:get, "http://www.example.com/").body.should == "abc"
|
282
|
+
lambda {
|
283
|
+
http_request(:get, "http://www.example.com/")
|
284
|
+
}.should raise_error(MyException, "Exception from WebMock")
|
232
285
|
end
|
233
|
-
end
|
234
286
|
|
235
|
-
it "should close IO declared as response body after reading" do
|
236
|
-
stub_http_request(:get, "www.example.com").to_return(:body => @file = File.new(__FILE__))
|
237
|
-
@file.should be_closed
|
238
287
|
end
|
239
288
|
|
240
|
-
describe "dynamic responses" do
|
241
|
-
|
242
|
-
it "should return evaluated response body" do
|
243
|
-
stub_http_request(:post, "www.example.com").to_return(:body => lambda { |request| request.body })
|
244
|
-
http_request(:post, "http://www.example.com/", :body => "echo").body.should == "echo"
|
245
|
-
end
|
246
|
-
|
247
|
-
it "should return evaluated response headers" do
|
248
|
-
stub_http_request(:post, "www.example.com").to_return(:headers => lambda { |request| request.headers })
|
249
|
-
http_request(:post, "http://www.example.com/", :headers => {'A' => 'B'}).headers['A'].should == 'B'
|
250
|
-
end
|
251
|
-
|
252
|
-
end
|
253
289
|
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
describe "precedence of stubs" do
|
258
|
-
|
259
|
-
it "should use the last declared matching request stub" do
|
260
|
-
stub_http_request(:get, "www.example.com").to_return(:body => "abc")
|
261
|
-
stub_http_request(:get, "www.example.com").to_return(:body => "def")
|
262
|
-
http_request(:get, "http://www.example.com/").body.should == "def"
|
263
|
-
end
|
290
|
+
describe "returning stubbed responses" do
|
264
291
|
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
http_request(:get, "http://www.example.com/").body.should == "def"
|
269
|
-
end
|
270
|
-
|
271
|
-
end
|
272
|
-
|
273
|
-
describe "verification of request expectation" do
|
274
|
-
|
275
|
-
describe "when net connect not allowed" do
|
276
|
-
|
277
|
-
before(:each) do
|
278
|
-
WebMock.disable_net_connect!
|
279
|
-
stub_http_request(:any, "http://www.example.com")
|
280
|
-
stub_http_request(:any, "https://www.example.com")
|
281
|
-
end
|
282
|
-
|
283
|
-
it "should pass if request was executed with the same uri and method" do
|
284
|
-
lambda {
|
285
|
-
http_request(:get, "http://www.example.com/")
|
286
|
-
request(:get, "http://www.example.com").should have_been_made.once
|
287
|
-
}.should_not raise_error
|
292
|
+
it "should return declared body" do
|
293
|
+
stub_http_request(:get, "www.example.com").to_return(:body => "abc")
|
294
|
+
http_request(:get, "http://www.example.com/").body.should == "abc"
|
288
295
|
end
|
289
296
|
|
290
|
-
it "should
|
291
|
-
|
292
|
-
|
293
|
-
|
297
|
+
it "should return declared headers" do
|
298
|
+
stub_http_request(:get, "www.example.com").to_return(:headers => SAMPLE_HEADERS)
|
299
|
+
response = http_request(:get, "http://www.example.com/")
|
300
|
+
response.headers["Content-Length"].should == "8888"
|
294
301
|
end
|
295
302
|
|
296
|
-
it "should
|
297
|
-
|
298
|
-
|
299
|
-
request(:get, "http://www.example.com").should_not have_been_made
|
300
|
-
}.should fail_with("The request GET http://www.example.com/ was expected to execute 0 times but it executed 1 time")
|
303
|
+
it "should return declared status" do
|
304
|
+
stub_http_request(:get, "www.example.com").to_return(:status => 500)
|
305
|
+
http_request(:get, "http://www.example.com/").status.should == "500"
|
301
306
|
end
|
302
307
|
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
request(:get, "http://www.example.com").should have_been_made
|
307
|
-
}.should fail_with("The request GET http://www.example.com/ was expected to execute 1 time but it executed 0 times")
|
308
|
-
end
|
309
|
-
|
310
|
-
it "should fail if request was executed to different uri" do
|
311
|
-
lambda {
|
312
|
-
http_request(:get, "http://www.example.com/")
|
313
|
-
request(:get, "http://www.example.org").should have_been_made
|
314
|
-
}.should fail_with("The request GET http://www.example.org/ was expected to execute 1 time but it executed 0 times")
|
308
|
+
it "should return body declared as IO" do
|
309
|
+
stub_http_request(:get, "www.example.com").to_return(:body => File.new(__FILE__))
|
310
|
+
http_request(:get, "http://www.example.com/").body.should == File.new(__FILE__).read
|
315
311
|
end
|
316
312
|
|
317
|
-
it "should
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
313
|
+
it "should return body declared as IO if requested many times" do
|
314
|
+
stub_http_request(:get, "www.example.com").to_return(:body => File.new(__FILE__))
|
315
|
+
2.times do
|
316
|
+
http_request(:get, "http://www.example.com/").body.should == File.new(__FILE__).read
|
317
|
+
end
|
322
318
|
end
|
323
319
|
|
324
|
-
it "should
|
325
|
-
|
326
|
-
|
327
|
-
request(:get, "www.example.com").should have_been_made
|
328
|
-
}.should_not raise_error
|
320
|
+
it "should close IO declared as response body after reading" do
|
321
|
+
stub_http_request(:get, "www.example.com").to_return(:body => @file = File.new(__FILE__))
|
322
|
+
@file.should be_closed
|
329
323
|
end
|
330
324
|
|
331
|
-
|
332
|
-
lambda {
|
333
|
-
http_request(:get, "http://www.example.com/")
|
334
|
-
request(:get, "www.example.com:80").should have_been_made
|
335
|
-
}.should_not raise_error
|
336
|
-
end
|
325
|
+
describe "dynamic responses" do
|
337
326
|
|
338
|
-
|
339
|
-
|
340
|
-
http_request(:
|
341
|
-
|
342
|
-
}.should_not raise_error
|
343
|
-
end
|
327
|
+
it "should return evaluated response body" do
|
328
|
+
stub_http_request(:post, "www.example.com").to_return(:body => lambda { |request| request.body })
|
329
|
+
http_request(:post, "http://www.example.com/", :body => "echo").body.should == "echo"
|
330
|
+
end
|
344
331
|
|
345
|
-
|
346
|
-
|
347
|
-
http_request(:
|
348
|
-
|
349
|
-
}.should fail_with("The request GET http://www.example.com:90/ was expected to execute 1 time but it executed 0 times")
|
350
|
-
end
|
332
|
+
it "should return evaluated response headers" do
|
333
|
+
stub_http_request(:post, "www.example.com").to_return(:headers => lambda { |request| request.headers })
|
334
|
+
http_request(:post, "http://www.example.com/", :headers => {'A' => 'B'}).headers['A'].should == 'B'
|
335
|
+
end
|
351
336
|
|
352
|
-
it "should pass if request was executed with different form of uri with https port" do
|
353
|
-
lambda {
|
354
|
-
http_request(:get, "https://www.example.com/")
|
355
|
-
request(:get, "https://www.example.com:443/").should have_been_made
|
356
|
-
}.should_not raise_error
|
357
337
|
end
|
358
338
|
|
359
|
-
describe "
|
339
|
+
describe "replying raw responses from file" do
|
360
340
|
|
361
341
|
before(:each) do
|
362
|
-
|
363
|
-
stub_http_request(:
|
342
|
+
@file = File.new(File.expand_path(File.dirname(__FILE__)) + "/example_curl_output.txt")
|
343
|
+
stub_http_request(:get, "www.example.com").to_return(@file)
|
344
|
+
@response = http_request(:get, "http://www.example.com/")
|
364
345
|
end
|
365
346
|
|
366
|
-
it "should
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
347
|
+
it "should return recorded headers" do
|
348
|
+
@response.headers.should == {
|
349
|
+
"Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
|
350
|
+
"Content-Type"=>"text/html; charset=UTF-8",
|
351
|
+
"Content-Length"=>"438",
|
352
|
+
"Connection"=>"Keep-Alive"
|
353
|
+
}
|
371
354
|
end
|
372
355
|
|
373
|
-
it "should
|
374
|
-
|
375
|
-
http_request(:get, "http://www.example.com/?#{NOT_ESCAPED_PARAMS}")
|
376
|
-
request(:get, "http://www.example.com/?#{ESCAPED_PARAMS}").should have_been_made
|
377
|
-
}.should_not raise_error
|
356
|
+
it "should return recorded body" do
|
357
|
+
@response.body.size.should == 438
|
378
358
|
end
|
379
359
|
|
380
|
-
it "should
|
381
|
-
|
382
|
-
http_request(:get, "http://www.example.com/?#{ESCAPED_PARAMS}")
|
383
|
-
request(:get, /.*example.*/).should have_been_made
|
384
|
-
}.should_not raise_error
|
360
|
+
it "should ensure file is closed" do
|
361
|
+
@file.should be_closed
|
385
362
|
end
|
386
|
-
end
|
387
363
|
|
388
|
-
it "should fail if requested more times than expected" do
|
389
|
-
lambda {
|
390
|
-
http_request(:get, "http://www.example.com/")
|
391
|
-
http_request(:get, "http://www.example.com/")
|
392
|
-
request(:get, "http://www.example.com").should have_been_made
|
393
|
-
}.should fail_with("The request GET http://www.example.com/ was expected to execute 1 time but it executed 2 times")
|
394
364
|
end
|
395
365
|
|
396
|
-
|
397
|
-
lambda {
|
398
|
-
http_request(:get, "http://www.example.com/")
|
399
|
-
request(:get, "http://www.example.com").should have_been_made.twice
|
400
|
-
}.should fail_with("The request GET http://www.example.com/ was expected to execute 2 times but it executed 1 time")
|
401
|
-
end
|
402
|
-
|
403
|
-
it "should fail if requested less times than expected when 3 times expected" do
|
404
|
-
lambda {
|
405
|
-
http_request(:get, "http://www.example.com/")
|
406
|
-
request(:get, "http://www.example.com").should have_been_made.times(3)
|
407
|
-
}.should fail_with("The request GET http://www.example.com/ was expected to execute 3 times but it executed 1 time")
|
408
|
-
end
|
409
|
-
|
410
|
-
it "should succeed if request was executed with the same body" do
|
411
|
-
lambda {
|
412
|
-
http_request(:get, "http://www.example.com/", :body => "abc")
|
413
|
-
request(:get, "www.example.com").with(:body => "abc").should have_been_made
|
414
|
-
}.should_not raise_error
|
415
|
-
end
|
416
|
-
|
417
|
-
it "should fail if request was executed with different body" do
|
418
|
-
lambda {
|
419
|
-
http_request(:get, "http://www.example.com/", :body => "abc")
|
420
|
-
request(:get, "www.example.com").
|
421
|
-
with(:body => "def").should have_been_made
|
422
|
-
}.should fail_with("The request GET http://www.example.com/ with body 'def' was expected to execute 1 time but it executed 0 times")
|
423
|
-
end
|
366
|
+
describe "replying responses raw responses from string" do
|
424
367
|
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
}.should_not raise_error
|
431
|
-
end
|
432
|
-
|
433
|
-
it "should fail if request was executed with different headers" do
|
434
|
-
lambda {
|
435
|
-
http_request(:get, "http://www.example.com/", :headers => SAMPLE_HEADERS)
|
436
|
-
request(:get, "www.example.com").
|
437
|
-
with(:headers => { 'Content-Length' => '9999'}).should have_been_made
|
438
|
-
}.should fail_with("The request GET http://www.example.com/ with headers {'Content-Length'=>'9999'} was expected to execute 1 time but it executed 0 times")
|
439
|
-
end
|
368
|
+
before(:each) do
|
369
|
+
@input = File.new(File.expand_path(File.dirname(__FILE__)) + "/example_curl_output.txt").read
|
370
|
+
stub_http_request(:get, "www.example.com").to_return(@input)
|
371
|
+
@response = http_request(:get, "http://www.example.com/")
|
372
|
+
end
|
440
373
|
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
374
|
+
it "should return recorded headers" do
|
375
|
+
@response.headers.should == {
|
376
|
+
"Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
|
377
|
+
"Content-Type"=>"text/html; charset=UTF-8",
|
378
|
+
"Content-Length"=>"438",
|
379
|
+
"Connection"=>"Keep-Alive"
|
380
|
+
}
|
381
|
+
end
|
448
382
|
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
:headers => {'A' => 'a', 'B' => 'b'}
|
453
|
-
)
|
454
|
-
request(:get, "www.example.com").
|
455
|
-
with(:headers => {'A' => 'a'}).should have_been_made
|
456
|
-
}.should_not raise_error
|
457
|
-
end
|
383
|
+
it "should return recorded body" do
|
384
|
+
@response.body.size.should == 438
|
385
|
+
end
|
458
386
|
|
459
|
-
it "should succeed if request was executed with body and headers but they were not specified in expectantion" do
|
460
|
-
lambda {
|
461
|
-
http_request(:get, "http://www.example.com/",
|
462
|
-
:body => "abc",
|
463
|
-
:headers => SAMPLE_HEADERS
|
464
|
-
)
|
465
|
-
request(:get, "www.example.com").should have_been_made
|
466
|
-
}.should_not raise_error
|
467
387
|
end
|
468
388
|
|
389
|
+
describe "sequences of responses" do
|
469
390
|
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
391
|
+
it "should return responses one by one if declared in array" do
|
392
|
+
stub_http_request(:get, "www.example.com").to_return([ {:body => "1"}, {:body => "2"}, {:body => "3"} ])
|
393
|
+
http_request(:get, "http://www.example.com/").body.should == "1"
|
394
|
+
http_request(:get, "http://www.example.com/").body.should == "2"
|
395
|
+
http_request(:get, "http://www.example.com/").body.should == "3"
|
474
396
|
end
|
475
397
|
|
476
|
-
it "should
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
398
|
+
it "should repeat returning last declared response from a sequence after all responses were returned" do
|
399
|
+
stub_http_request(:get, "www.example.com").to_return([ {:body => "1"}, {:body => "2"} ])
|
400
|
+
http_request(:get, "http://www.example.com/").body.should == "1"
|
401
|
+
http_request(:get, "http://www.example.com/").body.should == "2"
|
402
|
+
http_request(:get, "http://www.example.com/").body.should == "2"
|
481
403
|
end
|
482
404
|
|
483
|
-
it "should
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
405
|
+
it "should return responses one by one if declared as comma separated params" do
|
406
|
+
stub_http_request(:get, "www.example.com").to_return({:body => "1"}, {:body => "2"}, {:body => "3"})
|
407
|
+
http_request(:get, "http://www.example.com/").body.should == "1"
|
408
|
+
http_request(:get, "http://www.example.com/").body.should == "2"
|
409
|
+
http_request(:get, "http://www.example.com/").body.should == "3"
|
488
410
|
end
|
489
411
|
|
490
|
-
it "should
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
412
|
+
it "should return responses one by one if declared with several to_return invokations" do
|
413
|
+
stub_http_request(:get, "www.example.com").
|
414
|
+
to_return({:body => "1"}).
|
415
|
+
to_return({:body => "2"}).
|
416
|
+
to_return({:body => "3"})
|
417
|
+
http_request(:get, "http://www.example.com/").body.should == "1"
|
418
|
+
http_request(:get, "http://www.example.com/").body.should == "2"
|
419
|
+
http_request(:get, "http://www.example.com/").body.should == "3"
|
495
420
|
end
|
496
421
|
|
497
|
-
it "should
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
422
|
+
it "should return responses one by one if declared with to_return invocations separated with then syntactic sugar" do
|
423
|
+
stub_http_request(:get, "www.example.com").to_return({:body => "1"}).then.
|
424
|
+
to_return({:body => "2"}).then.to_return({:body => "3"})
|
425
|
+
http_request(:get, "http://www.example.com/").body.should == "1"
|
426
|
+
http_request(:get, "http://www.example.com/").body.should == "2"
|
427
|
+
http_request(:get, "http://www.example.com/").body.should == "3"
|
502
428
|
end
|
503
429
|
|
504
430
|
end
|
505
431
|
|
506
|
-
describe "
|
507
|
-
|
508
|
-
it "should
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
432
|
+
describe "repeating declared responses more than once" do
|
433
|
+
|
434
|
+
it "should repeat one response declared number of times" do
|
435
|
+
stub_http_request(:get, "www.example.com").
|
436
|
+
to_return({:body => "1"}).times(2).
|
437
|
+
to_return({:body => "2"})
|
438
|
+
http_request(:get, "http://www.example.com/").body.should == "1"
|
439
|
+
http_request(:get, "http://www.example.com/").body.should == "1"
|
440
|
+
http_request(:get, "http://www.example.com/").body.should == "2"
|
513
441
|
end
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
442
|
+
|
443
|
+
|
444
|
+
it "should repeat sequence of response declared number of times" do
|
445
|
+
stub_http_request(:get, "www.example.com").
|
446
|
+
to_return({:body => "1"}, {:body => "2"}).times(2).
|
447
|
+
to_return({:body => "3"})
|
448
|
+
http_request(:get, "http://www.example.com/").body.should == "1"
|
449
|
+
http_request(:get, "http://www.example.com/").body.should == "2"
|
450
|
+
http_request(:get, "http://www.example.com/").body.should == "1"
|
451
|
+
http_request(:get, "http://www.example.com/").body.should == "2"
|
452
|
+
http_request(:get, "http://www.example.com/").body.should == "3"
|
520
453
|
end
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
454
|
+
|
455
|
+
|
456
|
+
it "should repeat infinitely last response even if number of declared times is lower" do
|
457
|
+
stub_http_request(:get, "www.example.com").
|
458
|
+
to_return({:body => "1"}).times(2)
|
459
|
+
http_request(:get, "http://www.example.com/").body.should == "1"
|
460
|
+
http_request(:get, "http://www.example.com/").body.should == "1"
|
461
|
+
http_request(:get, "http://www.example.com/").body.should == "1"
|
527
462
|
end
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
describe "using assert_requested" do
|
533
|
-
|
534
|
-
it "should verify expected requests occured" do
|
463
|
+
|
464
|
+
it "should give error if times is declared without specifying response" do
|
535
465
|
lambda {
|
536
|
-
|
537
|
-
|
538
|
-
assert_requested(:get, "http://www.example.com")
|
539
|
-
}.should_not raise_error
|
466
|
+
stub_http_request(:get, "www.example.com").times(3)
|
467
|
+
}.should raise_error("Invalid WebMock stub declaration. times(N) can be declared only after response declaration.")
|
540
468
|
end
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
469
|
+
|
470
|
+
end
|
471
|
+
|
472
|
+
describe "raising declared exceptions more than once" do
|
473
|
+
|
474
|
+
it "should repeat raising exception declared number of times" do
|
475
|
+
stub_http_request(:get, "www.example.com").
|
476
|
+
to_raise(MyException).times(2).
|
477
|
+
to_return({:body => "2"})
|
478
|
+
lambda {
|
479
|
+
http_request(:get, "http://www.example.com/")
|
480
|
+
}.should raise_error(MyException, "Exception from WebMock")
|
481
|
+
lambda {
|
482
|
+
http_request(:get, "http://www.example.com/")
|
483
|
+
}.should raise_error(MyException, "Exception from WebMock")
|
484
|
+
http_request(:get, "http://www.example.com/").body.should == "2"
|
547
485
|
end
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
486
|
+
|
487
|
+
it "should repeat raising sequence of exceptions declared number of times" do
|
488
|
+
stub_http_request(:get, "www.example.com").
|
489
|
+
to_raise(MyException, ArgumentError).times(2).
|
490
|
+
to_return({:body => "2"})
|
491
|
+
lambda {
|
492
|
+
http_request(:get, "http://www.example.com/")
|
493
|
+
}.should raise_error(MyException, "Exception from WebMock")
|
494
|
+
lambda {
|
495
|
+
http_request(:get, "http://www.example.com/")
|
496
|
+
}.should raise_error(ArgumentError)
|
497
|
+
lambda {
|
498
|
+
http_request(:get, "http://www.example.com/")
|
499
|
+
}.should raise_error(MyException, "Exception from WebMock")
|
500
|
+
lambda {
|
501
|
+
http_request(:get, "http://www.example.com/")
|
502
|
+
}.should raise_error(ArgumentError)
|
503
|
+
http_request(:get, "http://www.example.com/").body.should == "2"
|
554
504
|
end
|
555
505
|
end
|
556
506
|
end
|
557
507
|
|
508
|
+
describe "precedence of stubs" do
|
558
509
|
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
510
|
+
it "should use the last declared matching request stub" do
|
511
|
+
stub_http_request(:get, "www.example.com").to_return(:body => "abc")
|
512
|
+
stub_http_request(:get, "www.example.com").to_return(:body => "def")
|
513
|
+
http_request(:get, "http://www.example.com/").body.should == "def"
|
514
|
+
end
|
563
515
|
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
}.should_not raise_error
|
570
|
-
end
|
516
|
+
it "should not be affected by the type of uri or request method" do
|
517
|
+
stub_http_request(:get, "www.example.com").to_return(:body => "abc")
|
518
|
+
stub_http_request(:any, /.*example.*/).to_return(:body => "def")
|
519
|
+
http_request(:get, "http://www.example.com/").body.should == "def"
|
520
|
+
end
|
571
521
|
|
572
|
-
|
573
|
-
lambda {
|
574
|
-
http_request(:get, "http://www.example.com/")
|
575
|
-
request(:get, "http://www.example.com").should_not have_been_made
|
576
|
-
}.should fail_with("The request GET http://www.example.com/ was expected to execute 0 times but it executed 1 time")
|
577
|
-
end
|
578
|
-
end
|
522
|
+
end
|
579
523
|
|
580
|
-
|
524
|
+
describe "verification of request expectation" do
|
525
|
+
|
526
|
+
describe "when net connect not allowed" do
|
527
|
+
|
528
|
+
before(:each) do
|
529
|
+
WebMock.disable_net_connect!
|
530
|
+
stub_http_request(:any, "http://www.example.com")
|
531
|
+
stub_http_request(:any, "https://www.example.com")
|
532
|
+
end
|
533
|
+
|
534
|
+
it "should pass if request was executed with the same uri and method" do
|
535
|
+
lambda {
|
536
|
+
http_request(:get, "http://www.example.com/")
|
537
|
+
request(:get, "http://www.example.com").should have_been_made.once
|
538
|
+
}.should_not raise_error
|
539
|
+
end
|
540
|
+
|
541
|
+
it "should pass if request was not expected and not executed" do
|
542
|
+
lambda {
|
543
|
+
request(:get, "http://www.example.com").should_not have_been_made
|
544
|
+
}.should_not raise_error
|
545
|
+
end
|
546
|
+
|
547
|
+
it "should fail if request was not expected but executed" do
|
548
|
+
lambda {
|
549
|
+
http_request(:get, "http://www.example.com/")
|
550
|
+
request(:get, "http://www.example.com").should_not have_been_made
|
551
|
+
}.should fail_with("The request GET http://www.example.com/ was expected to execute 0 times but it executed 1 time")
|
552
|
+
end
|
553
|
+
|
554
|
+
|
555
|
+
it "should fail if request was not executed" do
|
556
|
+
lambda {
|
557
|
+
request(:get, "http://www.example.com").should have_been_made
|
558
|
+
}.should fail_with("The request GET http://www.example.com/ was expected to execute 1 time but it executed 0 times")
|
559
|
+
end
|
560
|
+
|
561
|
+
it "should fail if request was executed to different uri" do
|
562
|
+
lambda {
|
563
|
+
http_request(:get, "http://www.example.com/")
|
564
|
+
request(:get, "http://www.example.org").should have_been_made
|
565
|
+
}.should fail_with("The request GET http://www.example.org/ was expected to execute 1 time but it executed 0 times")
|
566
|
+
end
|
567
|
+
|
568
|
+
it "should fail if request was executed with different method" do
|
569
|
+
lambda {
|
570
|
+
http_request(:post, "http://www.example.com/")
|
571
|
+
request(:get, "http://www.example.com").should have_been_made
|
572
|
+
}.should fail_with("The request GET http://www.example.com/ was expected to execute 1 time but it executed 0 times")
|
573
|
+
end
|
574
|
+
|
575
|
+
it "should pass if request was executed with different form of uri" do
|
576
|
+
lambda {
|
577
|
+
http_request(:get, "http://www.example.com/")
|
578
|
+
request(:get, "www.example.com").should have_been_made
|
579
|
+
}.should_not raise_error
|
580
|
+
end
|
581
|
+
|
582
|
+
it "should pass if request was executed with different form of uri without port " do
|
583
|
+
lambda {
|
584
|
+
http_request(:get, "http://www.example.com/")
|
585
|
+
request(:get, "www.example.com:80").should have_been_made
|
586
|
+
}.should_not raise_error
|
587
|
+
end
|
588
|
+
|
589
|
+
it "should pass if request was executed with different form of uri with port" do
|
590
|
+
lambda {
|
591
|
+
http_request(:get, "http://www.example.com/")
|
592
|
+
request(:get, "www.example.com:80").should have_been_made
|
593
|
+
}.should_not raise_error
|
594
|
+
end
|
595
|
+
|
596
|
+
it "should fail if request was executed with different port" do
|
597
|
+
lambda {
|
598
|
+
http_request(:get, "http://www.example.com:80/")
|
599
|
+
request(:get, "www.example.com:90").should have_been_made
|
600
|
+
}.should fail_with("The request GET http://www.example.com:90/ was expected to execute 1 time but it executed 0 times")
|
601
|
+
end
|
602
|
+
|
603
|
+
it "should pass if request was executed with different form of uri with https port" do
|
604
|
+
lambda {
|
605
|
+
http_request(:get, "https://www.example.com/")
|
606
|
+
request(:get, "https://www.example.com:443/").should have_been_made
|
607
|
+
}.should_not raise_error
|
608
|
+
end
|
609
|
+
|
610
|
+
describe "when matching requests with escaped uris" do
|
611
|
+
|
612
|
+
before(:each) do
|
613
|
+
WebMock.disable_net_connect!
|
614
|
+
stub_http_request(:any, "http://www.example.com/?#{NOT_ESCAPED_PARAMS}")
|
615
|
+
end
|
616
|
+
|
617
|
+
it "should pass if request was executed with escaped params" do
|
618
|
+
lambda {
|
619
|
+
http_request(:get, "http://www.example.com/?#{ESCAPED_PARAMS}")
|
620
|
+
request(:get, "http://www.example.com/?#{NOT_ESCAPED_PARAMS}").should have_been_made
|
621
|
+
}.should_not raise_error
|
622
|
+
end
|
623
|
+
|
624
|
+
it "should pass if request was executed with non escaped params but escaped expected" do
|
625
|
+
lambda {
|
626
|
+
http_request(:get, "http://www.example.com/?#{NOT_ESCAPED_PARAMS}")
|
627
|
+
request(:get, "http://www.example.com/?#{ESCAPED_PARAMS}").should have_been_made
|
628
|
+
}.should_not raise_error
|
629
|
+
end
|
630
|
+
|
631
|
+
it "should pass if request was executed with escaped params but uri matichg regexp expected" do
|
632
|
+
lambda {
|
633
|
+
http_request(:get, "http://www.example.com/?#{ESCAPED_PARAMS}")
|
634
|
+
request(:get, /.*example.*/).should have_been_made
|
635
|
+
}.should_not raise_error
|
636
|
+
end
|
637
|
+
end
|
638
|
+
|
639
|
+
it "should fail if requested more times than expected" do
|
640
|
+
lambda {
|
641
|
+
http_request(:get, "http://www.example.com/")
|
642
|
+
http_request(:get, "http://www.example.com/")
|
643
|
+
request(:get, "http://www.example.com").should have_been_made
|
644
|
+
}.should fail_with("The request GET http://www.example.com/ was expected to execute 1 time but it executed 2 times")
|
645
|
+
end
|
646
|
+
|
647
|
+
it "should fail if requested less times than expected" do
|
648
|
+
lambda {
|
649
|
+
http_request(:get, "http://www.example.com/")
|
650
|
+
request(:get, "http://www.example.com").should have_been_made.twice
|
651
|
+
}.should fail_with("The request GET http://www.example.com/ was expected to execute 2 times but it executed 1 time")
|
652
|
+
end
|
653
|
+
|
654
|
+
it "should fail if requested less times than expected when 3 times expected" do
|
655
|
+
lambda {
|
656
|
+
http_request(:get, "http://www.example.com/")
|
657
|
+
request(:get, "http://www.example.com").should have_been_made.times(3)
|
658
|
+
}.should fail_with("The request GET http://www.example.com/ was expected to execute 3 times but it executed 1 time")
|
659
|
+
end
|
660
|
+
|
661
|
+
it "should succeed if request was executed with the same body" do
|
662
|
+
lambda {
|
663
|
+
http_request(:get, "http://www.example.com/", :body => "abc")
|
664
|
+
request(:get, "www.example.com").with(:body => "abc").should have_been_made
|
665
|
+
}.should_not raise_error
|
666
|
+
end
|
667
|
+
|
668
|
+
it "should fail if request was executed with different body" do
|
669
|
+
lambda {
|
670
|
+
http_request(:get, "http://www.example.com/", :body => "abc")
|
671
|
+
request(:get, "www.example.com").
|
672
|
+
with(:body => "def").should have_been_made
|
673
|
+
}.should fail_with("The request GET http://www.example.com/ with body 'def' was expected to execute 1 time but it executed 0 times")
|
674
|
+
end
|
675
|
+
|
676
|
+
it "should succeed if request was executed with the same body" do
|
677
|
+
lambda {
|
678
|
+
http_request(:get, "http://www.example.com/", :body => "abc")
|
679
|
+
request(:get, "www.example.com").with(:body => /^abc$/).should have_been_made
|
680
|
+
}.should_not raise_error
|
681
|
+
end
|
682
|
+
|
683
|
+
it "should fail if request was executed with different body" do
|
684
|
+
lambda {
|
685
|
+
http_request(:get, "http://www.example.com/", :body => /^abc/)
|
686
|
+
request(:get, "www.example.com").
|
687
|
+
with(:body => "xabc").should have_been_made
|
688
|
+
}.should fail_with("The request GET http://www.example.com/ with body 'xabc' was expected to execute 1 time but it executed 0 times")
|
689
|
+
end
|
690
|
+
|
691
|
+
it "should succeed if request was executed with the same headers" do
|
692
|
+
lambda {
|
693
|
+
http_request(:get, "http://www.example.com/", :headers => SAMPLE_HEADERS)
|
694
|
+
request(:get, "www.example.com").
|
695
|
+
with(:headers => SAMPLE_HEADERS).should have_been_made
|
696
|
+
}.should_not raise_error
|
697
|
+
end
|
698
|
+
|
699
|
+
it "should fail if request was executed with different headers" do
|
700
|
+
lambda {
|
701
|
+
http_request(:get, "http://www.example.com/", :headers => SAMPLE_HEADERS)
|
702
|
+
request(:get, "www.example.com").
|
703
|
+
with(:headers => { 'Content-Length' => '9999'}).should have_been_made
|
704
|
+
}.should fail_with("The request GET http://www.example.com/ with headers {'Content-Length'=>'9999'} was expected to execute 1 time but it executed 0 times")
|
705
|
+
end
|
706
|
+
|
707
|
+
it "should fail if request was executed with less headers" do
|
708
|
+
lambda {
|
709
|
+
http_request(:get, "http://www.example.com/", :headers => {'A' => 'a'})
|
710
|
+
request(:get, "www.example.com").
|
711
|
+
with(:headers => {'A' => 'a', 'B' => 'b'}).should have_been_made
|
712
|
+
}.should fail_with("The request GET http://www.example.com/ with headers {'A'=>'a', 'B'=>'b'} was expected to execute 1 time but it executed 0 times")
|
713
|
+
end
|
714
|
+
|
715
|
+
it "should succeed if request was executed with more headers" do
|
716
|
+
lambda {
|
717
|
+
http_request(:get, "http://www.example.com/",
|
718
|
+
:headers => {'A' => 'a', 'B' => 'b'}
|
719
|
+
)
|
720
|
+
request(:get, "www.example.com").
|
721
|
+
with(:headers => {'A' => 'a'}).should have_been_made
|
722
|
+
}.should_not raise_error
|
723
|
+
end
|
724
|
+
|
725
|
+
it "should succeed if request was executed with body and headers but they were not specified in expectantion" do
|
726
|
+
lambda {
|
727
|
+
http_request(:get, "http://www.example.com/",
|
728
|
+
:body => "abc",
|
729
|
+
:headers => SAMPLE_HEADERS
|
730
|
+
)
|
731
|
+
request(:get, "www.example.com").should have_been_made
|
732
|
+
}.should_not raise_error
|
733
|
+
end
|
734
|
+
|
735
|
+
it "should succeed if request was executed with headers matching regular expressions" do
|
736
|
+
lambda {
|
737
|
+
http_request(:get, "http://www.example.com/", :headers => { 'user-agent' => 'MyAppName' })
|
738
|
+
request(:get, "www.example.com").
|
739
|
+
with(:headers => { :user_agent => /^MyAppName$/ }).should have_been_made
|
740
|
+
}.should_not raise_error
|
741
|
+
end
|
742
|
+
|
743
|
+
it "should fail if request was executed with headers not matching regular expression" do
|
744
|
+
lambda {
|
745
|
+
http_request(:get, "http://www.example.com/", :headers => { 'user_agent' => 'xMyAppName' })
|
746
|
+
request(:get, "www.example.com").
|
747
|
+
with(:headers => { :user_agent => /^MyAppName$/ }).should have_been_made
|
748
|
+
}.should fail_with("The request GET http://www.example.com/ with headers {'User-Agent'=>/^MyAppName$/} was expected to execute 1 time but it executed 0 times")
|
749
|
+
end
|
750
|
+
|
751
|
+
it "should suceed if request was executed and block evaluated to true" do
|
752
|
+
lambda {
|
753
|
+
http_request(:post, "http://www.example.com/", :body => "wadus")
|
754
|
+
request(:post, "www.example.com").with { |req| req.body == "wadus" }.should have_been_made
|
755
|
+
}.should_not raise_error
|
756
|
+
end
|
757
|
+
|
758
|
+
it "should fail if request was executed and block evaluated to false" do
|
759
|
+
lambda {
|
760
|
+
http_request(:post, "http://www.example.com/")
|
761
|
+
request(:post, "www.example.com").with { |req| req.body == "wadus" }.should have_been_made
|
762
|
+
}.should fail_with("The request POST http://www.example.com/ with given block was expected to execute 1 time but it executed 0 times")
|
763
|
+
end
|
764
|
+
|
765
|
+
it "should fail if request was not expected but it executed and block matched request" do
|
766
|
+
lambda {
|
767
|
+
http_request(:post, "http://www.example.com/", :body => "wadus")
|
768
|
+
request(:post, "www.example.com").with { |req| req.body == "wadus" }.should_not have_been_made
|
769
|
+
}.should fail_with("The request POST http://www.example.com/ with given block was expected to execute 0 times but it executed 1 time")
|
770
|
+
end
|
771
|
+
|
772
|
+
describe "with authentication" do
|
773
|
+
before(:each) do
|
774
|
+
stub_http_request(:any, "http://user:pass@www.example.com")
|
775
|
+
stub_http_request(:any, "http://user:pazz@www.example.com")
|
776
|
+
end
|
777
|
+
|
778
|
+
it "should succeed if succeed if request was executed with expected credentials" do
|
779
|
+
lambda {
|
780
|
+
http_request(:get, "http://user:pass@www.example.com/")
|
781
|
+
request(:get, "http://user:pass@www.example.com").should have_been_made.once
|
782
|
+
}.should_not raise_error
|
783
|
+
end
|
784
|
+
|
785
|
+
it "should fail if request was executed with different credentials than expected" do
|
786
|
+
lambda {
|
787
|
+
http_request(:get, "http://user:pass@www.example.com/")
|
788
|
+
request(:get, "http://user:pazz@www.example.com").should have_been_made.once
|
789
|
+
}.should fail_with("The request GET http://user:pazz@www.example.com/ was expected to execute 1 time but it executed 0 times")
|
790
|
+
end
|
791
|
+
|
792
|
+
it "should fail if request was executed without credentials but credentials were expected" do
|
793
|
+
lambda {
|
794
|
+
http_request(:get, "http://www.example.com/")
|
795
|
+
request(:get, "http://user:pass@www.example.com").should have_been_made.once
|
796
|
+
}.should fail_with("The request GET http://user:pass@www.example.com/ was expected to execute 1 time but it executed 0 times")
|
797
|
+
end
|
798
|
+
|
799
|
+
it "should fail if request was executed with credentials but expected without" do
|
800
|
+
lambda {
|
801
|
+
http_request(:get, "http://user:pass@www.example.com/")
|
802
|
+
request(:get, "http://www.example.com").should have_been_made.once
|
803
|
+
}.should fail_with("The request GET http://www.example.com/ was expected to execute 1 time but it executed 0 times")
|
804
|
+
end
|
805
|
+
|
806
|
+
end
|
807
|
+
|
808
|
+
describe "using webmock matcher" do
|
809
|
+
|
810
|
+
it "should verify expected requests occured" do
|
811
|
+
lambda {
|
812
|
+
http_request(:get, "http://www.example.com/")
|
813
|
+
WebMock.should have_requested(:get, "http://www.example.com").once
|
814
|
+
}.should_not raise_error
|
815
|
+
end
|
816
|
+
|
817
|
+
it "should verify expected requests occured" do
|
818
|
+
lambda {
|
819
|
+
http_request(:get, "http://www.example.com/", :body => "abc", :headers => {'A' => 'a'})
|
820
|
+
WebMock.should have_requested(:get, "http://www.example.com").with(:body => "abc", :headers => {'A' => 'a'}).once
|
821
|
+
}.should_not raise_error
|
822
|
+
end
|
823
|
+
|
824
|
+
it "should verify that non expected requests didn't occur" do
|
825
|
+
lambda {
|
826
|
+
http_request(:get, "http://www.example.com/")
|
827
|
+
WebMock.should_not have_requested(:get, "http://www.example.com")
|
828
|
+
}.should fail_with("The request GET http://www.example.com/ was expected to execute 0 times but it executed 1 time")
|
829
|
+
end
|
830
|
+
|
831
|
+
it "should succeed if request was executed and block evaluated to true" do
|
832
|
+
lambda {
|
833
|
+
http_request(:post, "http://www.example.com/", :body => "wadus")
|
834
|
+
WebMock.should have_requested(:post, "www.example.com").with { |req| req.body == "wadus" }
|
835
|
+
}.should_not raise_error
|
836
|
+
end
|
837
|
+
|
838
|
+
it "should fail if request was executed and block evaluated to false" do
|
839
|
+
lambda {
|
840
|
+
http_request(:post, "http://www.example.com/")
|
841
|
+
WebMock.should have_requested(:post, "www.example.com").with { |req| req.body == "wadus" }
|
842
|
+
}.should fail_with("The request POST http://www.example.com/ with given block was expected to execute 1 time but it executed 0 times")
|
843
|
+
end
|
844
|
+
|
845
|
+
it "should fail if request was not expected but executed and block matched request" do
|
846
|
+
lambda {
|
847
|
+
http_request(:post, "http://www.example.com/", :body => "wadus")
|
848
|
+
WebMock.should_not have_requested(:post, "www.example.com").with { |req| req.body == "wadus" }
|
849
|
+
}.should fail_with("The request POST http://www.example.com/ with given block was expected to execute 0 times but it executed 1 time")
|
850
|
+
end
|
851
|
+
end
|
852
|
+
|
853
|
+
|
854
|
+
|
855
|
+
describe "using assert_requested" do
|
856
|
+
|
857
|
+
it "should verify expected requests occured" do
|
858
|
+
lambda {
|
859
|
+
http_request(:get, "http://www.example.com/")
|
860
|
+
assert_requested(:get, "http://www.example.com", :times => 1)
|
861
|
+
assert_requested(:get, "http://www.example.com")
|
862
|
+
}.should_not raise_error
|
863
|
+
end
|
864
|
+
|
865
|
+
it "should verify expected requests occured" do
|
866
|
+
lambda {
|
867
|
+
http_request(:get, "http://www.example.com/", :body => "abc", :headers => {'A' => 'a'})
|
868
|
+
assert_requested(:get, "http://www.example.com", :body => "abc", :headers => {'A' => 'a'})
|
869
|
+
}.should_not raise_error
|
870
|
+
end
|
871
|
+
|
872
|
+
it "should verify that non expected requests didn't occur" do
|
873
|
+
lambda {
|
874
|
+
http_request(:get, "http://www.example.com/")
|
875
|
+
assert_not_requested(:get, "http://www.example.com")
|
876
|
+
}.should fail_with("The request GET http://www.example.com/ was expected to execute 0 times but it executed 1 time")
|
877
|
+
end
|
878
|
+
|
879
|
+
it "should verify if non expected request executed and block evaluated to true" do
|
880
|
+
lambda {
|
881
|
+
http_request(:post, "http://www.example.com/", :body => "wadus")
|
882
|
+
assert_not_requested(:post, "www.example.com") { |req| req.body == "wadus" }
|
883
|
+
}.should fail_with("The request POST http://www.example.com/ with given block was expected to execute 0 times but it executed 1 time")
|
884
|
+
end
|
885
|
+
|
886
|
+
it "should verify if request was executed and block evaluated to true" do
|
887
|
+
lambda {
|
888
|
+
http_request(:post, "http://www.example.com/", :body => "wadus")
|
889
|
+
assert_requested(:post, "www.example.com") { |req| req.body == "wadus" }
|
890
|
+
}.should_not raise_error
|
891
|
+
end
|
892
|
+
|
893
|
+
it "should verify if request was executed and block evaluated to false" do
|
894
|
+
lambda {
|
895
|
+
http_request(:post, "http://www.example.com/")
|
896
|
+
assert_requested(:post, "www.example.com") { |req| req.body == "wadus" }
|
897
|
+
}.should fail_with("The request POST http://www.example.com/ with given block was expected to execute 1 time but it executed 0 times")
|
898
|
+
end
|
899
|
+
end
|
900
|
+
end
|
901
|
+
|
902
|
+
|
903
|
+
describe "when net connect allowed" do
|
904
|
+
before(:each) do
|
905
|
+
WebMock.allow_net_connect!
|
906
|
+
end
|
907
|
+
|
908
|
+
it "should verify expected requests occured" do
|
909
|
+
setup_expectations_for_real_example_com_request
|
910
|
+
lambda {
|
911
|
+
http_request(:get, "http://www.example.com/")
|
912
|
+
request(:get, "http://www.example.com").should have_been_made
|
913
|
+
}.should_not raise_error
|
914
|
+
end
|
915
|
+
|
916
|
+
it "should verify that non expected requests didn't occur" do
|
917
|
+
lambda {
|
918
|
+
http_request(:get, "http://www.example.com/")
|
919
|
+
request(:get, "http://www.example.com").should_not have_been_made
|
920
|
+
}.should fail_with("The request GET http://www.example.com/ was expected to execute 0 times but it executed 1 time")
|
921
|
+
end
|
922
|
+
end
|
581
923
|
|
582
|
-
|
924
|
+
end
|
925
|
+
|
926
|
+
end
|