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,156 +1,167 @@
1
1
  class MyException < StandardError; end;
2
2
 
3
+ class Responder
4
+ def call(request)
5
+ {body: request.body}
6
+ end
7
+ end
8
+
3
9
  shared_context "declared responses" do |*adapter_info|
4
10
  describe "when request stub declares that request should raise exception" do
5
11
  it "should raise exception" do
6
12
  stub_request(:get, "www.example.com").to_raise(MyException)
7
- lambda {
13
+ expect {
8
14
  http_request(:get, "http://www.example.com/")
9
- }.should raise_error(MyException, "Exception from WebMock")
15
+ }.to raise_error(MyException, "Exception from WebMock")
10
16
  end
11
17
 
12
18
  it "should raise exception if declared as and exception instance" do
13
19
  stub_request(:get, "www.example.com").to_raise(MyException.new("hello world"))
14
- lambda {
20
+ expect {
15
21
  http_request(:get, "http://www.example.com/")
16
- }.should raise_error(MyException, "hello world")
22
+ }.to raise_error(MyException, "hello world")
17
23
  end
18
24
 
19
25
  it "should raise exception if declared as an exception instance" do
20
26
  stub_request(:get, "www.example.com").to_raise("hello world")
21
- lambda {
27
+ expect {
22
28
  http_request(:get, "http://www.example.com/")
23
- }.should raise_error("hello world")
29
+ }.to raise_error("hello world")
24
30
  end
25
31
 
26
32
  it "should raise exception after returning declared successful response first" do
27
- stub_request(:get, "www.example.com").to_return(:body => "abc").then.to_raise(MyException)
28
- http_request(:get, "http://www.example.com/").body.should == "abc"
29
- lambda {
33
+ stub_request(:get, "www.example.com").to_return(body: "abc").then.to_raise(MyException)
34
+ expect(http_request(:get, "http://www.example.com/").body).to eq("abc")
35
+ expect {
30
36
  http_request(:get, "http://www.example.com/")
31
- }.should raise_error(MyException, "Exception from WebMock")
37
+ }.to raise_error(MyException, "Exception from WebMock")
32
38
  end
33
39
  end
34
40
 
35
41
  describe "when request stub declares that request should timeout" do
36
42
  it "should timeout" do
37
43
  stub_request(:get, "www.example.com").to_timeout
38
- lambda {
44
+ expect {
39
45
  http_request(:get, "http://www.example.com/")
40
- }.should raise_error(client_timeout_exception_class)
46
+ }.to raise_error(client_timeout_exception_class)
41
47
  end
42
48
 
43
49
  it "should timeout after returning declared successful response" do
44
- stub_request(:get, "www.example.com").to_return(:body => "abc").then.to_timeout
45
- http_request(:get, "http://www.example.com/").body.should == "abc"
46
- lambda {
50
+ stub_request(:get, "www.example.com").to_return(body: "abc").then.to_timeout
51
+ expect(http_request(:get, "http://www.example.com/").body).to eq("abc")
52
+ expect {
47
53
  http_request(:get, "http://www.example.com/")
48
- }.should raise_error(client_timeout_exception_class)
54
+ }.to raise_error(client_timeout_exception_class)
49
55
  end
50
56
  end
51
57
 
52
58
  describe "when request stub declares that request should return a response" do
53
59
  it "should return response with declared body" do
54
- stub_request(:get, "www.example.com").to_return(:body => "abc")
55
- http_request(:get, "http://www.example.com/").body.should == "abc"
60
+ stub_request(:get, "www.example.com").to_return(body: "abc")
61
+ expect(http_request(:get, "http://www.example.com/").body).to eq("abc")
56
62
  end
57
63
 
58
64
  it "should return response with declared headers" do
59
- stub_request(:get, "www.example.com").to_return(:headers => SAMPLE_HEADERS)
65
+ stub_request(:get, "www.example.com").to_return(headers: SAMPLE_HEADERS)
60
66
  response = http_request(:get, "http://www.example.com/")
