http.rb 0.20.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,295 +0,0 @@
1
- # spec/HTTP/get_spec.rb
2
-
3
- require_relative '../spec_helper'
4
- require 'http'
5
-
6
- describe ".get" do
7
- context "with uri-only supplied" do
8
- before do
9
- stub_request(:get, '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.get(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.get(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.get(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.get(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(:get_argument){parsed_uri.request_uri + '?' + x_www_form_urlencoded_arguments}
56
- let(:request_object){Net::HTTP::Get.new(get_argument)}
57
-
58
- before do
59
- stub_request(:get, '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.get(uri, args)
67
- expect(response.success?).to eq(true)
68
- end
69
-
70
- it "creates a new Net::HTTP::Get object" do
71
- expect(Net::HTTP::Get).to receive(:new).with(get_argument).and_return(request_object)
72
- response = HTTP.get(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(:get_argument){parsed_uri.request_uri}
82
- let(:request_object){Net::HTTP::Get.new(get_argument)}
83
-
84
- before do
85
- stub_request(:get, '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::Get).to receive(:new).with(get_argument).and_return(request_object)
92
- response = HTTP.get(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(:get, '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.get(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 default verify_mode" do
119
- let(:uri){'http://example.com/path'}
120
- let(:parsed_uri){URI.parse(uri)}
121
- let(:net_http_object){Net::HTTP.new(parsed_uri.host, parsed_uri.port)}
122
-
123
- before do
124
- stub_request(:get, uri).
125
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
126
- to_return(status: 200, body: '', headers: {})
127
- end
128
-
129
- it "defaults verify_mode to OpenSSL::SSL::VERIFY_PEER" do
130
- allow(Net::HTTP).to receive(:new).with(parsed_uri.host, parsed_uri.port).and_return(net_http_object)
131
- response = HTTP.get(uri)
132
- expect(net_http_object.verify_mode).to eq(OpenSSL::SSL::VERIFY_PEER)
133
- expect(response.success?).to eq(true)
134
- end
135
-
136
- it "allows opting back into VERIFY_NONE via options" do
137
- allow(Net::HTTP).to receive(:new).with(parsed_uri.host, parsed_uri.port).and_return(net_http_object)
138
- response = HTTP.get(uri, {}, {}, {verify_mode: OpenSSL::SSL::VERIFY_NONE})
139
- expect(net_http_object.verify_mode).to eq(OpenSSL::SSL::VERIFY_NONE)
140
- expect(response.success?).to eq(true)
141
- end
142
- end
143
-
144
- context "with block supplied" do
145
- let(:uri){'http://example.com/path'}
146
-
147
- before do
148
- stub_request(:get, 'http://example.com/path').
149
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
150
- to_return(status: 200, body: '', headers: {})
151
- end
152
-
153
- it "yields an instance of Net::HTTPResponse" do
154
- expect{|b| HTTP.get(uri, &b)}.to yield_with_args(Net::HTTPResponse)
155
- end
156
- end
157
-
158
- context "with redirection" do
159
- let(:request_uri){'http://example.com/path'}
160
- let(:redirect_uri){'http://redirected.com'}
161
-
162
- before do
163
- stub_request(:get, redirect_uri).
164
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
165
- to_return(status: 200, body: '', headers: {})
166
- end
167
-
168
- context "via 301" do
169
- before do
170
- stub_request(:get, request_uri).
171
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
172
- to_return(status: 301, body: '', headers: {'location' => redirect_uri})
173
- end
174
-
175
- it "does a redirect" do
176
- expect(HTTP).to receive(:get).once.with(request_uri).and_call_original
177
- expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: OpenSSL::SSL::VERIFY_PEER}).and_call_original
178
- response = HTTP.get(request_uri)
179
- expect(response.success?).to eq(true)
180
- end
181
- end
182
-
183
- context "via 302" do
184
- before do
185
- stub_request(:get, request_uri).
186
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
187
- to_return(status: 302, body: '', headers: {'location' => redirect_uri})
188
- end
189
-
190
- it "does a redirect" do
191
- expect(HTTP).to receive(:get).once.with(request_uri).and_call_original
192
- expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: OpenSSL::SSL::VERIFY_PEER}).and_call_original
193
- response = HTTP.get(request_uri)
194
- expect(response.success?).to eq(true)
195
- end
196
- end
197
- end
198
-
199
- context "with path only redirection" do
200
- let(:request_uri){'http://example.com/path'}
201
- let(:redirect_path){'/new_path'}
202
- let(:redirect_uri){"http://example.com#{redirect_path}"}
203
-
204
- before do
205
- stub_request(:get, redirect_uri).
206
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
207
- to_return(status: 200, body: '', headers: {})
208
- end
209
-
210
- context "via 301" do
211
- before do
212
- stub_request(:get, request_uri).
213
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
214
- to_return(status: 301, body: '', headers: {'location' => redirect_path})
215
- end
216
-
217
- it "does a redirect" do
218
- expect(HTTP).to receive(:get).once.with(request_uri).and_call_original
219
- expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: OpenSSL::SSL::VERIFY_PEER}).and_call_original
220
- response = HTTP.get(request_uri)
221
- expect(response.success?).to eq(true)
222
- end
223
- end
224
-
225
- context "via 302" do
226
- before do
227
- stub_request(:get, request_uri).
228
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
229
- to_return(status: 302, body: '', headers: {'location' => redirect_path})
230
- end
231
-
232
- it "does a redirect" do
233
- expect(HTTP).to receive(:get).once.with(request_uri).and_call_original
234
- expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: false, verify_mode: OpenSSL::SSL::VERIFY_PEER}).and_call_original
235
- response = HTTP.get(request_uri)
236
- expect(response.success?).to eq(true)
237
- end
238
- end
239
- end
240
-
241
- context "with path only redirection from HTTPS" do
242
- let(:request_uri){'https://example.com/path'}
243
- let(:redirect_path){'/new_path'}
244
- let(:redirect_uri){"https://example.com#{redirect_path}"}
245
-
246
- before do
247
- stub_request(:get, redirect_uri).
248
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
249
- to_return(status: 200, body: '', headers: {})
250
- stub_request(:get, request_uri).
251
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
252
- to_return(status: 301, body: '', headers: {'location' => redirect_path})
253
- end
254
-
255
- it "preserves the HTTPS scheme on a relative redirect" do
256
- expect(HTTP).to receive(:get).once.with(request_uri).and_call_original
257
- expect(HTTP).to receive(:get).once.with(redirect_uri, {}, {}, {use_ssl: true, verify_mode: OpenSSL::SSL::VERIFY_PEER}).and_call_original
258
- response = HTTP.get(request_uri)
259
- expect(response.success?).to eq(true)
260
- end
261
- end
262
-
263
- context "no_redirect true" do
264
- let(:request_uri){'http://example.com/path'}
265
- let(:redirect_uri){'http://redirected.com'}
266
-
267
- context "via 301" do
268
- before do
269
- stub_request(:get, request_uri).
270
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
271
- to_return(status: 301, body: '', headers: {'location' => redirect_uri})
272
- end
273
-
274
- it "doesn't redirect" do
275
- expect(HTTP).to receive(:get).once.with(request_uri, {}, {}, {no_redirect: true}).and_call_original
276
- response = HTTP.get(request_uri, {}, {}, {no_redirect: true})
277
- expect(response.redirection?).to eq(true)
278
- end
279
- end
280
-
281
- context "via 302" do
282
- before do
283
- stub_request(:get, request_uri).
284
- with(headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
285
- to_return(status: 302, body: '', headers: {'location' => redirect_uri})
286
- end
287
-
288
- it "doesn't redirect" do
289
- expect(HTTP).to receive(:get).once.with(request_uri, {}, {}, {no_redirect: true}).and_call_original
290
- response = HTTP.get(request_uri, {}, {}, {no_redirect: true})
291
- expect(response.redirection?).to eq(true)
292
- end
293
- end
294
- end
295
- end