webmock 1.8.6 → 3.14.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (138) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/CI.yml +37 -0
  3. data/.gitignore +6 -0
  4. data/CHANGELOG.md +1198 -0
  5. data/Gemfile +3 -15
  6. data/README.md +761 -305
  7. data/Rakefile +13 -40
  8. data/lib/webmock/api.rb +63 -17
  9. data/lib/webmock/callback_registry.rb +1 -1
  10. data/lib/webmock/config.rb +8 -0
  11. data/lib/webmock/cucumber.rb +2 -0
  12. data/lib/webmock/errors.rb +8 -24
  13. data/lib/webmock/http_lib_adapters/async_http_client_adapter.rb +216 -0
  14. data/lib/webmock/http_lib_adapters/curb_adapter.rb +148 -84
  15. data/lib/webmock/http_lib_adapters/em_http_request_adapter.rb +224 -4
  16. data/lib/webmock/http_lib_adapters/excon_adapter.rb +104 -34
  17. data/lib/webmock/http_lib_adapters/http_rb/client.rb +17 -0
  18. data/lib/webmock/http_lib_adapters/http_rb/request.rb +16 -0
  19. data/lib/webmock/http_lib_adapters/http_rb/response.rb +64 -0
  20. data/lib/webmock/http_lib_adapters/http_rb/streamer.rb +29 -0
  21. data/lib/webmock/http_lib_adapters/http_rb/webmock.rb +68 -0
  22. data/lib/webmock/http_lib_adapters/http_rb_adapter.rb +37 -0
  23. data/lib/webmock/http_lib_adapters/httpclient_adapter.rb +152 -86
  24. data/lib/webmock/http_lib_adapters/manticore_adapter.rb +145 -0
  25. data/lib/webmock/http_lib_adapters/net_http.rb +155 -46
  26. data/lib/webmock/http_lib_adapters/net_http_response.rb +1 -1
  27. data/lib/webmock/http_lib_adapters/patron_adapter.rb +16 -15
  28. data/lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb +76 -82
  29. data/lib/webmock/matchers/any_arg_matcher.rb +13 -0
  30. data/lib/webmock/matchers/hash_argument_matcher.rb +21 -0
  31. data/lib/webmock/matchers/hash_excluding_matcher.rb +15 -0
  32. data/lib/webmock/matchers/hash_including_matcher.rb +4 -12
  33. data/lib/webmock/minitest.rb +29 -3
  34. data/lib/webmock/rack_response.rb +14 -7
  35. data/lib/webmock/request_body_diff.rb +64 -0
  36. data/lib/webmock/request_execution_verifier.rb +38 -17
  37. data/lib/webmock/request_pattern.rb +158 -38
  38. data/lib/webmock/request_registry.rb +3 -3
  39. data/lib/webmock/request_signature.rb +7 -3
  40. data/lib/webmock/request_signature_snippet.rb +61 -0
  41. data/lib/webmock/request_stub.rb +9 -6
  42. data/lib/webmock/response.rb +30 -15
  43. data/lib/webmock/rspec/matchers/request_pattern_matcher.rb +38 -2
  44. data/lib/webmock/rspec/matchers/webmock_matcher.rb +23 -2
  45. data/lib/webmock/rspec/matchers.rb +0 -1
  46. data/lib/webmock/rspec.rb +11 -2
  47. data/lib/webmock/stub_registry.rb +31 -10
  48. data/lib/webmock/stub_request_snippet.rb +14 -6
  49. data/lib/webmock/test_unit.rb +4 -4
  50. data/lib/webmock/util/hash_counter.rb +20 -6
  51. data/lib/webmock/util/hash_keys_stringifier.rb +5 -3
  52. data/lib/webmock/util/hash_validator.rb +17 -0
  53. data/lib/webmock/util/headers.rb +23 -2
  54. data/lib/webmock/util/json.rb +20 -7
  55. data/lib/webmock/util/query_mapper.rb +281 -0
  56. data/lib/webmock/util/uri.rb +29 -19
  57. data/lib/webmock/util/values_stringifier.rb +20 -0
  58. data/lib/webmock/util/version_checker.rb +40 -2
  59. data/lib/webmock/version.rb +1 -1
  60. data/lib/webmock/webmock.rb +56 -17
  61. data/lib/webmock.rb +56 -46
  62. data/minitest/test_helper.rb +8 -3
  63. data/minitest/test_webmock.rb +4 -1
  64. data/minitest/webmock_spec.rb +16 -6
  65. data/spec/acceptance/async_http_client/async_http_client_spec.rb +375 -0
  66. data/spec/acceptance/async_http_client/async_http_client_spec_helper.rb +73 -0
  67. data/spec/acceptance/curb/curb_spec.rb +227 -68
  68. data/spec/acceptance/curb/curb_spec_helper.rb +11 -8
  69. data/spec/acceptance/em_http_request/em_http_request_spec.rb +322 -28
  70. data/spec/acceptance/em_http_request/em_http_request_spec_helper.rb +15 -10
  71. data/spec/acceptance/excon/excon_spec.rb +66 -4
  72. data/spec/acceptance/excon/excon_spec_helper.rb +21 -7
  73. data/spec/acceptance/http_rb/http_rb_spec.rb +93 -0
  74. data/spec/acceptance/http_rb/http_rb_spec_helper.rb +54 -0
  75. data/spec/acceptance/httpclient/httpclient_spec.rb +152 -11
  76. data/spec/acceptance/httpclient/httpclient_spec_helper.rb +25 -16
  77. data/spec/acceptance/manticore/manticore_spec.rb +107 -0
  78. data/spec/acceptance/manticore/manticore_spec_helper.rb +35 -0
  79. data/spec/acceptance/net_http/net_http_shared.rb +52 -24
  80. data/spec/acceptance/net_http/net_http_spec.rb +164 -50
  81. data/spec/acceptance/net_http/net_http_spec_helper.rb +19 -10
  82. data/spec/acceptance/net_http/real_net_http_spec.rb +1 -1
  83. data/spec/acceptance/patron/patron_spec.rb +29 -40
  84. data/spec/acceptance/patron/patron_spec_helper.rb +15 -11
  85. data/spec/acceptance/shared/allowing_and_disabling_net_connect.rb +229 -58
  86. data/spec/acceptance/shared/callbacks.rb +32 -30
  87. data/spec/acceptance/shared/complex_cross_concern_behaviors.rb +20 -5
  88. data/spec/acceptance/shared/enabling_and_disabling_webmock.rb +14 -14
  89. data/spec/acceptance/shared/precedence_of_stubs.rb +6 -6
  90. data/spec/acceptance/shared/request_expectations.rb +560 -296
  91. data/spec/acceptance/shared/returning_declared_responses.rb +180 -138
  92. data/spec/acceptance/shared/stubbing_requests.rb +385 -154
  93. data/spec/acceptance/typhoeus/typhoeus_hydra_spec.rb +78 -17
  94. data/spec/acceptance/typhoeus/typhoeus_hydra_spec_helper.rb +19 -15
  95. data/spec/acceptance/webmock_shared.rb +2 -2
  96. data/spec/fixtures/test.txt +1 -0
  97. data/spec/quality_spec.rb +27 -3
  98. data/spec/spec_helper.rb +11 -20
  99. data/spec/support/failures.rb +9 -0
  100. data/spec/support/my_rack_app.rb +8 -3
  101. data/spec/support/network_connection.rb +7 -13
  102. data/spec/support/webmock_server.rb +8 -3
  103. data/spec/unit/api_spec.rb +175 -0
  104. data/spec/unit/errors_spec.rb +116 -19
  105. data/spec/unit/http_lib_adapters/http_lib_adapter_registry_spec.rb +1 -1
  106. data/spec/unit/http_lib_adapters/http_lib_adapter_spec.rb +2 -2
  107. data/spec/unit/matchers/hash_excluding_matcher_spec.rb +61 -0
  108. data/spec/unit/matchers/hash_including_matcher_spec.rb +87 -0
  109. data/spec/unit/rack_response_spec.rb +54 -16
  110. data/spec/unit/request_body_diff_spec.rb +90 -0
  111. data/spec/unit/request_execution_verifier_spec.rb +147 -39
  112. data/spec/unit/request_pattern_spec.rb +462 -198
  113. data/spec/unit/request_registry_spec.rb +29 -9
  114. data/spec/unit/request_signature_snippet_spec.rb +89 -0
  115. data/spec/unit/request_signature_spec.rb +91 -49
  116. data/spec/unit/request_stub_spec.rb +71 -70
  117. data/spec/unit/response_spec.rb +100 -81
  118. data/spec/unit/stub_registry_spec.rb +37 -20
  119. data/spec/unit/stub_request_snippet_spec.rb +51 -31
  120. data/spec/unit/util/hash_counter_spec.rb +6 -6
  121. data/spec/unit/util/hash_keys_stringifier_spec.rb +4 -4
  122. data/spec/unit/util/headers_spec.rb +4 -4
  123. data/spec/unit/util/json_spec.rb +29 -3
  124. data/spec/unit/util/query_mapper_spec.rb +157 -0
  125. data/spec/unit/util/uri_spec.rb +150 -36
  126. data/spec/unit/util/version_checker_spec.rb +15 -9
  127. data/spec/unit/webmock_spec.rb +57 -4
  128. data/test/http_request.rb +3 -3
  129. data/test/shared_test.rb +45 -13
  130. data/test/test_helper.rb +1 -1
  131. data/test/test_webmock.rb +6 -0
  132. data/webmock.gemspec +30 -11
  133. metadata +308 -199
  134. data/.rvmrc +0 -1
  135. data/.travis.yml +0 -11
  136. data/Guardfile +0 -24
  137. data/lib/webmock/http_lib_adapters/em_http_request/em_http_request_0_x.rb +0 -151
  138. data/lib/webmock/http_lib_adapters/em_http_request/em_http_request_1_x.rb +0 -210