61
- response.headers["Content-Length"].should == "8888"
67
+ expect(response.headers["Accept"]).to eq("application/json")
68
+ unless adapter_info.include?(:no_content_length_header)
69
+ expect(response.headers["Content-Length"]).to eq("8888")
70
+ end
62
71
  end
63
72
 
64
73
  it "should return response with declared headers even if there are multiple headers with the same key" do
65
- stub_request(:get, "www.example.com").to_return(:headers => {"a" => ["b", "c"]})
74
+ stub_request(:get, "www.example.com").to_return(headers: {"a" => ["b", "c"]})
66
75
  response = http_request(:get, "http://www.example.com/")
67
- response.headers["A"].should == "b, c"
76
+ expect(response.headers["A"]).to eq("b, c")
68
77
  end
69
78
 
70
79
  it "should return response with declared status code" do
71
- stub_request(:get, "www.example.com").to_return(:status => 500)
72
- http_request(:get, "http://www.example.com/").status.should == "500"
80
+ stub_request(:get, "www.example.com").to_return(status: 500)
81
+ expect(http_request(:get, "http://www.example.com/").status).to eq("500")
73
82
  end
74
83
 
75
- it "should return response with declared status message", :unless => (adapter_info.include?(:no_status_message)) do
76
- stub_request(:get, "www.example.com").to_return(:status => [500, "Internal Server Error"])
84
+ it "should return response with declared status message", unless: (adapter_info.include?(:no_status_message)) do
85
+ stub_request(:get, "www.example.com").to_return(status: [500, "Internal Server Error"])
77
86
  response = http_request(:get, "http://www.example.com/")
78
- response.message.should == "Internal Server Error"
87
+ expect(response.message).to eq("Internal Server Error")
79
88
  end
80
89
 
81
90
  it "should return response with a default status code" do
82
91
  stub_request(:get, "www.example.com")
83
- http_request(:get, "http://www.example.com/").status.should == "200"
92
+ expect(http_request(:get, "http://www.example.com/").status).to eq("200")
84
93
  end
85
94
 
86
- it "should return default response with empty message if response was not declared", :unless => (adapter_info.include?(:no_status_message)) do
95
+ it "should return default response with empty message if response was not declared", unless: (adapter_info.include?(:no_status_message)) do
87
96
  stub_request(:get, "www.example.com")
88
97
  response = http_request(:get, "http://www.example.com/")
89
- response.message.should == ""
98
+ expect(response.message).to eq("")
90
99
  end
91
100
 
92
101
  describe "when response body was declared as IO" do
93
102
  it "should return response body" do
94
- stub_request(:get, "www.example.com").to_return(:body => File.new(__FILE__))
95
- http_request(:get, "http://www.example.com/").body.should == File.new(__FILE__).read
103
+ stub_request(:get, "www.example.com").to_return(body: File.new(__FILE__))
104
+ expect(http_request(:get, "http://www.example.com/").body).to eq(File.read(__FILE__))
96
105
  end
97
106
 
98
107
  it "should return response body if requested many times" do
99
- stub_request(:get, "www.example.com").to_return(:body => File.new(__FILE__))
108
+ stub_request(:get, "www.example.com").to_return(body: File.new(__FILE__))
100
109
  2.times do
101
- http_request(:get, "http://www.example.com/").body.should == File.new(__FILE__).read
110
+ expect(http_request(:get, "http://www.example.com/").body).to eq(File.read(__FILE__))
102
111
  end
103
112
  end
104
113
 
105
114
  it "should close IO after request" do
106
- stub_request(:get, "www.example.com").to_return(:body => @file = File.new(__FILE__))
107
- @file.should be_closed
115
+ stub_request(:get, "www.example.com").to_return(body: @file = File.new(__FILE__))
116
+ expect(@file).to be_closed
108
117
  end
109
118
  end
110
119
 
111
120
  describe "when response parts were declared as lambdas" do
