httpi 4.0.3 → 4.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/httpi/version.rb +1 -1
- metadata +2 -50
- data/.devcontainer/devcontainer.json +0 -22
- data/.github/workflows/development.yml +0 -49
- data/.gitignore +0 -11
- data/.rspec +0 -1
- data/Gemfile +0 -22
- data/Rakefile +0 -18
- data/httpi.gemspec +0 -40
- data/spec/fixtures/attachment.gif +0 -0
- data/spec/fixtures/client_cert.pem +0 -20
- data/spec/fixtures/client_key.pem +0 -27
- data/spec/fixtures/xml.gz +0 -0
- data/spec/fixtures/xml.xml +0 -10
- data/spec/fixtures/xml_dime.dime +0 -0
- data/spec/fixtures/xml_dime.xml +0 -1
- data/spec/httpi/adapter/base_spec.rb +0 -23
- data/spec/httpi/adapter/curb_spec.rb +0 -351
- data/spec/httpi/adapter/em_http_spec.rb +0 -180
- data/spec/httpi/adapter/excon_spec.rb +0 -34
- data/spec/httpi/adapter/http_spec.rb +0 -28
- data/spec/httpi/adapter/httpclient_spec.rb +0 -238
- data/spec/httpi/adapter/net_http_persistent_spec.rb +0 -46
- data/spec/httpi/adapter/net_http_spec.rb +0 -54
- data/spec/httpi/adapter/rack_spec.rb +0 -109
- data/spec/httpi/adapter_spec.rb +0 -68
- data/spec/httpi/auth/config_spec.rb +0 -163
- data/spec/httpi/auth/ssl_spec.rb +0 -216
- data/spec/httpi/cookie_spec.rb +0 -36
- data/spec/httpi/cookie_store_spec.rb +0 -26
- data/spec/httpi/error_spec.rb +0 -43
- data/spec/httpi/httpi_spec.rb +0 -382
- data/spec/httpi/request_spec.rb +0 -290
- data/spec/httpi/response_spec.rb +0 -146
- data/spec/integration/curb_spec.rb +0 -140
- data/spec/integration/em_http_spec.rb +0 -108
- data/spec/integration/excon_spec.rb +0 -174
- data/spec/integration/fixtures/ca_all.pem +0 -19
- data/spec/integration/fixtures/server.cert +0 -19
- data/spec/integration/fixtures/server.key +0 -27
- data/spec/integration/http_spec.rb +0 -156
- data/spec/integration/httpclient_spec.rb +0 -137
- data/spec/integration/net_http_persistent_spec.rb +0 -171
- data/spec/integration/net_http_spec.rb +0 -282
- data/spec/integration/support/application.rb +0 -88
- data/spec/integration/support/server.rb +0 -83
- data/spec/spec_helper.rb +0 -23
- data/spec/support/error_helper.rb +0 -26
- data/spec/support/fixture.rb +0 -27
- data/spec/support/matchers.rb +0 -19
data/spec/httpi/auth/ssl_spec.rb
DELETED
@@ -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
|
data/spec/httpi/cookie_spec.rb
DELETED
@@ -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
|
data/spec/httpi/error_spec.rb
DELETED
@@ -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
|
data/spec/httpi/httpi_spec.rb
DELETED
@@ -1,382 +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 ".adapter_client_setup=" do
|
37
|
-
after do
|
38
|
-
HTTPI.adapter_client_setup = nil
|
39
|
-
end
|
40
|
-
|
41
|
-
it "sets the adapter client setup block" do
|
42
|
-
block = proc { }
|
43
|
-
HTTPI.adapter_client_setup = block
|
44
|
-
expect(HTTPI::Adapter.client_setup_block).to eq(block)
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
describe ".query_builder" do
|
49
|
-
it "gets flat builder by default" do
|
50
|
-
expect(client.query_builder).to eq(HTTPI::QueryBuilder::Flat)
|
51
|
-
end
|
52
|
-
context "setter" do
|
53
|
-
after { client.query_builder = HTTPI::QueryBuilder::Flat }
|
54
|
-
it "looks up for class if symbol" do
|
55
|
-
client.query_builder = :nested
|
56
|
-
expect(client.query_builder).to eq(HTTPI::QueryBuilder::Nested)
|
57
|
-
end
|
58
|
-
it "validates if symbol is a valid option" do
|
59
|
-
expect do
|
60
|
-
client.query_builder = :xxx
|
61
|
-
end.to raise_error(ArgumentError)
|
62
|
-
end
|
63
|
-
it "validates if value respond to build" do
|
64
|
-
expect do
|
65
|
-
client.query_builder = nil
|
66
|
-
end.to raise_error(ArgumentError)
|
67
|
-
end
|
68
|
-
it "accepts valid class" do
|
69
|
-
client.query_builder = HTTPI::QueryBuilder::Nested
|
70
|
-
expect(client.query_builder).to eq(HTTPI::QueryBuilder::Nested)
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
describe ".get(request)" do
|
76
|
-
it "executes a GET request using the default adapter" do
|
77
|
-
request = HTTPI::Request.new("http://example.com")
|
78
|
-
httpclient.any_instance.expects(:request).with(:get)
|
79
|
-
|
80
|
-
client.get(request)
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
describe ".get(request, adapter)" do
|
85
|
-
it "executes a GET request using the given adapter" do
|
86
|
-
request = HTTPI::Request.new("http://example.com")
|
87
|
-
net_http.any_instance.expects(:request).with(:get)
|
88
|
-
|
89
|
-
client.get(request, :net_http)
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
describe ".get(url)" do
|
94
|
-
it "executes a GET request using the default adapter" do
|
95
|
-
httpclient.any_instance.expects(:request).with(:get)
|
96
|
-
client.get("http://example.com")
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
describe ".get(url, adapter)" do
|
101
|
-
it "executes a GET request using the given adapter" do
|
102
|
-
net_http.any_instance.expects(:request).with(:get)
|
103
|
-
client.get("http://example.com", :net_http)
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
describe ".post(request)" do
|
108
|
-
it "executes a POST request using the default adapter" do
|
109
|
-
request = HTTPI::Request.new("http://example.com")
|
110
|
-
httpclient.any_instance.expects(:request).with(:post)
|
111
|
-
|
112
|
-
client.post(request)
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
|
-
describe ".post(request, adapter)" do
|
117
|
-
it "executes a POST request using the given adapter" do
|
118
|
-
request = HTTPI::Request.new("http://example.com")
|
119
|
-
net_http.any_instance.expects(:request).with(:post, anything)
|
120
|
-
|
121
|
-
client.post(request, :net_http)
|
122
|
-
end
|
123
|
-
end
|
124
|
-
|
125
|
-
describe ".post(url, body)" do
|
126
|
-
it "executes a POST request using the default adapter" do
|
127
|
-
httpclient.any_instance.expects(:request).with(:post)
|
128
|
-
client.post("http://example.com", "<some>xml</some>")
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
describe ".post(url, body, adapter)" do
|
133
|
-
it "executes a POST request using the given adapter" do
|
134
|
-
net_http.any_instance.expects(:request).with(:post)
|
135
|
-
client.post("http://example.com", "<some>xml</some>", :net_http)
|
136
|
-
end
|
137
|
-
end
|
138
|
-
|
139
|
-
describe ".head(request)" do
|
140
|
-
it "executes a HEAD request using the default adapter" do
|
141
|
-
request = HTTPI::Request.new("http://example.com")
|
142
|
-
httpclient.any_instance.expects(:request).with(:head, anything)
|
143
|
-
|
144
|
-
client.head(request)
|
145
|
-
end
|
146
|
-
end
|
147
|
-
|
148
|
-
describe ".head(request, adapter)" do
|
149
|
-
it "executes a HEAD request using the given adapter" do
|
150
|
-
request = HTTPI::Request.new("http://example.com")
|
151
|
-
net_http.any_instance.expects(:request).with(:head, anything)
|
152
|
-
|
153
|
-
client.head(request, :net_http)
|
154
|
-
end
|
155
|
-
end
|
156
|
-
|
157
|
-
describe ".head(url)" do
|
158
|
-
it "executes a HEAD request using the default adapter" do
|
159
|
-
httpclient.any_instance.expects(:request).with(:head)
|
160
|
-
client.head("http://example.com")
|
161
|
-
end
|
162
|
-
end
|
163
|
-
|
164
|
-
describe ".head(url, adapter)" do
|
165
|
-
it "executes a HEAD request using the given adapter" do
|
166
|
-
net_http.any_instance.expects(:request).with(:head)
|
167
|
-
client.head("http://example.com", :net_http)
|
168
|
-
end
|
169
|
-
end
|
170
|
-
|
171
|
-
describe ".put(request)" do
|
172
|
-
it "executes a PUT request using the default adapter" do
|
173
|
-
request = HTTPI::Request.new("http://example.com")
|
174
|
-
httpclient.any_instance.expects(:request).with(:put, anything)
|
175
|
-
|
176
|
-
client.put(request)
|
177
|
-
end
|
178
|
-
end
|
179
|
-
|
180
|
-
describe ".put(request, adapter)" do
|
181
|
-
it "executes a PUT request using the given adapter" do
|
182
|
-
request = HTTPI::Request.new("http://example.com")
|
183
|
-
net_http.any_instance.expects(:request).with(:put, anything)
|
184
|
-
|
185
|
-
client.put(request, :net_http)
|
186
|
-
end
|
187
|
-
end
|
188
|
-
|
189
|
-
describe ".put(url, body)" do
|
190
|
-
it "executes a PUT request using the default adapter" do
|
191
|
-
httpclient.any_instance.expects(:request).with(:put)
|
192
|
-
client.put("http://example.com", "<some>xml</some>")
|
193
|
-
end
|
194
|
-
end
|
195
|
-
|
196
|
-
describe ".put(url, body, adapter)" do
|
197
|
-
it "executes a PUT request using the given adapter" do
|
198
|
-
net_http.any_instance.expects(:request).with(:put)
|
199
|
-
client.put("http://example.com", "<some>xml</some>", :net_http)
|
200
|
-
end
|
201
|
-
end
|
202
|
-
|
203
|
-
describe ".delete(request)" do
|
204
|
-
it "executes a DELETE request using the default adapter" do
|
205
|
-
request = HTTPI::Request.new("http://example.com")
|
206
|
-
httpclient.any_instance.expects(:request).with(:delete, anything)
|
207
|
-
|
208
|
-
client.delete(request)
|
209
|
-
end
|
210
|
-
end
|
211
|
-
|
212
|
-
describe ".delete(request, adapter)" do
|
213
|
-
it "executes a DELETE request using the given adapter" do
|
214
|
-
request = HTTPI::Request.new("http://example.com")
|
215
|
-
net_http.any_instance.expects(:request).with(:delete, anything)
|
216
|
-
|
217
|
-
client.delete(request, :net_http)
|
218
|
-
end
|
219
|
-
end
|
220
|
-
|
221
|
-
describe ".delete(url)" do
|
222
|
-
it "executes a DELETE request using the default adapter" do
|
223
|
-
httpclient.any_instance.expects(:request).with(:delete)
|
224
|
-
client.delete("http://example.com")
|
225
|
-
end
|
226
|
-
end
|
227
|
-
|
228
|
-
describe ".delete(url, adapter)" do
|
229
|
-
it "executes a DELETE request using the given adapter" do
|
230
|
-
net_http.any_instance.expects(:request).with(:delete)
|
231
|
-
client.delete("http://example.com", :net_http)
|
232
|
-
end
|
233
|
-
end
|
234
|
-
|
235
|
-
describe ".request" do
|
236
|
-
let(:request) { HTTPI::Request.new('http://example.com/foo/') }
|
237
|
-
|
238
|
-
it "allows custom HTTP methods" do
|
239
|
-
httpclient.any_instance.expects(:request).with(:custom)
|
240
|
-
|
241
|
-
client.request(:custom, request, :httpclient)
|
242
|
-
end
|
243
|
-
|
244
|
-
it 'follows redirects' do
|
245
|
-
request.follow_redirect = true
|
246
|
-
redirect_location = 'http://foo.bar'
|
247
|
-
|
248
|
-
redirect = HTTPI::Response.new(302, {'location' => redirect_location}, 'Moved')
|
249
|
-
response = HTTPI::Response.new(200, {}, 'success')
|
250
|
-
|
251
|
-
httpclient.any_instance.expects(:request).twice.with(:custom).returns(redirect, response)
|
252
|
-
request.expects(:url=).with(URI.parse(redirect_location))
|
253
|
-
|
254
|
-
client.request(:custom, request, :httpclient)
|
255
|
-
end
|
256
|
-
|
257
|
-
it 'follows redirects with absolute path' do
|
258
|
-
request.follow_redirect = true
|
259
|
-
redirect_location = '/bar/foo'
|
260
|
-
|
261
|
-
redirect = HTTPI::Response.new(302, {'location' => redirect_location}, 'Moved')
|
262
|
-
response = HTTPI::Response.new(200, {}, 'success')
|
263
|
-
|
264
|
-
httpclient.any_instance.expects(:request).twice.with(:custom).returns(redirect, response)
|
265
|
-
request.expects(:url=).with(URI.parse('http://example.com/bar/foo'))
|
266
|
-
|
267
|
-
client.request(:custom, request, :httpclient)
|
268
|
-
end
|
269
|
-
|
270
|
-
it 'follows redirects with relative path' do
|
271
|
-
request.follow_redirect = true
|
272
|
-
redirect_location = 'bar/foo'
|
273
|
-
|
274
|
-
redirect = HTTPI::Response.new(302, {'location' => redirect_location}, 'Moved')
|
275
|
-
response = HTTPI::Response.new(200, {}, 'success')
|
276
|
-
|
277
|
-
httpclient.any_instance.expects(:request).twice.with(:custom).returns(redirect, response)
|
278
|
-
request.expects(:url=).with(URI.parse('http://example.com/foo/bar/foo'))
|
279
|
-
|
280
|
-
client.request(:custom, request, :httpclient)
|
281
|
-
end
|
282
|
-
|
283
|
-
it 'follows redirects at maximum of the redirect limit' do
|
284
|
-
request.follow_redirect = true
|
285
|
-
request.redirect_limit = 2
|
286
|
-
redirect_location = 'http://foo.bar'
|
287
|
-
|
288
|
-
redirect = HTTPI::Response.new(302, {'location' => redirect_location}, 'Moved')
|
289
|
-
response = HTTPI::Response.new(200, {}, 'success')
|
290
|
-
|
291
|
-
httpclient.any_instance.expects(:request).times(2).with(:custom).returns(redirect, response)
|
292
|
-
request.expects(:url=).with(URI.parse(redirect_location))
|
293
|
-
|
294
|
-
client.request(:custom, request, :httpclient)
|
295
|
-
end
|
296
|
-
|
297
|
-
describe "client setup block present" do
|
298
|
-
around do |example|
|
299
|
-
HTTPI::Adapter.client_setup_block = proc { |client| client.base_url = 'https://google.com' }
|
300
|
-
example.run
|
301
|
-
HTTPI::Adapter.client_setup_block = nil
|
302
|
-
end
|
303
|
-
|
304
|
-
it 'calls client setup block' do
|
305
|
-
client.request(:get, request, :httpclient) { |client| expect(client.base_url).to eq('https://google.com') }
|
306
|
-
end
|
307
|
-
end
|
308
|
-
end
|
309
|
-
|
310
|
-
HTTPI::REQUEST_METHODS.each do |method|
|
311
|
-
describe ".#{method}" do
|
312
|
-
let(:request) { HTTPI::Request.new("http://example.com") }
|
313
|
-
|
314
|
-
it "raises an ArgumentError in case of an invalid adapter" do
|
315
|
-
expect { client.request method, request, :invalid }.to raise_error(ArgumentError)
|
316
|
-
end
|
317
|
-
|
318
|
-
HTTPI::Adapter::ADAPTERS.each do |adapter, opts|
|
319
|
-
unless (adapter == :em_http || adapter == :curb) && RUBY_PLATFORM =~ /java/
|
320
|
-
client_class = {
|
321
|
-
:httpclient => lambda { HTTPClient },
|
322
|
-
:curb => lambda { Curl::Easy },
|
323
|
-
:net_http => lambda { Net::HTTP },
|
324
|
-
:net_http_persistent => lambda { Net::HTTP::Persistent },
|
325
|
-
:em_http => lambda { EventMachine::HttpConnection },
|
326
|
-
:rack => lambda { Rack::MockRequest },
|
327
|
-
:excon => lambda { Excon::Connection },
|
328
|
-
:http => lambda { ::HTTP::Client }
|
329
|
-
}
|
330
|
-
|
331
|
-
context "using #{adapter}" do
|
332
|
-
before { opts[:class].any_instance.expects(:request).with(method) }
|
333
|
-
|
334
|
-
it "#request yields the HTTP client instance" do
|
335
|
-
expect { |b| client.request(method, request, adapter, &b) }.to yield_with_args(client_class[adapter].call)
|
336
|
-
end
|
337
|
-
|
338
|
-
it "##{method} yields the HTTP client instance" do
|
339
|
-
expect { |b| client.send(method, request, adapter, &b) }.to yield_with_args(client_class[adapter].call)
|
340
|
-
end
|
341
|
-
end
|
342
|
-
end
|
343
|
-
end
|
344
|
-
end
|
345
|
-
end
|
346
|
-
|
347
|
-
context "(with reset)" do
|
348
|
-
before { HTTPI.reset_config! }
|
349
|
-
|
350
|
-
after do
|
351
|
-
HTTPI.reset_config!
|
352
|
-
HTTPI.log = false # disable for specs
|
353
|
-
end
|
354
|
-
|
355
|
-
describe ".log" do
|
356
|
-
it "defaults to true" do
|
357
|
-
expect(HTTPI.log?).to be_truthy
|
358
|
-
end
|
359
|
-
end
|
360
|
-
|
361
|
-
describe ".logger" do
|
362
|
-
it "defaults to Logger writing to STDOUT" do
|
363
|
-
expect(HTTPI.logger).to be_a(Logger)
|
364
|
-
end
|
365
|
-
end
|
366
|
-
|
367
|
-
describe ".log_level" do
|
368
|
-
it "defaults to :debug" do
|
369
|
-
expect(HTTPI.log_level).to eq(:debug)
|
370
|
-
end
|
371
|
-
end
|
372
|
-
|
373
|
-
describe ".log" do
|
374
|
-
it "logs the given messages" do
|
375
|
-
HTTPI.log_level = :info
|
376
|
-
HTTPI.logger.expects(:info).with("Log this")
|
377
|
-
HTTPI.log "Log this"
|
378
|
-
end
|
379
|
-
end
|
380
|
-
end
|
381
|
-
|
382
|
-
end
|