httpi 2.5.0 → 4.0.4

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.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +41 -0
  3. data/README.md +25 -10
  4. data/UPDATING.md +11 -0
  5. data/lib/httpi/adapter/net_http.rb +0 -2
  6. data/lib/httpi/adapter.rb +2 -0
  7. data/lib/httpi/request.rb +2 -2
  8. data/lib/httpi/response.rb +1 -1
  9. data/lib/httpi/utils.rb +95 -0
  10. data/lib/httpi/version.rb +1 -1
  11. data/lib/httpi.rb +7 -1
  12. metadata +56 -61
  13. data/.github/workflows/development.yml +0 -48
  14. data/.gitignore +0 -11
  15. data/.rspec +0 -1
  16. data/Gemfile +0 -22
  17. data/Rakefile +0 -18
  18. data/httpi.gemspec +0 -31
  19. data/spec/fixtures/attachment.gif +0 -0
  20. data/spec/fixtures/client_cert.pem +0 -20
  21. data/spec/fixtures/client_key.pem +0 -27
  22. data/spec/fixtures/xml.gz +0 -0
  23. data/spec/fixtures/xml.xml +0 -10
  24. data/spec/fixtures/xml_dime.dime +0 -0
  25. data/spec/fixtures/xml_dime.xml +0 -1
  26. data/spec/httpi/adapter/base_spec.rb +0 -23
  27. data/spec/httpi/adapter/curb_spec.rb +0 -351
  28. data/spec/httpi/adapter/em_http_spec.rb +0 -180
  29. data/spec/httpi/adapter/excon_spec.rb +0 -34
  30. data/spec/httpi/adapter/http_spec.rb +0 -28
  31. data/spec/httpi/adapter/httpclient_spec.rb +0 -238
  32. data/spec/httpi/adapter/net_http_persistent_spec.rb +0 -46
  33. data/spec/httpi/adapter/net_http_spec.rb +0 -54
  34. data/spec/httpi/adapter/rack_spec.rb +0 -109
  35. data/spec/httpi/adapter_spec.rb +0 -68
  36. data/spec/httpi/auth/config_spec.rb +0 -163
  37. data/spec/httpi/auth/ssl_spec.rb +0 -216
  38. data/spec/httpi/cookie_spec.rb +0 -36
  39. data/spec/httpi/cookie_store_spec.rb +0 -26
  40. data/spec/httpi/error_spec.rb +0 -43
  41. data/spec/httpi/httpi_spec.rb +0 -358
  42. data/spec/httpi/request_spec.rb +0 -290
  43. data/spec/httpi/response_spec.rb +0 -142
  44. data/spec/integration/curb_spec.rb +0 -139
  45. data/spec/integration/em_http_spec.rb +0 -108
  46. data/spec/integration/excon_spec.rb +0 -174
  47. data/spec/integration/fixtures/ca_all.pem +0 -19
  48. data/spec/integration/fixtures/server.cert +0 -19
  49. data/spec/integration/fixtures/server.key +0 -27
  50. data/spec/integration/http_spec.rb +0 -156
  51. data/spec/integration/httpclient_spec.rb +0 -136
  52. data/spec/integration/net_http_persistent_spec.rb +0 -171
  53. data/spec/integration/net_http_spec.rb +0 -274
  54. data/spec/integration/support/application.rb +0 -97
  55. data/spec/integration/support/server.rb +0 -83
  56. data/spec/spec_helper.rb +0 -23
  57. data/spec/support/error_helper.rb +0 -26
  58. data/spec/support/fixture.rb +0 -27
  59. data/spec/support/matchers.rb +0 -19