112
121
  it "should return evaluated response body" do
113
- stub_request(:post, "www.example.com").to_return(:body => lambda { |request| request.body })
114
- http_request(:post, "http://www.example.com/", :body => "echo").body.should == "echo"
122
+ stub_request(:post, "www.example.com").to_return(body: lambda { |request| request.body })
123
+ expect(http_request(:post, "http://www.example.com/", body: "echo").body).to eq("echo")
115
124
  end
116
125
 
117
126
  it "should return evaluated response headers" do
118
- stub_request(:post, "www.example.com").to_return(:headers => lambda { |request| request.headers })
119
- http_request(:post, "http://www.example.com/", :body => "abc", :headers => {'A' => 'B'}).headers['A'].should == 'B'
127
+ stub_request(:post, "www.example.com").to_return(headers: lambda { |request| request.headers })
128
+ expect(http_request(:post, "http://www.example.com/", body: "abc", headers: {'A' => 'B'}).headers['A']).to eq('B')
129
+ expect(http_request(:post, "http://www.example.com/", body: "abc", headers: {'A' => 'C'}).headers['A']).to eq('C')
120
130
  end
121
- end
122
131
 
123
- describe "when response was declared as lambda" do
124
- class Responder
125
- def call(request)
126
- {:body => request.body}
127
- end
132
+ it "should evaluate response body for each request" do
133
+ stub_request(:post, "www.example.com").to_return(body: lambda { |request| request.body })
134
+ expect(http_request(:post, "http://www.example.com/", body: "echo").body).to eq("echo")
135
+ expect(http_request(:post, "http://www.example.com/", body: "foxtrot").body).to eq("foxtrot")
128
136
  end
137
+ end
129
138
 
139
+ describe "when response was declared as lambda" do
130
140
  it "should return evaluated response body" do
131
141
  stub_request(:post, "www.example.com").to_return(lambda {|request|
132
- {:body => request.body}
142
+ {body: request.body}
133
143
  })
134
- http_request(:post, "http://www.example.com/", :body => "echo").body.should == "echo"
144
+ expect(http_request(:post, "http://www.example.com/", body: "echo").body).to eq("echo")
145
+ expect(http_request(:post, "http://www.example.com/", body: "foxtrot").body).to eq("foxtrot")
135
146
  end
136
147
 
137
148
  it "should return evaluated response headers" do
138
149
  stub_request(:get, "www.example.com").to_return(lambda { |request|
139
- {:headers => request.headers}
150
+ {headers: request.headers}
140
151
  })
141
- http_request(:get, "http://www.example.com/", :headers => {'A' => 'B'}).headers['A'].should == 'B'
152
+ expect(http_request(:get, "http://www.example.com/", headers: {'A' => 'B'}).headers['A']).to eq('B')
142
153
  end
143
154
 
144
155
  it "should return dynamic response declared as a block" do
145
156
  stub_request(:post, "www.example.com").to_return do |request|
146
- {:body => request.body}
157
+ {body: request.body}
147
158
  end
148
- http_request(:post, "http://www.example.com/", :body => "echo").body.should == "echo"
159
+ expect(http_request(:post, "http://www.example.com/", body: "echo").body).to eq("echo")
149
160
  end
150
161
 
151
162
  it "should return dynamic response declared as an object responding to call" do
152
163
  stub_request(:post, "www.example.com").to_return(Responder.new)
153
- http_request(:post, "http://www.example.com/", :body => "echo").body.should == "echo"
164
+ expect(http_request(:post, "http://www.example.com/", body: "echo").body).to eq("echo")
154
165
  end
155
166
  end
156
167
 
@@ -163,59 +174,77 @@ shared_context "declared responses" do |*adapter_info|
163
174
  end
164
175
 
165
176
  it "should return recorded headers" do
