http.rb 0.19.0 → 0.21.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.
@@ -0,0 +1,247 @@
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.19.0
4
+ version: 0.21.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - thoran
@@ -80,6 +80,7 @@ files:
80
80
  - Rakefile
81
81
  - http.rb.gemspec
82
82
  - lib/HTTP.rb
83
+ - lib/HTTP/RETRY.rb
83
84
  - lib/HTTP/VERSION.rb
84
85
  - lib/HTTP/request.rb
85
86
  - lib/HTTP/verbs.rb
@@ -93,11 +94,15 @@ files:
93
94
  - lib/Thoran/Array/FirstX/firstX.rb
94
95
  - lib/Thoran/String/ToConst/to_const.rb
95
96
  - lib/URI/Generic/use_sslQ.rb
97
+ - spec/HTTP/RETRY_spec.rb
96
98
  - spec/HTTP/delete_spec.rb
97
99
  - spec/HTTP/get_spec.rb
98
100
  - spec/HTTP/head_spec.rb
101
+ - spec/HTTP/options_spec.rb
102
+ - spec/HTTP/patch_spec.rb
99
103
  - spec/HTTP/post_spec.rb
100
104
  - spec/HTTP/put_spec.rb
105
+ - spec/HTTP/trace_spec.rb
101
106
  - spec/spec_helper.rb
102
107
  homepage: http://github.com/thoran/HTTP
103
108
  licenses: