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,35 +1,34 @@
1
- # spec/HTTP/head_spec.rb
1
+ # test/HTTP/head_test.rb
2
2
 
3
- require_relative '../spec_helper'
4
- require 'http'
3
+ require_relative '../helper'
5
4
 
6
5
  describe ".head" do
7
- context "with uri-only supplied" do
6
+ describe "with uri-only supplied" do
8
7
  before do
9
8
  stub_request(:head, 'http://example.com/path').
10
9
  to_return(status: 200, body: '', headers: {'Content-Type' => 'text/html'})
11
10
  end
12
11
 
13
- context "uri as a string" do
12
+ describe "uri as a string" do
14
13
  let(:uri){'http://example.com/path'}
15
14
 
16
15
  it "returns a successful response" do
17
16
  response = HTTP.head(uri)
18
- expect(response.success?).to eq(true)
17
+ _(response.success?).must_equal(true)
19
18
  end
20
19
  end
21
20
 
22
- context "uri as a URI" do
21
+ describe "uri as a URI" do
23
22
  let(:uri){URI.parse('http://example.com/path')}
24
23
 
25
24
  it "returns a successful response" do
26
25
  response = HTTP.head(uri)
27
- expect(response.success?).to eq(true)
26
+ _(response.success?).must_equal(true)
28
27
  end
29
28
  end
30
29
  end
31
30
 
32
- context "with args supplied" do
31
+ describe "with args supplied" do
33
32
  let(:uri){'http://example.com/path'}
34
33
 
35
34
  before do
@@ -39,26 +38,26 @@ describe ".head" do
39
38
 
40
39
  it "appends query parameters" do
41
40
  response = HTTP.head(uri, {a: 1, b: 2})
42
- expect(response.success?).to eq(true)
41
+ _(response.success?).must_equal(true)
43
42
  end
44
43
  end
45
44
 
46
- context "with headers supplied" do
45
+ describe "with headers supplied" do
47
46
  let(:uri){'http://example.com/path'}
48
47
 
49
48
  before do
50
49
  stub_request(:head, 'http://example.com/path').
51
- with(headers: {'User-Agent' => 'Rspec'}).
52
- to_return(status: 200, body: '', headers: {})
50
+ with(headers: {'User-Agent' => 'Minitest'}).
51
+ to_return(status: 200, body: '', headers: {})
53
52
  end
54
53
 
55
54
  it "sets the headers on the request" do
56
- response = HTTP.head(uri, {}, {'User-Agent' => 'Rspec'})
57
- expect(response.success?).to eq(true)
55
+ response = HTTP.head(uri, {}, {'User-Agent' => 'Minitest'})
56
+ _(response.success?).must_equal(true)
58
57
  end
59
58
  end
60
59
 
61
- context "with options supplied" do
60
+ describe "with options supplied" do
62
61
  let(:uri){'http://example.com/path'}
63
62
 
64
63
  before do
@@ -68,11 +67,11 @@ describe ".head" do
68
67
 
69
68
  it "sets the use_ssl option on the Net::HTTP instance" do
70
69
  response = HTTP.head(uri, {}, {}, {use_ssl: true})
71
- expect(response.success?).to eq(true)
70
+ _(response.success?).must_equal(true)
72
71
  end
73
72
  end
74
73
 
75
- context "with block supplied" do
74
+ describe "with block supplied" do
76
75
  let(:uri){'http://example.com/path'}
77
76
 
78
77
  before do
@@ -81,11 +80,13 @@ describe ".head" do
81
80
  end
82
81
 
83
82
  it "yields an instance of Net::HTTPResponse" do
84
- expect{|b| HTTP.head(uri, &b)}.to yield_with_args(Net::HTTPResponse)
83
+ yielded = nil
84
+ HTTP.head(uri){|response| yielded = response}
85
+ _(yielded).must_be_kind_of(Net::HTTPResponse)
85
86
  end
86
87
  end
87
88
 
88
- context "with redirection" do
89
+ describe "with redirection" do
89
90
  let(:request_uri){'http://example.com/path'}
90
91
  let(:redirect_uri){'http://redirected.com'}
91
92
 