166
- @response.headers.should == {
167
- "Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
168
- "Content-Type"=>"text/html; charset=UTF-8",
169
- "Content-Length"=>"419",
170
- "Connection"=>"Keep-Alive",
171
- "Accept"=>"image/jpeg, image/png"
172
- }
177
+ if adapter_info.include?(:no_content_length_header)
178
+ expect(@response.headers).to eq({
179
+ "Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
180
+ "Content-Type"=>"text/html; charset=UTF-8",
181
+ "Connection"=>"Keep-Alive",
182
+ "Accept"=>"image/jpeg, image/png"
183
+ })
184
+ else
185
+ expect(@response.headers).to eq({
186
+ "Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
187
+ "Content-Type"=>"text/html; charset=UTF-8",
188
+ "Content-Length"=>"419",
189
+ "Connection"=>"Keep-Alive",
190
+ "Accept"=>"image/jpeg, image/png"
191
+ })
192
+ end
173
193
  end
174
194
 
175
195
  it "should return recorded body" do
176
- @response.body.size.should == 419
196
+ expect(@response.body.size).to eq(419)
177
197
  end
178
198
 
179
199
  it "should return recorded status" do
180
- @response.status.should == "202"
200
+ expect(@response.status).to eq("202")
181
201
  end
182
202
 
183
- it "should return recorded status message", :unless => (adapter_info.include?(:no_status_message)) do
184
- @response.message.should == "OK"
203
+ it "should return recorded status message", unless: (adapter_info.include?(:no_status_message)) do
204
+ expect(@response.message).to eq("OK")
185
205
  end
186
206
 
187
207
  it "should ensure file is closed" do
188
- @file.should be_closed
208
+ expect(@file).to be_closed
189
209
  end
190
210
  end
191
211
 
192
212
  describe "when response was declared as a string with a raw response" do
193
213
  before(:each) do
194
- @input = File.new(CURL_EXAMPLE_OUTPUT_PATH).read
214
+ @input = File.read(CURL_EXAMPLE_OUTPUT_PATH)
195
215
  stub_request(:get, "www.example.com").to_return(@input)
196
216
  @response = http_request(:get, "http://www.example.com/")
197
217
  end
198
218
 
199
219
  it "should return recorded headers" do
200
- @response.headers.should == {
201
- "Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
202
- "Content-Type"=>"text/html; charset=UTF-8",
203
- "Content-Length"=>"419",
204
- "Connection"=>"Keep-Alive",
205
- "Accept"=>"image/jpeg, image/png"
206
- }
220
+ if adapter_info.include?(:no_content_length_header)
221
+ expect(@response.headers).to eq({
222
+ "Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
223
+ "Content-Type"=>"text/html; charset=UTF-8",
224
+ "Connection"=>"Keep-Alive",
225
+ "Accept"=>"image/jpeg, image/png"
226
+ })
227
+ else
228
+ expect(@response.headers).to eq({
229
+ "Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
230
+ "Content-Type"=>"text/html; charset=UTF-8",
231
+ "Content-Length"=>"419",
232
+ "Connection"=>"Keep-Alive",
233
+ "Accept"=>"image/jpeg, image/png"
234
+ })
235
+ end
207
236
  end
208
237
 
209
238
  it "should return recorded body" do
210
- @response.body.size.should == 419
239
+ expect(@response.body.size).to eq(419)
211
240
  end
212
241
 
213
242
  it "should return recorded status" do
214
- @response.status.should == "202"
243
+ expect(@response.status).to eq("202")
215
244
  end
216
245
 
217
- it "should return recorded status message", :unless => (adapter_info.include?(:no_status_message)) do
218
- @response.message.should == "OK"
246
+ it "should return recorded status message", unless: (adapter_info.include?(:no_status_message)) do
247
+ expect(@response.message).to eq("OK")
219
248
  end
220
249
  end
221
250
 
@@ -228,65 +257,78 @@ shared_context "declared responses" do |*adapter_info|
228
257
 
229
258
  it "should return response from evaluated file" do
