/
net_http_spec.rb
385 lines (327 loc) · 14 KB
/
net_http_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
require 'spec_helper'
require 'ostruct'
require 'acceptance/webmock_shared'
require 'acceptance/net_http/net_http_spec_helper'
require 'acceptance/net_http/net_http_shared'
include NetHTTPSpecHelper
describe "Net:HTTP" do
include_examples "with WebMock", :no_url_auth
let(:port) { WebMockServer.instance.port }
describe "marshalling" do
class TestMarshalingInWebMockNetHTTP
attr_accessor :r
end
before(:each) do
@b = TestMarshalingInWebMockNetHTTP.new
end
after(:each) do
WebMock.enable!
end
it "should be possible to load object marshalled when webmock was disabled" do
WebMock.disable!
original_constants = [
Net::HTTP::Get,
Net::HTTP::Post,
Net::HTTP::Put,
Net::HTTP::Delete,
Net::HTTP::Head,
Net::HTTP::Options
]
@b.r = original_constants
original_serialized = Marshal.dump(@b)
Marshal.load(original_serialized)
WebMock.enable!
Marshal.load(original_serialized)
end
it "should be possible to load object marshalled when webmock was enabled" do
WebMock.enable!
new_constants = [
Net::HTTP::Get,
Net::HTTP::Post,
Net::HTTP::Put,
Net::HTTP::Delete,
Net::HTTP::Head,
Net::HTTP::Options
]
@b.r = new_constants
new_serialized = Marshal.dump(@b)
Marshal.load(new_serialized)
WebMock.disable!
Marshal.load(new_serialized)
end
end
describe "constants" do
it "should still have const Get defined on replaced Net::HTTP" do
expect(Object.const_get("Net").const_get("HTTP").const_defined?("Get")).to be_truthy
end
it "should still have const Get within constants on replaced Net::HTTP" do
expect(Object.const_get("Net").const_get("HTTP").constants.map(&:to_s)).to include("Get")
end
it "should still have const Get within constants on replaced Net::HTTP" do
expect(Object.const_get("Net").const_get("HTTP").const_get("Get")).not_to be_nil
end
if Module.method(:const_defined?).arity != 1
it "should still have const Get defined (and not inherited) on replaced Net::HTTP" do
expect(Object.const_get("Net").const_get("HTTP").const_defined?("Get", false)).to be_truthy
end
end
if Module.method(:const_get).arity != 1
it "should still be able to get non inherited constant Get on replaced Net::HTTP" do
expect(Object.const_get("Net").const_get("HTTP").const_get("Get", false)).not_to be_nil
end
end
if Module.method(:constants).arity != 0
it "should still Get within non inherited constants on replaced Net::HTTP" do
expect(Object.const_get("Net").const_get("HTTP").constants(false).map(&:to_s)).to include("Get")
end
end
describe "after WebMock is disabled" do
after(:each) do
WebMock.enable!
end
it "Net::HTTP should have the same constants" do
orig_consts_number = WebMock::HttpLibAdapters::NetHttpAdapter::OriginalNetHTTP.constants.size
Net::HTTP.send(:const_set, "TEST_CONST", 10)
expect(Net::HTTP.constants.size).to eq(orig_consts_number + 1)
WebMock.disable!
expect(Net::HTTP.constants.size).to eq(orig_consts_number + 1)
end
end
end
it "should work with block provided" do
stub_http_request(:get, "www.example.com").to_return(body: "abc"*100000)
expect(Net::HTTP.start("www.example.com") { |query| query.get("/") }.body).to eq("abc"*100000)
end
it "should handle requests with raw binary data" do
body = "\x14\x00\x00\x00\x70\x69\x6e\x67\x00\x00"
stub_http_request(:post, "www.example.com").with(body: body).to_return(body: "abc")
req = Net::HTTP::Post.new("/")
req.body = body
req.content_type = "application/octet-stream"
expect(Net::HTTP.start("www.example.com") { |http| http.request(req)}.body).to eq("abc")
end
it "raises an ArgumentError if passed headers as symbols if RUBY_VERSION < 2.3.0" do
uri = URI.parse("http://google.com/")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
# Net::HTTP calls downcase on header keys assigned with []=
# In Ruby 1.8.7 symbols do not respond to downcase
#
# Meaning you can not assign header keys as symbols in ruby 1.8.7 using []=
if :symbol.respond_to?(:downcase)
request[:InvalidHeaderSinceItsASymbol] = "this will not be valid"
else
request.instance_eval do
@header = request.to_hash.merge({InvalidHeaderSinceItsASymbol: "this will not be valid"})
end
end
if Gem::Version.new(RUBY_VERSION.dup) < Gem::Version.new('2.3.0')
expect do
http.request(request)
end.to raise_error ArgumentError, "Net:HTTP does not accept headers as symbols"
else
stub_http_request(:get, "google.com").with(headers: { InvalidHeaderSinceItsASymbol: "this will not be valid" })
expect do
http.request(request)
end.not_to raise_error
end
end
it "should handle multiple values for the same response header" do
stub_http_request(:get, "www.example.com").to_return(headers: { 'Set-Cookie' => ['foo=bar', 'bar=bazz'] })
response = Net::HTTP.get_response(URI.parse("http://www.example.com/"))
expect(response.get_fields('Set-Cookie')).to eq(['bar=bazz', 'foo=bar'])
end
it "should yield block on response" do
stub_http_request(:get, "www.example.com").to_return(body: "abc")
response_body = ""
http_request(:get, "http://www.example.com/") do |response|
response_body = response.body
end
expect(response_body).to eq("abc")
end
it "should handle Net::HTTP::Post#body" do
stub_http_request(:post, "www.example.com").with(body: "my_params").to_return(body: "abc")
req = Net::HTTP::Post.new("/")
req.body = "my_params"
expect(Net::HTTP.start("www.example.com") { |http| http.request(req)}.body).to eq("abc")
end
it "should handle Net::HTTP::Post#body_stream" do
stub_http_request(:post, "www.example.com").with(body: "my_params").to_return(body: "abc")
req = Net::HTTP::Post.new("/")
req.body_stream = StringIO.new("my_params")
expect(Net::HTTP.start("www.example.com") { |http| http.request(req)}.body).to eq("abc")
end
it "should behave like Net::HTTP and raise error if both request body and body argument are set" do
stub_http_request(:post, "www.example.com").with(body: "my_params").to_return(body: "abc")
req = Net::HTTP::Post.new("/")
req.body = "my_params"
expect {
Net::HTTP.start("www.example.com") { |http| http.request(req, "my_params")}
}.to raise_error("both of body argument and HTTPRequest#body set")
end
it "should return a Net::ReadAdapter from response.body when a stubbed request is made with a block and #read_body" do
WebMock.stub_request(:get, 'http://example.com/').to_return(body: "the body")
response = Net::HTTP.new('example.com', 80).request_get('/') { |r| r.read_body { } }
expect(response.body).to be_a(Net::ReadAdapter)
end
it "should have request 1 time executed in registry after 1 real request", net_connect: true do
WebMock.allow_net_connect!
http = Net::HTTP.new('localhost', port)
http.get('/') {}
expect(WebMock::RequestRegistry.instance.requested_signatures.hash.size).to eq(1)
expect(WebMock::RequestRegistry.instance.requested_signatures.hash.values.first).to eq(1)
end
it "should work with Addressable::URI passed to Net::HTTP.get_response" do
stub_request(:get, 'http://www.example.com/hello?a=1').to_return(body: "abc")
expect(Net::HTTP.get_response(Addressable::URI.parse('http://www.example.com/hello?a=1')).body).to eq("abc")
end
it "should support method calls on stubbed socket" do
WebMock.allow_net_connect!
stub_request(:get, 'www.google.com')#.with(headers: {"My-Header" => 99})
req = Net::HTTP::Get.new('/')
Net::HTTP.start('www.google.com') do |http|
http.request(req, '')
socket = http.instance_variable_get(:@socket)
expect(socket).to be_a(StubSocket)
expect { socket.io.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) }.to_not raise_error
end
end
describe "connecting on Net::HTTP.start" do
before(:each) do
@http = Net::HTTP.new('www.google.com', 443)
@http.use_ssl = true
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
describe "when net http is allowed" do
it "should not connect to the server until the request", net_connect: true do
WebMock.allow_net_connect!
@http.start {|conn|
expect(conn.peer_cert).to be_nil
}
end
it "should connect to the server on start", net_connect: true do
WebMock.allow_net_connect!(net_http_connect_on_start: true)
@http.start {|conn|
cert = OpenSSL::X509::Certificate.new conn.peer_cert
expect(cert).to be_a(OpenSSL::X509::Certificate)
}
end
end
describe "when net http is disabled and allowed only for some hosts" do
it "should not connect to the server until the request", net_connect: true do
WebMock.disable_net_connect!(allow: "www.google.com")
@http.start {|conn|
expect(conn.peer_cert).to be_nil
}
end
it "should connect to the server on start", net_connect: true do
WebMock.disable_net_connect!(allow: "www.google.com", net_http_connect_on_start: true)
@http.start {|conn|
cert = OpenSSL::X509::Certificate.new conn.peer_cert
expect(cert).to be_a(OpenSSL::X509::Certificate)
}
end
it "should connect to the server if the URI matches an regex", net_connect: true do
WebMock.disable_net_connect!(allow: /google.com/)
Net::HTTP.get('www.google.com','/')
end
it "should connect to the server if the URI matches any regex the array", net_connect: true do
WebMock.disable_net_connect!(allow: [/google.com/, /yahoo.com/])
Net::HTTP.get('www.google.com','/')
end
end
end
describe "when net_http_connect_on_start is true" do
before(:each) do
WebMock.allow_net_connect!(net_http_connect_on_start: true)
end
it_should_behave_like "Net::HTTP"
end
describe "when net_http_connect_on_start is false" do
before(:each) do
WebMock.allow_net_connect!(net_http_connect_on_start: false)
end
it_should_behave_like "Net::HTTP"
end
describe 'after_request callback support', net_connect: true do
let(:expected_body_regex) { /hello world/ }
before(:each) do
WebMock.allow_net_connect!
@callback_invocation_count = 0
WebMock.after_request do |_, response|
@callback_invocation_count += 1
@callback_response = response
end
end
after(:each) do
WebMock.reset_callbacks
end
def perform_get_with_returning_block
http_request(:get, "http://localhost:#{port}/") do |response|
return response.body
end
end
it "should support the after_request callback on an request with block and read_body" do
response_body = ''.dup
http_request(:get, "http://localhost:#{port}/") do |response|
response.read_body { |fragment| response_body << fragment }
end
expect(response_body).to match(expected_body_regex)
expect(@callback_response.body).to eq(response_body)
end
it "should support the after_request callback on a request with a returning block" do
response_body = perform_get_with_returning_block
expect(response_body).to match(expected_body_regex)
expect(@callback_response).to be_instance_of(WebMock::Response)
expect(@callback_response.body).to eq(response_body)
end
it "should only invoke the after_request callback once, even for a recursive post request" do
Net::HTTP.new('localhost', port).post('/', nil)
expect(@callback_invocation_count).to eq(1)
end
end
it "should match http headers, even if their values have been set in a request as numbers" do
WebMock.disable_net_connect!
stub_request(:post, "www.example.com").with(headers: {"My-Header" => 99})
uri = URI.parse('http://www.example.com/')
req = Net::HTTP::Post.new(uri.path)
req['My-Header'] = 99
res = Net::HTTP.start(uri.host, uri.port) do |http|
http.request(req, '')
end
end
describe "hostname handling" do
it "should set brackets around the hostname if it is an IPv6 address" do
net_http = Net::HTTP.new('b2dc:5bdf:4f0d::3014:e0ca', 80)
path = '/example.jpg'
expect(WebMock::NetHTTPUtility.get_uri(net_http, path)).to eq('http://[b2dc:5bdf:4f0d::3014:e0ca]:80/example.jpg')
end
it "should not set brackets around the hostname if it is already wrapped by brackets" do
net_http = Net::HTTP.new('[b2dc:5bdf:4f0d::3014:e0ca]', 80)
path = '/example.jpg'
expect(WebMock::NetHTTPUtility.get_uri(net_http, path)).to eq('http://[b2dc:5bdf:4f0d::3014:e0ca]:80/example.jpg')
end
it "should not set brackets around the hostname if it is an IPv4 address" do
net_http = Net::HTTP.new('181.152.137.168', 80)
path = '/example.jpg'
expect(WebMock::NetHTTPUtility.get_uri(net_http, path)).to eq('http://181.152.137.168:80/example.jpg')
end
it "should not set brackets around the hostname if it is a domain" do
net_http = Net::HTTP.new('www.example.com', 80)
path = '/example.jpg'
expect(WebMock::NetHTTPUtility.get_uri(net_http, path)).to eq('http://www.example.com:80/example.jpg')
end
it 'should work with IPv6 addresses' do
WebMock.stub_request(:get, 'http://[b2dc:5bdf:4f0d::3014:e0c1]:80/example.jpg')
uri = URI.parse('http://[b2dc:5bdf:4f0d::3014:e0c1]:80/example.jpg')
request = Net::HTTP::Get.new(uri)
expect { Net::HTTP.start(uri.hostname, uri.port) { |http| http.request(request) } }.not_to raise_error
end
it 'should work with IPv4 addresses' do
WebMock.stub_request(:get, 'http://181.152.137.168:80/example.jpg')
uri = URI.parse('http://181.152.137.168:80/example.jpg')
request = Net::HTTP::Get.new(uri)
expect { Net::HTTP.start(uri.hostname, uri.port) { |http| http.request(request) } }.not_to raise_error
end
end
end