@@ -98,11 +99,13 @@ describe ".head" do
98
99
 
99
100
  it "follows the redirect" do
100
101
  response = HTTP.head(request_uri)
101
- expect(response.success?).to eq(true)
102
+ _(response.success?).must_equal(true)
103
+ assert_requested(:head, request_uri)
104
+ assert_requested(:get, redirect_uri)
102
105
  end
103
106
  end
104
107
 
105
- context "no_redirect true" do
108
+ describe "no_redirect true" do
106
109
  let(:request_uri){'http://example.com/path'}
107
110
 
108
111
  before do
@@ -110,9 +113,9 @@ describe ".head" do
110
113
  to_return(status: 301, headers: {'location' => 'http://redirected.com'})
111
114
  end
112
115
 
113
- it "returns the redirect response" do
116
+ it "returns the redirect response without following it" do
114
117
  response = HTTP.head(request_uri, {}, {}, {no_redirect: true})
115
- expect(response.redirection?).to eq(true)
118
+ _(response.redirection?).must_equal(true)
116
119
  end
117
120
  end
118
121
  end
@@ -0,0 +1,246 @@
1
+ # test/HTTP/options_test.rb
2
+
3
+ require_relative '../helper'
4
+
5
+ describe ".options" do
6
+ describe "with uri-only supplied" do
7
+ before do
8
+ stub_request(:options, '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.options(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.options(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.options(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(:options, '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.options(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::Options object" do
74
+ received_arg = nil
75
+ options_argument = parsed_uri.request_uri + '?a=1&b=2'
76
+ request_object = Net::HTTP::Options.new(options_argument)
77
+ Net::HTTP::Options.stub(:new, ->(arg){received_arg = arg; request_object}) do
78
+ response = HTTP.options(uri, {a: 1, b: 2})
79
+ _(received_arg).must_equal(options_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(:options, '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::Options.new(parsed_uri.request_uri)
97
+ Net::HTTP::Options.stub(:new, request_object) do
98
+ response = HTTP.options(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(:options, '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.options(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(:options, '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.options(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(:options, request_uri).
153
+ to_return(status: 301, headers: {'location' => redirect_uri})
154
+ end
155
+
156
+ it "follows the redirect" do
157
+ response = HTTP.options(request_uri)
158
+ _(response.success?).must_equal(true)
159
+ assert_requested(:options, request_uri)
160
+ assert_requested(:get, redirect_uri)
161
+ end
162
+ end
163
+
164
+ describe "via 302" do
165
+ before do
166
+ stub_request(:options, request_uri).
167
+ to_return(status: 302, headers: {'location' => redirect_uri})
168
+ end
169
+
170
+ it "follows the redirect" do
171
+ response = HTTP.options(request_uri)
172
+ _(response.success?).must_equal(true)
173
+ assert_requested(:options, 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(:options, 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.options(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(:options, 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.options(request_uri)
210
+ _(response.success?).must_equal(true)
211
+ assert_requested(:get, redirect_uri)
212
+ end
213
+ end
214
+ end
215
+
216
+ describe "no_redirect true" do
217
+ let(:request_uri){'http://example.com/path'}
218
+ let(:redirect_uri){'http://redirected.com'}
219
+
220
+ describe "via 301" do
221
+ before do
222
+ stub_request(:options, request_uri).
223
+ to_return(status: 301, headers: {'location' => redirect_uri})
224
+ end
225
+
226
+ it "returns the redirect response without following it" do
227
+ response = HTTP.options(request_uri, {}, {}, {no_redirect: true})
228
+ _(response.redirection?).must_equal(true)
229
+ assert_not_requested(:get, redirect_uri)
230
+ end
231
+ end
232
+
233
+ describe "via 302" do
234
+ before do
235
+ stub_request(:options, request_uri).
236
+ to_return(status: 302, headers: {'location' => redirect_uri})
237
+ end
238
+
239
+ it "returns the redirect response without following it" do
240
+ response = HTTP.options(request_uri, {}, {}, {no_redirect: true})
241
+ _(response.redirection?).must_equal(true)
242
+ assert_not_requested(:get, redirect_uri)
243
+ end
244
+ end
245
+ end
246
+ end