@@ -1,216 +0,0 @@
1
- require "spec_helper"
2
- require "httpi/auth/ssl"
3
-
4
- describe HTTPI::Auth::SSL do
5
- before(:all) do
6
- @ssl_versions = HTTPI::Auth::SSL::SSL_VERSIONS
7
- @min_max_versions = HTTPI::Auth::SSL::MIN_MAX_VERSIONS
8
- end
9
-
10
- describe "VERIFY_MODES" do
11
- it "contains the supported SSL verify modes" do
12
- expect(HTTPI::Auth::SSL::VERIFY_MODES).to eq([:none, :peer, :fail_if_no_peer_cert, :client_once])
13
- end
14
- end
15
-
16
- describe "#present?" do
17
- it "defaults to return false" do
18
- ssl = HTTPI::Auth::SSL.new
19
- expect(ssl).not_to be_present
20
- end
21
-
22
- it "returns false if only a client key was specified" do
23
- ssl = HTTPI::Auth::SSL.new
24
- ssl.cert_key_file = "spec/fixtures/client_key.pem"
25
-
26
- expect(ssl).not_to be_present
27
- end
28
-
29
- it "returns false if only a client key was specified" do
30
- ssl = HTTPI::Auth::SSL.new
31
- ssl.cert_file = "spec/fixtures/client_cert.pem"
32
-
33
- expect(ssl).not_to be_present
34
- end
35
-
36
- it "returns true if both client key and cert are present" do
37
- expect(ssl).to be_present
38
- end
39
-
40
- it "returns true of the verify_mode is :none" do
41
- ssl = HTTPI::Auth::SSL.new
42
- ssl.verify_mode = :none
43
- expect(ssl).to be_present
44
- end
45
- end
46
-
47
- describe "#verify_mode" do
48
- it "defaults to return :peer" do
49
- expect(ssl.verify_mode).to eq(:peer)
50
- end
51
-
52
- it "sets the verify mode to use" do
53
- ssl = HTTPI::Auth::SSL.new
54
-
55
- ssl.verify_mode = :none
56
- expect(ssl.verify_mode).to eq(:none)
57
- end
58
-
59
- it "raises an ArgumentError if the given mode is not supported" do
60
- expect { ssl.verify_mode = :invalid }.
61
- to raise_error(ArgumentError, "Invalid SSL verify mode :invalid\n" +
62
- "Please specify one of [:none, :peer, :fail_if_no_peer_cert, :client_once]")
63
- end
64
- end
65
-
66
- describe "#cert" do
67
- it "returns an OpenSSL::X509::Certificate for the given cert_file" do
68
- expect(ssl.cert).to be_a(OpenSSL::X509::Certificate)
69
- end
70
-
71
- it "returns nil if no cert_file was given" do
72
- ssl = HTTPI::Auth::SSL.new
73
- expect(ssl.cert).to be_nil
74
- end
75
-
76
- it "returns the explicitly given certificate if set" do
77
- ssl = HTTPI::Auth::SSL.new
78
- cert = OpenSSL::X509::Certificate.new
79
- ssl.cert = cert
80
- expect(ssl.cert).to eq(cert)
81
- end
82
- end
83
-
84
- describe "#cert_key" do
85
- it "returns a OpenSSL::PKey::RSA for the given cert_key" do
86
- expect(ssl.cert_key).to be_a(OpenSSL::PKey::RSA)
87
- end
88
-
89
- it "returns nil if no cert_key_file was given" do
90
- ssl = HTTPI::Auth::SSL.new
91
- expect(ssl.cert_key).to be_nil
92
- end
93
-
94
- it "returns the explicitly given key if set" do
95
- ssl = HTTPI::Auth::SSL.new
96
- key = OpenSSL::PKey::RSA.new
97
- ssl.cert_key = key
98
- expect(ssl.cert_key).to eq(key)
99
- end
100
- end
101
-
102
- describe "#ca_cert" do
103
- it "returns an OpenSSL::X509::Certificate for the given ca_cert_file" do
104
- ssl = HTTPI::Auth::SSL.new
105
-
106
- ssl.ca_cert_file = "spec/fixtures/client_cert.pem"
107
- expect(ssl.ca_cert).to be_a(OpenSSL::X509::Certificate)
108
- end
109
- end
110
-
111
- describe "#openssl_verify_mode" do
112
- it "returns the OpenSSL verify mode for :none" do
113
- ssl = HTTPI::Auth::SSL.new
114
-
115
- ssl.verify_mode = :none
116
- expect(ssl.openssl_verify_mode).to eq(OpenSSL::SSL::VERIFY_NONE)
117
- end
118
-
119
- it "returns the OpenSSL verify mode for :peer" do
120
- ssl = HTTPI::Auth::SSL.new
121
-
122
- ssl.verify_mode = :peer
123
- expect(ssl.openssl_verify_mode).to eq(OpenSSL::SSL::VERIFY_PEER)
124
- end
125
-
126
- it "returns the OpenSSL verify mode for :fail_if_no_peer_cert" do
127
- ssl = HTTPI::Auth::SSL.new
128
-
129
- ssl.verify_mode = :fail_if_no_peer_cert
130
- expect(ssl.openssl_verify_mode).to eq(OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT)
131
- end
132
-
133
- it "returns the OpenSSL verify mode for :client_once" do
134
- ssl = HTTPI::Auth::SSL.new
135
-
136
- ssl.verify_mode = :client_once
137
- expect(ssl.openssl_verify_mode).to eq(OpenSSL::SSL::VERIFY_CLIENT_ONCE)
138
- end
139
- end
140
-
141
- describe "SSL_VERSIONS" do
142
- it "contains the supported SSL versions" do
143
- expect(HTTPI::Auth::SSL::SSL_VERSIONS).to eq(@ssl_versions)
144
- end
145
- end
146
-
147
- describe "#ssl_version" do
148
- subject { HTTPI::Auth::SSL.new }
149
-
150
- it "returns the SSL version" do
151
- subject.ssl_version = @ssl_versions.first
152
- expect(subject.ssl_version).to eq(@ssl_versions.first)
153
- end
154
-
155
- it 'raises ArgumentError if the version is unsupported' do
156
- expect { ssl.ssl_version = :ssl_fail }.
157
- to raise_error(ArgumentError, "Invalid SSL version :ssl_fail\n" +
158
- "Please specify one of #{@ssl_versions}")
159
- end
160
- end
161
-
162
- describe "#min_version" do
163
- subject { HTTPI::Auth::SSL.new }
164
-
165
- it "returns the min_version" do
166
- subject.min_version = @min_max_versions.first
167
- expect(subject.min_version).to eq(@min_max_versions.first)
168
- end
169
-
170
- it 'raises ArgumentError if the version is unsupported' do
171
- expect { ssl.min_version = :ssl_fail }.
172
- to raise_error(ArgumentError, "Invalid SSL min_version :ssl_fail\n" +
173
- "Please specify one of #{@min_max_versions}")
174
- end
175
- end
176
-
177
- describe "#max_version" do
178
- subject { HTTPI::Auth::SSL.new }
179
-
180
- it "returns the SSL version" do
181
- subject.max_version = @min_max_versions.first
182
- expect(subject.max_version).to eq(@min_max_versions.first)
183
- end
184
-
185
- it 'raises ArgumentError if the version is unsupported' do
186
- expect { ssl.max_version = :ssl_fail }.
187
- to raise_error(ArgumentError, "Invalid SSL max_version :ssl_fail\n" +
188
- "Please specify one of #{@min_max_versions}")
189
- end
190
- end
191
-
192
- describe '#ciphers' do
193
- subject { ssl.ciphers }
194
- let(:ssl) { HTTPI::Auth::SSL.new }
195
-
196
- context 'without ciphers' do
197
- before { ssl.ciphers = nil }
198
-
199
- it { is_expected.to eq(nil) }
200
- end
201
-
202
- context 'with ciphers' do
203
- before { ssl.ciphers = OpenSSL::SSL::SSLContext.new.ciphers }
204
-
205
- it { is_expected.to be_any.and(all(be_an_instance_of(String))) }
206
- end
207
- end
208
-
209
- def ssl
210
- ssl = HTTPI::Auth::SSL.new
211
- ssl.cert_key_file = "spec/fixtures/client_key.pem"
212
- ssl.cert_file = "spec/fixtures/client_cert.pem"
213
- ssl
214
- end
215
-
216
- end
@@ -1,36 +0,0 @@
1
- require "spec_helper"
2
- require "httpi"
3
-
4
- describe HTTPI::Cookie do
5
-
6
- let(:cookie) { HTTPI::Cookie.new("token=choc-choc-chip; Path=/; HttpOnly") }
7
-
8
- describe ".list_from_headers" do
9
- it "returns a list of cookies from a Hash of headers" do
10
- headers = { "Set-Cookie" => "token=strawberry; Path=/; HttpOnly" }
11
- cookies = HTTPI::Cookie.list_from_headers(headers)
12
-
13
- expect(cookies.size).to eq(1)
14
- expect(cookies.first).to be_a(HTTPI::Cookie)
15
- end
16
-
17
- it "handles multiple cookies" do
18
- headers = { "Set-Cookie" => ["user=chucknorris; Path=/; HttpOnly", "token=strawberry; Path=/; HttpOnly"] }
19
- cookies = HTTPI::Cookie.list_from_headers(headers)
20
- expect(cookies.size).to eq(2)
21
- end
22
- end
23
-
24
- describe "#name" do
25
- it "returns the name of the cookie" do
26
- expect(cookie.name).to eq("token")
27
- end
28
- end
29
-
30
- describe "#name_and_value" do
31
- it "returns the name and value of the cookie" do
32
- expect(cookie.name_and_value).to eq("token=choc-choc-chip")
33
- end
34
- end
35
-
36
- end
@@ -1,26 +0,0 @@
1
- require "spec_helper"
2
- require "httpi"
3
-
4
- describe HTTPI::CookieStore do
5
-
6
- let(:user_cookie) { some_cookie(:user, "chucknorris") }
7
- let(:token_cookie) { some_cookie(:token, "strawberry") }
8
-
9
- it "stores a set of cookies" do
10
- cookie_store = HTTPI::CookieStore.new
11
- cookie_store.add(user_cookie, token_cookie)
12
- expect(cookie_store.fetch).to include("user=chucknorris", "token=strawberry")
13
-
14
- # add a new token cookie with a different value
15
- token_cookie = some_cookie(:token, "choc-choc-chip")
16
- cookie_store.add(token_cookie)
17
-
18
- expect(cookie_store.fetch).to include("token=choc-choc-chip")
19
- expect(cookie_store.fetch).not_to include("token=strawberry")
20
- end
21
-
22
- def some_cookie(name, value)
23
- HTTPI::Cookie.new("#{name}=#{value}; Path=/; HttpOnly")
24
- end
25
-
26
- end
@@ -1,43 +0,0 @@
1
- require "spec_helper"
2
- require "support/error_helper"
3
-
4
- describe HTTPI do
5
- include ErrorHelper
6
-
7
- context "with :httpclient" do
8
- it "tags Errno::ECONNREFUSED with HTTPI::ConnectionError" do
9
- expect_error(Errno::ECONNREFUSED, "Connection refused - connect(2)").to be_tagged_with(HTTPI::ConnectionError)
10
- end
11
-
12
- def fake_error(error, message)
13
- request(:httpclient) { |client| client.expects(:request).raises(error, message) }
14
- end
15
- end
16
-
17
- unless RUBY_PLATFORM =~ /java/
18
- context "with :curb" do
19
- it "tags Curl::Err::ConnectionFailedError with HTTPI::ConnectionError" do
20
- expect_error(Curl::Err::ConnectionFailedError, "Curl::Err::ConnectionFailedError").to be_tagged_with(HTTPI::ConnectionError)
21
- end
22
-
23
- def fake_error(error, message)
24
- request(:curb) { |client| client.expects(:send).raises(error, message) }
25
- end
26
- end
27
- end
28
-
29
- context "with :net_http" do
30
- it "tags Errno::ECONNREFUSED with HTTPI::ConnectionError" do
31
- expect_error(Errno::ECONNREFUSED, "Connection refused - connect(2)").to be_tagged_with(HTTPI::ConnectionError)
32
- end
33
-
34
- def fake_error(error, message)
35
- request(:net_http) { |client| client.expects(:start).raises(error, message) }
36
- end
37
- end
38
-
39
- def request(adapter)
40
- HTTPI.get("http://example.com", adapter) { |client| yield client }
41
- end
42
-
43
- end
@@ -1,358 +0,0 @@
1
- require "spec_helper"
2
- require "integration/support/server"
3
-
4
- # find out why httpi doesn't load these automatically. [dh, 2012-12-15]
5
- require "excon"
6
- require "net/http/persistent"
7
- require "http"
8
-
9
- unless RUBY_PLATFORM =~ /java/
10
- require "em-synchrony"
11
- require "em-http-request"
12
- require "curb"
13
- end
14
-
15
- describe HTTPI do
16
- let(:client) { HTTPI }
17
- let(:httpclient) { HTTPI::Adapter.load(:httpclient) }
18
- let(:net_http) { HTTPI::Adapter.load(:net_http) }
19
- let(:net_http_persistent) { HTTPI::Adapter.load(:net_http_persistent) }
20
-
21
- before(:all) do
22
- HTTPI::Adapter::Rack.mount('example.com', IntegrationServer::Application)
23
- end
24
-
25
- after(:all) do
26
- HTTPI::Adapter::Rack.unmount('example.com')
27
- end
28
-
29
- describe ".adapter=" do
30
- it "sets the default adapter to use" do
31
- HTTPI::Adapter.expects(:use=).with(:net_http)
32
- HTTPI.adapter = :net_http
33
- end
34
- end
35
-
36
- describe ".query_builder" do
37
- it "gets flat builder by default" do
38
- expect(client.query_builder).to eq(HTTPI::QueryBuilder::Flat)
39
- end
40
- context "setter" do
41
- after { client.query_builder = HTTPI::QueryBuilder::Flat }
42
- it "looks up for class if symbol" do
43
- client.query_builder = :nested
44
- expect(client.query_builder).to eq(HTTPI::QueryBuilder::Nested)
45
- end
46
- it "validates if symbol is a valid option" do
47
- expect do
48
- client.query_builder = :xxx
49
- end.to raise_error(ArgumentError)
50
- end
51
- it "validates if value respond to build" do
52
- expect do
53
- client.query_builder = nil
54
- end.to raise_error(ArgumentError)
55
- end
56
- it "accepts valid class" do
57
- client.query_builder = HTTPI::QueryBuilder::Nested
58
- expect(client.query_builder).to eq(HTTPI::QueryBuilder::Nested)
59
- end
60
- end
61
- end
62
-
63
- describe ".get(request)" do
64
- it "executes a GET request using the default adapter" do
65
- request = HTTPI::Request.new("http://example.com")
66
- httpclient.any_instance.expects(:request).with(:get)
67
-
68
- client.get(request)
69
- end
70
- end
71
-
72
- describe ".get(request, adapter)" do
73
- it "executes a GET request using the given adapter" do
74
- request = HTTPI::Request.new("http://example.com")
75
- net_http.any_instance.expects(:request).with(:get)
76
-
77
- client.get(request, :net_http)
78
- end
79
- end
80
-
81
- describe ".get(url)" do
82
- it "executes a GET request using the default adapter" do
83
- httpclient.any_instance.expects(:request).with(:get)
84
- client.get("http://example.com")
85
- end
86
- end
87
-
88
- describe ".get(url, adapter)" do
89
- it "executes a GET request using the given adapter" do
90
- net_http.any_instance.expects(:request).with(:get)
91
- client.get("http://example.com", :net_http)
92
- end
93
- end
94
-
95
- describe ".post(request)" do
96
- it "executes a POST request using the default adapter" do
97
- request = HTTPI::Request.new("http://example.com")
98
- httpclient.any_instance.expects(:request).with(:post)
99
-
100
- client.post(request)
101
- end
102
- end
103
-
104
- describe ".post(request, adapter)" do
105
- it "executes a POST request using the given adapter" do
106
- request = HTTPI::Request.new("http://example.com")
107
- net_http.any_instance.expects(:request).with(:post, anything)
108
-
109
- client.post(request, :net_http)
110
- end
111
- end
112
-
113
- describe ".post(url, body)" do
114
- it "executes a POST request using the default adapter" do
115
- httpclient.any_instance.expects(:request).with(:post)
116
- client.post("http://example.com", "<some>xml</some>")
117
- end
118
- end
119
-
120
- describe ".post(url, body, adapter)" do
121
- it "executes a POST request using the given adapter" do
122
- net_http.any_instance.expects(:request).with(:post)
123
- client.post("http://example.com", "<some>xml</some>", :net_http)
124
- end
125
- end
126
-
127
- describe ".head(request)" do
128
- it "executes a HEAD request using the default adapter" do
129
- request = HTTPI::Request.new("http://example.com")
130
- httpclient.any_instance.expects(:request).with(:head, anything)
131
-
132
- client.head(request)
133
- end
134
- end
135
-
136
- describe ".head(request, adapter)" do
137
- it "executes a HEAD request using the given adapter" do
138
- request = HTTPI::Request.new("http://example.com")
139
- net_http.any_instance.expects(:request).with(:head, anything)
140
-
141
- client.head(request, :net_http)
142
- end
143
- end
144
-
145
- describe ".head(url)" do
146
- it "executes a HEAD request using the default adapter" do
147
- httpclient.any_instance.expects(:request).with(:head)
148
- client.head("http://example.com")
149
- end
150
- end
151
-
152
- describe ".head(url, adapter)" do
153
- it "executes a HEAD request using the given adapter" do
154
- net_http.any_instance.expects(:request).with(:head)
155
- client.head("http://example.com", :net_http)
156
- end
157
- end
158
-
159
- describe ".put(request)" do
160
- it "executes a PUT request using the default adapter" do
161
- request = HTTPI::Request.new("http://example.com")
162
- httpclient.any_instance.expects(:request).with(:put, anything)
163
-
164
- client.put(request)
165
- end
166
- end
167
-
168
- describe ".put(request, adapter)" do
169
- it "executes a PUT request using the given adapter" do
170
- request = HTTPI::Request.new("http://example.com")
171
- net_http.any_instance.expects(:request).with(:put, anything)
172
-
173
- client.put(request, :net_http)
174
- end
175
- end
176
-
177
- describe ".put(url, body)" do
178
- it "executes a PUT request using the default adapter" do
179
- httpclient.any_instance.expects(:request).with(:put)
180
- client.put("http://example.com", "<some>xml</some>")
181
- end
182
- end
183
-
184
- describe ".put(url, body, adapter)" do
185
- it "executes a PUT request using the given adapter" do
186
- net_http.any_instance.expects(:request).with(:put)
187
- client.put("http://example.com", "<some>xml</some>", :net_http)
188
- end
189
- end
190
-
191
- describe ".delete(request)" do
192
- it "executes a DELETE request using the default adapter" do
193
- request = HTTPI::Request.new("http://example.com")
194
- httpclient.any_instance.expects(:request).with(:delete, anything)
195
-
196
- client.delete(request)
197
- end
198
- end
199
-
200
- describe ".delete(request, adapter)" do
201
- it "executes a DELETE request using the given adapter" do
202
- request = HTTPI::Request.new("http://example.com")
203
- net_http.any_instance.expects(:request).with(:delete, anything)
204
-
205
- client.delete(request, :net_http)
206
- end
207
- end
208
-
209
- describe ".delete(url)" do
210
- it "executes a DELETE request using the default adapter" do
211
- httpclient.any_instance.expects(:request).with(:delete)
212
- client.delete("http://example.com")
213
- end
214
- end
215
-
216
- describe ".delete(url, adapter)" do
217
- it "executes a DELETE request using the given adapter" do
218
- net_http.any_instance.expects(:request).with(:delete)
219
- client.delete("http://example.com", :net_http)
220
- end
221
- end
222
-
223
- describe ".request" do
224
- let(:request) { HTTPI::Request.new('http://example.com/foo/') }
225
-
226
- it "allows custom HTTP methods" do
227
- httpclient.any_instance.expects(:request).with(:custom)
228
-
229
- client.request(:custom, request, :httpclient)
230
- end
231
-
232
- it 'follows redirects' do
233
- request.follow_redirect = true
234
- redirect_location = 'http://foo.bar'
235
-
236
- redirect = HTTPI::Response.new(302, {'location' => redirect_location}, 'Moved')
237
- response = HTTPI::Response.new(200, {}, 'success')
238
-
239
- httpclient.any_instance.expects(:request).twice.with(:custom).returns(redirect, response)
240
- request.expects(:url=).with(URI.parse(redirect_location))
241
-
242
- client.request(:custom, request, :httpclient)
243
- end
244
-
245
- it 'follows redirects with absolute path' do
246
- request.follow_redirect = true
247
- redirect_location = '/bar/foo'
248
-
249
- redirect = HTTPI::Response.new(302, {'location' => redirect_location}, 'Moved')
250
- response = HTTPI::Response.new(200, {}, 'success')
251
-
252
- httpclient.any_instance.expects(:request).twice.with(:custom).returns(redirect, response)
253
- request.expects(:url=).with(URI.parse('http://example.com/bar/foo'))
254
-
255
- client.request(:custom, request, :httpclient)
256
- end
257
-
258
- it 'follows redirects with relative path' do
259
- request.follow_redirect = true
260
- redirect_location = 'bar/foo'
261
-
262
- redirect = HTTPI::Response.new(302, {'location' => redirect_location}, 'Moved')
263
- response = HTTPI::Response.new(200, {}, 'success')
264
-
265
- httpclient.any_instance.expects(:request).twice.with(:custom).returns(redirect, response)
266
- request.expects(:url=).with(URI.parse('http://example.com/foo/bar/foo'))
267
-
268
- client.request(:custom, request, :httpclient)
269
- end
270
-
271
- it 'follows redirects at maximum of the redirect limit' do
272
- request.follow_redirect = true
273
- request.redirect_limit = 2
274
- redirect_location = 'http://foo.bar'
275
-
276
- redirect = HTTPI::Response.new(302, {'location' => redirect_location}, 'Moved')
277
- response = HTTPI::Response.new(200, {}, 'success')
278
-
279
- httpclient.any_instance.expects(:request).times(2).with(:custom).returns(redirect, response)
280
- request.expects(:url=).with(URI.parse(redirect_location))
281
-
282
- client.request(:custom, request, :httpclient)
283
- end
284
- end
285
-
286
- HTTPI::REQUEST_METHODS.each do |method|
287
- describe ".#{method}" do
288
- let(:request) { HTTPI::Request.new("http://example.com") }
289
-
290
- it "raises an ArgumentError in case of an invalid adapter" do
291
- expect { client.request method, request, :invalid }.to raise_error(ArgumentError)
292
- end
293
-
294
- HTTPI::Adapter::ADAPTERS.each do |adapter, opts|
295
- unless (adapter == :em_http || adapter == :curb) && RUBY_PLATFORM =~ /java/
296
- client_class = {
297
- :httpclient => lambda { HTTPClient },
298
- :curb => lambda { Curl::Easy },
299
- :net_http => lambda { Net::HTTP },
300
- :net_http_persistent => lambda { Net::HTTP::Persistent },
301
- :em_http => lambda { EventMachine::HttpConnection },
302
- :rack => lambda { Rack::MockRequest },
303
- :excon => lambda { Excon::Connection },
304
- :http => lambda { ::HTTP::Client }
305
- }
306
-
307
- context "using #{adapter}" do
308
- before { opts[:class].any_instance.expects(:request).with(method) }
309
-
310
- it "#request yields the HTTP client instance" do
311
- expect { |b| client.request(method, request, adapter, &b) }.to yield_with_args(client_class[adapter].call)
312
- end
313
-
314
- it "##{method} yields the HTTP client instance" do
315
- expect { |b| client.send(method, request, adapter, &b) }.to yield_with_args(client_class[adapter].call)
316
- end
317
- end
318
- end
319
- end
320
- end
321
- end
322
-
323
- context "(with reset)" do
324
- before { HTTPI.reset_config! }
325
-
326
- after do
327
- HTTPI.reset_config!
328
- HTTPI.log = false # disable for specs
329
- end
330
-
331
- describe ".log" do
332
- it "defaults to true" do
333
- expect(HTTPI.log?).to be_truthy
334
- end
335
- end
336
-
337
- describe ".logger" do
338
- it "defaults to Logger writing to STDOUT" do
339
- expect(HTTPI.logger).to be_a(Logger)
340
- end
341
- end
342
-
343
- describe ".log_level" do
344
- it "defaults to :debug" do
345
- expect(HTTPI.log_level).to eq(:debug)
346
- end
347
- end
348
-
349
- describe ".log" do
350
- it "logs the given messages" do
351
- HTTPI.log_level = :info
352
- HTTPI.logger.expects(:info).with("Log this")
353
- HTTPI.log "Log this"
354
- end
355
- end
356
- end
357
-
358
- end