webmock 0.7.3 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +15 -0
- data/README.md +80 -68
- data/Rakefile +1 -0
- data/VERSION +1 -1
- data/lib/webmock.rb +2 -0
- data/lib/webmock/adapters/rspec/webmock_matcher.rb +1 -4
- data/lib/webmock/http_lib_adapters/httpclient.rb +96 -0
- data/lib/webmock/http_lib_adapters/net_http.rb +4 -7
- data/lib/webmock/request.rb +29 -0
- data/lib/webmock/request_profile.rb +28 -15
- data/lib/webmock/request_registry.rb +12 -2
- data/lib/webmock/request_signature.rb +2 -1
- data/lib/webmock/request_stub.rb +1 -2
- data/lib/webmock/response.rb +18 -6
- data/lib/webmock/util/uri.rb +2 -2
- data/lib/webmock/webmock.rb +2 -2
- data/spec/httpclient_spec.rb +36 -0
- data/spec/httpclient_spec_helper.rb +57 -0
- data/spec/net_http_spec.rb +15 -24
- data/spec/net_http_spec_helper.rb +53 -0
- data/spec/request_execution_verifier_spec.rb +5 -5
- data/spec/request_profile_spec.rb +17 -10
- data/spec/request_registry_spec.rb +15 -15
- data/spec/request_signature_spec.rb +71 -56
- data/spec/request_stub_spec.rb +3 -3
- data/spec/response_spec.rb +7 -1
- data/spec/spec_helper.rb +5 -33
- data/spec/util/uri_spec.rb +67 -40
- data/spec/webmock_spec.rb +179 -164
- data/test/test_webmock.rb +25 -19
- data/webmock.gemspec +14 -3
- metadata +20 -2
data/spec/util/uri_spec.rb
CHANGED
@@ -3,62 +3,75 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
|
3
3
|
|
4
4
|
URIS_WITHOUT_PATH_OR_PARAMS =
|
5
5
|
[
|
6
|
-
"www.
|
7
|
-
"www.
|
8
|
-
"www.
|
9
|
-
"www.
|
10
|
-
"http://www.
|
11
|
-
"http://www.
|
12
|
-
"http://www.
|
13
|
-
"http://www.
|
6
|
+
"www.example.com",
|
7
|
+
"www.example.com/",
|
8
|
+
"www.example.com:80",
|
9
|
+
"www.example.com:80/",
|
10
|
+
"http://www.example.com",
|
11
|
+
"http://www.example.com/",
|
12
|
+
"http://www.example.com:80",
|
13
|
+
"http://www.example.com:80/"
|
14
|
+
].sort
|
15
|
+
|
16
|
+
URIS_WITHOUT_PATH_BUT_WITH_PARAMS =
|
17
|
+
[
|
18
|
+
"www.example.com?a=b",
|
19
|
+
"www.example.com/?a=b",
|
20
|
+
"www.example.com:80?a=b",
|
21
|
+
"www.example.com:80/?a=b",
|
22
|
+
"http://www.example.com?a=b",
|
23
|
+
"http://www.example.com/?a=b",
|
24
|
+
"http://www.example.com:80?a=b",
|
25
|
+
"http://www.example.com:80/?a=b"
|
14
26
|
].sort
|
15
27
|
|
16
28
|
URIS_WITH_AUTH =
|
17
29
|
[
|
18
|
-
"a b:pass@www.
|
19
|
-
"a b:pass@www.
|
20
|
-
"a b:pass@www.
|
21
|
-
"a b:pass@www.
|
22
|
-
"http://a b:pass@www.
|
23
|
-
"http://a b:pass@www.
|
24
|
-
"http://a b:pass@www.
|
25
|
-
"http://a b:pass@www.
|
26
|
-
"a%20b:pass@www.
|
27
|
-
"a%20b:pass@www.
|
28
|
-
"a%20b:pass@www.
|
29
|
-
"a%20b:pass@www.
|
30
|
-
"http://a%20b:pass@www.
|
31
|
-
"http://a%20b:pass@www.
|
32
|
-
"http://a%20b:pass@www.
|
33
|
-
"http://a%20b:pass@www.
|
30
|
+
"a b:pass@www.example.com",
|
31
|
+
"a b:pass@www.example.com/",
|
32
|
+
"a b:pass@www.example.com:80",
|
33
|
+
"a b:pass@www.example.com:80/",
|
34
|
+
"http://a b:pass@www.example.com",
|
35
|
+
"http://a b:pass@www.example.com/",
|
36
|
+
"http://a b:pass@www.example.com:80",
|
37
|
+
"http://a b:pass@www.example.com:80/",
|
38
|
+
"a%20b:pass@www.example.com",
|
39
|
+
"a%20b:pass@www.example.com/",
|
40
|
+
"a%20b:pass@www.example.com:80",
|
41
|
+
"a%20b:pass@www.example.com:80/",
|
42
|
+
"http://a%20b:pass@www.example.com",
|
43
|
+
"http://a%20b:pass@www.example.com/",
|
44
|
+
"http://a%20b:pass@www.example.com:80",
|
45
|
+
"http://a%20b:pass@www.example.com:80/"
|
34
46
|
].sort
|
35
47
|
|
36
48
|
URIS_WITH_PATH_AND_PARAMS =
|
37
49
|
[
|
38
|
-
"www.
|
39
|
-
"www.
|
40
|
-
"www.
|
41
|
-
"www.
|
42
|
-
"http://www.
|
43
|
-
"http://www.
|
44
|
-
"http://www.
|
45
|
-
"http://www.
|
50
|
+
"www.example.com/my path/?a=my param&b=c",
|
51
|
+
"www.example.com/my%20path/?a=my%20param&b=c",
|
52
|
+
"www.example.com:80/my path/?a=my param&b=c",
|
53
|
+
"www.example.com:80/my%20path/?a=my%20param&b=c",
|
54
|
+
"http://www.example.com/my path/?a=my param&b=c",
|
55
|
+
"http://www.example.com/my%20path/?a=my%20param&b=c",
|
56
|
+
"http://www.example.com:80/my path/?a=my param&b=c",
|
57
|
+
"http://www.example.com:80/my%20path/?a=my%20param&b=c",
|
46
58
|
].sort
|
47
59
|
|
48
60
|
URIS_WITH_DIFFERENT_PORT =
|
49
61
|
[
|
50
|
-
"www.
|
51
|
-
"www.
|
52
|
-
"http://www.
|
53
|
-
"http://www.
|
62
|
+
"www.example.com:88",
|
63
|
+
"www.example.com:88/",
|
64
|
+
"http://www.example.com:88",
|
65
|
+
"http://www.example.com:88/"
|
54
66
|
].sort
|
55
67
|
|
68
|
+
|
56
69
|
URIS_FOR_HTTPS =
|
57
70
|
[
|
58
|
-
"https://www.
|
59
|
-
"https://www.
|
60
|
-
"https://www.
|
61
|
-
"https://www.
|
71
|
+
"https://www.example.com",
|
72
|
+
"https://www.example.com/",
|
73
|
+
"https://www.example.com:443",
|
74
|
+
"https://www.example.com:443/"
|
62
75
|
].sort
|
63
76
|
|
64
77
|
|
@@ -72,6 +85,12 @@ describe WebMock::Util::URI do
|
|
72
85
|
end
|
73
86
|
end
|
74
87
|
|
88
|
+
it "should find all variations of the same uri for all variations of uri with params but without path" do
|
89
|
+
URIS_WITHOUT_PATH_BUT_WITH_PARAMS.each do |uri|
|
90
|
+
WebMock::Util::URI.variations_of_uri_as_strings(uri).sort.should == URIS_WITHOUT_PATH_BUT_WITH_PARAMS
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
75
94
|
it "should find all variations of the same uri for all variations of uri without params or path" do
|
76
95
|
URIS_WITHOUT_PATH_OR_PARAMS.each do |uri|
|
77
96
|
WebMock::Util::URI.variations_of_uri_as_strings(uri).sort.should == URIS_WITHOUT_PATH_OR_PARAMS
|
@@ -108,6 +127,14 @@ describe WebMock::Util::URI do
|
|
108
127
|
end
|
109
128
|
end
|
110
129
|
|
130
|
+
it "should successfully compare all variations of the same uri with path but with params" do
|
131
|
+
URIS_WITHOUT_PATH_BUT_WITH_PARAMS.each do |uri_a|
|
132
|
+
URIS_WITHOUT_PATH_BUT_WITH_PARAMS.each do |uri_b|
|
133
|
+
WebMock::Util::URI.normalize_uri(uri_a).should === WebMock::Util::URI.normalize_uri(uri_b)
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
111
138
|
it "should successfully compare all variations of the same uri without path or params" do
|
112
139
|
URIS_WITHOUT_PATH_OR_PARAMS.each do |uri_a|
|
113
140
|
URIS_WITHOUT_PATH_OR_PARAMS.each do |uri_b|
|
data/spec/webmock_spec.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
unless defined? SAMPLE_HEADERS
|
4
|
+
SAMPLE_HEADERS = { "Content-Length" => "8888" }
|
5
|
+
ESCAPED_PARAMS = "x=ab%2Bc&z=%27Stop%21%27%20said%20Fred"
|
6
|
+
NOT_ESCAPED_PARAMS = "z='Stop!' said Fred&x=ab c"
|
7
|
+
end
|
6
8
|
|
7
9
|
describe "WebMock", :shared => true do
|
8
10
|
before(:each) do
|
@@ -18,14 +20,20 @@ describe "WebMock", :shared => true do
|
|
18
20
|
end
|
19
21
|
|
20
22
|
it "should make a real web request if request is not stubbed" do
|
21
|
-
|
22
|
-
http_request(:get, "http://www.
|
23
|
+
setup_expectations_for_real_example_com_request
|
24
|
+
http_request(:get, "http://www.example.com/").
|
25
|
+
body.should =~ /.*Google fake response.*/
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should make a real https request if request is not stubbed" do
|
29
|
+
setup_expectations_for_real_example_com_request(:port => 443)
|
30
|
+
http_request(:get, "https://www.example.com/").
|
23
31
|
body.should =~ /.*Google fake response.*/
|
24
32
|
end
|
25
33
|
|
26
34
|
it "should return stubbed response if request was stubbed" do
|
27
|
-
stub_http_request(:get, "www.
|
28
|
-
http_request(:get, "http://www.
|
35
|
+
stub_http_request(:get, "www.example.com").to_return(:body => "abc")
|
36
|
+
http_request(:get, "http://www.example.com/").body.should == "abc"
|
29
37
|
end
|
30
38
|
end
|
31
39
|
|
@@ -35,15 +43,14 @@ describe "WebMock", :shared => true do
|
|
35
43
|
end
|
36
44
|
|
37
45
|
it "should return stubbed response if request was stubbed" do
|
38
|
-
stub_http_request(:get, "www.
|
39
|
-
http_request(:get, "http://www.
|
46
|
+
stub_http_request(:get, "www.example.com").to_return(:body => "abc")
|
47
|
+
http_request(:get, "http://www.example.com/").body.should == "abc"
|
40
48
|
end
|
41
49
|
|
42
50
|
it "should raise exception if request was not stubbed" do
|
43
51
|
lambda {
|
44
|
-
http_request(:get, "http://www.
|
45
|
-
}.should
|
46
|
-
"Real HTTP connections are disabled. Unregistered request: GET http://www.google.com/")
|
52
|
+
http_request(:get, "http://www.example.com/")
|
53
|
+
}.should fail_with("Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/")
|
47
54
|
end
|
48
55
|
end
|
49
56
|
|
@@ -54,18 +61,18 @@ describe "WebMock", :shared => true do
|
|
54
61
|
describe "on uri" do
|
55
62
|
|
56
63
|
it "should match the request by uri with non escaped params if request have escaped parameters" do
|
57
|
-
stub_http_request(:get, "www.
|
58
|
-
http_request(:get, "http://www.
|
64
|
+
stub_http_request(:get, "www.example.com/?#{NOT_ESCAPED_PARAMS}").to_return(:body => "abc")
|
65
|
+
http_request(:get, "http://www.example.com/?#{ESCAPED_PARAMS}").body.should == "abc"
|
59
66
|
end
|
60
67
|
|
61
68
|
it "should match the request by uri with escaped parameters even if request has non escaped params" do
|
62
|
-
stub_http_request(:get, "www.
|
63
|
-
http_request(:get, "http://www.
|
69
|
+
stub_http_request(:get, "www.example.com/?#{ESCAPED_PARAMS}").to_return(:body => "abc")
|
70
|
+
http_request(:get, "http://www.example.com/?#{NOT_ESCAPED_PARAMS}").body.should == "abc"
|
64
71
|
end
|
65
72
|
|
66
73
|
it "should match the request by regexp matching non escaped params uri if request params are escaped" do
|
67
74
|
stub_http_request(:get, /.*x=ab c.*/).to_return(:body => "abc")
|
68
|
-
http_request(:get, "http://www.
|
75
|
+
http_request(:get, "http://www.example.com/?#{ESCAPED_PARAMS}").body.should == "abc"
|
69
76
|
end
|
70
77
|
|
71
78
|
end
|
@@ -73,17 +80,16 @@ describe "WebMock", :shared => true do
|
|
73
80
|
describe "on method" do
|
74
81
|
|
75
82
|
it "should match the request by method if registered" do
|
76
|
-
stub_http_request(:get, "www.
|
77
|
-
http_request(:get, "http://www.
|
83
|
+
stub_http_request(:get, "www.example.com")
|
84
|
+
http_request(:get, "http://www.example.com/").status.should == "200"
|
78
85
|
end
|
79
86
|
|
80
87
|
it "should not match requests if method is different" do
|
81
|
-
stub_http_request(:get, "www.
|
82
|
-
http_request(:get, "http://www.
|
88
|
+
stub_http_request(:get, "www.example.com")
|
89
|
+
http_request(:get, "http://www.example.com/").status.should == "200"
|
83
90
|
lambda {
|
84
|
-
http_request(:post, "http://www.
|
85
|
-
}.should
|
86
|
-
"Real HTTP connections are disabled. Unregistered request: POST http://www.google.com/"
|
91
|
+
http_request(:post, "http://www.example.com/")
|
92
|
+
}.should fail_with("Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/"
|
87
93
|
)
|
88
94
|
end
|
89
95
|
|
@@ -92,26 +98,24 @@ describe "WebMock", :shared => true do
|
|
92
98
|
describe "on body" do
|
93
99
|
|
94
100
|
it "should match requests if body is the same" do
|
95
|
-
stub_http_request(:get, "www.
|
101
|
+
stub_http_request(:get, "www.example.com").with(:body => "abc")
|
96
102
|
http_request(
|
97
|
-
:get, "http://www.
|
103
|
+
:get, "http://www.example.com/",
|
98
104
|
:body => "abc").status.should == "200"
|
99
105
|
end
|
100
106
|
|
101
107
|
it "should match requests if body is not set in the stub" do
|
102
|
-
stub_http_request(:get, "www.
|
108
|
+
stub_http_request(:get, "www.example.com")
|
103
109
|
http_request(
|
104
|
-
:get, "http://www.
|
110
|
+
:get, "http://www.example.com/",
|
105
111
|
:body => "abc").status.should == "200"
|
106
112
|
end
|
107
113
|
|
108
114
|
it "should not match requests if body is different" do
|
109
|
-
stub_http_request(:get, "www.
|
110
|
-
|
115
|
+
stub_http_request(:get, "www.example.com").with(:body => "abc")
|
111
116
|
lambda {
|
112
|
-
http_request(:get, "http://www.
|
113
|
-
}.should
|
114
|
-
"Real HTTP connections are disabled. Unregistered request: GET http://www.google.com/ with body 'def'")
|
117
|
+
http_request(:get, "http://www.example.com/", :body => "def")
|
118
|
+
}.should fail_with("Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with body 'def'")
|
115
119
|
end
|
116
120
|
|
117
121
|
end
|
@@ -119,61 +123,57 @@ describe "WebMock", :shared => true do
|
|
119
123
|
describe "on headers" do
|
120
124
|
|
121
125
|
it "should match requests if headers are the same" do
|
122
|
-
stub_http_request(:get, "www.
|
126
|
+
stub_http_request(:get, "www.example.com").with(:headers => SAMPLE_HEADERS )
|
123
127
|
http_request(
|
124
|
-
:get, "http://www.
|
128
|
+
:get, "http://www.example.com/",
|
125
129
|
:headers => SAMPLE_HEADERS).status.should == "200"
|
126
130
|
end
|
127
131
|
|
128
132
|
it "should match requests if request headers are not stubbed" do
|
129
|
-
stub_http_request(:get, "www.
|
133
|
+
stub_http_request(:get, "www.example.com")
|
130
134
|
http_request(
|
131
|
-
:get, "http://www.
|
135
|
+
:get, "http://www.example.com/",
|
132
136
|
:headers => SAMPLE_HEADERS).status.should == "200"
|
133
137
|
end
|
134
138
|
|
135
139
|
|
136
140
|
it "should not match requests if headers are different" do
|
137
|
-
stub_http_request(:get, "www.
|
141
|
+
stub_http_request(:get, "www.example.com").with(:headers => SAMPLE_HEADERS)
|
138
142
|
|
139
143
|
lambda {
|
140
144
|
http_request(
|
141
|
-
:get, "http://www.
|
145
|
+
:get, "http://www.example.com/",
|
142
146
|
:headers => { 'Content-Length' => '9999'})
|
143
|
-
}.should
|
144
|
-
%q(Real HTTP connections are disabled. Unregistered request: GET http://www.google.com/ with headers {'Content-Length'=>'9999'}))
|
147
|
+
}.should fail_with(%q(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers {'Content-Length'=>'9999'}))
|
145
148
|
end
|
146
149
|
end
|
147
150
|
|
148
151
|
describe "with basic authentication" do
|
149
152
|
|
150
153
|
it "should match if credentials are the same" do
|
151
|
-
stub_http_request(:get, "user:pass@www.
|
152
|
-
http_request(:get, "http://user:pass@www.
|
154
|
+
stub_http_request(:get, "user:pass@www.example.com")
|
155
|
+
http_request(:get, "http://user:pass@www.example.com/").status.should == "200"
|
153
156
|
end
|
154
157
|
|
155
158
|
it "should not match if credentials are different" do
|
156
|
-
stub_http_request(:get, "user:pass@www.
|
159
|
+
stub_http_request(:get, "user:pass@www.example.com")
|
157
160
|
lambda {
|
158
|
-
http_request(:get, "http://user:pazz@www.
|
159
|
-
}.should
|
160
|
-
%q(Real HTTP connections are disabled. Unregistered request: GET http://user:pazz@www.google.com/ with headers {'Authorization'=>'Basic dXNlcjpwYXp6'}))
|
161
|
+
http_request(:get, "http://user:pazz@www.example.com/").status.should == "200"
|
162
|
+
}.should fail_with(%q(Real HTTP connections are disabled. Unregistered request: GET http://user:pazz@www.example.com/))
|
161
163
|
end
|
162
164
|
|
163
165
|
it "should not match if credentials are stubbed but not provided in the request" do
|
164
|
-
stub_http_request(:get, "user:pass@www.
|
166
|
+
stub_http_request(:get, "user:pass@www.example.com")
|
165
167
|
lambda {
|
166
|
-
http_request(:get, "http://www.
|
167
|
-
}.should
|
168
|
-
%q(Real HTTP connections are disabled. Unregistered request: GET http://www.google.com/))
|
168
|
+
http_request(:get, "http://www.example.com/").status.should == "200"
|
169
|
+
}.should fail_with(%q(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
|
169
170
|
end
|
170
171
|
|
171
172
|
it "should not match if credentials are not stubbed but exist in the request" do
|
172
|
-
stub_http_request(:get, "www.
|
173
|
+
stub_http_request(:get, "www.example.com")
|
173
174
|
lambda {
|
174
|
-
http_request(:get, "http://user:pazz@www.
|
175
|
-
}.should
|
176
|
-
%q(Real HTTP connections are disabled. Unregistered request: GET http://user:pazz@www.google.com/ with headers {'Authorization'=>'Basic dXNlcjpwYXp6'}))
|
175
|
+
http_request(:get, "http://user:pazz@www.example.com/").status.should == "200"
|
176
|
+
}.should fail_with(%q(Real HTTP connections are disabled. Unregistered request: GET http://user:pazz@www.example.com/))
|
177
177
|
end
|
178
178
|
|
179
179
|
end
|
@@ -184,9 +184,9 @@ describe "WebMock", :shared => true do
|
|
184
184
|
|
185
185
|
it "should raise exception if declared in a stubbed response" do
|
186
186
|
class MyException < StandardError; end;
|
187
|
-
stub_http_request(:get, "www.
|
187
|
+
stub_http_request(:get, "www.example.com").to_raise(MyException)
|
188
188
|
lambda {
|
189
|
-
http_request(:get, "http://www.
|
189
|
+
http_request(:get, "http://www.example.com/")
|
190
190
|
}.should raise_error(MyException, "Exception from WebMock")
|
191
191
|
end
|
192
192
|
|
@@ -196,19 +196,34 @@ describe "WebMock", :shared => true do
|
|
196
196
|
describe "returning stubbed responses" do
|
197
197
|
|
198
198
|
it "should return declared body" do
|
199
|
-
stub_http_request(:get, "www.
|
200
|
-
http_request(:get, "http://www.
|
199
|
+
stub_http_request(:get, "www.example.com").to_return(:body => "abc")
|
200
|
+
http_request(:get, "http://www.example.com/").body.should == "abc"
|
201
201
|
end
|
202
202
|
|
203
203
|
it "should return declared headers" do
|
204
|
-
stub_http_request(:get, "www.
|
205
|
-
response = http_request(:get, "http://www.
|
204
|
+
stub_http_request(:get, "www.example.com").to_return(:headers => SAMPLE_HEADERS)
|
205
|
+
response = http_request(:get, "http://www.example.com/")
|
206
206
|
response.headers["Content-Length"].should == "8888"
|
207
207
|
end
|
208
208
|
|
209
209
|
it "should return declared status" do
|
210
|
-
stub_http_request(:get, "www.
|
211
|
-
http_request(:get, "http://www.
|
210
|
+
stub_http_request(:get, "www.example.com").to_return(:status => 500)
|
211
|
+
http_request(:get, "http://www.example.com/").status.should == "500"
|
212
|
+
end
|
213
|
+
|
214
|
+
|
215
|
+
describe "dynamic responses" do
|
216
|
+
|
217
|
+
it "should return evaluated response body" do
|
218
|
+
stub_http_request(:post, "www.example.com").to_return(:body => lambda { |request| request.body })
|
219
|
+
http_request(:post, "http://www.example.com/", :body => "echo").body.should == "echo"
|
220
|
+
end
|
221
|
+
|
222
|
+
it "should return evaluated response headers" do
|
223
|
+
stub_http_request(:post, "www.example.com").to_return(:headers => lambda { |request| request.headers })
|
224
|
+
http_request(:post, "http://www.example.com/", :headers => {'A' => 'B'}).headers['A'].should == 'B'
|
225
|
+
end
|
226
|
+
|
212
227
|
end
|
213
228
|
|
214
229
|
end
|
@@ -217,15 +232,15 @@ describe "WebMock", :shared => true do
|
|
217
232
|
describe "precedence of stubs" do
|
218
233
|
|
219
234
|
it "should use the last declared matching request stub" do
|
220
|
-
stub_http_request(:get, "www.
|
221
|
-
stub_http_request(:get, "www.
|
222
|
-
http_request(:get, "http://www.
|
235
|
+
stub_http_request(:get, "www.example.com").to_return(:body => "abc")
|
236
|
+
stub_http_request(:get, "www.example.com").to_return(:body => "def")
|
237
|
+
http_request(:get, "http://www.example.com/").body.should == "def"
|
223
238
|
end
|
224
239
|
|
225
240
|
it "should not be affected by the type of uri or request method" do
|
226
|
-
stub_http_request(:get, "www.
|
227
|
-
stub_http_request(:any, /.*
|
228
|
-
http_request(:get, "http://www.
|
241
|
+
stub_http_request(:get, "www.example.com").to_return(:body => "abc")
|
242
|
+
stub_http_request(:any, /.*example.*/).to_return(:body => "def")
|
243
|
+
http_request(:get, "http://www.example.com/").body.should == "def"
|
229
244
|
end
|
230
245
|
|
231
246
|
end
|
@@ -236,83 +251,83 @@ describe "WebMock", :shared => true do
|
|
236
251
|
|
237
252
|
before(:each) do
|
238
253
|
WebMock.disable_net_connect!
|
239
|
-
stub_http_request(:any, "http://www.
|
240
|
-
stub_http_request(:any, "https://www.
|
254
|
+
stub_http_request(:any, "http://www.example.com")
|
255
|
+
stub_http_request(:any, "https://www.example.com")
|
241
256
|
end
|
242
257
|
|
243
258
|
it "should pass if request was executed with the same uri and method" do
|
244
259
|
lambda {
|
245
|
-
http_request(:get, "http://www.
|
246
|
-
request(:get, "http://www.
|
260
|
+
http_request(:get, "http://www.example.com/")
|
261
|
+
request(:get, "http://www.example.com").should have_been_made.once
|
247
262
|
}.should_not raise_error
|
248
263
|
end
|
249
264
|
|
250
265
|
it "should pass if request was not expected and not executed" do
|
251
266
|
lambda {
|
252
|
-
request(:get, "http://www.
|
267
|
+
request(:get, "http://www.example.com").should_not have_been_made
|
253
268
|
}.should_not raise_error
|
254
269
|
end
|
255
270
|
|
256
271
|
it "should fail if request was not expected but executed" do
|
257
272
|
lambda {
|
258
|
-
http_request(:get, "http://www.
|
259
|
-
request(:get, "http://www.
|
260
|
-
}.should fail_with("The request GET http://www.
|
273
|
+
http_request(:get, "http://www.example.com/")
|
274
|
+
request(:get, "http://www.example.com").should_not have_been_made
|
275
|
+
}.should fail_with("The request GET http://www.example.com/ was expected to execute 0 times but it executed 1 time")
|
261
276
|
end
|
262
277
|
|
263
278
|
|
264
279
|
it "should fail if request was not executed" do
|
265
280
|
lambda {
|
266
|
-
request(:get, "http://www.
|
267
|
-
}.should fail_with("The request GET http://www.
|
281
|
+
request(:get, "http://www.example.com").should have_been_made
|
282
|
+
}.should fail_with("The request GET http://www.example.com/ was expected to execute 1 time but it executed 0 times")
|
268
283
|
end
|
269
284
|
|
270
285
|
it "should fail if request was executed to different uri" do
|
271
286
|
lambda {
|
272
|
-
http_request(:get, "http://www.
|
273
|
-
request(:get, "http://www.
|
274
|
-
}.should fail_with("The request GET http://www.
|
287
|
+
http_request(:get, "http://www.example.com/")
|
288
|
+
request(:get, "http://www.example.org").should have_been_made
|
289
|
+
}.should fail_with("The request GET http://www.example.org/ was expected to execute 1 time but it executed 0 times")
|
275
290
|
end
|
276
291
|
|
277
292
|
it "should fail if request was executed with different method" do
|
278
293
|
lambda {
|
279
|
-
http_request(:post, "http://www.
|
280
|
-
request(:get, "http://www.
|
281
|
-
}.should fail_with("The request GET http://www.
|
294
|
+
http_request(:post, "http://www.example.com/")
|
295
|
+
request(:get, "http://www.example.com").should have_been_made
|
296
|
+
}.should fail_with("The request GET http://www.example.com/ was expected to execute 1 time but it executed 0 times")
|
282
297
|
end
|
283
298
|
|
284
299
|
it "should pass if request was executed with different form of uri" do
|
285
300
|
lambda {
|
286
|
-
http_request(:get, "http://www.
|
287
|
-
request(:get, "www.
|
301
|
+
http_request(:get, "http://www.example.com/")
|
302
|
+
request(:get, "www.example.com").should have_been_made
|
288
303
|
}.should_not raise_error
|
289
304
|
end
|
290
305
|
|
291
306
|
it "should pass if request was executed with different form of uri without port " do
|
292
307
|
lambda {
|
293
|
-
http_request(:get, "http://www.
|
294
|
-
request(:get, "www.
|
308
|
+
http_request(:get, "http://www.example.com/")
|
309
|
+
request(:get, "www.example.com:80").should have_been_made
|
295
310
|
}.should_not raise_error
|
296
311
|
end
|
297
312
|
|
298
313
|
it "should pass if request was executed with different form of uri with port" do
|
299
314
|
lambda {
|
300
|
-
http_request(:get, "http://www.
|
301
|
-
request(:get, "www.
|
315
|
+
http_request(:get, "http://www.example.com/")
|
316
|
+
request(:get, "www.example.com:80").should have_been_made
|
302
317
|
}.should_not raise_error
|
303
318
|
end
|
304
319
|
|
305
320
|
it "should fail if request was executed with different port" do
|
306
321
|
lambda {
|
307
|
-
http_request(:get, "http://www.
|
308
|
-
request(:get, "www.
|
309
|
-
}.should fail_with("The request GET http://www.
|
322
|
+
http_request(:get, "http://www.example.com:80/")
|
323
|
+
request(:get, "www.example.com:90").should have_been_made
|
324
|
+
}.should fail_with("The request GET http://www.example.com:90/ was expected to execute 1 time but it executed 0 times")
|
310
325
|
end
|
311
326
|
|
312
327
|
it "should pass if request was executed with different form of uri with https port" do
|
313
328
|
lambda {
|
314
|
-
http_request(:get, "https://www.
|
315
|
-
request(:get, "https://www.
|
329
|
+
http_request(:get, "https://www.example.com/")
|
330
|
+
request(:get, "https://www.example.com:443/").should have_been_made
|
316
331
|
}.should_not raise_error
|
317
332
|
end
|
318
333
|
|
@@ -320,145 +335,145 @@ describe "WebMock", :shared => true do
|
|
320
335
|
|
321
336
|
before(:each) do
|
322
337
|
WebMock.disable_net_connect!
|
323
|
-
stub_http_request(:any, "http://www.
|
338
|
+
stub_http_request(:any, "http://www.example.com/?#{NOT_ESCAPED_PARAMS}")
|
324
339
|
end
|
325
340
|
|
326
341
|
it "should pass if request was executed with escaped params" do
|
327
342
|
lambda {
|
328
|
-
http_request(:get, "http://www.
|
329
|
-
request(:get, "http://www.
|
343
|
+
http_request(:get, "http://www.example.com/?#{ESCAPED_PARAMS}")
|
344
|
+
request(:get, "http://www.example.com/?#{NOT_ESCAPED_PARAMS}").should have_been_made
|
330
345
|
}.should_not raise_error
|
331
346
|
end
|
332
347
|
|
333
348
|
it "should pass if request was executed with non escaped params but escaped expected" do
|
334
349
|
lambda {
|
335
|
-
http_request(:get, "http://www.
|
336
|
-
request(:get, "http://www.
|
350
|
+
http_request(:get, "http://www.example.com/?#{NOT_ESCAPED_PARAMS}")
|
351
|
+
request(:get, "http://www.example.com/?#{ESCAPED_PARAMS}").should have_been_made
|
337
352
|
}.should_not raise_error
|
338
353
|
end
|
339
354
|
|
340
355
|
it "should pass if request was executed with escaped params but uri matichg regexp expected" do
|
341
356
|
lambda {
|
342
|
-
http_request(:get, "http://www.
|
343
|
-
request(:get, /.*
|
357
|
+
http_request(:get, "http://www.example.com/?#{ESCAPED_PARAMS}")
|
358
|
+
request(:get, /.*example.*/).should have_been_made
|
344
359
|
}.should_not raise_error
|
345
360
|
end
|
346
361
|
end
|
347
362
|
|
348
363
|
it "should fail if requested more times than expected" do
|
349
364
|
lambda {
|
350
|
-
http_request(:get, "http://www.
|
351
|
-
http_request(:get, "http://www.
|
352
|
-
request(:get, "http://www.
|
353
|
-
}.should fail_with("The request GET http://www.
|
365
|
+
http_request(:get, "http://www.example.com/")
|
366
|
+
http_request(:get, "http://www.example.com/")
|
367
|
+
request(:get, "http://www.example.com").should have_been_made
|
368
|
+
}.should fail_with("The request GET http://www.example.com/ was expected to execute 1 time but it executed 2 times")
|
354
369
|
end
|
355
370
|
|
356
371
|
it "should fail if requested less times than expected" do
|
357
372
|
lambda {
|
358
|
-
http_request(:get, "http://www.
|
359
|
-
request(:get, "http://www.
|
360
|
-
}.should fail_with("The request GET http://www.
|
373
|
+
http_request(:get, "http://www.example.com/")
|
374
|
+
request(:get, "http://www.example.com").should have_been_made.twice
|
375
|
+
}.should fail_with("The request GET http://www.example.com/ was expected to execute 2 times but it executed 1 time")
|
361
376
|
end
|
362
377
|
|
363
378
|
it "should fail if requested less times than expected when 3 times expected" do
|
364
379
|
lambda {
|
365
|
-
http_request(:get, "http://www.
|
366
|
-
request(:get, "http://www.
|
367
|
-
}.should fail_with("The request GET http://www.
|
380
|
+
http_request(:get, "http://www.example.com/")
|
381
|
+
request(:get, "http://www.example.com").should have_been_made.times(3)
|
382
|
+
}.should fail_with("The request GET http://www.example.com/ was expected to execute 3 times but it executed 1 time")
|
368
383
|
end
|
369
384
|
|
370
385
|
it "should succeed if request was executed with the same body" do
|
371
386
|
lambda {
|
372
|
-
http_request(:get, "http://www.
|
373
|
-
request(:get, "www.
|
387
|
+
http_request(:get, "http://www.example.com/", :body => "abc")
|
388
|
+
request(:get, "www.example.com").with(:body => "abc").should have_been_made
|
374
389
|
}.should_not raise_error
|
375
390
|
end
|
376
391
|
|
377
392
|
it "should fail if request was executed with different body" do
|
378
393
|
lambda {
|
379
|
-
http_request(:get, "http://www.
|
380
|
-
request(:get, "www.
|
394
|
+
http_request(:get, "http://www.example.com/", :body => "abc")
|
395
|
+
request(:get, "www.example.com").
|
381
396
|
with(:body => "def").should have_been_made
|
382
|
-
}.should fail_with("The request GET http://www.
|
397
|
+
}.should fail_with("The request GET http://www.example.com/ with body 'def' was expected to execute 1 time but it executed 0 times")
|
383
398
|
end
|
384
399
|
|
385
400
|
it "should succeed if request was executed with the same headers" do
|
386
401
|
lambda {
|
387
|
-
http_request(:get, "http://www.
|
388
|
-
request(:get, "www.
|
402
|
+
http_request(:get, "http://www.example.com/", :headers => SAMPLE_HEADERS)
|
403
|
+
request(:get, "www.example.com").
|
389
404
|
with(:headers => SAMPLE_HEADERS).should have_been_made
|
390
405
|
}.should_not raise_error
|
391
406
|
end
|
392
407
|
|
393
408
|
it "should fail if request was executed with different headers" do
|
394
409
|
lambda {
|
395
|
-
http_request(:get, "http://www.
|
396
|
-
request(:get, "www.
|
410
|
+
http_request(:get, "http://www.example.com/", :headers => SAMPLE_HEADERS)
|
411
|
+
request(:get, "www.example.com").
|
397
412
|
with(:headers => { 'Content-Length' => '9999'}).should have_been_made
|
398
|
-
}.should fail_with("The request GET http://www.
|
413
|
+
}.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")
|
399
414
|
end
|
400
415
|
|
401
416
|
it "should fail if request was executed with less headers" do
|
402
417
|
lambda {
|
403
|
-
http_request(:get, "http://www.
|
404
|
-
request(:get, "www.
|
418
|
+
http_request(:get, "http://www.example.com/", :headers => {'A' => 'a'})
|
419
|
+
request(:get, "www.example.com").
|
405
420
|
with(:headers => {'A' => 'a', 'B' => 'b'}).should have_been_made
|
406
|
-
}.should fail_with("The request GET http://www.
|
421
|
+
}.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")
|
407
422
|
end
|
408
423
|
|
409
424
|
it "should succeed if request was executed with more headers" do
|
410
425
|
lambda {
|
411
|
-
http_request(:get, "http://www.
|
426
|
+
http_request(:get, "http://www.example.com/",
|
412
427
|
:headers => {'A' => 'a', 'B' => 'b'}
|
413
428
|
)
|
414
|
-
request(:get, "www.
|
429
|
+
request(:get, "www.example.com").
|
415
430
|
with(:headers => {'A' => 'a'}).should have_been_made
|
416
431
|
}.should_not raise_error
|
417
432
|
end
|
418
433
|
|
419
434
|
it "should succeed if request was executed with body and headers but they were not specified in expectantion" do
|
420
435
|
lambda {
|
421
|
-
http_request(:get, "http://www.
|
436
|
+
http_request(:get, "http://www.example.com/",
|
422
437
|
:body => "abc",
|
423
438
|
:headers => SAMPLE_HEADERS
|
424
439
|
)
|
425
|
-
request(:get, "www.
|
440
|
+
request(:get, "www.example.com").should have_been_made
|
426
441
|
}.should_not raise_error
|
427
442
|
end
|
428
443
|
|
429
444
|
|
430
445
|
describe "with authentication" do
|
431
446
|
before(:each) do
|
432
|
-
stub_http_request(:any, "http://user:pass@www.
|
433
|
-
stub_http_request(:any, "http://user:pazz@www.
|
447
|
+
stub_http_request(:any, "http://user:pass@www.example.com")
|
448
|
+
stub_http_request(:any, "http://user:pazz@www.example.com")
|
434
449
|
end
|
435
450
|
|
436
451
|
it "should succeed if succeed if request was executed with expected credentials" do
|
437
452
|
lambda {
|
438
|
-
http_request(:get, "http://user:pass@www.
|
439
|
-
request(:get, "http://user:pass@www.
|
453
|
+
http_request(:get, "http://user:pass@www.example.com/")
|
454
|
+
request(:get, "http://user:pass@www.example.com").should have_been_made.once
|
440
455
|
}.should_not raise_error
|
441
456
|
end
|
442
457
|
|
443
458
|
it "should fail if request was executed with different credentials than expected" do
|
444
459
|
lambda {
|
445
|
-
http_request(:get, "http://user:pass@www.
|
446
|
-
request(:get, "http://user:pazz@www.
|
447
|
-
}.should fail_with("The request GET http://user:pazz@www.
|
460
|
+
http_request(:get, "http://user:pass@www.example.com/")
|
461
|
+
request(:get, "http://user:pazz@www.example.com").should have_been_made.once
|
462
|
+
}.should fail_with("The request GET http://user:pazz@www.example.com/ was expected to execute 1 time but it executed 0 times")
|
448
463
|
end
|
449
464
|
|
450
465
|
it "should fail if request was executed without credentials but credentials were expected" do
|
451
466
|
lambda {
|
452
|
-
http_request(:get, "http://www.
|
453
|
-
request(:get, "http://user:pass@www.
|
454
|
-
}.should fail_with("The request GET http://user:pass@www.
|
467
|
+
http_request(:get, "http://www.example.com/")
|
468
|
+
request(:get, "http://user:pass@www.example.com").should have_been_made.once
|
469
|
+
}.should fail_with("The request GET http://user:pass@www.example.com/ was expected to execute 1 time but it executed 0 times")
|
455
470
|
end
|
456
471
|
|
457
472
|
it "should fail if request was executed with credentials but expected without" do
|
458
473
|
lambda {
|
459
|
-
http_request(:get, "http://user:pass@www.
|
460
|
-
request(:get, "http://www.
|
461
|
-
}.should fail_with("The request GET http://www.
|
474
|
+
http_request(:get, "http://user:pass@www.example.com/")
|
475
|
+
request(:get, "http://www.example.com").should have_been_made.once
|
476
|
+
}.should fail_with("The request GET http://www.example.com/ was expected to execute 1 time but it executed 0 times")
|
462
477
|
end
|
463
478
|
|
464
479
|
end
|
@@ -467,23 +482,23 @@ describe "WebMock", :shared => true do
|
|
467
482
|
|
468
483
|
it "should verify expected requests occured" do
|
469
484
|
lambda {
|
470
|
-
http_request(:get, "http://www.
|
471
|
-
WebMock.should have_requested(:get, "http://www.
|
485
|
+
http_request(:get, "http://www.example.com/")
|
486
|
+
WebMock.should have_requested(:get, "http://www.example.com").once
|
472
487
|
}.should_not raise_error
|
473
488
|
end
|
474
489
|
|
475
490
|
it "should verify expected requests occured" do
|
476
491
|
lambda {
|
477
|
-
http_request(:get, "http://www.
|
478
|
-
WebMock.should have_requested(:get, "http://www.
|
492
|
+
http_request(:get, "http://www.example.com/", :body => "abc", :headers => {'A' => 'a'})
|
493
|
+
WebMock.should have_requested(:get, "http://www.example.com").with(:body => "abc", :headers => {'A' => 'a'}).once
|
479
494
|
}.should_not raise_error
|
480
495
|
end
|
481
496
|
|
482
497
|
it "should verify that non expected requests didn't occur" do
|
483
498
|
lambda {
|
484
|
-
http_request(:get, "http://www.
|
485
|
-
WebMock.should_not have_requested(:get, "http://www.
|
486
|
-
}.should fail_with("The request GET http://www.
|
499
|
+
http_request(:get, "http://www.example.com/")
|
500
|
+
WebMock.should_not have_requested(:get, "http://www.example.com")
|
501
|
+
}.should fail_with("The request GET http://www.example.com/ was expected to execute 0 times but it executed 1 time")
|
487
502
|
end
|
488
503
|
end
|
489
504
|
|
@@ -493,24 +508,24 @@ describe "WebMock", :shared => true do
|
|
493
508
|
|
494
509
|
it "should verify expected requests occured" do
|
495
510
|
lambda {
|
496
|
-
http_request(:get, "http://www.
|
497
|
-
assert_requested(:get, "http://www.
|
498
|
-
assert_requested(:get, "http://www.
|
511
|
+
http_request(:get, "http://www.example.com/")
|
512
|
+
assert_requested(:get, "http://www.example.com", :times => 1)
|
513
|
+
assert_requested(:get, "http://www.example.com")
|
499
514
|
}.should_not raise_error
|
500
515
|
end
|
501
516
|
|
502
517
|
it "should verify expected requests occured" do
|
503
518
|
lambda {
|
504
|
-
http_request(:get, "http://www.
|
505
|
-
assert_requested(:get, "http://www.
|
519
|
+
http_request(:get, "http://www.example.com/", :body => "abc", :headers => {'A' => 'a'})
|
520
|
+
assert_requested(:get, "http://www.example.com", :body => "abc", :headers => {'A' => 'a'})
|
506
521
|
}.should_not raise_error
|
507
522
|
end
|
508
523
|
|
509
524
|
it "should verify that non expected requests didn't occur" do
|
510
525
|
lambda {
|
511
|
-
http_request(:get, "http://www.
|
512
|
-
assert_not_requested(:get, "http://www.
|
513
|
-
}.should fail_with("The request GET http://www.
|
526
|
+
http_request(:get, "http://www.example.com/")
|
527
|
+
assert_not_requested(:get, "http://www.example.com")
|
528
|
+
}.should fail_with("The request GET http://www.example.com/ was expected to execute 0 times but it executed 1 time")
|
514
529
|
end
|
515
530
|
end
|
516
531
|
end
|
@@ -522,18 +537,18 @@ describe "WebMock", :shared => true do
|
|
522
537
|
end
|
523
538
|
|
524
539
|
it "should verify expected requests occured" do
|
525
|
-
|
540
|
+
setup_expectations_for_real_example_com_request
|
526
541
|
lambda {
|
527
|
-
http_request(:get, "http://www.
|
528
|
-
request(:get, "http://www.
|
542
|
+
http_request(:get, "http://www.example.com/")
|
543
|
+
request(:get, "http://www.example.com").should have_been_made
|
529
544
|
}.should_not raise_error
|
530
545
|
end
|
531
546
|
|
532
547
|
it "should verify that non expected requests didn't occur" do
|
533
548
|
lambda {
|
534
|
-
http_request(:get, "http://www.
|
535
|
-
request(:get, "http://www.
|
536
|
-
}.should fail_with("The request GET http://www.
|
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")
|
537
552
|
end
|
538
553
|
end
|
539
554
|
|