@@ -1,147 +1,246 @@
1
+ # encoding: utf-8
2
+
1
3
  shared_examples_for "stubbing requests" do |*adapter_info|
2
4
  describe "when requests are stubbed" do
3
5
  describe "based on uri" do
4
6
  it "should return stubbed response even if request have escaped parameters" do
5
- stub_request(:get, "www.example.com/hello/?#{NOT_ESCAPED_PARAMS}").to_return(:body => "abc")
6
- http_request(:get, "http://www.example.com/hello/?#{ESCAPED_PARAMS}").body.should == "abc"
7
+ stub_request(:get, "www.example.com/hello+/?#{NOT_ESCAPED_PARAMS}").to_return(body: "abc")
8
+ expect(http_request(:get, "http://www.example.com/hello%2B/?#{ESCAPED_PARAMS}").body).to eq("abc")
9
+ end
10
+
11
+ it "should return stubbed response even if query params have integer values" do
12
+ stub_request(:get, "www.example.com").with(query: {"a" => 1}).to_return(body: "abc")
13
+ expect(http_request(:get, "http://www.example.com/?a=1").body).to eq("abc")
7
14
  end
8
15
 
9
16
  it "should return stubbed response even if request has non escaped params" do
10
- stub_request(:get, "www.example.com/hello/?#{ESCAPED_PARAMS}").to_return(:body => "abc")
11
- http_request(:get, "http://www.example.com/hello/?#{NOT_ESCAPED_PARAMS}").body.should == "abc"
17
+ stub_request(:get, "www.example.com/hello%2B/?#{ESCAPED_PARAMS}").to_return(body: "abc")
18
+ expect(http_request(:get, "http://www.example.com/hello+/?#{NOT_ESCAPED_PARAMS}").body).to eq("abc")
19
+ end
20
+
21
+ it "should return stubbed response for url with non utf query params" do
22
+ param = 'aäoöuü'.encode('iso-8859-1')
23
+ param = CGI.escape(param)
24
+ stub_request(:get, "www.example.com/?#{param}").to_return(body: "abc")
25
+ expect(http_request(:get, "http://www.example.com/?#{param}").body).to eq("abc")
12
26
  end
13
27
 
14
28
  it "should return stubbed response even if stub uri is declared as regexp and request params are escaped" do
