http.rb 0.21.0 → 0.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,406 +0,0 @@
1
- # spec/HTTP/put_spec.rb
2
-
3
- require_relative '../spec_helper'
4
- require 'http'
5
-
6
- describe ".put" do
7
- context "with uri-only supplied" do
8
- before do
9
- stub_request(:put, 'http://example.com/path').
10
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Ruby'}).
11
- to_return(status: 200, body: '', headers: {})
12
- end
13
-
14
- context "uri as a string" do
15
- let(:uri){'http://example.com/path'}
16
- let(:parsed_uri){URI.parse(uri)}
17
- let(:net_http_object){Net::HTTP.new(parsed_uri.host, parsed_uri.port)}
18
-
19
- it "creates an instance of URI" do
20
- expect(URI).to receive(:parse).with(uri).and_return(parsed_uri)
21
- response = HTTP.put(uri)
22
- expect(response.success?).to eq(true)
23
- end
24
-
25
- it "creates a new Net::HTTP object" do
26
- expect(Net::HTTP).to receive(:new).with(parsed_uri.host, parsed_uri.port).and_return(net_http_object)
27
- response = HTTP.put(uri)
28
- expect(response.success?).to eq(true)
29
- end
30
- end
31
-
32
- context "uri as a URI" do
33
- let(:uri_string){'http://example.com/path'}
34
- let(:uri){URI.parse(uri_string)}
35
- let(:net_http_object){Net::HTTP.new(uri.host, uri.port)}
36
-
37
- it "returns an instance of URI" do
38
- expect(uri).to eq(uri)
39
- response = HTTP.put(uri)
40
- expect(response.success?).to eq(true)
41
- end
42
-
43
- it "creates a new Net::HTTP object" do
44
- expect(Net::HTTP).to receive(:new).with(uri.host, uri.port).and_return(net_http_object)
45
- response = HTTP.put(uri)
46
- expect(response.success?).to eq(true)
47
- end
48
- end
49
- end
50
-
51
- context "with form data supplied" do
52
- let(:uri){'http://example.com/path'}
53
- let(:parsed_uri){URI.parse(uri)}
54
- let(:args) do; {a: 1, b: 2}; end
55
- let(:headers) do; {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Ruby'}; end
56
- let(:encoded_form_data){args.x_www_form_urlencode}
57
- let(:request_uri){parsed_uri.request_uri}
58
- let(:request_object){Net::HTTP::Put.new(request_uri)}
59
-
60
- before do
61
- stub_request(:put, "http://example.com/path").
62
- with(body: encoded_form_data, headers: headers).
63
- to_return(status: 200, body: '', headers: {})
64
- end
65
-
66
- it "sets the form data" do
67
- allow(Net::HTTP::Put).to receive(:new).with(request_uri).and_return(request_object)
68
- response = HTTP.put(uri, args, headers)
69
- expect(request_object.body).to eq(encoded_form_data)
70
- expect(response.success?).to eq(true)
71
- end
72
-
73
- it "creates a new Net::HTTP::Put object" do
74
- expect(Net::HTTP::Put).to receive(:new).with(request_uri).and_return(request_object)
75
- response = HTTP.put(uri, args, headers)
76
- expect(response.success?).to eq(true)
77
- end
78
- end
79
-
80
- context "with raw form data supplied" do
81
- let(:uri){'http://example.com/path'}
82
- let(:parsed_uri){URI.parse(uri)}
83
- let(:args) do; {a: 1, b: 2}.x_www_form_urlencode; end
84
- let(:headers) do; {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Ruby'}; end
85
- let(:encoded_form_data){args}
86
- let(:request_uri){parsed_uri.request_uri}
87
- let(:request_object){Net::HTTP::Put.new(request_uri)}
88
-
89
- before do
90
- stub_request(:put, "http://example.com/path").
91
- with(body: encoded_form_data, headers: headers).
92
- to_return(status: 200, body: '', headers: {})
93
- end
94
-
95
- it "sets the form data" do
96
- allow(Net::HTTP::Put).to receive(:new).with(request_uri).and_return(request_object)
97
- response = HTTP.put(uri, args, headers)
98
- expect(request_object.body).to eq(encoded_form_data)
99
- expect(response.success?).to eq(true)
100
- end
101
-
102
- it "creates a new Net::HTTP::Put object" do
103
- expect(Net::HTTP::Put).to receive(:new).with(request_uri).and_return(request_object)
104
- response = HTTP.put(uri, args, headers)
105
- expect(response.success?).to eq(true)
106
- end
107
- end
108
-
109
- context "with JSON data supplied" do
110
- let(:uri){'http://example.com/path'}
111
- let(:parsed_uri){URI.parse(uri)}
112
- let(:args) do; {a: 1, b: 2}; end
113
- let(:headers) do; {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'}; end
114
- let(:json_data){JSON.dump(args)}
115
- let(:request_uri){parsed_uri.request_uri}
116
- let(:request_object){Net::HTTP::Put.new(request_uri)}
117
-
118
- before do
119
- stub_request(:put, "http://example.com/path").
120
- with(body: json_data, headers: headers).
121
- to_return(status: 200, body: '', headers: {})
122
- end
123
-
124
- it "sets the body" do
125
- allow(Net::HTTP::Put).to receive(:new).with(request_uri).and_return(request_object)
126
- response = HTTP.put(uri, args, headers)
127
- expect(request_object.body).to eq(json_data)
128
- expect(response.success?).to eq(true)
129
- end
130
-
131
- it "creates a new Net::HTTP::Put object" do
132
- expect(Net::HTTP::Put).to receive(:new).with(request_uri).and_return(request_object)
133
- response = HTTP.put(uri, args, headers)
134
- expect(response.success?).to eq(true)
135
- end
136
- end
137
-
138
- context "with raw JSON data supplied" do
139
- let(:uri){'http://example.com/path'}
140
- let(:parsed_uri){URI.parse(uri)}
141
- let(:args) do; JSON.dump({a: 1, b: 2}); end
142
- let(:headers) do; {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'}; end
143
- let(:json_data){args}
144
- let(:request_uri){parsed_uri.request_uri}
145
- let(:request_object){Net::HTTP::Put.new(request_uri)}
146
-
147
- before do
148
- stub_request(:put, "http://example.com/path").
149
- with(body: json_data, headers: headers).
150
- to_return(status: 200, body: '', headers: {})
151
- end
152
-
153
- it "sets the body" do
154
- allow(Net::HTTP::Put).to receive(:new).with(request_uri).and_return(request_object)
155
- response = HTTP.put(uri, args, headers)
156
- expect(request_object.body).to eq(json_data)
157
- expect(response.success?).to eq(true)
158
- end
159
-
160
- it "creates a new Net::HTTP::Put object" do
161
- expect(Net::HTTP::Put).to receive(:new).with(request_uri).and_return(request_object)
162
- response = HTTP.put(uri, args, headers)
163
- expect(response.success?).to eq(true)
164
- end
165
- end
166
-
167
- context "with headers supplied" do
168
- let(:uri){'http://example.com/path'}
169
- let(:parsed_uri){URI.parse(uri)}
170
- let(:headers) do; {'User-Agent' => 'Rspec'}; end
171
- let(:request_uri){parsed_uri.request_uri}
172
- let(:request_object){Net::HTTP::Put.new(request_uri)}
173
-
174
- before do
175
- stub_request(:put, 'http://example.com/path').
176
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Rspec'}).
177
- to_return(status: 200, body: '', headers: {})
178
- end
179
-
180
- it "sets the headers on the request object" do
181
- allow(Net::HTTP::Put).to receive(:new).with(request_uri).and_return(request_object)
182
- response = HTTP.put(uri, {}, headers)
183
- expect(request_object['User-Agent']).to eq('Rspec')
184
- expect(response.success?).to eq(true)
185
- end
186
- end
187
-
188
- context "when different content-type header key cases are supplied" do
189
- let(:uri){'http://example.com/path'}
190
- let(:parsed_uri){URI.parse(uri)}
191
- let(:headers) do; {content_type_key => 'application/json'}; end
192
- let(:request_uri){parsed_uri.request_uri}
193
- let(:request_object){Net::HTTP::Put.new(request_uri)}
194
- let(:args)do; {a: 1, b: 2}; end
195
-
196
- before do
197
- stub_request(:put, 'http://example.com/path').
198
- with(headers: {'Content-Type'=>'application/json'}).
199
- to_return(status: 200, body: '', headers: {})
200
- end
201
-
202
- context "title-cased" do
203
- let(:content_type_key){'Content-Type'}
204
-
205
- it "detects the content type" do
206
- allow(Net::HTTP::Put).to receive(:new).with(request_uri).and_return(request_object)
207
- response = HTTP.put(uri, args, headers)
208
- expect(request_object['Content-Type']).to eq('application/json') # The request object doesn't care about the case of the content-type key.
209
- expect(request_object.body).to eq(JSON.dump(args))
210
- end
211
- end
212
-
213
- context "title-case only at the start" do
214
- let(:content_type_key){'Content-type'}
215
-
216
- it "detects the content type" do
217
- allow(Net::HTTP::Put).to receive(:new).with(request_uri).and_return(request_object)
218
- response = HTTP.put(uri, args, headers)
219
- expect(request_object['Content-Type']).to eq('application/json') # The request object doesn't care about the case of the content-type key.
220
- expect(request_object.body).to eq(JSON.dump(args))
221
- end
222
- end
223
-
224
- context "lowercase" do
225
- let(:content_type_key){'content-type'}
226
-
227
- it "detects the content type" do
228
- allow(Net::HTTP::Put).to receive(:new).with(request_uri).and_return(request_object)
229
- response = HTTP.put(uri, args, headers)
230
- expect(request_object['Content-Type']).to eq('application/json') # The request object doesn't care about the case of the content-type key.
231
- expect(request_object.body).to eq(JSON.dump(args))
232
- end
233
- end
234
- end
235
-
236
- context "with options supplied" do
237
- let(:uri){'http://example.com/path'}
238
- let(:parsed_uri){URI.parse(uri)}
239
- let(:net_http_object){Net::HTTP.new(parsed_uri.host, parsed_uri.port)}
240
- let(:options) do; {use_ssl: true}; end
241
-
242
- before do
243
- stub_request(:put, 'https://example.com:80/path').
244
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Ruby'}).
245
- to_return(status: 200, body: '', headers: {})
246
- end
247
-
248
- it "sets the use_ssl option on the Net::HTTP instance" do
249
- allow(Net::HTTP).to receive(:new).with(parsed_uri.host, parsed_uri.port).and_return(net_http_object)
250
- response = HTTP.put(uri, {}, {}, options)
251
- expect(net_http_object.instance_variable_get(:@use_ssl)).to be_truthy
252
- expect(response.success?).to eq(true)
253
- end
254
- end
255
-
256
- context "with block supplied" do
257
- let(:uri){'http://example.com/path'}
258
-
259
- before do
260
- stub_request(:put, 'http://example.com/path').
261
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Ruby'}).
262
- to_return(status: 200, body: '', headers: {})
263
- end
264
-
265
- it "yields an instance of Net::HTTPResponse" do
266
- expect{|b| HTTP.put(uri, &b)}.to yield_with_args(Net::HTTPResponse)
267
- end
268
- end
269
-
270
- context "with redirection" do
271
- let(:request_uri){'http://example.com/path'}
272
- let(:redirect_uri){'http://redirected.com'}
273
-
274
- before do
275
- stub_request(:get, redirect_uri).
276
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
277
- to_return(status: 200, body: '', headers: {})
278
- end
279
-
280
- context "via 301" do
281
- before do
282
- stub_request(:put, request_uri).
283
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
284
- to_return(status: 301, body: '', headers: {'location' => redirect_uri})
285
- end
286
-
287
- it "does a redirect" do
288
- expect(HTTP).to receive(:put).once.with(request_uri).and_call_original
289
- expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: OpenSSL::SSL::VERIFY_PEER}).and_call_original
290
- response = HTTP.put(request_uri)
291
- expect(response.success?).to eq(true)
292
- end
293
- end
294
-
295
- context "via 302" do
296
- before do
297
- stub_request(:put, request_uri).
298
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
299
- to_return(status: 302, body: '', headers: {'location' => redirect_uri})
300
- end
301
-
302
- it "does a redirect" do
303
- expect(HTTP).to receive(:put).once.with(request_uri).and_call_original
304
- expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: OpenSSL::SSL::VERIFY_PEER}).and_call_original
305
- response = HTTP.put(request_uri)
306
- expect(response.success?).to eq(true)
307
- end
308
- end
309
- end
310
-
311
- context "with path only redirection" do
312
- let(:request_uri){'http://example.com/path'}
313
- let(:redirect_path){'/new_path'}
314
- let(:redirect_uri){"http://example.com#{redirect_path}"}
315
-
316
- before do
317
- stub_request(:get, redirect_uri).
318
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
319
- to_return(status: 200, body: '', headers: {})
320
- end
321
-
322
- context "via 301" do
323
- before do
324
- stub_request(:put, request_uri).
325
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
326
- to_return(status: 301, body: '', headers: {'location' => redirect_path})
327
- end
328
-
329
- it "does a redirect" do
330
- expect(HTTP).to receive(:put).once.with(request_uri).and_call_original
331
- expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: OpenSSL::SSL::VERIFY_PEER}).and_call_original
332
- response = HTTP.put(request_uri)
333
- expect(response.success?).to eq(true)
334
- end
335
- end
336
-
337
- context "via 302" do
338
- before do
339
- stub_request(:put, request_uri).
340
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
341
- to_return(status: 302, body: '', headers: {'location' => redirect_path})
342
- end
343
-
344
- it "does a redirect" do
345
- expect(HTTP).to receive(:put).once.with(request_uri).and_call_original
346
- expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: OpenSSL::SSL::VERIFY_PEER}).and_call_original
347
- response = HTTP.put(request_uri)
348
- expect(response.success?).to eq(true)
349
- end
350
- end
351
- end
352
-
353
- context "with verb-preserving redirection via 307" do
354
- let(:request_uri){'http://example.com/path'}
355
- let(:redirect_uri){'http://redirected.com'}
356
-
357
- before do
358
- stub_request(:put, request_uri).
359
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
360
- to_return(status: 307, body: '', headers: {'location' => redirect_uri})
361
- stub_request(:put, redirect_uri).
362
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
363
- to_return(status: 200, body: '', headers: {})
364
- end
365
-
366
- it "preserves the verb" do
367
- expect(HTTP).to receive(:put).with(request_uri).and_call_original.ordered
368
- expect(HTTP).to receive(:put).with(redirect_uri, '', {}, {use_ssl: false, verify_mode: OpenSSL::SSL::VERIFY_PEER}).and_call_original.ordered
369
- response = HTTP.put(request_uri)
370
- expect(response.success?).to eq(true)
371
- end
372
- end
373
-
374
- context "no_redirect true" do
375
- let(:request_uri){'http://example.com/path'}
376
- let(:redirect_uri){'http://redirected.com'}
377
-
378
- context "via 301" do
379
- before do
380
- stub_request(:put, request_uri).
381
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
382
- to_return(status: 301, body: '', headers: {'location' => redirect_uri})
383
- end
384
-
385
- it "doesn't redirect" do
386
- expect(HTTP).to receive(:put).once.with(request_uri, {}, {}, {no_redirect: true}).and_call_original
387
- response = HTTP.put(request_uri, {}, {}, {no_redirect: true})
388
- expect(response.redirection?).to eq(true)
389
- end
390
- end
391
-
392
- context "via 302" do
393
- before do
394
- stub_request(:put, request_uri).
395
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
396
- to_return(status: 302, body: '', headers: {'location' => redirect_uri})
397
- end
398
-
399
- it "doesn't redirect" do
400
- expect(HTTP).to receive(:put).once.with(request_uri, {}, {}, {no_redirect: true}).and_call_original
401
- response = HTTP.put(request_uri, {}, {}, {no_redirect: true})
402
- expect(response.redirection?).to eq(true)
403
- end
404
- end
405
- end
406
- end
@@ -1,247 +0,0 @@
1
- # spec/HTTP/trace_spec.rb
2
-
3
- require_relative '../spec_helper'
4
- require 'http'
5
-
6
- describe ".trace" do
7
- context "with uri-only supplied" do
8
- before do
9
- stub_request(:trace, 'http://example.com/path').
10
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
11
- to_return(status: 200, body: '', headers: {})
12
- end
13
-
14
- context "uri as a string" do
15
- let(:uri){'http://example.com/path'}
16
- let(:parsed_uri){URI.parse(uri)}
17
- let(:net_http_object){Net::HTTP.new(parsed_uri.host, parsed_uri.port)}
18
-
19
- it "creates an instance of URI" do
20
- expect(URI).to receive(:parse).with(uri).and_return(parsed_uri)
21
- response = HTTP.trace(uri)
22
- expect(response.success?).to eq(true)
23
- end
24
-
25
- it "creates a new Net::HTTP object" do
26
- expect(Net::HTTP).to receive(:new).with(parsed_uri.host, parsed_uri.port).and_return(net_http_object)
27
- response = HTTP.trace(uri)
28
- expect(response.success?).to eq(true)
29
- end
30
- end
31
-
32
- context "uri as a URI" do
33
- let(:uri_string){'http://example.com/path'}
34
- let(:uri){URI.parse(uri_string)}
35
- let(:net_http_object){Net::HTTP.new(uri.host, uri.port)}
36
-
37
- it "returns an instance of URI" do
38
- expect(uri).to eq(uri)
39
- HTTP.trace(uri)
40
- end
41
-
42
- it "creates a new Net::HTTP object" do
43
- expect(Net::HTTP).to receive(:new).with(uri.host, uri.port).and_return(net_http_object)
44
- response = HTTP.trace(uri)
45
- expect(response.success?).to eq(true)
46
- end
47
- end
48
- end
49
-
50
- context "with args supplied" do
51
- let(:uri){'http://example.com/path'}
52
- let(:parsed_uri){URI.parse(uri)}
53
- let(:args) do; {a: 1, b: 2}; end
54
- let(:x_www_form_urlencoded_arguments) do; args.x_www_form_urlencode; end
55
- let(:trace_argument){parsed_uri.request_uri + '?' + x_www_form_urlencoded_arguments}
56
- let(:request_object){Net::HTTP::Trace.new(trace_argument)}
57
-
58
- before do
59
- stub_request(:trace, 'http://example.com/path?a=1&b=2').
60
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
61
- to_return(status: 200, body: '', headers: {})
62
- end
63
-
64
- it "x_www_form_urlencode's the args" do
65
- expect(args).to receive(:x_www_form_urlencode).and_return(x_www_form_urlencoded_arguments)
66
- response = HTTP.trace(uri, args)
67
- expect(response.success?).to eq(true)
68
- end
69
-
70
- it "creates a new Net::HTTP::Trace object" do
71
- expect(Net::HTTP::Trace).to receive(:new).with(trace_argument).and_return(request_object)
72
- response = HTTP.trace(uri, args)
73
- expect(response.success?).to eq(true)
74
- end
75
- end
76
-
77
- context "with headers supplied" do
78
- let(:uri){'http://example.com/path'}
79
- let(:parsed_uri){URI.parse(uri)}
80
- let(:headers) do; {'User-Agent' => 'Rspec'}; end
81
- let(:trace_argument){parsed_uri.request_uri}
82
- let(:request_object){Net::HTTP::Trace.new(trace_argument)}
83
-
84
- before do
85
- stub_request(:trace, 'http://example.com/path').
86
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Rspec'}).
87
- to_return(status: 200, body: '', headers: {})
88
- end
89
-
90
- it "sets the headers on the request object" do
91
- allow(Net::HTTP::Trace).to receive(:new).with(trace_argument).and_return(request_object)
92
- response = HTTP.trace(uri, {}, headers)
93
- expect(request_object['User-Agent']).to eq('Rspec')
94
- expect(response.success?).to eq(true)
95
- end
96
- end
97
-
98
- context "with options supplied" do
99
- let(:uri){'http://example.com/path'}
100
- let(:parsed_uri){URI.parse(uri)}
101
- let(:net_http_object){Net::HTTP.new(parsed_uri.host, parsed_uri.port)}
102
- let(:options) do; {use_ssl: true}; end
103
-
104
- before do
105
- stub_request(:trace, 'https://example.com:80/path').
106
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
107
- to_return(status: 200, body: '', headers: {})
108
- end
109
-
110
- it "sets the use_ssl option on the Net::HTTP instance" do
111
- allow(Net::HTTP).to receive(:new).with(parsed_uri.host, parsed_uri.port).and_return(net_http_object)
112
- response = HTTP.trace(uri, {}, {}, options)
113
- expect(net_http_object.instance_variable_get(:@use_ssl)).to be_truthy
114
- expect(response.success?).to eq(true)
115
- end
116
- end
117
-
118
- context "with block supplied" do
119
- let(:uri){'http://example.com/path'}
120
-
121
- before do
122
- stub_request(:trace, 'http://example.com/path').
123
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
124
- to_return(status: 200, body: '', headers: {})
125
- end
126
-
127
- it "yields an instance of Net::HTTPResponse" do
128
- expect{|b| HTTP.trace(uri, &b)}.to yield_with_args(Net::HTTPResponse)
129
- end
130
- end
131
-
132
- context "with redirection" do
133
- let(:request_uri){'http://example.com/path'}
134
- let(:redirect_uri){'http://redirected.com'}
135
-
136
- before do
137
- stub_request(:get, redirect_uri).
138
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
139
- to_return(status: 200, body: '', headers: {})
140
- end
141
-
142
- context "via 301" do
143
- before do
144
- stub_request(:trace, request_uri).
145
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
146
- to_return(status: 301, body: '', headers: {'location' => redirect_uri})
147
- end
148
-
149
- it "does a redirect" do
150
- expect(HTTP).to receive(:trace).once.with(request_uri).and_call_original
151
- expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: OpenSSL::SSL::VERIFY_PEER}).and_call_original
152
- response = HTTP.trace(request_uri)
153
- expect(response.success?).to eq(true)
154
- end
155
- end
156
-
157
- context "via 302" do
158
- before do
159
- stub_request(:trace, request_uri).
160
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
161
- to_return(status: 302, body: '', headers: {'location' => redirect_uri})
162
- end
163
-
164
- it "does a redirect" do
165
- expect(HTTP).to receive(:trace).once.with(request_uri).and_call_original
166
- expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: OpenSSL::SSL::VERIFY_PEER}).and_call_original
167
- response = HTTP.trace(request_uri)
168
- expect(response.success?).to eq(true)
169
- end
170
- end
171
- end
172
-
173
- context "with path only redirection" do
174
- let(:request_uri){'http://example.com/path'}
175
- let(:redirect_path){'/new_path'}
176
- let(:redirect_uri){"http://example.com#{redirect_path}"}
177
-
178
- before do
179
- stub_request(:get, redirect_uri).
180
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
181
- to_return(status: 200, body: '', headers: {})
182
- end
183
-
184
- context "via 301" do
185
- before do
186
- stub_request(:trace, request_uri).
187
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
188
- to_return(status: 301, body: '', headers: {'location' => redirect_path})
189
- end
190
-
191
- it "does a redirect" do
192
- expect(HTTP).to receive(:trace).once.with(request_uri).and_call_original
193
- expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: OpenSSL::SSL::VERIFY_PEER}).and_call_original
194
- response = HTTP.trace(request_uri)
195
- expect(response.success?).to eq(true)
196
- end
197
- end
198
-
199
- context "via 302" do
200
- before do
201
- stub_request(:trace, request_uri).
202
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
203
- to_return(status: 302, body: '', headers: {'location' => redirect_path})
204
- end
205
-
206
- it "does a redirect" do
207
- expect(HTTP).to receive(:trace).once.with(request_uri).and_call_original
208
- expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: OpenSSL::SSL::VERIFY_PEER}).and_call_original
209
- response = HTTP.trace(request_uri)
210
- expect(response.success?).to eq(true)
211
- end
212
- end
213
- end
214
-
215
- context "no_redirect true" do
216
- let(:request_uri){'http://example.com/path'}
217
- let(:redirect_uri){'http://redirected.com'}
218
-
219
- context "via 301" do
220
- before do
221
- stub_request(:trace, request_uri).
222
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
223
- to_return(status: 301, body: '', headers: {'location' => redirect_uri})
224
- end
225
-
226
- it "doesn't redirect" do
227
- expect(HTTP).to receive(:trace).once.with(request_uri, {}, {}, {no_redirect: true}).and_call_original
228
- response = HTTP.trace(request_uri, {}, {}, {no_redirect: true})
229
- expect(response.redirection?).to eq(true)
230
- end
231
- end
232
-
233
- context "via 302" do
234
- before do
235
- stub_request(:trace, request_uri).
236
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
237
- to_return(status: 302, body: '', headers: {'location' => redirect_uri})
238
- end
239
-
240
- it "doesn't redirect" do
241
- expect(HTTP).to receive(:trace).once.with(request_uri, {}, {}, {no_redirect: true}).and_call_original
242
- response = HTTP.trace(request_uri, {}, {}, {no_redirect: true})
243
- expect(response.redirection?).to eq(true)
244
- end
245
- end
246
- end
247
- end