webmock 1.7.5 → 1.7.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. data/.rspec-tm +2 -0
  2. data/CHANGELOG.md +6 -0
  3. data/Gemfile +2 -2
  4. data/README.md +1 -0
  5. data/Rakefile +3 -3
  6. data/lib/webmock/http_lib_adapters/httpclient_adapter.rb +11 -4
  7. data/lib/webmock/version.rb +1 -1
  8. data/spec/{curb_spec.rb → acceptance/curb/curb_spec.rb} +9 -9
  9. data/spec/{curb_spec_helper.rb → acceptance/curb/curb_spec_helper.rb} +0 -0
  10. data/spec/{em_http_request_spec.rb → acceptance/em_http_request/em_http_request_spec.rb} +13 -13
  11. data/spec/{em_http_request_spec_helper.rb → acceptance/em_http_request/em_http_request_spec_helper.rb} +0 -0
  12. data/spec/acceptance/httpclient/httpclient_spec.rb +67 -0
  13. data/spec/{httpclient_spec_helper.rb → acceptance/httpclient/httpclient_spec_helper.rb} +0 -0
  14. data/spec/{net_http_shared.rb → acceptance/net_http/net_http_shared.rb} +0 -0
  15. data/spec/{net_http_spec.rb → acceptance/net_http/net_http_spec.rb} +6 -6
  16. data/spec/{net_http_spec_helper.rb → acceptance/net_http/net_http_spec_helper.rb} +0 -0
  17. data/spec/{real_net_http_spec.rb → acceptance/net_http/real_net_http_spec.rb} +1 -1
  18. data/spec/{patron_spec.rb → acceptance/patron/patron_spec.rb} +12 -12
  19. data/spec/{patron_spec_helper.rb → acceptance/patron/patron_spec_helper.rb} +0 -0
  20. data/spec/acceptance/shared/allowing_and_disabling_net_connect.rb +142 -0
  21. data/spec/acceptance/shared/callbacks.rb +130 -0
  22. data/spec/acceptance/shared/enabling_and_disabling_webmock.rb +96 -0
  23. data/spec/acceptance/shared/precedence_of_stubs.rb +15 -0
  24. data/spec/acceptance/shared/request_expectations.rb +620 -0
  25. data/spec/acceptance/shared/returning_declared_responses.rb +377 -0
  26. data/spec/acceptance/shared/stubbing_requests.rb +314 -0
  27. data/spec/{typhoeus_hydra_spec.rb → acceptance/typhoeus/typhoeus_hydra_spec.rb} +5 -5
  28. data/spec/{typhoeus_hydra_spec_helper.rb → acceptance/typhoeus/typhoeus_hydra_spec_helper.rb} +0 -0
  29. data/spec/acceptance/webmock_shared.rb +38 -0
  30. data/spec/spec_helper.rb +4 -2
  31. data/spec/{example_curl_output.txt → support/example_curl_output.txt} +0 -0
  32. data/spec/{my_rack_app.rb → support/my_rack_app.rb} +0 -0
  33. data/spec/{network_connection.rb → support/network_connection.rb} +0 -0
  34. data/spec/{errors_spec.rb → unit/errors_spec.rb} +1 -1
  35. data/spec/{http_lib_adapters → unit/http_lib_adapters}/http_lib_adapter_registry_spec.rb +1 -1
  36. data/spec/{http_lib_adapters → unit/http_lib_adapters}/http_lib_adapter_spec.rb +1 -1
  37. data/spec/{rack_response_spec.rb → unit/rack_response_spec.rb} +1 -1
  38. data/spec/{request_execution_verifier_spec.rb → unit/request_execution_verifier_spec.rb} +1 -1
  39. data/spec/{request_pattern_spec.rb → unit/request_pattern_spec.rb} +1 -1
  40. data/spec/{request_registry_spec.rb → unit/request_registry_spec.rb} +1 -1
  41. data/spec/{request_signature_spec.rb → unit/request_signature_spec.rb} +1 -1
  42. data/spec/{request_stub_spec.rb → unit/request_stub_spec.rb} +1 -1
  43. data/spec/{response_spec.rb → unit/response_spec.rb} +4 -4
  44. data/spec/{stub_registry_spec.rb → unit/stub_registry_spec.rb} +1 -1
  45. data/spec/{stub_request_snippet_spec.rb → unit/stub_request_snippet_spec.rb} +1 -1
  46. data/spec/{util → unit/util}/hash_counter_spec.rb +1 -1
  47. data/spec/{util → unit/util}/hash_keys_stringifier_spec.rb +1 -1
  48. data/spec/{util → unit/util}/headers_spec.rb +1 -1
  49. data/spec/{util → unit/util}/json_spec.rb +1 -1
  50. data/spec/{util → unit/util}/uri_spec.rb +1 -1
  51. data/spec/{webmock_spec.rb → unit/webmock_spec.rb} +1 -1
  52. data/webmock.gemspec +1 -1
  53. metadata +111 -116
  54. data/spec/httpclient_spec.rb +0 -43
  55. data/spec/vendor/addressable/lib/addressable/uri.rb +0 -8
  56. data/spec/vendor/addressable/lib/uri.rb +0 -1
  57. data/spec/vendor/crack/lib/crack.rb +0 -1
  58. data/spec/vendor/right_http_connection-1.2.4/History.txt +0 -59
  59. data/spec/vendor/right_http_connection-1.2.4/Manifest.txt +0 -7
  60. data/spec/vendor/right_http_connection-1.2.4/README.txt +0 -54
  61. data/spec/vendor/right_http_connection-1.2.4/Rakefile +0 -103
  62. data/spec/vendor/right_http_connection-1.2.4/lib/net_fix.rb +0 -160
  63. data/spec/vendor/right_http_connection-1.2.4/lib/right_http_connection.rb +0 -435
  64. data/spec/vendor/right_http_connection-1.2.4/setup.rb +0 -1585
  65. data/spec/webmock_shared.rb +0 -1770