15
- stub_request(:get, /.*x=ab c.*/).to_return(:body => "abc")
16
- http_request(:get, "http://www.example.com/hello/?#{ESCAPED_PARAMS}").body.should == "abc"
29
+ stub_request(:get, /.*x=ab c.*/).to_return(body: "abc")
30
+ expect(http_request(:get, "http://www.example.com/hello/?#{ESCAPED_PARAMS}").body).to eq("abc")
31
+ end
32
+
33
+ it "should raise error specifying stubbing instructions with escaped characters in params if there is no matching stub" do
34
+ begin
35
+ http_request(:get, "http://www.example.com/hello+/?#{NOT_ESCAPED_PARAMS}")
36
+ rescue WebMock::NetConnectNotAllowedError => e
37
+ expect(e.message).to match(/Unregistered request: GET http:\/\/www\.example\.com\/hello\+\/\?x=ab%20c&z='Stop!'%20said%20Fred%20m/m)
38
+ expect(e.message).to match(/stub_request\(:get, "http:\/\/www\.example\.com\/hello\+\/\?x=ab%20c&z='Stop!'%20said%20Fred%20m"\)/m)
39
+ end
40
+
41
+ stub_request(:get, "http://www.example.com/hello+/?x=ab%20c&z='Stop!'%20said%20Fred%20m")
42
+ http_request(:get, "http://www.example.com/hello+/?#{NOT_ESCAPED_PARAMS}")
17
43
  end
18
44
  end
19
45
 
20
46
  describe "based on query params" do
21
47
  it "should return stubbed response when stub declares query params as a hash" do
22
- stub_request(:get, "www.example.com").with(:query => {"a" => ["b", "c"]}).to_return(:body => "abc")
23
- http_request(:get, "http://www.example.com/?a[]=b&a[]=c").body.should == "abc"
48
+ stub_request(:get, "www.example.com").with(query: {"a" => ["b x", "c d"]}).to_return(body: "abc")
49
+ expect(http_request(:get, "http://www.example.com/?a[]=b+x&a[]=c%20d").body).to eq("abc")
24
50
  end
25
51
 
26
52
  it "should return stubbed response when stub declares query params as a hash" do
27
- stub_request(:get, "www.example.com").with(:query => "a[]=b&a[]=c").to_return(:body => "abc")
28
- http_request(:get, "http://www.example.com/?a[]=b&a[]=c").body.should == "abc"
53
+ stub_request(:get, "www.example.com").with(query: "a[]=b&a[]=c").to_return(body: "abc")
54
+ expect(http_request(:get, "http://www.example.com/?a[]=b&a[]=c").body).to eq("abc")
29
55
  end
30
56
 
31
57
  it "should return stubbed response when stub declares query params both in uri and as a hash" do
32
- stub_request(:get, "www.example.com/?x=3").with(:query => {"a" => ["b", "c"]}).to_return(:body => "abc")
33
- http_request(:get, "http://www.example.com/?x=3&a[]=b&a[]=c").body.should == "abc"
58
+ stub_request(:get, "www.example.com/?x=3").with(query: {"a" => ["b", "c"]}).to_return(body: "abc")
59
+ expect(http_request(:get, "http://www.example.com/?x=3&a[]=b&a[]=c").body).to eq("abc")
34
60
  end
35
61
 
36
62
  it "should return stubbed response when stub expects only part of query params" do
37
- stub_request(:get, "www.example.com").with(:query => hash_including({"a" => ["b", "c"]})).to_return(:body => "abc")
38
- http_request(:get, "http://www.example.com/?a[]=b&a[]=c&b=1").body.should == "abc"
63
+ stub_request(:get, "www.example.com").with(query: hash_including({"a" => ["b", "c"]})).to_return(body: "abc")
64
+ expect(http_request(:get, "http://www.example.com/?a[]=b&a[]=c&b=1").body).to eq("abc")
65
+ end
66
+
67
+ it 'should return stubbed response when stub expects exclude part of query params' do
68
+ stub_request(:get, 'www.example.com').with(query: hash_excluding(a: ['b', 'c'])).to_return(body: 'abc')
69
+ expect(http_request(:get, 'http://www.example.com/?a[]=c&a[]=d&b=1').body).to eq('abc')
70
+ end
71
+
72
+ it "should return stubbed response when stub expects an empty array" do
73
+ stub_request(:get, 'www.example.com').with(query: { a: [] }).to_return(body: 'abc')
74
+ expect(http_request(:get, 'http://www.example.com/?a[]').body).to eq('abc')
39
75
  end
40
76
  end
41
77
 
42
78
  describe "based on method" do
43
79
  it "should return stubbed response" do
44
80
  stub_request(:get, "www.example.com")
45
- http_request(:get, "http://www.example.com/").status.should == "200"
81
+ expect(http_request(:get, "http://www.example.com/").status).to eq("200")
82
+ end
83
+
84
+ it "should match stubbed request when http request was made with method given as string" do
85
+ stub_request(:get, "www.example.com")
86
+ expect(http_request('get', "http://www.example.com/").status).to eq("200")
46
87
  end
47
88
 
48
89
  it "should raise error if stubbed request has different method" do
49
90
  stub_request(:get, "www.example.com")
50
- http_request(:get, "http://www.example.com/").status.should == "200"
51
- lambda {
91
+ expect(http_request(:get, "http://www.example.com/").status).to eq("200")
92
+ expect {
52
93
  http_request(:delete, "http://www.example.com/")
53
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: DELETE http://www.example.com/)
94
+ }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: DELETE http://www.example.com/)
54
95
  )
55
96
  end
56
97
  end
57
98
 
58
99
  describe "based on body" do
59
100
  it "should match requests if body is the same" do
60
- stub_request(:post, "www.example.com").with(:body => "abc")
61
- http_request(
101
+ stub_request(:post, "www.example.com").with(body: "abc")
102
+ expect(http_request(
62
103
  :post, "http://www.example.com/",
63
- :body => "abc").status.should == "200"
104
+ body: "abc").status).to eq("200")
64
105
  end
65
106
 
66
107
  it "should match requests if body is not set in the stub" do
67
108
  stub_request(:post, "www.example.com")
68
- http_request(
109
+ expect(http_request(
69
110
  :post, "http://www.example.com/",
70
- :body => "abc").status.should == "200"
111
+ body: "abc").status).to eq("200")
71
112
  end
72
113
 
73
114
  it "should not match requests if body is different" do
74
- stub_request(:post, "www.example.com").with(:body => "abc")
75
- lambda {
76
- http_request(:post, "http://www.example.com/", :body => "def")
77
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/ with body 'def'))
115
+ stub_request(:post, "www.example.com").with(body: "abc")
116
+ expect {
117
+ http_request(:post, "http://www.example.com/", body: "def")
118
+ }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/ with body 'def'))
78
119
  end
