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.
- checksums.yaml +4 -4
- data/CHANGELOG +86 -36
- data/Rakefile +4 -4
- data/http.rb.gemspec +8 -7
- data/lib/HTTP/VERSION.rb +1 -1
- data/lib/HTTP/request.rb +1 -1
- data/test/HTTP/RETRY_test.rb +281 -0
- data/test/HTTP/delete_test.rb +265 -0
- data/test/HTTP/get_test.rb +304 -0
- data/{spec/HTTP/head_spec.rb → test/HTTP/head_test.rb} +27 -24
- data/test/HTTP/options_test.rb +246 -0
- data/test/HTTP/patch_test.rb +408 -0
- data/test/HTTP/post_test.rb +448 -0
- data/test/HTTP/put_test.rb +408 -0
- data/test/HTTP/trace_test.rb +246 -0
- data/{spec/spec_helper.rb → test/helper.rb} +15 -2
- metadata +28 -14
- data/spec/HTTP/RETRY_spec.rb +0 -251
- data/spec/HTTP/delete_spec.rb +0 -268
- data/spec/HTTP/get_spec.rb +0 -295
- data/spec/HTTP/options_spec.rb +0 -247
- data/spec/HTTP/patch_spec.rb +0 -406
- data/spec/HTTP/post_spec.rb +0 -447
- data/spec/HTTP/put_spec.rb +0 -406
- data/spec/HTTP/trace_spec.rb +0 -247
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
# test/HTTP/delete_test.rb
|
|
2
|
+
|
|
3
|
+
require_relative '../helper'
|
|
4
|
+
|
|
5
|
+
describe ".delete" do
|
|
6
|
+
describe "with uri-only supplied" do
|
|
7
|
+
before do
|
|
8
|
+
stub_request(:delete, 'http://example.com/path').
|
|
9
|
+
with(headers: {'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent' => 'Ruby'}).
|
|
10
|
+
to_return(status: 200, body: '', headers: {})
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
describe "uri as a string" do
|
|
14
|
+
let(:uri){'http://example.com/path'}
|
|
15
|
+
|
|
16
|
+
it "creates an instance of URI" do
|
|
17
|
+
received_arg = nil
|
|
18
|
+
parsed_uri = URI.parse(uri)
|
|
19
|
+
URI.stub(:parse, ->(arg){received_arg = arg; parsed_uri}) do
|
|
20
|
+
response = HTTP.delete(uri)
|
|
21
|
+
_(received_arg).must_equal(uri)
|
|
22
|
+
_(response.success?).must_equal(true)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
it "creates a new Net::HTTP object" do
|
|
27
|
+
received_args = nil
|
|
28
|
+
parsed_uri = URI.parse(uri)
|
|
29
|
+
net_http_object = Net::HTTP.new(parsed_uri.host, parsed_uri.port)
|
|
30
|
+
Net::HTTP.stub(:new, ->(*args){received_args = args; net_http_object}) do
|
|
31
|
+
response = HTTP.delete(uri)
|
|
32
|
+
_(received_args).must_equal([parsed_uri.host, parsed_uri.port])
|
|
33
|
+
_(response.success?).must_equal(true)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
describe "uri as a URI" do
|
|
39
|
+
let(:uri){URI.parse('http://example.com/path')}
|
|
40
|
+
|
|
41
|
+
it "creates a new Net::HTTP object" do
|
|
42
|
+
received_args = nil
|
|
43
|
+
net_http_object = Net::HTTP.new(uri.host, uri.port)
|
|
44
|
+
Net::HTTP.stub(:new, ->(*args){received_args = args; net_http_object}) do
|
|
45
|
+
response = HTTP.delete(uri)
|
|
46
|
+
_(received_args).must_equal([uri.host, uri.port])
|
|
47
|
+
_(response.success?).must_equal(true)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
describe "with args supplied" do
|
|
54
|
+
let(:uri){'http://example.com/path'}
|
|
55
|
+
let(:parsed_uri){URI.parse(uri)}
|
|
56
|
+
|
|
57
|
+
before do
|
|
58
|
+
stub_request(:delete, 'http://example.com/path?a=1&b=2').
|
|
59
|
+
with(headers: {'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent' => 'Ruby'}).
|
|
60
|
+
to_return(status: 200, body: '', headers: {})
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
it "x_www_form_urlencode's the args" do
|
|
64
|
+
args = {a: 1, b: 2}
|
|
65
|
+
called = false
|
|
66
|
+
args.stub(:x_www_form_urlencode, ->{called = true; 'a=1&b=2'}) do
|
|
67
|
+
response = HTTP.delete(uri, args)
|
|
68
|
+
_(called).must_equal(true)
|
|
69
|
+
_(response.success?).must_equal(true)
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
it "creates a new Net::HTTP::Delete object" do
|
|
74
|
+
received_arg = nil
|
|
75
|
+
delete_argument = parsed_uri.request_uri + '?a=1&b=2'
|
|
76
|
+
request_object = Net::HTTP::Delete.new(delete_argument)
|
|
77
|
+
Net::HTTP::Delete.stub(:new, ->(arg){received_arg = arg; request_object}) do
|
|
78
|
+
response = HTTP.delete(uri, {a: 1, b: 2})
|
|
79
|
+
_(received_arg).must_equal(delete_argument)
|
|
80
|
+
_(response.success?).must_equal(true)
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
describe "with headers supplied" do
|
|
86
|
+
let(:uri){'http://example.com/path'}
|
|
87
|
+
let(:parsed_uri){URI.parse(uri)}
|
|
88
|
+
|
|
89
|
+
before do
|
|
90
|
+
stub_request(:delete, 'http://example.com/path').
|
|
91
|
+
with(headers: {'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent' => 'Minitest'}).
|
|
92
|
+
to_return(status: 200, body: '', headers: {})
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
it "sets the headers on the request object" do
|
|
96
|
+
request_object = Net::HTTP::Delete.new(parsed_uri.request_uri)
|
|
97
|
+
Net::HTTP::Delete.stub(:new, request_object) do
|
|
98
|
+
response = HTTP.delete(uri, {}, {'User-Agent' => 'Minitest'})
|
|
99
|
+
_(request_object['User-Agent']).must_equal('Minitest')
|
|
100
|
+
_(response.success?).must_equal(true)
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
describe "with options supplied" do
|
|
106
|
+
let(:uri){'http://example.com/path'}
|
|
107
|
+
let(:parsed_uri){URI.parse(uri)}
|
|
108
|
+
|
|
109
|
+
before do
|
|
110
|
+
stub_request(:delete, 'https://example.com:80/path').
|
|
111
|
+
with(headers: {'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent' => 'Ruby'}).
|
|
112
|
+
to_return(status: 200, body: '', headers: {})
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
it "sets the use_ssl option on the Net::HTTP instance" do
|
|
116
|
+
net_http_object = Net::HTTP.new(parsed_uri.host, parsed_uri.port)
|
|
117
|
+
Net::HTTP.stub(:new, net_http_object) do
|
|
118
|
+
response = HTTP.delete(uri, {}, {}, {use_ssl: true})
|
|
119
|
+
_(net_http_object.instance_variable_get(:@use_ssl)).must_equal(true)
|
|
120
|
+
_(response.success?).must_equal(true)
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
describe "with block supplied" do
|
|
126
|
+
let(:uri){'http://example.com/path'}
|
|
127
|
+
|
|
128
|
+
before do
|
|
129
|
+
stub_request(:delete, 'http://example.com/path').
|
|
130
|
+
with(headers: {'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent' => 'Ruby'}).
|
|
131
|
+
to_return(status: 200, body: '', headers: {})
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
it "yields an instance of Net::HTTPResponse" do
|
|
135
|
+
yielded = nil
|
|
136
|
+
HTTP.delete(uri){|response| yielded = response}
|
|
137
|
+
_(yielded).must_be_kind_of(Net::HTTPResponse)
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
describe "with redirection" do
|
|
142
|
+
let(:request_uri){'http://example.com/path'}
|
|
143
|
+
let(:redirect_uri){'http://redirected.com'}
|
|
144
|
+
|
|
145
|
+
before do
|
|
146
|
+
stub_request(:get, redirect_uri).
|
|
147
|
+
to_return(status: 200, body: '', headers: {})
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
describe "via 301" do
|
|
151
|
+
before do
|
|
152
|
+
stub_request(:delete, request_uri).
|
|
153
|
+
to_return(status: 301, headers: {'location' => redirect_uri})
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
it "follows the redirect" do
|
|
157
|
+
response = HTTP.delete(request_uri)
|
|
158
|
+
_(response.success?).must_equal(true)
|
|
159
|
+
assert_requested(:delete, request_uri)
|
|
160
|
+
assert_requested(:get, redirect_uri)
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
describe "via 302" do
|
|
165
|
+
before do
|
|
166
|
+
stub_request(:delete, request_uri).
|
|
167
|
+
to_return(status: 302, headers: {'location' => redirect_uri})
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
it "follows the redirect" do
|
|
171
|
+
response = HTTP.delete(request_uri)
|
|
172
|
+
_(response.success?).must_equal(true)
|
|
173
|
+
assert_requested(:delete, request_uri)
|
|
174
|
+
assert_requested(:get, redirect_uri)
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
describe "with path only redirection" do
|
|
180
|
+
let(:request_uri){'http://example.com/path'}
|
|
181
|
+
let(:redirect_path){'/new_path'}
|
|
182
|
+
let(:redirect_uri){"http://example.com#{redirect_path}"}
|
|
183
|
+
|
|
184
|
+
before do
|
|
185
|
+
stub_request(:get, redirect_uri).
|
|
186
|
+
to_return(status: 200, body: '', headers: {})
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
describe "via 301" do
|
|
190
|
+
before do
|
|
191
|
+
stub_request(:delete, request_uri).
|
|
192
|
+
to_return(status: 301, headers: {'location' => redirect_path})
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
it "resolves the relative redirect against the original URI" do
|
|
196
|
+
response = HTTP.delete(request_uri)
|
|
197
|
+
_(response.success?).must_equal(true)
|
|
198
|
+
assert_requested(:get, redirect_uri)
|
|
199
|
+
end
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
describe "via 302" do
|
|
203
|
+
before do
|
|
204
|
+
stub_request(:delete, request_uri).
|
|
205
|
+
to_return(status: 302, headers: {'location' => redirect_path})
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
it "resolves the relative redirect against the original URI" do
|
|
209
|
+
response = HTTP.delete(request_uri)
|
|
210
|
+
_(response.success?).must_equal(true)
|
|
211
|
+
assert_requested(:get, redirect_uri)
|
|
212
|
+
end
|
|
213
|
+
end
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
describe "with verb-preserving redirection via 307" do
|
|
217
|
+
let(:request_uri){'http://example.com/path'}
|
|
218
|
+
let(:redirect_uri){'http://redirected.com'}
|
|
219
|
+
|
|
220
|
+
before do
|
|
221
|
+
stub_request(:delete, request_uri).
|
|
222
|
+
to_return(status: 307, headers: {'location' => redirect_uri})
|
|
223
|
+
stub_request(:delete, redirect_uri).
|
|
224
|
+
to_return(status: 200, body: '', headers: {})
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
it "preserves the verb" do
|
|
228
|
+
response = HTTP.delete(request_uri)
|
|
229
|
+
_(response.success?).must_equal(true)
|
|
230
|
+
assert_requested(:delete, request_uri)
|
|
231
|
+
assert_requested(:delete, redirect_uri)
|
|
232
|
+
end
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
describe "no_redirect true" do
|
|
236
|
+
let(:request_uri){'http://example.com/path'}
|
|
237
|
+
let(:redirect_uri){'http://redirected.com'}
|
|
238
|
+
|
|
239
|
+
describe "via 301" do
|
|
240
|
+
before do
|
|
241
|
+
stub_request(:delete, request_uri).
|
|
242
|
+
to_return(status: 301, headers: {'location' => redirect_uri})
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
it "returns the redirect response without following it" do
|
|
246
|
+
response = HTTP.delete(request_uri, {}, {}, {no_redirect: true})
|
|
247
|
+
_(response.redirection?).must_equal(true)
|
|
248
|
+
assert_not_requested(:get, redirect_uri)
|
|
249
|
+
end
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
describe "via 302" do
|
|
253
|
+
before do
|
|
254
|
+
stub_request(:delete, request_uri).
|
|
255
|
+
to_return(status: 302, headers: {'location' => redirect_uri})
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
it "returns the redirect response without following it" do
|
|
259
|
+
response = HTTP.delete(request_uri, {}, {}, {no_redirect: true})
|
|
260
|
+
_(response.redirection?).must_equal(true)
|
|
261
|
+
assert_not_requested(:get, redirect_uri)
|
|
262
|
+
end
|
|
263
|
+
end
|
|
264
|
+
end
|
|
265
|
+
end
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
# test/HTTP/get_test.rb
|
|
2
|
+
|
|
3
|
+
require_relative '../helper'
|
|
4
|
+
|
|
5
|
+
describe ".get" do
|
|
6
|
+
describe "with uri-only supplied" do
|
|
7
|
+
before do
|
|
8
|
+
stub_request(:get, 'http://example.com/path').
|
|
9
|
+
with(headers: {'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent' => 'Ruby'}).
|
|
10
|
+
to_return(status: 200, body: '', headers: {})
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
describe "uri as a string" do
|
|
14
|
+
let(:uri){'http://example.com/path'}
|
|
15
|
+
|
|
16
|
+
it "creates an instance of URI" do
|
|
17
|
+
received_arg = nil
|
|
18
|
+
parsed_uri = URI.parse(uri)
|
|
19
|
+
URI.stub(:parse, ->(arg){received_arg = arg; parsed_uri}) do
|
|
20
|
+
response = HTTP.get(uri)
|
|
21
|
+
_(received_arg).must_equal(uri)
|
|
22
|
+
_(response.success?).must_equal(true)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
it "creates a new Net::HTTP object" do
|
|
27
|
+
received_args = nil
|
|
28
|
+
parsed_uri = URI.parse(uri)
|
|
29
|
+
net_http_object = Net::HTTP.new(parsed_uri.host, parsed_uri.port)
|
|
30
|
+
Net::HTTP.stub(:new, ->(*args){received_args = args; net_http_object}) do
|
|
31
|
+
response = HTTP.get(uri)
|
|
32
|
+
_(received_args).must_equal([parsed_uri.host, parsed_uri.port])
|
|
33
|
+
_(response.success?).must_equal(true)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
describe "uri as a URI" do
|
|
39
|
+
let(:uri){URI.parse('http://example.com/path')}
|
|
40
|
+
|
|
41
|
+
it "creates a new Net::HTTP object" do
|
|
42
|
+
received_args = nil
|
|
43
|
+
net_http_object = Net::HTTP.new(uri.host, uri.port)
|
|
44
|
+
Net::HTTP.stub(:new, ->(*args){received_args = args; net_http_object}) do
|
|
45
|
+
response = HTTP.get(uri)
|
|
46
|
+
_(received_args).must_equal([uri.host, uri.port])
|
|
47
|
+
_(response.success?).must_equal(true)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
describe "with args supplied" do
|
|
54
|
+
let(:uri){'http://example.com/path'}
|
|
55
|
+
let(:parsed_uri){URI.parse(uri)}
|
|
56
|
+
|
|
57
|
+
before do
|
|
58
|
+
stub_request(:get, 'http://example.com/path?a=1&b=2').
|
|
59
|
+
with(headers: {'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent' => 'Ruby'}).
|
|
60
|
+
to_return(status: 200, body: '', headers: {})
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
it "x_www_form_urlencode's the args" do
|
|
64
|
+
args = {a: 1, b: 2}
|
|
65
|
+
called = false
|
|
66
|
+
args.stub(:x_www_form_urlencode, ->{called = true; 'a=1&b=2'}) do
|
|
67
|
+
response = HTTP.get(uri, args)
|
|
68
|
+
_(called).must_equal(true)
|
|
69
|
+
_(response.success?).must_equal(true)
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
it "creates a new Net::HTTP::Get object" do
|
|
74
|
+
received_arg = nil
|
|
75
|
+
get_argument = parsed_uri.request_uri + '?a=1&b=2'
|
|
76
|
+
request_object = Net::HTTP::Get.new(get_argument)
|
|
77
|
+
Net::HTTP::Get.stub(:new, ->(arg){received_arg = arg; request_object}) do
|
|
78
|
+
response = HTTP.get(uri, {a: 1, b: 2})
|
|
79
|
+
_(received_arg).must_equal(get_argument)
|
|
80
|
+
_(response.success?).must_equal(true)
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
describe "with headers supplied" do
|
|
86
|
+
let(:uri){'http://example.com/path'}
|
|
87
|
+
let(:parsed_uri){URI.parse(uri)}
|
|
88
|
+
|
|
89
|
+
before do
|
|
90
|
+
stub_request(:get, 'http://example.com/path').
|
|
91
|
+
with(headers: {'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent' => 'Minitest'}).
|
|
92
|
+
to_return(status: 200, body: '', headers: {})
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
it "sets the headers on the request object" do
|
|
96
|
+
request_object = Net::HTTP::Get.new(parsed_uri.request_uri)
|
|
97
|
+
Net::HTTP::Get.stub(:new, request_object) do
|
|
98
|
+
response = HTTP.get(uri, {}, {'User-Agent' => 'Minitest'})
|
|
99
|
+
_(request_object['User-Agent']).must_equal('Minitest')
|
|
100
|
+
_(response.success?).must_equal(true)
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
describe "with options supplied" do
|
|
106
|
+
let(:uri){'http://example.com/path'}
|
|
107
|
+
let(:parsed_uri){URI.parse(uri)}
|
|
108
|
+
|
|
109
|
+
before do
|
|
110
|
+
stub_request(:get, 'https://example.com:80/path').
|
|
111
|
+
with(headers: {'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent' => 'Ruby'}).
|
|
112
|
+
to_return(status: 200, body: '', headers: {})
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
it "sets the use_ssl option on the Net::HTTP instance" do
|
|
116
|
+
net_http_object = Net::HTTP.new(parsed_uri.host, parsed_uri.port)
|
|
117
|
+
Net::HTTP.stub(:new, net_http_object) do
|
|
118
|
+
response = HTTP.get(uri, {}, {}, {use_ssl: true})
|
|
119
|
+
_(net_http_object.instance_variable_get(:@use_ssl)).must_equal(true)
|
|
120
|
+
_(response.success?).must_equal(true)
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
describe "with default verify_mode" do
|
|
126
|
+
let(:uri){'http://example.com/path'}
|
|
127
|
+
let(:parsed_uri){URI.parse(uri)}
|
|
128
|
+
|
|
129
|
+
before do
|
|
130
|
+
stub_request(:get, uri).
|
|
131
|
+
with(headers: {'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent' => 'Ruby'}).
|
|
132
|
+
to_return(status: 200, body: '', headers: {})
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
it "defaults verify_mode to OpenSSL::SSL::VERIFY_PEER" do
|
|
136
|
+
net_http_object = Net::HTTP.new(parsed_uri.host, parsed_uri.port)
|
|
137
|
+
Net::HTTP.stub(:new, net_http_object) do
|
|
138
|
+
response = HTTP.get(uri)
|
|
139
|
+
_(net_http_object.verify_mode).must_equal(OpenSSL::SSL::VERIFY_PEER)
|
|
140
|
+
_(response.success?).must_equal(true)
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
it "allows opting back into VERIFY_NONE via options" do
|
|
145
|
+
net_http_object = Net::HTTP.new(parsed_uri.host, parsed_uri.port)
|
|
146
|
+
Net::HTTP.stub(:new, net_http_object) do
|
|
147
|
+
response = HTTP.get(uri, {}, {}, {verify_mode: OpenSSL::SSL::VERIFY_NONE})
|
|
148
|
+
_(net_http_object.verify_mode).must_equal(OpenSSL::SSL::VERIFY_NONE)
|
|
149
|
+
_(response.success?).must_equal(true)
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
describe "with block supplied" do
|
|
155
|
+
let(:uri){'http://example.com/path'}
|
|
156
|
+
|
|
157
|
+
before do
|
|
158
|
+
stub_request(:get, 'http://example.com/path').
|
|
159
|
+
with(headers: {'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent' => 'Ruby'}).
|
|
160
|
+
to_return(status: 200, body: '', headers: {})
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
it "yields an instance of Net::HTTPResponse" do
|
|
164
|
+
yielded = nil
|
|
165
|
+
HTTP.get(uri){|response| yielded = response}
|
|
166
|
+
_(yielded).must_be_kind_of(Net::HTTPResponse)
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
describe "with redirection" do
|
|
171
|
+
let(:request_uri){'http://example.com/path'}
|
|
172
|
+
let(:redirect_uri){'http://redirected.com'}
|
|
173
|
+
|
|
174
|
+
before do
|
|
175
|
+
stub_request(:get, redirect_uri).
|
|
176
|
+
with(headers: {'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent' => 'Ruby'}).
|
|
177
|
+
to_return(status: 200, body: '', headers: {})
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
describe "via 301" do
|
|
181
|
+
before do
|
|
182
|
+
stub_request(:get, request_uri).
|
|
183
|
+
with(headers: {'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent' => 'Ruby'}).
|
|
184
|
+
to_return(status: 301, body: '', headers: {'location' => redirect_uri})
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
it "follows the redirect" do
|
|
188
|
+
response = HTTP.get(request_uri)
|
|
189
|
+
_(response.success?).must_equal(true)
|
|
190
|
+
assert_requested(:get, request_uri)
|
|
191
|
+
assert_requested(:get, redirect_uri)
|
|
192
|
+
end
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
describe "via 302" do
|
|
196
|
+
before do
|
|
197
|
+
stub_request(:get, request_uri).
|
|
198
|
+
with(headers: {'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent' => 'Ruby'}).
|
|
199
|
+
to_return(status: 302, body: '', headers: {'location' => redirect_uri})
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
it "follows the redirect" do
|
|
203
|
+
response = HTTP.get(request_uri)
|
|
204
|
+
_(response.success?).must_equal(true)
|
|
205
|
+
assert_requested(:get, request_uri)
|
|
206
|
+
assert_requested(:get, redirect_uri)
|
|
207
|
+
end
|
|
208
|
+
end
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
describe "with path only redirection" do
|
|
212
|
+
let(:request_uri){'http://example.com/path'}
|
|
213
|
+
let(:redirect_path){'/new_path'}
|
|
214
|
+
let(:redirect_uri){"http://example.com#{redirect_path}"}
|
|
215
|
+
|
|
216
|
+
before do
|
|
217
|
+
stub_request(:get, redirect_uri).
|
|
218
|
+
with(headers: {'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent' => 'Ruby'}).
|
|
219
|
+
to_return(status: 200, body: '', headers: {})
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
describe "via 301" do
|
|
223
|
+
before do
|
|
224
|
+
stub_request(:get, request_uri).
|
|
225
|
+
with(headers: {'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent' => 'Ruby'}).
|
|
226
|
+
to_return(status: 301, body: '', headers: {'location' => redirect_path})
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
it "resolves the relative redirect against the original URI" do
|
|
230
|
+
response = HTTP.get(request_uri)
|
|
231
|
+
_(response.success?).must_equal(true)
|
|
232
|
+
assert_requested(:get, redirect_uri)
|
|
233
|
+
end
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
describe "via 302" do
|
|
237
|
+
before do
|
|
238
|
+
stub_request(:get, request_uri).
|
|
239
|
+
with(headers: {'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent' => 'Ruby'}).
|
|
240
|
+
to_return(status: 302, body: '', headers: {'location' => redirect_path})
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
it "resolves the relative redirect against the original URI" do
|
|
244
|
+
response = HTTP.get(request_uri)
|
|
245
|
+
_(response.success?).must_equal(true)
|
|
246
|
+
assert_requested(:get, redirect_uri)
|
|
247
|
+
end
|
|
248
|
+
end
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
describe "with path only redirection from HTTPS" do
|
|
252
|
+
let(:request_uri){'https://example.com/path'}
|
|
253
|
+
let(:redirect_path){'/new_path'}
|
|
254
|
+
let(:redirect_uri){"https://example.com#{redirect_path}"}
|
|
255
|
+
|
|
256
|
+
before do
|
|
257
|
+
stub_request(:get, redirect_uri).
|
|
258
|
+
with(headers: {'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent' => 'Ruby'}).
|
|
259
|
+
to_return(status: 200, body: '', headers: {})
|
|
260
|
+
stub_request(:get, request_uri).
|
|
261
|
+
with(headers: {'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent' => 'Ruby'}).
|
|
262
|
+
to_return(status: 301, body: '', headers: {'location' => redirect_path})
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
it "preserves the HTTPS scheme on a relative redirect" do
|
|
266
|
+
response = HTTP.get(request_uri)
|
|
267
|
+
_(response.success?).must_equal(true)
|
|
268
|
+
assert_requested(:get, redirect_uri)
|
|
269
|
+
end
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
describe "no_redirect true" do
|
|
273
|
+
let(:request_uri){'http://example.com/path'}
|
|
274
|
+
let(:redirect_uri){'http://redirected.com'}
|
|
275
|
+
|
|
276
|
+
describe "via 301" do
|
|
277
|
+
before do
|
|
278
|
+
stub_request(:get, request_uri).
|
|
279
|
+
with(headers: {'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent' => 'Ruby'}).
|
|
280
|
+
to_return(status: 301, body: '', headers: {'location' => redirect_uri})
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
it "returns the redirect response without following it" do
|
|
284
|
+
response = HTTP.get(request_uri, {}, {}, {no_redirect: true})
|
|
285
|
+
_(response.redirection?).must_equal(true)
|
|
286
|
+
assert_not_requested(:get, redirect_uri)
|
|
287
|
+
end
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
describe "via 302" do
|
|
291
|
+
before do
|
|
292
|
+
stub_request(:get, request_uri).
|
|
293
|
+
with(headers: {'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent' => 'Ruby'}).
|
|
294
|
+
to_return(status: 302, body: '', headers: {'location' => redirect_uri})
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
it "returns the redirect response without following it" do
|
|
298
|
+
response = HTTP.get(request_uri, {}, {}, {no_redirect: true})
|
|
299
|
+
_(response.redirection?).must_equal(true)
|
|
300
|
+
assert_not_requested(:get, redirect_uri)
|
|
301
|
+
end
|
|
302
|
+
end
|
|
303
|
+
end
|
|
304
|
+
end
|