@@ -0,0 +1,130 @@
1
+ shared_context "callbacks" do
2
+ describe "when after_request callback is declared" do
3
+ before(:each) do
4
+ WebMock.reset_callbacks
5
+ stub_request(:get, "http://www.example.com")
6
+ end
7
+
8
+ it "should not invoke callback unless request is made" do
9
+ WebMock.after_request {
10
+ @called = true
11
+ }
12
+ @called.should == nil
13
+ end
14
+
15
+ it "should invoke a callback after request is made" do
16
+ WebMock.after_request {
17
+ @called = true
18
+ }
19
+ http_request(:get, "http://www.example.com/")
20
+ @called.should == true
21
+ end
22
+
23
+ it "should not invoke a callback if this http library should be ignored" do
24
+ WebMock.after_request(:except => [http_library()]) {
25
+ @called = true
26
+ }
27
+ http_request(:get, "http://www.example.com/")
28
+ @called.should == nil
29
+ end
30
+
31
+ it "should invoke a callback even if other http libraries should be ignored" do
32
+ WebMock.after_request(:except => [:other_lib]) {
33
+ @called = true
34
+ }
35
+ http_request(:get, "http://www.example.com/")
36
+ @called.should == true
37
+ end
38
+
39
+ it "should pass request signature to the callback" do
40
+ WebMock.after_request(:except => [:other_lib]) do |request_signature, _|
41
+ @request_signature = request_signature
42
+ end
43
+ http_request(:get, "http://www.example.com/")
44
+ @request_signature.uri.to_s.should == "http://www.example.com:80/"
45
+ end
46
+
47
+ context "passing response to callback" do
48
+ context "when request is stubbed" do
49
+ before(:each) do
50
+ stub_request(:get, "http://www.example.com").
51
+ to_return(
52
+ :status => ["200", "hello"],
53
+ :headers => {'Content-Length' => '666', 'Hello' => 'World'},
54
+ :body => "foo bar"
55
+ )
56
+ WebMock.after_request(:except => [:other_lib]) do |_, response|
57
+ @response = response
58
+ end
59
+ http_request(:get, "http://www.example.com/")
60
+ end
61
+
62
+ it "should pass response to callback with the status and message" do
63
+ @response.status.should == ["200", "hello"]
64
+ end
65
+
66
+ it "should pass response to callback with headers" do
67
+ @response.headers.should == {
68
+ 'Content-Length' => '666',
69
+ 'Hello' => 'World'
70
+ }
71
+ end
72
+
73
+ it "should pass response to callback with body" do
74
+ @response.body.should == "foo bar"
75
+ end
76
+ end
77
+
78
+ describe "when request is not stubbed", :net_connect => true do
79
+ before(:each) do
80
+ WebMock.reset!
81
+ WebMock.allow_net_connect!
82
+ WebMock.after_request(:except => [:other_lib]) do |_, response|
83
+ @response = response
84
+ end
85
+ http_request(:get, "http://www.example.com/")
86
+ end
87
+
88
+ it "should pass real response to callback with status and message" do
89
+ # not supported by em-http-request, it always returns "unknown" for http_reason
90
+ unless http_library == :em_http_request
91
+ @response.status[0].should == 302
92
+ @response.status[1].should == "Found"
93
+ end
94
+ end
95
+
96
+ it "should pass real response to callback with headers" do
97
+ @response.headers["Content-Length"].should == "0"
98
+ end
99
+
100
+ it "should pass response to callback with body" do
101
+ @response.body.size.should == 0
102
+ end
103
+ end
104
+ end
105
+
106
+ it "should invoke multiple callbacks in order of their declarations" do
107
+ WebMock.after_request { @called = 1 }
108
+ WebMock.after_request { @called += 1 }
109
+ http_request(:get, "http://www.example.com/")
110
+ @called.should == 2
111
+ end
112
+
113
+ it "should invoke callbacks only for real requests if requested", :net_connect => true do
114
+ WebMock.after_request(:real_requests_only => true) { @called = true }
115
+ http_request(:get, "http://www.example.com/")
116
+ @called.should == nil
117
+ WebMock.allow_net_connect!
118
+ http_request(:get, "http://www.example.net/")
119
+ @called.should == true
120
+ end
121
+
122
+ it "should not invoke any callbacks after callbacks were reset" do
123
+ WebMock.after_request { @called = 1 }
124
+ WebMock.reset_callbacks
125
+ stub_request(:get, "http://www.example.com/")
126
+ http_request(:get, "http://www.example.com/")
127
+ @called.should == nil
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,96 @@
1
+ shared_context "enabled and disabled webmock" do
2
+ describe "when webmock is disabled" do
3
+ before(:each) do
4
+ WebMock.disable!
5
+ end
6
+ after(:each) do
7
+ WebMock.enable!
8
+ end
9
+ include_context "disabled WebMock"
10
+ end
11
+
12
+ describe "when webmock is enabled again" do
13
+ before(:each) do
14
+ WebMock.disable!
15
+ WebMock.enable!
16
+ end
17
+ include_context "enabled WebMock"
18
+ end
19
+
20
+ describe "when webmock is disabled except this lib" do
21
+ before(:each) do
22
+ WebMock.disable!(:except => [http_library])
23
+ end
24
+ after(:each) do
25
+ WebMock.enable!
26
+ end
27
+ include_context "enabled WebMock"
28
+ end
29
+
30
+ describe "when webmock is enabled except this lib" do
31
+ before(:each) do
32
+ WebMock.disable!
33
+ WebMock.enable!(:except => [http_library])
34
+ end
35
+ after(:each) do
36
+ WebMock.enable!
37
+ end
38
+ include_context "disabled WebMock"
39
+ end
40
+ end
41
+
42
+ shared_context "disabled WebMock" do
43
+ it "should not register executed requests" do
44
+ http_request(:get, "http://www.example.com/")
45
+ a_request(:get, "http://www.example.com/").should_not have_been_made
46
+ end
47
+
48
+ it "should not block unstubbed requests" do
49
+ lambda {
50
+ http_request(:get, "http://www.example.com/")
51
+ }.should_not raise_error
52
+ end
53
+
54
+ it "should return real response even if there are stubs" do
55
+ stub_request(:get, /.*/).to_return(:body => "x")
56
+ http_request(:get, "http://www.example.com/").
57
+ status.should == "302"
58
+ end
59
+
60
+ it "should not invoke any callbacks" do
61
+ WebMock.reset_callbacks
62
+ stub_request(:get, "http://www.example.com/")
63
+ @called = nil
64
+ WebMock.after_request { @called = 1 }
65
+ http_request(:get, "http://www.example.com/")
66
+ @called.should == nil
67
+ end
68
+ end
69
+
70
+ shared_context "enabled WebMock" do
71
+ it "should register executed requests" do
72
+ WebMock.allow_net_connect!
73
+ http_request(:get, "http://www.example.com/")
74
+ a_request(:get, "http://www.example.com/").should have_been_made
75
+ end
76
+
77
+ it "should block unstubbed requests" do
78
+ lambda {
79
+ http_request(:get, "http://www.example.com/")
80
+ }.should raise_error(WebMock::NetConnectNotAllowedError)
81
+ end
82
+
83
+ it "should return stubbed response" do
84
+ stub_request(:get, /.*/).to_return(:body => "x")
85
+ http_request(:get, "http://www.example.com/").body.should == "x"
86
+ end
87
+
88
+ it "should invoke callbacks" do
89
+ WebMock.allow_net_connect!
90
+ WebMock.reset_callbacks
91
+ @called = nil
92
+ WebMock.after_request { @called = 1 }
93
+ http_request(:get, "http://www.example.com/")
94
+ @called.should == 1
95
+ end
96
+ end
@@ -0,0 +1,15 @@
1
+ shared_context "precedence of stubs" do
2
+ describe "when choosing a matching request stub" do
3
+ it "should use the last declared matching request stub" do
4
+ stub_request(:get, "www.example.com").to_return(:body => "abc")
5
+ stub_request(:get, "www.example.com").to_return(:body => "def")
6
+ http_request(:get, "http://www.example.com/").body.should == "def"
7
+ end
8
+
9
+ it "should not be affected by the type of uri or request method" do
10
+ stub_request(:get, "www.example.com").to_return(:body => "abc")
11
+ stub_request(:any, /.*example.*/).to_return(:body => "def")
12
+ http_request(:get, "http://www.example.com/").body.should == "def"
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,620 @@
1
+ shared_context "request expectations" do
2
+ describe "when request expectations are set" do
3
+ describe "when net connect is not allowed" do
4
+ before(:each) do
5
+ WebMock.disable_net_connect!
6
+ stub_request(:any, "http://www.example.com")
7
+ stub_request(:any, "https://www.example.com")
8
+ end
9
+
10
+ it "should satisfy expectation if request was executed with the same uri and method" do
11
+ lambda {
12
+ http_request(:get, "http://www.example.com/")
13
+ a_request(:get, "http://www.example.com").should have_been_made.once
14
+ }.should_not raise_error
15
+ end
16
+
17
+ it "should satisfy expectation declared on WebMock.resuest" do
18
+ lambda {
19
+ http_request(:get, "http://www.example.com/")
20
+ WebMock.request(:get, "http://www.example.com").should have_been_made.once
21
+ }.should_not raise_error
22
+ end
23
+
24
+ it "should satisfy expectation if request was not expected and not executed" do
25
+ lambda {
26
+ a_request(:get, "http://www.example.com").should_not have_been_made
27
+ }.should_not raise_error
28
+ end
29
+
30
+ it "should fail if request was not expected but executed" do
31
+ lambda {
32
+ http_request(:get, "http://www.example.com/")
33
+ a_request(:get, "http://www.example.com").should_not have_been_made
34
+ }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 0 times but it executed 1 time))
35
+ end
36
+
37
+ it "should fail resulting with failure with a message and executed requests listed" do
38
+ lambda {
39
+ http_request(:get, "http://www.example.com/")
40
+ a_request(:get, "http://www.example.com").should_not have_been_made
41
+ }.should fail_with(%r{The following requests were made:\n\nGET http://www.example.com/.+was made 1 time})
42
+ end
43
+
44
+ it "should fail if request was not executed" do
45
+ lambda {
46
+ a_request(:get, "http://www.example.com").should have_been_made
47
+ }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 1 time but it executed 0 times))
48
+ end
49
+
50
+ it "should fail if request was executed to different uri" do
51
+ lambda {
52
+ http_request(:get, "http://www.example.com/")
53
+ a_request(:get, "http://www.example.org").should have_been_made
54
+ }.should fail_with(%r(The request GET http://www.example.org/ was expected to execute 1 time but it executed 0 times))
55
+ end
56
+
57
+ it "should fail if request was executed with different method" do
58
+ lambda {
59
+ http_request(:post, "http://www.example.com/", :body => "abc")
60
+ a_request(:get, "http://www.example.com").should have_been_made
61
+ }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 1 time but it executed 0 times))
62
+ end
63
+
64
+ it "should satisfy expectation if request was executed with different form of uri" do
65
+ lambda {
66
+ http_request(:get, "http://www.example.com/")
67
+ a_request(:get, "www.example.com").should have_been_made
68
+ }.should_not raise_error
69
+ end
70
+
71
+ it "should satisfy expectation if request was executed with different form of uri without port " do
72
+ lambda {
73
+ http_request(:get, "http://www.example.com/")
74
+ a_request(:get, "www.example.com:80").should have_been_made
75
+ }.should_not raise_error
76
+ end
77
+
78
+ it "should satisfy expectation if request was executed with different form of uri with port" do
79
+ lambda {
80
+ http_request(:get, "http://www.example.com/")
81
+ a_request(:get, "www.example.com:80").should have_been_made
82
+ }.should_not raise_error
83
+ end
84
+
85
+ it "should fail if request was executed to a different port" do
86
+ lambda {
87
+ http_request(:get, "http://www.example.com:80/")
88
+ a_request(:get, "www.example.com:90").should have_been_made
89
+ }.should fail_with(%r(The request GET http://www.example.com:90/ was expected to execute 1 time but it executed 0 times))
90
+ end
91
+
92
+ it "should satisfy expectation if request was executed with different form of uri with https port" do
93
+ lambda {
94
+ http_request(:get, "https://www.example.com/")
95
+ a_request(:get, "https://www.example.com:443/").should have_been_made
96
+ }.should_not raise_error
97
+ end
98
+
99
+ describe "when matching requests with escaped or unescaped uris" do
100
+ before(:each) do
101
+ WebMock.disable_net_connect!
102
+ stub_request(:any, "http://www.example.com/?#{NOT_ESCAPED_PARAMS}")
103
+ end
104
+
105
+ it "should satisfy expectation if request was executed with escaped params" do
106
+ lambda {
107
+ http_request(:get, "http://www.example.com/?#{ESCAPED_PARAMS}")
108
+ a_request(:get, "http://www.example.com/?#{NOT_ESCAPED_PARAMS}").should have_been_made
109
+ }.should_not raise_error
110
+ end
111
+
112
+ it "should satisfy expectation if request was executed with non escaped params" do
113
+ lambda {
114
+ http_request(:get, "http://www.example.com/?#{NOT_ESCAPED_PARAMS}")
115
+ a_request(:get, "http://www.example.com/?#{ESCAPED_PARAMS}").should have_been_made
116
+ }.should_not raise_error
117
+ end
118
+
119
+ it "should satisfy expectation if request was executed with escaped params and uri matching regexp was expected" do
120
+ lambda {
121
+ http_request(:get, "http://www.example.com/?#{ESCAPED_PARAMS}")
122
+ a_request(:get, /.*example.*/).should have_been_made
123
+ }.should_not raise_error
124
+ end
125
+
126
+ end
127
+
128
+ describe "when matching requests with query params" do
129
+ before(:each) do
130
+ stub_request(:any, /.*example.*/)
131
+ end
132
+
133
+ it "should satisfy expectation if the request was executed with query params declared as a hash in a query option" do
134
+ lambda {
135
+ http_request(:get, "http://www.example.com/?a[]=b&a[]=c")
136
+ a_request(:get, "www.example.com").with(:query => {"a" => ["b", "c"]}).should have_been_made
137
+ }.should_not raise_error
138
+ end
139
+
140
+ it "should satisfy expectation if the request was executed with query params declared as string in query option" do
141
+ lambda {
142
+ http_request(:get, "http://www.example.com/?a[]=b&a[]=c")
143
+ a_request(:get, "www.example.com").with(:query => "a[]=b&a[]=c").should have_been_made
144
+ }.should_not raise_error
145
+ end
146
+
147
+ it "should satisfy expectation if the request was executed with query params both in uri and in query option" do
148
+ lambda {
149
+ http_request(:get, "http://www.example.com/?x=3&a[]=b&a[]=c")
150
+ a_request(:get, "www.example.com/?x=3").with(:query => {"a" => ["b", "c"]}).should have_been_made
151
+ }.should_not raise_error
152
+ end
153
+ end
154
+
155
+ it "should fail if request was made more times than expected" do
156
+ lambda {
157
+ http_request(:get, "http://www.example.com/")
158
+ http_request(:get, "http://www.example.com/")
159
+ a_request(:get, "http://www.example.com").should have_been_made
160
+ }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 1 time but it executed 2 times))
161
+ end
162
+
163
+ it "should fail if request was made less times than expected" do
164
+ lambda {
165
+ http_request(:get, "http://www.example.com/")
166
+ a_request(:get, "http://www.example.com").should have_been_made.twice
167
+ }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 2 times but it executed 1 time))
168
+ end
169
+
170
+ it "should fail if request was made less times than expected when 3 times expected" do
171
+ lambda {
172
+ http_request(:get, "http://www.example.com/")
173
+ a_request(:get, "http://www.example.com").should have_been_made.times(3)
174
+ }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 3 times but it executed 1 time))
175
+ end
176
+
177
+ it "should satisfy expectation if request was executed with the same body" do
178
+ lambda {
179
+ http_request(:post, "http://www.example.com/", :body => "abc")
180
+ a_request(:post, "www.example.com").with(:body => "abc").should have_been_made
181
+ }.should_not raise_error
182
+ end
183
+
184
+ it "should fail if request was executed with different body" do
185
+ lambda {
186
+ http_request(:get, "http://www.example.com/", :body => "abc")
187
+ a_request(:get, "www.example.com").
188
+ with(:body => "def").should have_been_made
189
+ }.should fail_with(%r(The request GET http://www.example.com/ with body "def" was expected to execute 1 time but it executed 0 times))
190
+ end
191
+
192
+ describe "when expected request body is declared as a regexp" do
193
+ it "should satisfy expectation if request was executed with body matching regexp" do
194
+ lambda {
195
+ http_request(:post, "http://www.example.com/", :body => "abc")
196
+ a_request(:post, "www.example.com").with(:body => /^abc$/).should have_been_made
197
+ }.should_not raise_error
198
+ end
199
+
200
+ it "should fail if request was executed with body not matching regexp" do
201
+ lambda {
202
+ http_request(:get, "http://www.example.com/", :body => "abc")
203
+ a_request(:get, "www.example.com").
204
+ with(:body => /^xabc/).should have_been_made
205
+ }.should fail_with(%r(The request GET http://www.example.com/ with body /\^xabc/ was expected to execute 1 time but it executed 0 times))
206
+ end
207
+
208
+ end
209
+
210
+ describe "when expected reqest body is declared as a hash" do
211
+ let(:body_hash) { {:a => '1', :b => 'five', 'c' => {'d' => ['e', 'f']}} }
212
+ let(:fail_message) {%r(The request POST http://www.example.com/ with body \{"a"=>"1", "b"=>"five", "c"=>\{"d"=>\["e", "f"\]\}\} was expected to execute 1 time but it executed 0 times)}
213
+
214
+ describe "when request is made with url encoded body matching hash" do
215
+ it "should satisfy expectation" do
216
+ lambda {
217
+ http_request(:post, "http://www.example.com/", :body => 'a=1&c[d][]=e&c[d][]=f&b=five')
218
+ a_request(:post, "www.example.com").with(:body => body_hash).should have_been_made
219
+ }.should_not raise_error
220
+ end
221
+
222
+ it "should satisfy expectation even if url encoded params have different order" do
223
+ lambda {
224
+ http_request(:post, "http://www.example.com/", :body => 'a=1&c[d][]=e&b=five&c[d][]=f')
225
+ a_request(:post, "www.example.com").with(:body => body_hash).should have_been_made
226
+ }.should_not raise_error
227
+ end
228
+
229
+ it "should fail if request is executed with url encoded body not matching hash" do
230
+ lambda {
231
+ http_request(:post, "http://www.example.com/", :body => 'c[d][]=f&a=1&c[d][]=e')
232
+ a_request(:post, "www.example.com").with(:body => body_hash).should have_been_made
233
+ }.should fail_with(fail_message)
234
+ end
235
+ end
236
+
237
+ describe "when request is executed with json body matching hash and Content-Type is set to json" do
238
+ it "should satisfy expectation" do
239
+ lambda {
240
+ http_request(:post, "http://www.example.com/", :headers => {'Content-Type' => 'application/json'},
241
+ :body => "{\"a\":\"1\",\"c\":{\"d\":[\"e\",\"f\"]},\"b\":\"five\"}")
242
+ a_request(:post, "www.example.com").with(:body => body_hash).should have_been_made
243
+ }.should_not raise_error
244
+ end
245
+
246
+ it "should satisfy expectation even if json body is in different form" do
247
+ lambda {
248
+ http_request(:post, "http://www.example.com/", :headers => {'Content-Type' => 'application/json'},
249
+ :body => "{\"a\":\"1\",\"b\":\"five\",\"c\":{\"d\":[\"e\",\"f\"]}}")
250
+ a_request(:post, "www.example.com").with(:body => body_hash).should have_been_made
251
+ }.should_not raise_error
252
+ end
253
+
254
+ it "should satisfy expectation even if json body contains date string" do
255
+ lambda {
256
+ http_request(:post, "http://www.example.com/", :headers => {'Content-Type' => 'application/json'},
257
+ :body => "{\"foo\":\"2010-01-01\"}")
258
+ a_request(:post, "www.example.com").with(:body => {"foo" => "2010-01-01"}).should have_been_made
259
+ }.should_not raise_error
260
+ end
261
+ end
262
+
263
+
264
+ describe "when request is executed with xml body matching hash and content type is set to xml" do
265
+ let(:body_hash) { { "opt" => {:a => "1", :b => 'five', 'c' => {'d' => ['e', 'f']}} }}
266
+
267
+ it "should satisfy expectation" do
268
+ lambda {
269
+ http_request(:post, "http://www.example.com/", :headers => {'Content-Type' => 'application/xml'},
270
+ :body => "<opt a=\"1\" b=\"five\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n")
271
+ a_request(:post, "www.example.com").with(:body => body_hash).should have_been_made
272
+ }.should_not raise_error
273
+ end
274
+
275
+ it "should satisfy expectation even if xml body is in different form" do
276
+ lambda {
277
+ http_request(:post, "http://www.example.com/", :headers => {'Content-Type' => 'application/xml'},
278
+ :body => "<opt b=\"five\" a=\"1\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n")
279
+ a_request(:post, "www.example.com").with(:body => body_hash).should have_been_made
280
+ }.should_not raise_error
281
+ end
282
+
283
+ it "should satisfy expectation even if xml body contains date string" do
284
+ lambda {
285
+ http_request(:post, "http://www.example.com/", :headers => {'Content-Type' => 'application/xml'},
286
+ :body => "<opt foo=\"2010-01-01\">\n</opt>\n")
287
+ a_request(:post, "www.example.com").with(:body => {"opt" => {"foo" => "2010-01-01"}}).should have_been_made
288
+ }.should_not raise_error
289
+ end
290
+ end
291
+ end
292
+
293
+
294
+ describe "when request with headers is expected" do
295
+ it "should satisfy expectation if request was executed with the same headers" do
296
+ lambda {
297
+ http_request(:get, "http://www.example.com/", :headers => SAMPLE_HEADERS)
298
+ a_request(:get, "www.example.com").
299
+ with(:headers => SAMPLE_HEADERS).should have_been_made
300
+ }.should_not raise_error
301
+ end
302
+
303
+ it "should satisfy expectation if request was executed with the same headers but with header value declared as array" do
304
+ lambda {
305
+ http_request(:get, "http://www.example.com/", :headers => {"a" => "b"})
306
+ a_request(:get, "www.example.com").
307
+ with(:headers => {"a" => ["b"]}).should have_been_made
308
+ }.should_not raise_error
309
+ end
310
+
311
+ describe "when multiple headers with the same key are passed" do
312
+ it "should satisfy expectation" do
313
+ lambda {
314
+ http_request(:get, "http://www.example.com/", :headers => {"a" => ["b", "c"]})
315
+ a_request(:get, "www.example.com").
316
+ with(:headers => {"a" => ["b", "c"]}).should have_been_made
317
+ }.should_not raise_error
318
+ end
319
+
320
+ it "should satisfy expectation even if request was executed with the same headers but different order" do
321
+ lambda {
322
+ http_request(:get, "http://www.example.com/", :headers => {"a" => ["b", "c"]})
323
+ a_request(:get, "www.example.com").
324
+ with(:headers => {"a" => ["c", "b"]}).should have_been_made
325
+ }.should_not raise_error
326
+ end
327
+
328
+ it "should fail if request was executed with different headers" do
329
+ lambda {
330
+ http_request(:get, "http://www.example.com/", :headers => {"a" => ["b", "c"]})
331
+ a_request(:get, "www.example.com").
332
+ with(:headers => {"a" => ["b", "d"]}).should have_been_made
333
+ }.should fail_with(%r(The request GET http://www.example.com/ with headers \{'A'=>\['b', 'd'\]\} was expected to execute 1 time but it executed 0 times))
334
+ end
335
+ end
336
+
337
+ it "should fail if request was executed with different headers" do
338
+ lambda {
339
+ http_request(:get, "http://www.example.com/", :headers => SAMPLE_HEADERS)
340
+ a_request(:get, "www.example.com").
341
+ with(:headers => { 'Content-Length' => '9999'}).should have_been_made
342
+ }.should fail_with(%r(The request GET http://www.example.com/ with headers \{'Content-Length'=>'9999'\} was expected to execute 1 time but it executed 0 times))
343
+ end
344
+
345
+ it "should fail if request was executed with less headers" do
346
+ lambda {
347
+ http_request(:get, "http://www.example.com/", :headers => {'A' => 'a'})
348
+ a_request(:get, "www.example.com").
349
+ with(:headers => {'A' => 'a', 'B' => 'b'}).should have_been_made
350
+ }.should fail_with(%r(The request GET http://www.example.com/ with headers \{'A'=>'a', 'B'=>'b'\} was expected to execute 1 time but it executed 0 times))
351
+ end
352
+
353
+ it "should satisfy expectation if request was executed with more headers" do
354
+ lambda {
355
+ http_request(:get, "http://www.example.com/",
356
+ :headers => {'A' => 'a', 'B' => 'b'}
357
+ )
358
+ a_request(:get, "www.example.com").
359
+ with(:headers => {'A' => 'a'}).should have_been_made
360
+ }.should_not raise_error
361
+ end
362
+
363
+ it "should satisfy expectation if request was executed with body and headers but they were not specified in expectantion" do
364
+ lambda {
365
+ http_request(:get, "http://www.example.com/",
366
+ :body => "abc",
367
+ :headers => SAMPLE_HEADERS
368
+ )
369
+ a_request(:get, "www.example.com").should have_been_made
370
+ }.should_not raise_error
371
+ end
372
+
373
+ it "should satisfy expectation if request was executed with headers matching regular expressions" do
374
+ lambda {
375
+ http_request(:get, "http://www.example.com/", :headers => { 'some-header' => 'MyAppName' })
376
+ a_request(:get, "www.example.com").
377
+ with(:headers => { :some_header => /^MyAppName$/ }).should have_been_made
378
+ }.should_not raise_error
379
+ end
380
+
381
+ it "should fail if request was executed with headers not matching regular expression" do
382
+ lambda {
383
+ http_request(:get, "http://www.example.com/", :headers => { 'some-header' => 'xMyAppName' })
384
+ a_request(:get, "www.example.com").
385
+ with(:headers => { :some_header => /^MyAppName$/ }).should have_been_made
386
+ }.should fail_with(%r(The request GET http://www.example.com/ with headers \{'Some-Header'=>/\^MyAppName\$/\} was expected to execute 1 time but it executed 0 times))
387
+ end
388
+ end
389
+
390
+ describe "when expectation contains a request matching block" do
391
+ it "should satisfy expectation if request was executed and block evaluated to true" do
392
+ lambda {
393
+ http_request(:post, "http://www.example.com/", :body => "wadus")
394
+ a_request(:post, "www.example.com").with { |req| req.body == "wadus" }.should have_been_made
395
+ }.should_not raise_error
396
+ end
397
+
398
+ it "should fail if request was executed and block evaluated to false" do
399
+ lambda {
400
+ http_request(:post, "http://www.example.com/", :body => "abc")
401
+ a_request(:post, "www.example.com").with { |req| req.body == "wadus" }.should have_been_made
402
+ }.should fail_with(%r(The request POST http://www.example.com/ with given block was expected to execute 1 time but it executed 0 times))
403
+ end
404
+
405
+ it "should fail if request was not expected but it executed and block matched request" do
406
+ lambda {
407
+ http_request(:post, "http://www.example.com/", :body => "wadus")
408
+ a_request(:post, "www.example.com").with { |req| req.body == "wadus" }.should_not have_been_made
409
+ }.should fail_with(%r(The request POST http://www.example.com/ with given block was expected to execute 0 times but it executed 1 time))
410
+ end
411
+ end
412
+
413
+ describe "with authentication" do
414
+ before(:each) do
415
+ stub_request(:any, "http://user:pass@www.example.com")
416
+ stub_request(:any, "http://user:pazz@www.example.com")
417
+ end
418
+
419
+ it "should satisfy expectation if request was executed with expected credentials" do
420
+ lambda {
421
+ http_request(:get, "http://user:pass@www.example.com/")
422
+ a_request(:get, "http://user:pass@www.example.com").should have_been_made.once
423
+ }.should_not raise_error
424
+ end
425
+
426
+ it "should fail if request was executed with different credentials than expected" do
427
+ lambda {
428
+ http_request(:get, "http://user:pass@www.example.com/")
429
+ a_request(:get, "http://user:pazz@www.example.com").should have_been_made.once
430
+ }.should fail_with(%r(The request GET http://user:pazz@www.example.com/ was expected to execute 1 time but it executed 0 times))
431
+ end
432
+
433
+ it "should fail if request was executed without credentials and credentials were expected" do
434
+ lambda {
435
+ http_request(:get, "http://www.example.com/")
436
+ a_request(:get, "http://user:pass@www.example.com").should have_been_made.once
437
+ }.should fail_with(%r(The request GET http://user:pass@www.example.com/ was expected to execute 1 time but it executed 0 times))
438
+ end
439
+
440
+ it "should fail if request was executed with credentials but expected without credentials" do
441
+ lambda {
442
+ http_request(:get, "http://user:pass@www.example.com/")
443
+ a_request(:get, "http://www.example.com").should have_been_made.once
444
+ }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 1 time but it executed 0 times))
445
+ end
446
+
447
+ it "should satisfy expectations even if requests were executed in different order than expectations were declared" do
448
+ stub_request(:post, "http://www.example.com")
449
+ http_request(:post, "http://www.example.com/", :body => "def")
450
+ http_request(:post, "http://www.example.com/", :body => "abc")
451
+ WebMock.should have_requested(:post, "www.example.com").with(:body => "abc")
452
+ WebMock.should have_requested(:post, "www.example.com").with(:body => "def")
453
+ end
454
+ end
455
+
456
+ describe "when expectations were set on WebMock object" do
457
+ it "should satisfy expectation if expected request was made" do
458
+ lambda {
459
+ http_request(:get, "http://www.example.com/")
460
+ WebMock.should have_requested(:get, "http://www.example.com").once
461
+ }.should_not raise_error
462
+ end
463
+
464
+ it "should satisfy expectation if request with body and headers was expected and request was made" do
465
+ lambda {
466
+ http_request(:post, "http://www.example.com/", :body => "abc", :headers => {'A' => 'a'})
467
+ WebMock.should have_requested(:post, "http://www.example.com").with(:body => "abc", :headers => {'A' => 'a'}).once
468
+ }.should_not raise_error
469
+ end
470
+
471
+ it "should fail if request expected not to be made was made" do
472
+ lambda {
473
+ http_request(:get, "http://www.example.com/")
474
+ WebMock.should_not have_requested(:get, "http://www.example.com")
475
+ }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 0 times but it executed 1 time))
476
+ end
477
+
478
+ it "should satisfy expectation if request was executed and expectation had block which evaluated to true" do
479
+ lambda {
480
+ http_request(:post, "http://www.example.com/", :body => "wadus")
481
+ WebMock.should have_requested(:post, "www.example.com").with { |req| req.body == "wadus" }
482
+ }.should_not raise_error
483
+ end
484
+
485
+ it "should fail if request was executed and expectation had block which evaluated to false" do
486
+ lambda {
487
+ http_request(:post, "http://www.example.com/", :body => "abc")
488
+ WebMock.should have_requested(:post, "www.example.com").with { |req| req.body == "wadus" }
489
+ }.should fail_with(%r(The request POST http://www.example.com/ with given block was expected to execute 1 time but it executed 0 times))
490
+ end
491
+
492
+ it "should fail if request was expected not to be made but was made and block matched request" do
493
+ lambda {
494
+ http_request(:post, "http://www.example.com/", :body => "wadus")
495
+ WebMock.should_not have_requested(:post, "www.example.com").with { |req| req.body == "wadus" }
496
+ }.should fail_with(%r(The request POST http://www.example.com/ with given block was expected to execute 0 times but it executed 1 time))
497
+ end
498
+ end
499
+
500
+ describe "when expectation is declared using assert_requested" do
501
+ it "should satisfy expectation if requests was made" do
502
+ lambda {
503
+ http_request(:get, "http://www.example.com/")
504
+ assert_requested(:get, "http://www.example.com", :times => 1)
505
+ assert_requested(:get, "http://www.example.com")
506
+ }.should_not raise_error
507
+ end
508
+
509
+ it "should satisfy expectation if request was made with body and headers" do
510
+ lambda {
511
+ http_request(:post, "http://www.example.com/", :body => "abc", :headers => {'A' => 'a'})
512
+ assert_requested(:post, "http://www.example.com", :body => "abc", :headers => {'A' => 'a'})
513
+ }.should_not raise_error
514
+ end
515
+
516
+ it "should fail if request expected not to be made was not wade" do
517
+ lambda {
518
+ http_request(:get, "http://www.example.com/")
519
+ assert_not_requested(:get, "http://www.example.com")
520
+ }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 0 times but it executed 1 time))
521
+ end
522
+
523
+ it "should fail if request expected not to be made was made and expectation block evaluated to true" do
524
+ lambda {
525
+ http_request(:post, "http://www.example.com/", :body => "wadus")
526
+ assert_not_requested(:post, "www.example.com") { |req| req.body == "wadus" }
527
+ }.should fail_with(%r(The request POST http://www.example.com/ with given block was expected to execute 0 times but it executed 1 time))
528
+ end
529
+
530
+ it "should satisfy expectation if request was made and expectation block evaluated to true" do
531
+ lambda {
532
+ http_request(:post, "http://www.example.com/", :body => "wadus")
533
+ assert_requested(:post, "www.example.com") { |req| req.body == "wadus" }
534
+ }.should_not raise_error
535
+ end
536
+
537
+ it "should fail if request was made and expectation block evaluated to false" do
538
+ lambda {
539
+ http_request(:post, "http://www.example.com/", :body => "abc")
540
+ assert_requested(:post, "www.example.com") { |req| req.body == "wadus" }
541
+ }.should fail_with(%r(The request POST http://www.example.com/ with given block was expected to execute 1 time but it executed 0 times))
542
+ end
543
+ end
544
+ end
545
+
546
+
547
+ describe "expectation is set on the request stub" do
548
+ it "should satisfy expectation if expected request was made" do
549
+ stub = stub_request(:get, "http://www.example.com/")
550
+ http_request(:get, "http://www.example.com/")
551
+ stub.should have_been_requested.once
552
+ end
553
+
554
+ it "should satisfy expectations if subsequent requests were made" do
555
+ stub = stub_request(:get, "http://www.example.com/")
556
+ http_request(:get, "http://www.example.com/")
557
+ stub.should have_been_requested.once
558
+ http_request(:get, "http://www.example.com/")
559
+ stub.should have_been_requested.twice
560
+ end
561
+
562
+ it "should satisfy expectation if expected request with body and headers was made" do
563
+ stub = stub_request(:post, "http://www.example.com").with(:body => "abc", :headers => {'A' => 'a'})
564
+ http_request(:post, "http://www.example.com/", :body => "abc", :headers => {'A' => 'a'})
565
+ stub.should have_been_requested.once
566
+ end
567
+
568
+ it "should fail if request not expected to be made was made" do
569
+ lambda {
570
+ stub = stub_request(:get, "http://www.example.com")
571
+ http_request(:get, "http://www.example.com/")
572
+ stub.should_not have_been_requested
573
+ }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 0 times but it executed 1 time))
574
+ end
575
+
576
+ it "should fail request not expected to be made was made and expectation block evaluated to true" do
577
+ lambda {
578
+ stub = stub_request(:post, "www.example.com").with { |req| req.body == "wadus" }
579
+ http_request(:post, "http://www.example.com/", :body => "wadus")
580
+ stub.should_not have_been_requested
581
+ }.should fail_with(%r(The request POST http://www.example.com/ with given block was expected to execute 0 times but it executed 1 time))
582
+ end
583
+
584
+ it "should satisfy expectation if request was made and expectation block evaluated to true" do
585
+ stub = stub_request(:post, "www.example.com").with { |req| req.body == "wadus" }
586
+ http_request(:post, "http://www.example.com/", :body => "wadus")
587
+ stub.should have_been_requested
588
+ end
589
+
590
+ it "should satisfy expectation if request was made and expectation block evaluated to false" do
591
+ lambda {
592
+ stub_request(:any, /.+/) #stub any request
593
+ stub = stub_request(:post, "www.example.com").with { |req| req.body == "wadus" }
594
+ http_request(:post, "http://www.example.com/", :body => "abc")
595
+ stub.should have_been_requested
596
+ }.should fail_with(%r(The request POST http://www.example.com/ with given block was expected to execute 1 time but it executed 0 times))
597
+ end
598
+ end
599
+
600
+ describe "when net connect is allowed", :net_connect => true do
601
+ before(:each) do
602
+ WebMock.allow_net_connect!
603
+ end
604
+
605
+ it "should satisfy expectation if expected requests was made" do
606
+ lambda {
607
+ http_request(:get, "http://www.example.com/")
608
+ a_request(:get, "http://www.example.com").should have_been_made
609
+ }.should_not raise_error
610
+ end
611
+
612
+ it "should fail request expected not to be made, was made" do
613
+ lambda {
614
+ http_request(:get, "http://www.example.com/")
615
+ a_request(:get, "http://www.example.com").should_not have_been_made
616
+ }.should fail_with(%r(The request GET http://www.example.com/ was expected to execute 0 times but it executed 1 time))
617
+ end
618
+ end
619
+ end
620
+ end