79
120
 
80
121
  describe "with regular expressions" do
81
122
  it "should match requests if body matches regexp" do
82
- stub_request(:post, "www.example.com").with(:body => /\d+abc$/)
83
- http_request(
123
+ stub_request(:post, "www.example.com").with(body: /\d+abc$/)
124
+ expect(http_request(
84
125
  :post, "http://www.example.com/",
85
- :body => "123abc").status.should == "200"
126
+ body: "123abc").status).to eq("200")
86
127
  end
87
128
 
88
129
  it "should not match requests if body doesn't match regexp" do
89
- stub_request(:post, "www.example.com").with(:body => /^abc/)
90
- lambda {
91
- http_request(:post, "http://www.example.com/", :body => "xabc")
92
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/ with body 'xabc'))
130
+ stub_request(:post, "www.example.com").with(body: /^abc/)
131
+ expect {
132
+ http_request(:post, "http://www.example.com/", body: "xabc")
133
+ }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/ with body 'xabc'))
93
134
  end
94
135
  end
95
136
 
96
137
  describe "when body is declared as a hash" do
97
138
  before(:each) do
98
139
  stub_request(:post, "www.example.com").
99
- with(:body => {:a => '1', :b => 'five', 'c' => {'d' => ['e', 'f']} })
140
+ with(body: {:a => '1', :b => 'five x', 'c' => {'d' => ['e', 'f']} })
100
141
  end
101
142
 
102
143
  describe "for request with url encoded body" do
103
144
  it "should match request if hash matches body" do
104
- http_request(
145
+ expect(http_request(
105
146
  :post, "http://www.example.com/",
106
- :body => 'a=1&c[d][]=e&c[d][]=f&b=five').status.should == "200"
147
+ body: 'a=1&c[d][]=e&c[d][]=f&b=five+x').status).to eq("200")
107
148
  end
108
149
 
109
150
  it "should match request if hash matches body in different order of params" do
110
- http_request(
151
+ expect(http_request(
111
152
  :post, "http://www.example.com/",
112
- :body => 'a=1&c[d][]=e&b=five&c[d][]=f').status.should == "200"
153
+ body: 'a=1&c[d][]=e&b=five+x&c[d][]=f').status).to eq("200")
113
154
  end
114
155
 
115
156
  it "should not match if hash doesn't match url encoded body" do
116
- lambda {
157
+ expect {
117
158
  http_request(
118
159
  :post, "http://www.example.com/",
119
- :body => 'c[d][]=f&a=1&c[d][]=e')
120
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/ with body 'c\[d\]\[\]=f&a=1&c\[d\]\[\]=e'))
160
+ body: 'c[d][]=f&a=1&c[d][]=e')
161
+ }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/ with body 'c\[d\]\[\]=f&a=1&c\[d\]\[\]=e'))
162
+ end
163
+
164
+ describe "for request with form url encoded body and content type" do
165
+ it "should match if stubbed request body hash has string values matching string values in request body" do
166
+ WebMock.reset!
167
+ stub_request(:post, "www.example.com").with(body: {"foo" => '1'})
168
+ expect(http_request(
169
+ :post, "http://www.example.com/", headers: {'Content-Type' => 'application/x-www-form-urlencoded'},
170
+ body: "foo=1").status).to eq("200")
171
+ end
172
+
173
+ it "should match if stubbed request body hash has NON string values matching string values in request body" do
174
+ WebMock.reset!
175
+ stub_request(:post, "www.example.com").with(body: {"foo" => 1})
176
+ expect(http_request(
177
+ :post, "http://www.example.com/", headers: {'Content-Type' => 'application/x-www-form-urlencoded'},
178
+ body: "foo=1").status).to eq("200")
179
+ end
180
+
181
+ it "should match if stubbed request body is hash_included" do
182
+ WebMock.reset!
183
+ stub_request(:post, "www.example.com").with(body: {"foo" => hash_including("bar" => '1')})
184
+ expect(http_request(
185
+ :post, "http://www.example.com/", headers: {'Content-Type' => 'application/x-www-form-urlencoded'},
186
+ body: "foo[bar]=1").status).to eq("200")
187
+ end
121
188
  end
122
189
  end
123
190
 
124
191
 
125
192
  describe "for request with json body and content type is set to json" do
126
193
  it "should match if hash matches body" do
127
- http_request(
128
- :post, "http://www.example.com/", :headers => {'Content-Type' => 'application/json'},
129
- :body => "{\"a\":\"1\",\"c\":{\"d\":[\"e\",\"f\"]},\"b\":\"five\"}").status.should == "200"
194
+ expect(http_request(
195
+ :post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
196
+ body: "{\"a\":\"1\",\"c\":{\"d\":[\"e\",\"f\"]},\"b\":\"five x\"}").status).to eq("200")
130
197
  end
131
198
 
132
199
  it "should match if hash matches body in different form" do
133
- http_request(
134
- :post, "http://www.example.com/", :headers => {'Content-Type' => 'application/json'},
135
- :body => "{\"a\":\"1\",\"b\":\"five\",\"c\":{\"d\":[\"e\",\"f\"]}}").status.should == "200"
200
+ expect(http_request(
201
+ :post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
202
+ body: "{\"a\":\"1\",\"b\":\"five x\",\"c\":{\"d\":[\"e\",\"f\"]}}").status).to eq("200")
136
203
  end
137
204
 
138
205
  it "should match if hash contains date string" do #Crack creates date object
139
206
  WebMock.reset!
140
207
  stub_request(:post, "www.example.com").
