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/patch_spec.rb
2
-
3
- require_relative '../spec_helper'
4
- require 'http'
5
-
6
- describe ".patch" do
7
- context "with uri-only supplied" do
8
- before do
9
- stub_request(:patch, '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.patch(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.patch(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.patch(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.patch(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::Patch.new(request_uri)}
59
-
60
- before do
61
- stub_request(:patch, "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::Patch).to receive(:new).with(request_uri).and_return(request_object)
68
- response = HTTP.patch(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::Patch object" do
74
- expect(Net::HTTP::Patch).to receive(:new).with(request_uri).and_return(request_object)
75
- response = HTTP.patch(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::Patch.new(request_uri)}
88
-
89
- before do
90
- stub_request(:patch, "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::Patch).to receive(:new).with(request_uri).and_return(request_object)
97
- response = HTTP.patch(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::Patch object" do
103
- expect(Net::HTTP::Patch).to receive(:new).with(request_uri).and_return(request_object)
104
- response = HTTP.patch(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::Patch.new(request_uri)}
117
-
118
- before do
119
- stub_request(:patch, "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::Patch).to receive(:new).with(request_uri).and_return(request_object)
126
- response = HTTP.patch(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::Patch object" do
132
- expect(Net::HTTP::Patch).to receive(:new).with(request_uri).and_return(request_object)
133
- response = HTTP.patch(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::Patch.new(request_uri)}
146
-
147
- before do
148
- stub_request(:patch, "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::Patch).to receive(:new).with(request_uri).and_return(request_object)
155
- response = HTTP.patch(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::Patch object" do
161
- expect(Net::HTTP::Patch).to receive(:new).with(request_uri).and_return(request_object)
162
- response = HTTP.patch(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::Patch.new(request_uri)}
173
-
174
- before do
175
- stub_request(:patch, '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::Patch).to receive(:new).with(request_uri).and_return(request_object)
182
- response = HTTP.patch(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::Patch.new(request_uri)}
194
- let(:args)do; {a: 1, b: 2}; end
195
-
196
- before do
197
- stub_request(:patch, '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::Patch).to receive(:new).with(request_uri).and_return(request_object)
207
- response = HTTP.patch(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::Patch).to receive(:new).with(request_uri).and_return(request_object)
218
- response = HTTP.patch(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::Patch).to receive(:new).with(request_uri).and_return(request_object)
229
- response = HTTP.patch(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(:patch, '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.patch(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(:patch, '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.patch(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(:patch, 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(:patch).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.patch(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(:patch, 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(:patch).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.patch(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(:patch, 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(:patch).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.patch(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(:patch, 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(:patch).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.patch(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(:patch, 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(:patch, 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(:patch).with(request_uri).and_call_original.ordered
368
- expect(HTTP).to receive(:patch).with(redirect_uri, '', {}, {use_ssl: false, verify_mode: OpenSSL::SSL::VERIFY_PEER}).and_call_original.ordered
369
- response = HTTP.patch(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(:patch, 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(:patch).once.with(request_uri, {}, {}, {no_redirect: true}).and_call_original
387
- response = HTTP.patch(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(:patch, 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(:patch).once.with(request_uri, {}, {}, {no_redirect: true}).and_call_original
401
- response = HTTP.patch(request_uri, {}, {}, {no_redirect: true})
402
- expect(response.redirection?).to eq(true)
403
- end
404
- end
405
- end
406
- end