230
259
  stub_request(:get, "www.example.com").to_return(lambda {|request| @files[request.uri.host.to_s] })
231
- http_request(:get, "http://www.example.com/").body.size.should == 419
260
+ expect(http_request(:get, "http://www.example.com/").body.size).to eq(419)
232
261
  end
233
262
 
234
263
  it "should return response from evaluated string" do
235
264
  stub_request(:get, "www.example.com").to_return(lambda {|request| @files[request.uri.host.to_s].read })
236
- http_request(:get, "http://www.example.com/").body.size.should == 419
265
+ expect(http_request(:get, "http://www.example.com/").body.size).to eq(419)
237
266
  end
238
267
  end
239
268
 
240
269
  describe "when response is declared as an Rack app" do
241
270
  it "should return response returned by the rack app" do
242
271
  stub_request(:any, "http://www.example.com/greet").to_rack(MyRackApp)
243
- http_request(:post, 'http://www.example.com/greet', :body => 'name=Jimmy').body.should == 'Good to meet you, Jimmy!'
272
+ expect(http_request(:post, 'http://www.example.com/greet', body: 'name=Jimmy').body).to eq('Good to meet you, Jimmy!')
244
273
  end
245
274
 
246
275
  it "should pass along the port number to the rack app" do
247
276
  stub_request(:get, "http://www.example.com/compute").to_rack(MyRackApp)