141
- with(:body => {"foo" => "2010-01-01"})
142
- http_request(
143
- :post, "http://www.example.com/", :headers => {'Content-Type' => 'application/json'},
144
- :body => "{\"foo\":\"2010-01-01\"}").status.should == "200"
208
+ with(body: {"foo" => "2010-01-01"})
209
+ expect(http_request(
210
+ :post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
211
+ body: "{\"foo\":\"2010-01-01\"}").status).to eq("200")
212
+ end
213
+
214
+ it "should match if any of the strings have spaces" do
215
+ WebMock.reset!
216
+ stub_request(:post, "www.example.com").with(body: {"foo" => "a b c"})
217
+ expect(http_request(
218
+ :post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
219
+ body: "{\"foo\":\"a b c\"}").status).to eq("200")
220
+ end
221
+
222
+ it "should match if stubbed request body hash has NON string values matching NON string values in request body" do
223
+ WebMock.reset!
224
+ stub_request(:post, "www.example.com").with(body: {"foo" => 1})
225
+ expect(http_request(
226
+ :post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
227
+ body: "{\"foo\":1}").status).to eq("200")
228
+ end
229
+
230
+ it "should not match if stubbed request body hash has string values matching NON string values in request body" do
231
+ WebMock.reset!
232
+ stub_request(:post, "www.example.com").with(body: {"foo" => '1'})
233
+ expect{http_request(
234
+ :post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
235
+ body: "{\"foo\":1}") }.to raise_error(WebMock::NetConnectNotAllowedError)
236
+ end
237
+
238
+ it "should not match if stubbed request body hash has NON string values matching string values in request body" do
239
+ WebMock.reset!
240
+ stub_request(:post, "www.example.com").with(body: {"foo" => 1})
241
+ expect{http_request(
242
+ :post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
243
+ body: "{\"foo\":\"1\"}") }.to raise_error(WebMock::NetConnectNotAllowedError)
145
244
  end
146
245
  end
147
246
 
@@ -149,177 +248,260 @@ shared_examples_for "stubbing requests" do |*adapter_info|
149
248
  before(:each) do
150
249
  WebMock.reset!
151
250
  stub_request(:post, "www.example.com").
152
- with(:body => { "opt" => {:a => '1', :b => 'five', 'c' => {'d' => ['e', 'f']} }})
251
+ with(body: { "opt" => {:a => '1', :b => 'five', 'c' => {'d' => ['e', 'f']} }})
153
252
  end
154
253
 
155
254
  it "should match if hash matches body" do
156
- http_request(
157
- :post, "http://www.example.com/", :headers => {'Content-Type' => 'application/xml'},
158
- :body => "<opt a=\"1\" b=\"five\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n").status.should == "200"
255
+ expect(http_request(
256
+ :post, "http://www.example.com/", headers: {'Content-Type' => 'application/xml'},
257
+ body: "<opt a=\"1\" b=\"five\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n").status).to eq("200")
159
258
  end
160
259
 
161
260
  it "should match if hash matches body in different form" do
162
- http_request(
163
- :post, "http://www.example.com/", :headers => {'Content-Type' => 'application/xml'},
164
- :body => "<opt b=\"five\" a=\"1\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n").status.should == "200"
261
+ expect(http_request(
262
+ :post, "http://www.example.com/", headers: {'Content-Type' => 'application/xml'},
263
+ body: "<opt b=\"five\" a=\"1\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n").status).to eq("200")
165
264
  end
166
265
 
167
266
  it "should match if hash contains date string" do #Crack creates date object
168
267
  WebMock.reset!
169
268
  stub_request(:post, "www.example.com").
170
- with(:body => {"opt" => {"foo" => "2010-01-01"}})
171
- http_request(
172
- :post, "http://www.example.com/", :headers => {'Content-Type' => 'application/xml'},
173
- :body => "<opt foo=\"2010-01-01\">\n</opt>\n").status.should == "200"
269
+ with(body: {"opt" => {"foo" => "2010-01-01"}})
270
+ expect(http_request(
271
+ :post, "http://www.example.com/", headers: {'Content-Type' => 'application/xml'},
272
+ body: "<opt foo=\"2010-01-01\">\n</opt>\n").status).to eq("200")
174
273
  end
175
274
  end
176
275
  end
177
276
 
178
277
  describe "when body is declared as partial hash matcher" do
179
- before(:each) do
180
- stub_request(:post, "www.example.com").
181
- with(:body => hash_including({:a => '1', 'c' => {'d' => ['e', 'f']} }))
182
- end
278
+ subject(:request) { http_request( :post, "http://www.example.com/",
279
+ body: 'a=1&c[d][]=e&c[d][]=f&b=five') }
183
280
 
184
- describe "for request with url encoded body" do
185
- it "should match request if hash matches body" do
186
- http_request(
187
- :post, "http://www.example.com/",
188
- :body => 'a=1&c[d][]=e&c[d][]=f&b=five').status.should == "200"
281
+ subject(:wrong_request) { http_request(:post, "http://www.example.com/",
282
+ body: 'c[d][]=f&a=1&c[d][]=e').status }
283
+
284
+ describe "when using 'RSpec:Mocks::ArgumentMatchers#hash_including'" do
285
+ before(:each) do
286
+ stub_request(:post, "www.example.com").
287
+ with(body: hash_including(:a, c: {'d' => ['e', 'f']} ))
189
288
  end
190
289
 
191
- it "should not match if hash doesn't match url encoded body" do
192
- lambda {
193
- http_request(
194
- :post, "http://www.example.com/",
195
- :body => 'c[d][]=f&a=1&c[d][]=e').status
196
- }.should raise_error
290
+ describe "for request with url encoded body" do
291
+ it "should match request if hash matches body" do
292
+ expect(request.status).to eq("200")
293
+ end
294
+
295
+ it "should not match if hash doesn't match url encoded body" do
296
+ expect { wrong_request }.to raise_error(WebMock::NetConnectNotAllowedError)
297
+ end
298
+ end
299
+
300
+ describe "for request with json body and content type is set to json" do
301
+ it "should match if hash matches body" do
302
+ expect(http_request(
303
+ :post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
304
+ body: "{\"a\":\"1\",\"c\":{\"d\":[\"e\",\"f\"]},\"b\":\"five\"}").status).to eq("200")
305
+ end
197
306
  end
198
307
  end
199
308
 
200
- describe "for request with json body and content type is set to json" do
201
- it "should match if hash matches body" do
202
- http_request(
203
- :post, "http://www.example.com/", :headers => {'Content-Type' => 'application/json'},
204
- :body => "{\"a\":\"1\",\"c\":{\"d\":[\"e\",\"f\"]},\"b\":\"five\"}").status.should == "200"
309
+ describe "when using 'WebMock::API#hash_including'" do
310
+ before(:each) do
311
+ stub_request(:post, "www.example.com").
312
+ with(body: WebMock::API.hash_including(:a, c: {'d' => ['e', 'f']} ))
313
+ end
314
+
315
+ describe "for request with url encoded body" do
316
+ it "should match request if hash matches body" do
317
+ expect(request.status).to eq("200")
318
+ end
319
+
320
+ it "should not match if hash doesn't match url encoded body" do
321
+ expect { wrong_request }.to raise_error(WebMock::NetConnectNotAllowedError)
322
+ end
323
+ end
324
+
325
+ describe "for request with json body and content type is set to json" do
326
+ it "should match if hash matches body" do
327
+ expect(http_request(
328
+ :post, "http://www.example.com/", headers: {'Content-Type' => 'application/json'},
329
+ body: "{\"a\":\"1\",\"c\":{\"d\":[\"e\",\"f\"]},\"b\":\"five\"}").status).to eq("200")
330
+ end
205
331
  end
206
332
  end
333
+
207
334
  end
208
335
  end
209
336
 
210
337
  describe "based on headers" do
211
338
  it "should match requests if headers are the same" do
212
- stub_request(:get, "www.example.com").with(:headers => SAMPLE_HEADERS )
213
- http_request(
339
+ stub_request(:get, "www.example.com").with(headers: SAMPLE_HEADERS )
340
+ expect(http_request(
214
341
  :get, "http://www.example.com/",
215
- :headers => SAMPLE_HEADERS).status.should == "200"
342
+ headers: SAMPLE_HEADERS).status).to eq("200")
216
343
  end
217
344
 
218
345
  it "should match requests if headers are the same and declared as array" do
219
- stub_request(:get, "www.example.com").with(:headers => {"a" => ["b"]} )
220
- http_request(
346
+ stub_request(:get, "www.example.com").with(headers: {"a" => ["b"]} )
347
+ expect(http_request(
221
348
  :get, "http://www.example.com/",
222
- :headers => {"a" => "b"}).status.should == "200"
349
+ headers: {"a" => "b"}).status).to eq("200")
223
350
  end
224
351
 
225
352
  describe "when multiple headers with the same key are used" do
226
353
  it "should match requests if headers are the same" do
227
- stub_request(:get, "www.example.com").with(:headers => {"a" => ["b", "c"]} )
228
- http_request(
354
+ stub_request(:get, "www.example.com").with(headers: {"a" => ["b", "c"]} )
355
+ expect(http_request(
229
356
  :get, "http://www.example.com/",
230
- :headers => {"a" => ["b", "c"]}).status.should == "200"
357
+ headers: {"a" => ["b", "c"]}).status).to eq("200")
231
358
  end
232
359
 
233
360
  it "should match requests if headers are the same but in different order" do
234
- stub_request(:get, "www.example.com").with(:headers => {"a" => ["b", "c"]} )
235
- http_request(
361
+ stub_request(:get, "www.example.com").with(headers: {"a" => ["b", "c"]} )
362
+ expect(http_request(
236
363
  :get, "http://www.example.com/",
237
- :headers => {"a" => ["c", "b"]}).status.should == "200"
364
+ headers: {"a" => ["c", "b"]}).status).to eq("200")
238
365
  end
239
366
 
240
367
  it "should not match requests if headers are different" do
241
- stub_request(:get, "www.example.com").with(:headers => {"a" => ["b", "c"]})
368
+ stub_request(:get, "www.example.com").with(headers: {"a" => ["b", "c"]})
242
369
 
243
- lambda {
370
+ expect {
244
371
  http_request(
245
372
  :get, "http://www.example.com/",
246
- :headers => {"a" => ["b", "d"]})
247
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
373
+ headers: {"a" => ["b", "d"]})
374
+ }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
248
375
  end
249
376
  end
250
377
 
251
378
  it "should match requests if request headers are not stubbed" do
252
379
  stub_request(:get, "www.example.com")
253
- http_request(
380
+ expect(http_request(
254
381
  :get, "http://www.example.com/",
255
- :headers => SAMPLE_HEADERS).status.should == "200"
382
+ headers: SAMPLE_HEADERS).status).to eq("200")
256
383
  end
257
384
 
258
385
  it "should not match requests if headers are different" do
259
- stub_request(:get, "www.example.com").with(:headers => SAMPLE_HEADERS)
386
+ stub_request(:get, "www.example.com").with(headers: SAMPLE_HEADERS)
260
387
 
261
- lambda {
388
+ expect {
262
389
  http_request(
263
390
  :get, "http://www.example.com/",
264
- :headers => { 'Content-Length' => '9999'})
265
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
391
+ headers: { 'Content-Length' => '9999'})
392
+ }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
266
393
  end
267
394
 
268
395
  it "should not match if accept header is different" do
269
396
  stub_request(:get, "www.example.com").
270
- with(:headers => { 'Accept' => 'application/json'})
271
- lambda {
397
+ with(headers: { 'Accept' => 'application/json'})
398
+ expect {
272
399
  http_request(
273
400
  :get, "http://www.example.com/",
274
- :headers => { 'Accept' => 'application/xml'})
275
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
401
+ headers: { 'Accept' => 'application/xml'})
402
+ }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
276
403
  end
277
404
 
278
405
  describe "declared as regular expressions" do
279
406
  it "should match requests if header values match regular expression" do
280
- stub_request(:get, "www.example.com").with(:headers => { :some_header => /^MyAppName$/ })
281
- http_request(
407
+ stub_request(:get, "www.example.com").with(headers: { some_header: /^MyAppName$/ })
408
+ expect(http_request(
282
409
  :get, "http://www.example.com/",
283
- :headers => { 'some-header' => 'MyAppName' }).status.should == "200"
410
+ headers: { 'some-header' => 'MyAppName' }).status).to eq("200")
284
411
  end
285
412
 
286
413
  it "should not match requests if headers values do not match regular expression" do
287
- stub_request(:get, "www.example.com").with(:headers => { :some_header => /^MyAppName$/ })
414
+ stub_request(:get, "www.example.com").with(headers: { some_header: /^MyAppName$/ })
288
415
 
289
- lambda {
416
+ expect {
290
417
  http_request(
291
418
  :get, "http://www.example.com/",
292
- :headers => { 'some-header' => 'xMyAppName' })
293
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
419
+ headers: { 'some-header' => 'xMyAppName' })
420
+ }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
294
421
  end
295
422
  end
296
423
  end
297
424
 