248
- http_request(:get, "http://www.example.com/compute").status.should == "200"
277
+ expect(http_request(:get, "http://www.example.com/compute").status).to eq("200")
278
+ end
279
+
280
+ it "preserves content-type header when proxying to a rack app" do
281
+ stub_request(:any, //).to_rack(lambda {|req| [200, {}, ["OK"]] })
282
+
283
+ url = "https://google.com/hi/there"
284
+ headers = {
285
+ "Accept" => "application/json",
286
+ "Content-Type" => "application/json"
287
+ }
288
+
289
+ http_request(:get, url, headers: headers)
290
+ expect(WebMock).to have_requested(:get, url).with(headers: headers)
249
291
  end
250
292
  end
251
293
 
252
294
  describe "when sequences of responses are declared" do
253
295
  it "should return responses one by one if declared in array" do
254
- stub_request(:get, "www.example.com").to_return([ {:body => "1"}, {:body => "2"}, {:body => "3"} ])
255
- http_request(:get, "http://www.example.com/").body.should == "1"
256
- http_request(:get, "http://www.example.com/").body.should == "2"
257
- http_request(:get, "http://www.example.com/").body.should == "3"
296
+ stub_request(:get, "www.example.com").to_return([ {body: "1"}, {body: "2"}, {body: "3"} ])
297
+ expect(http_request(:get, "http://www.example.com/").body).to eq("1")
298
+ expect(http_request(:get, "http://www.example.com/").body).to eq("2")
299
+ expect(http_request(:get, "http://www.example.com/").body).to eq("3")
258
300
  end
259
301
 
260
302
  it "should repeat returning last declared response from a sequence after all responses were returned" do
261
- stub_request(:get, "www.example.com").to_return([ {:body => "1"}, {:body => "2"} ])
262
- http_request(:get, "http://www.example.com/").body.should == "1"
263
- http_request(:get, "http://www.example.com/").body.should == "2"
264
- http_request(:get, "http://www.example.com/").body.should == "2"
303
+ stub_request(:get, "www.example.com").to_return([ {body: "1"}, {body: "2"} ])
304
+ expect(http_request(:get, "http://www.example.com/").body).to eq("1")
305
+ expect(http_request(:get, "http://www.example.com/").body).to eq("2")
306
+ expect(http_request(:get, "http://www.example.com/").body).to eq("2")
265
307
  end
266
308
 
267
309
  it "should return responses one by one if declared as comma separated params" do
268
- stub_request(:get, "www.example.com").to_return({:body => "1"}, {:body => "2"}, {:body => "3"})
269
- http_request(:get, "http://www.example.com/").body.should == "1"
270
- http_request(:get, "http://www.example.com/").body.should == "2"
271
- http_request(:get, "http://www.example.com/").body.should == "3"
310
+ stub_request(:get, "www.example.com").to_return({body: "1"}, {body: "2"}, {body: "3"})
311
+ expect(http_request(:get, "http://www.example.com/").body).to eq("1")
312
+ expect(http_request(:get, "http://www.example.com/").body).to eq("2")
313
+ expect(http_request(:get, "http://www.example.com/").body).to eq("3")
272
314
  end
273
315
 
274
316
  it "should return responses one by one if declared with several to_return invokations" do
275
317
  stub_request(:get, "www.example.com").
276
- to_return({:body => "1"}).
277
- to_return({:body => "2"}).
278
- to_return({:body => "3"})
279
- http_request(:get, "http://www.example.com/").body.should == "1"
280
- http_request(:get, "http://www.example.com/").body.should == "2"
281
- http_request(:get, "http://www.example.com/").body.should == "3"
318
+ to_return({body: "1"}).
319
+ to_return({body: "2"}).
320
+ to_return({body: "3"})
321
+ expect(http_request(:get, "http://www.example.com/").body).to eq("1")
322
+ expect(http_request(:get, "http://www.example.com/").body).to eq("2")
323
+ expect(http_request(:get, "http://www.example.com/").body).to eq("3")
282
324
  end
283
325
 
284
326
  it "should return responses one by one if declared with to_return invocations separated with then syntactic sugar" do
285
- stub_request(:get, "www.example.com").to_return({:body => "1"}).then.
286
- to_return({:body => "2"}).then.to_return({:body => "3"})
287
- http_request(:get, "http://www.example.com/").body.should == "1"
288
- http_request(:get, "http://www.example.com/").body.should == "2"
289
- http_request(:get, "http://www.example.com/").body.should == "3"
327
+ stub_request(:get, "www.example.com").to_return({body: "1"}).then.
328
+ to_return({body: "2"}).then.to_return({body: "3"})
329
+ expect(http_request(:get, "http://www.example.com/").body).to eq("1")
330
+ expect(http_request(:get, "http://www.example.com/").body).to eq("2")
331
+ expect(http_request(:get, "http://www.example.com/").body).to eq("3")
290
332
  end
291
333
 
292
334
  end
@@ -294,38 +336,38 @@ shared_context "declared responses" do |*adapter_info|
294
336
  describe "when responses are declared to return more than once" do
295
337
  it "should repeat one response declared number of times" do
296
338
  stub_request(:get, "www.example.com").
297
- to_return({:body => "1"}).times(2).
298
- to_return({:body => "2"})
299
- http_request(:get, "http://www.example.com/").body.should == "1"
300
- http_request(:get, "http://www.example.com/").body.should == "1"
301
- http_request(:get, "http://www.example.com/").body.should == "2"
339
+ to_return({body: "1"}).times(2).
340
+ to_return({body: "2"})
341
+ expect(http_request(:get, "http://www.example.com/").body).to eq("1")
342
+ expect(http_request(:get, "http://www.example.com/").body).to eq("1")
343
+ expect(http_request(:get, "http://www.example.com/").body).to eq("2")
302
344
  end
303
345
 
304
346
 
305
347
  it "should repeat sequence of response declared number of times" do
306
348
  stub_request(:get, "www.example.com").
307
- to_return({:body => "1"}, {:body => "2"}).times(2).
308
- to_return({:body => "3"})
309
- http_request(:get, "http://www.example.com/").body.should == "1"
310
- http_request(:get, "http://www.example.com/").body.should == "2"
311
- http_request(:get, "http://www.example.com/").body.should == "1"
312
- http_request(:get, "http://www.example.com/").body.should == "2"
313
- http_request(:get, "http://www.example.com/").body.should == "3"
349
+ to_return({body: "1"}, {body: "2"}).times(2).
350
+ to_return({body: "3"})
351
+ expect(http_request(:get, "http://www.example.com/").body).to eq("1")
352
+ expect(http_request(:get, "http://www.example.com/").body).to eq("2")
353
+ expect(http_request(:get, "http://www.example.com/").body).to eq("1")
354
+ expect(http_request(:get, "http://www.example.com/").body).to eq("2")
355
+ expect(http_request(:get, "http://www.example.com/").body).to eq("3")
314
356
  end
315
357
 
316
358
 
317
359
  it "should repeat infinitely last response even if number of declared times is lower" do
318
360
  stub_request(:get, "www.example.com").
319
- to_return({:body => "1"}).times(2)
320
- http_request(:get, "http://www.example.com/").body.should == "1"
321
- http_request(:get, "http://www.example.com/").body.should == "1"
322
- http_request(:get, "http://www.example.com/").body.should == "1"
361
+ to_return({body: "1"}).times(2)
362
+ expect(http_request(:get, "http://www.example.com/").body).to eq("1")
363
+ expect(http_request(:get, "http://www.example.com/").body).to eq("1")
364
+ expect(http_request(:get, "http://www.example.com/").body).to eq("1")
323
365
  end
324
366
 
325
367
  it "should give error if times is declared without specifying response" do
326
- lambda {
368
+ expect {
327
369
  stub_request(:get, "www.example.com").times(3)
328
- }.should raise_error("Invalid WebMock stub declaration. times(N) can be declared only after response declaration.")
370
+ }.to raise_error("Invalid WebMock stub declaration. times(N) can be declared only after response declaration.")
329
371
  end
330
372
 
331
373
  end
@@ -334,33 +376,33 @@ shared_context "declared responses" do |*adapter_info|
334
376
  it "should repeat raising exception declared number of times" do
335
377
  stub_request(:get, "www.example.com").
336
378
  to_raise(MyException).times(2).
337
- to_return({:body => "2"})
338
- lambda {
379
+ to_return({body: "2"})
380
+ expect {
339
381
  http_request(:get, "http://www.example.com/")
340
- }.should raise_error(MyException, "Exception from WebMock")
341
- lambda {
382
+ }.to raise_error(MyException, "Exception from WebMock")
383
+ expect {
342
384
  http_request(:get, "http://www.example.com/")
343
- }.should raise_error(MyException, "Exception from WebMock")
344
- http_request(:get, "http://www.example.com/").body.should == "2"
385
+ }.to raise_error(MyException, "Exception from WebMock")
386
+ expect(http_request(:get, "http://www.example.com/").body).to eq("2")
345
387
  end
346
388
 
347
389
  it "should repeat raising sequence of exceptions declared number of times" do
348
390
  stub_request(:get, "www.example.com").
349
391
  to_raise(MyException, ArgumentError).times(2).
350
- to_return({:body => "2"})
351
- lambda {
392
+ to_return({body: "2"})
393
+ expect {
352
394
  http_request(:get, "http://www.example.com/")
353
- }.should raise_error(MyException, "Exception from WebMock")
354
- lambda {
395
+ }.to raise_error(MyException, "Exception from WebMock")
396
+ expect {
355
397
  http_request(:get, "http://www.example.com/")
356
- }.should raise_error(ArgumentError)
357
- lambda {
398
+ }.to raise_error(ArgumentError)
399
+ expect {
358
400
  http_request(:get, "http://www.example.com/")
359
- }.should raise_error(MyException, "Exception from WebMock")
360
- lambda {
401
+ }.to raise_error(MyException, "Exception from WebMock")
402
+ expect {
361
403
  http_request(:get, "http://www.example.com/")
362
- }.should raise_error(ArgumentError)
363
- http_request(:get, "http://www.example.com/").body.should == "2"
404
+ }.to raise_error(ArgumentError)
405
+ expect(http_request(:get, "http://www.example.com/").body).to eq("2")
364
406
  end
365
407
  end
366
408
  end