298
- describe "when stubbing request with basic authentication", :unless => (adapter_info.include?(:no_url_auth)) do
425
+ describe "when stubbing request with userinfo in url", unless: (adapter_info.include?(:no_url_auth)) do
299
426
  it "should match if credentials are the same" do
300
427
  stub_request(:get, "user:pass@www.example.com")
301
- http_request(:get, "http://user:pass@www.example.com/").status.should == "200"
428
+ expect(http_request(:get, "http://user:pass@www.example.com/").status).to eq("200")
302
429
  end
303
430
 
304
431
  it "should not match if credentials are different" do
305
432
  stub_request(:get, "user:pass@www.example.com")
306
- lambda {
307
- http_request(:get, "http://user:pazz@www.example.com/").status.should == "200"
308
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://user:pazz@www.example.com/))
433
+ expect {
434
+ expect(http_request(:get, "http://user:pazz@www.example.com/").status).to eq("200")
435
+ }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://user:pazz@www.example.com/))
309
436
  end
310
437
 
311
438
  it "should not match if credentials are stubbed but not provided in the request" do
312
439
  stub_request(:get, "user:pass@www.example.com")
313
- lambda {
314
- http_request(:get, "http://www.example.com/").status.should == "200"
315
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
440
+ expect {
441
+ expect(http_request(:get, "http://www.example.com/").status).to eq("200")
442
+ }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
316
443
  end
317
444
 
318
445
  it "should not match if credentials are not stubbed but exist in the request" do
319
446
  stub_request(:get, "www.example.com")
320
- lambda {
321
- http_request(:get, "http://user:pazz@www.example.com/").status.should == "200"
322
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://user:pazz@www.example.com/))
447
+ expect {
448
+ expect(http_request(:get, "http://user:pazz@www.example.com/").status).to eq("200")
449
+ }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://user:pazz@www.example.com/))
450
+ end
451
+
452
+ it "should not match if request has credentials provided in basic authentication herader" do
453
+ stub_request(:get, "user:pass@www.example.com")
454
+ expect {
455
+ expect(http_request(:get, "http://www.example.com/", basic_auth: ['user', 'pass']).status).to eq("200")
456
+ }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
457
+ end
458
+ end
459
+
460
+ describe "when stubbing request with basic authentication header" do
461
+ it "should match if credentials are the same" do
462
+ stub_request(:get, "www.example.com").with(basic_auth: ['user', 'pass'])
463
+ expect(http_request(:get, "http://www.example.com/", basic_auth: ['user', 'pass']).status).to eq("200")
464
+ end
465
+
466
+ it "should match if credentials are the same and encode to more than one line" do
467
+ stub_request(:get, "www.example.com").with(basic_auth: ['user' * 5, 'pass' * 5])
468
+ expect(http_request(:get, "http://www.example.com/", basic_auth: ['user' * 5, 'pass' * 5]).status).to eq("200")
469
+ end
470
+
471
+ it "should match if credentials are the same and were provided directly as authentication headers in request" do
472
+ stub_request(:get, "www.example.com").with(basic_auth: ['user', 'pass'])
473
+ expect(http_request(:get, "http://www.example.com/", headers: {'Authorization'=>'Basic dXNlcjpwYXNz'}).status).to eq("200")
474
+ end
475
+
476
+ it "should match if credentials are the same and have been declared in the stub as encoded header" do
477
+ stub_request(:get, "www.example.com").with(headers: {'Authorization'=>'Basic dXNlcjpwYXNz'})
478
+ expect(http_request(:get, "http://www.example.com/", basic_auth: ['user', 'pass']).status).to eq("200")
479
+ end
480
+
481
+ it "should not match if credentials are different" do
482
+ stub_request(:get, "www.example.com").with(basic_auth: ['user', 'pass'])
483
+ expect {
484
+ expect(http_request(:get, "http://www.example.com/", basic_auth: ['user', 'pazz']).status).to eq("200")
485
+ }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/ with headers))
486
+ end
487
+
488
+ it "should not match if credentials are stubbed but not provided in the request" do
489
+ stub_request(:get, "www.example.com").with(basic_auth: ['user', 'pass'])
490
+ expect {
491
+ expect(http_request(:get, "http://www.example.com/").status).to eq("200")
492
+ }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
493
+ end
494
+
495
+ it "should match if credentials are not stubbed but exist in the request" do
496
+ stub_request(:get, "www.example.com")
497
+ expect(http_request(:get, "http://www.example.com/", basic_auth: ['user', 'pass']).status).to eq("200")
498
+ end
499
+
500
+ it "should not match if request has credentials provides in userinfo", unless: (adapter_info.include?(:no_url_auth)) do
501
+ stub_request(:get, "www.example.com").with(basic_auth: ['user', 'pass'])
502
+ expect {
503
+ expect(http_request(:get, "http://user:pass@www.example.com/").status).to eq("200")
504
+ }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://user:pass@www.example.com/))
323
505
  end
324
506
  end
325
507
 
@@ -330,46 +512,46 @@ shared_examples_for "stubbing requests" do |*adapter_info|
330
512
 
331
513
  it 'returns the response returned by the hook' do
332
514
  WebMock.globally_stub_request do |request|
333
- { :body => "global stub body" }
515
+ { body: "global stub body" }
334
516
  end
335
517
 
336
- http_request(:get, "http://www.example.com/").body.should == "global stub body"
518
+ expect(http_request(:get, "http://www.example.com/").body).to eq("global stub body")
337
519
  end
338
520
 
339
521
  it 'does not get cleared when a user calls WebMock.reset!' do
340
522
  WebMock.globally_stub_request do |request|
341
- { :body => "global stub body" }
523
+ { body: "global stub body" }
342
524
  end
343
525
  WebMock.reset!
344
- http_request(:get, "http://www.example.com/").body.should == "global stub body"
526
+ expect(http_request(:get, "http://www.example.com/").body).to eq("global stub body")
345
527
  end
346
528
 
347
529
  it "does not stub the request if the hook does not return anything" do
348
530
  WebMock.globally_stub_request { |r| }
349
- lambda {
531
+ expect {
350
532
  http_request(:get, "http://www.example.com/")
351
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
533
+ }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
352
534
  end
353
535
 
354
536
  it "passes the request to the block" do
355
537
  passed_request = nil
356
538
  WebMock.globally_stub_request do |request|
357
539
  passed_request = request
358
- { :body => "global stub body" }
540
+ { body: "global stub body" }
359
541
  end
360
542
 
361
543
  http_request(:get, "http://www.example.com:456/bar")
362
- passed_request.uri.to_s.should == "http://www.example.com:456/bar"
544
+ expect(passed_request.uri.to_s).to eq("http://www.example.com:456/bar")
363
545
  end
364
546
 
365
547
  it "should call the block only once per request" do
366
548
  call_count = 0
367
549
  WebMock.globally_stub_request do |request|
368
550
  call_count += 1
369
- { :body => "global stub body" }
551
+ { body: "global stub body" }
370
552
  end
371
553
  http_request(:get, "http://www.example.com/")
372
- call_count.should == 1
554
+ expect(call_count).to eq(1)
373
555
  end
374
556
 
375
557
  it 'supports multiple global stubs; the first registered one that returns a non-nil value determines the stub' do
@@ -381,17 +563,17 @@ shared_examples_for "stubbing requests" do |*adapter_info|
381
563
 
382
564
  WebMock.globally_stub_request do |request|
383
565
  stub_invocation_order << :hash_stub
384
- { :body => "global stub body" }
566
+ { body: "global stub body" }
385
567
  end
386
568
 
387
- http_request(:get, "http://www.example.com/").body.should == "global stub body"
388
- stub_invocation_order.should eq([:nil_stub, :hash_stub])
569
+ expect(http_request(:get, "http://www.example.com/").body).to eq("global stub body")
570
+ expect(stub_invocation_order).to eq([:nil_stub, :hash_stub])
389
571
  end
390
572
 
391
573
  [:before, :after].each do |before_or_after|
392
574
  context "when there is also a non-global registered stub #{before_or_after} the global stub" do
393
575
  def stub_non_globally
394
- stub_request(:get, "www.example.com").to_return(:body => 'non-global stub body')
576
+ stub_request(:get, "www.example.com").to_return(body: 'non-global stub body')
395
577
  end
396
578
 
397
579
  define_method :register_stubs do |block|
@@ -401,47 +583,96 @@ shared_examples_for "stubbing requests" do |*adapter_info|
401
583
  end
402
584
 
403
585
  it 'uses the response from the global stub if the block returns a non-nil value' do
404
- register_stubs(lambda { |req| { :body => 'global stub body' } })
405
- http_request(:get, "http://www.example.com/").body.should == "global stub body"
586
+ register_stubs(lambda { |req| { body: 'global stub body' } })
587
+ expect(http_request(:get, "http://www.example.com/").body).to eq("global stub body")
406
588
  end
407
589
 
408
590
  it 'uses the response from the non-global stub if the block returns a nil value' do
409
591
  register_stubs(lambda { |req| nil })
410
- http_request(:get, "http://www.example.com/").body.should == "non-global stub body"
592
+ expect(http_request(:get, "http://www.example.com/").body).to eq("non-global stub body")
411
593
  end
412
594
  end
413
595
  end
596
+
597
+ context "when global stub should be invoked last" do
598
+ before do
599
+ WebMock.globally_stub_request(:after_local_stubs) do
600
+ { body: "global stub body" }
601
+ end
602
+ end
603
+
604
+ it "uses global stub when non-global stub is not defined" do
605
+ expect(http_request(:get, "http://www.example.com/").body).to eq("global stub body")
606
+ end
607
+
608
+ it "uses non-global stub first" do
609
+ stub_request(:get, "www.example.com").to_return(body: 'non-global stub body')
610
+ expect(http_request(:get, "http://www.example.com/").body).to eq("non-global stub body")
611
+ end
612
+ end
414
613
  end
415
614
 
416
615
  describe "when stubbing request with a block evaluated on request" do
417
616
  it "should match if block returns true" do
418
617
  stub_request(:get, "www.example.com").with { |request| true }
419
- http_request(:get, "http://www.example.com/").status.should == "200"
618
+ expect(http_request(:get, "http://www.example.com/").status).to eq("200")
420
619
  end
421
620
 
422
621
  it "should not match if block returns false" do
423
622
  stub_request(:get, "www.example.com").with { |request| false }
424
- lambda {
623
+ expect {
425
624
  http_request(:get, "http://www.example.com/")
426
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
625
+ }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
427
626
  end
428
627
 
429
628
  it "should pass the request to the block" do
430
629
  stub_request(:post, "www.example.com").with { |request| request.body == "wadus" }
431
- http_request(
630
+ expect(http_request(
432
631
  :post, "http://www.example.com/",
433
- :body => "wadus").status.should == "200"
434
- lambda {
435
- http_request(:post, "http://www.example.com/", :body => "jander")
436
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/ with body 'jander'))
632
+ body: "wadus").status).to eq("200")
633
+ expect {
634
+ http_request(:post, "http://www.example.com/", body: "jander")
635
+ }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: POST http://www.example.com/ with body 'jander'))
437
636
  end
438
637
 
439
638
  it "should call the block only once per request" do
440
639
  call_count = 0
441
640
  stub_request(:get, "www.example.com").with { |request| call_count += 1; true }
442
- http_request(:get, "http://www.example.com/").status.should == "200"
443
- call_count.should == 1
641
+ expect(http_request(:get, "http://www.example.com/").status).to eq("200")
642
+ expect(call_count).to eq(1)
444
643
  end
445
644
  end
446
645
  end
646
+
647
+ describe "when request stub was removed" do
648
+ it "should raise an error on request" do
649
+ stub = stub_request(:get, "www.example.com")
650
+
651
+ http_request(:get, "http://www.example.com/")
652
+
653
+ remove_request_stub(stub)
654
+
655
+ expect {
656
+ http_request(:get, "http://www.example.com/")
657
+ }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
658
+ end
659
+ end
660
+
661
+ describe "in Rspec around(:each) hook" do
662
+ # order goes
663
+ # around(:each)
664
+ # before(:each)
665
+ # after(:each)
666
+ # anything after example.run in around(:each)
667
+ around(:each) do |example|
668
+ example.run
669
+ expect {
670
+ http_request(:get, "http://www.example.com/")
671
+ }.to_not raise_error # WebMock::NetConnectNotAllowedError
672
+ end
673
+
674
+ it "should still allow me to make a mocked request" do
675
+ stub_request(:get, "www.example.com")
676
+ end
677
+ end
447
678
  end