cs-httpi 0.9.5.1
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.
- data/.autotest +5 -0
- data/.gitignore +9 -0
- data/.rspec +1 -0
- data/.travis.yml +8 -0
- data/CHANGELOG.md +66 -0
- data/Gemfile +9 -0
- data/LICENSE +20 -0
- data/README.md +229 -0
- data/Rakefile +18 -0
- data/autotest/discover.rb +1 -0
- data/cs-httpi.gemspec +26 -0
- data/lib/cs-httpi.rb +198 -0
- data/lib/cs-httpi/adapter.rb +67 -0
- data/lib/cs-httpi/adapter/curb.rb +119 -0
- data/lib/cs-httpi/adapter/httpclient.rb +98 -0
- data/lib/cs-httpi/adapter/net_http.rb +115 -0
- data/lib/cs-httpi/auth/config.rb +78 -0
- data/lib/cs-httpi/auth/ssl.rb +91 -0
- data/lib/cs-httpi/dime.rb +56 -0
- data/lib/cs-httpi/request.rb +99 -0
- data/lib/cs-httpi/response.rb +85 -0
- data/lib/cs-httpi/version.rb +5 -0
- data/nbproject/private/private.properties +2 -0
- data/nbproject/private/rake-d.txt +0 -0
- data/nbproject/project.properties +7 -0
- data/nbproject/project.xml +15 -0
- data/spec/cs-httpi/adapter/curb_spec.rb +232 -0
- data/spec/cs-httpi/adapter/httpclient_spec.rb +164 -0
- data/spec/cs-httpi/adapter/net_http_spec.rb +142 -0
- data/spec/cs-httpi/adapter_spec.rb +55 -0
- data/spec/cs-httpi/auth/config_spec.rb +117 -0
- data/spec/cs-httpi/auth/ssl_spec.rb +128 -0
- data/spec/cs-httpi/httpi_spec.rb +284 -0
- data/spec/cs-httpi/request_spec.rb +140 -0
- data/spec/cs-httpi/response_spec.rb +125 -0
- data/spec/fixtures/attachment.gif +0 -0
- data/spec/fixtures/client_cert.pem +16 -0
- data/spec/fixtures/client_key.pem +15 -0
- data/spec/fixtures/xml.gz +0 -0
- data/spec/fixtures/xml.xml +10 -0
- data/spec/fixtures/xml_dime.dime +0 -0
- data/spec/fixtures/xml_dime.xml +1 -0
- data/spec/integration/request_spec.rb +95 -0
- data/spec/integration/server.rb +39 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/support/fixture.rb +27 -0
- data/spec/support/matchers.rb +19 -0
- metadata +158 -0
@@ -0,0 +1,164 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "cs-httpi/adapter/httpclient"
|
3
|
+
require "cs-httpi/request"
|
4
|
+
|
5
|
+
require "httpclient"
|
6
|
+
|
7
|
+
describe HTTPI::Adapter::HTTPClient do
|
8
|
+
let(:adapter) { HTTPI::Adapter::HTTPClient.new }
|
9
|
+
let(:httpclient) { HTTPClient.any_instance }
|
10
|
+
let(:ssl_config) { HTTPClient::SSLConfig.any_instance }
|
11
|
+
|
12
|
+
describe "#get" do
|
13
|
+
it "returns a valid HTTPI::Response" do
|
14
|
+
httpclient.expects(:get).with(basic_request.url, nil, basic_request.headers).returns(http_message)
|
15
|
+
adapter.get(basic_request).should match_response(:body => Fixture.xml)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "#post" do
|
20
|
+
it "returns a valid HTTPI::Response" do
|
21
|
+
request = HTTPI::Request.new :url => "http://example.com", :body => Fixture.xml
|
22
|
+
httpclient.expects(:post).with(request.url, request.body, request.headers).returns(http_message)
|
23
|
+
|
24
|
+
adapter.post(request).should match_response(:body => Fixture.xml)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "#head" do
|
29
|
+
it "returns a valid HTTPI::Response" do
|
30
|
+
httpclient.expects(:head).with(basic_request.url, nil, basic_request.headers).returns(http_message)
|
31
|
+
adapter.head(basic_request).should match_response(:body => Fixture.xml)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "#put" do
|
36
|
+
it "returns a valid HTTPI::Response" do
|
37
|
+
request = HTTPI::Request.new :url => "http://example.com", :body => Fixture.xml
|
38
|
+
httpclient.expects(:put).with(request.url, request.body, request.headers).returns(http_message)
|
39
|
+
|
40
|
+
adapter.put(request).should match_response(:body => Fixture.xml)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "#delete" do
|
45
|
+
it "returns a valid HTTPI::Response" do
|
46
|
+
httpclient.expects(:delete).with(basic_request.url, basic_request.headers).returns(http_message(""))
|
47
|
+
adapter.delete(basic_request).should match_response(:body => "")
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "settings:" do
|
52
|
+
before { httpclient.stubs(:get).returns(http_message) }
|
53
|
+
|
54
|
+
describe "proxy" do
|
55
|
+
it "have should specs"
|
56
|
+
end
|
57
|
+
|
58
|
+
describe "connect_timeout" do
|
59
|
+
it "is not set unless specified" do
|
60
|
+
httpclient.expects(:connect_timeout=).never
|
61
|
+
adapter.get(basic_request)
|
62
|
+
end
|
63
|
+
|
64
|
+
it "is set if specified" do
|
65
|
+
request = basic_request { |request| request.open_timeout = 30 }
|
66
|
+
|
67
|
+
httpclient.expects(:connect_timeout=).with(30)
|
68
|
+
adapter.get(request)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe "receive_timeout" do
|
73
|
+
it "is not set unless specified" do
|
74
|
+
httpclient.expects(:receive_timeout=).never
|
75
|
+
adapter.get(basic_request)
|
76
|
+
end
|
77
|
+
|
78
|
+
it "is set if specified" do
|
79
|
+
request = basic_request { |request| request.read_timeout = 30 }
|
80
|
+
|
81
|
+
httpclient.expects(:receive_timeout=).with(30)
|
82
|
+
adapter.get(request)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
describe "set_auth" do
|
87
|
+
it "is set for HTTP basic auth" do
|
88
|
+
request = basic_request { |request| request.auth.basic "username", "password" }
|
89
|
+
|
90
|
+
httpclient.expects(:set_auth).with(request.url, *request.auth.credentials)
|
91
|
+
adapter.get(request)
|
92
|
+
end
|
93
|
+
|
94
|
+
it "is set for HTTP digest auth" do
|
95
|
+
request = basic_request { |request| request.auth.digest "username", "password" }
|
96
|
+
|
97
|
+
httpclient.expects(:set_auth).with(request.url, *request.auth.credentials)
|
98
|
+
adapter.get(request)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
context "(for SSL client auth)" do
|
103
|
+
let(:ssl_auth_request) do
|
104
|
+
basic_request do |request|
|
105
|
+
request.auth.ssl.cert_key_file = "spec/fixtures/client_key.pem"
|
106
|
+
request.auth.ssl.cert_file = "spec/fixtures/client_cert.pem"
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
it "client_cert, client_key and verify_mode should be set" do
|
111
|
+
ssl_config.expects(:client_cert=).with(ssl_auth_request.auth.ssl.cert)
|
112
|
+
ssl_config.expects(:client_key=).with(ssl_auth_request.auth.ssl.cert_key)
|
113
|
+
ssl_config.expects(:verify_mode=).with(ssl_auth_request.auth.ssl.openssl_verify_mode)
|
114
|
+
|
115
|
+
adapter.get(ssl_auth_request)
|
116
|
+
end
|
117
|
+
|
118
|
+
it "sets the client_ca if specified" do
|
119
|
+
ssl_auth_request.auth.ssl.ca_cert_file = "spec/fixtures/client_cert.pem"
|
120
|
+
ssl_config.expects(:client_ca=).with(ssl_auth_request.auth.ssl.ca_cert)
|
121
|
+
|
122
|
+
adapter.get(ssl_auth_request)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
context "(for SSL client auth with a verify mode of :none with no certs provided)" do
|
127
|
+
let(:ssl_auth_request) do
|
128
|
+
basic_request do |request|
|
129
|
+
request.auth.ssl.verify_mode = :none
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
it "verify_mode should be set" do
|
134
|
+
ssl_config.expects(:verify_mode=).with(ssl_auth_request.auth.ssl.openssl_verify_mode)
|
135
|
+
|
136
|
+
adapter.get(ssl_auth_request)
|
137
|
+
end
|
138
|
+
|
139
|
+
it "does not set client_cert and client_key "do
|
140
|
+
ssl_config.expects(:client_cert=).never
|
141
|
+
ssl_config.expects(:client_key=).never
|
142
|
+
|
143
|
+
adapter.get(ssl_auth_request)
|
144
|
+
end
|
145
|
+
|
146
|
+
it "does not raise an exception" do
|
147
|
+
expect { adapter.get(ssl_auth_request) }.to_not raise_error
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
def http_message(body = Fixture.xml)
|
153
|
+
message = HTTP::Message.new_response body
|
154
|
+
message.header.set "Accept-encoding", "utf-8"
|
155
|
+
message
|
156
|
+
end
|
157
|
+
|
158
|
+
def basic_request
|
159
|
+
request = HTTPI::Request.new "http://example.com"
|
160
|
+
yield request if block_given?
|
161
|
+
request
|
162
|
+
end
|
163
|
+
|
164
|
+
end
|
@@ -0,0 +1,142 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "cs-httpi/adapter/net_http"
|
3
|
+
require "cs-httpi/request"
|
4
|
+
|
5
|
+
describe HTTPI::Adapter::NetHTTP do
|
6
|
+
let(:net_http) { Net::HTTP.any_instance }
|
7
|
+
let(:basic_response) { { :body => Fixture.xml, :headers => { "Accept-encoding" => "utf-8" } } }
|
8
|
+
|
9
|
+
def adapter(request)
|
10
|
+
@adapter ||= HTTPI::Adapter::NetHTTP.new request
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "#get" do
|
14
|
+
it "should return a valid HTTPI::Response" do
|
15
|
+
stub_request(:get, basic_request.url.to_s).to_return(basic_response)
|
16
|
+
adapter(basic_request).get(basic_request).should match_response(:body => Fixture.xml)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "#post" do
|
21
|
+
it "should return a valid HTTPI::Response" do
|
22
|
+
request = HTTPI::Request.new :url => "http://example.com", :body => Fixture.xml
|
23
|
+
stub_request(:post, request.url.to_s).with(:body => request.body).to_return(basic_response)
|
24
|
+
|
25
|
+
adapter(request).post(request).should match_response(:body => Fixture.xml)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "#head" do
|
30
|
+
it "should return a valid HTTPI::Response" do
|
31
|
+
stub_request(:head, basic_request.url.to_s).to_return(basic_response)
|
32
|
+
adapter(basic_request).head(basic_request).should match_response(:body => Fixture.xml)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "#put" do
|
37
|
+
it "should return a valid HTTPI::Response" do
|
38
|
+
request = HTTPI::Request.new(
|
39
|
+
:url => "http://example.com",
|
40
|
+
:headers => { "Accept-encoding" => "utf-8" },
|
41
|
+
:body => Fixture.xml
|
42
|
+
)
|
43
|
+
stub_request(:put, request.url.to_s).with(:body => request.body).to_return(basic_response)
|
44
|
+
|
45
|
+
adapter(request).put(request).should match_response(:body => Fixture.xml)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "#delete" do
|
50
|
+
it "should return a valid HTTPI::Response" do
|
51
|
+
stub_request(:delete, basic_request.url.to_s).to_return(basic_response)
|
52
|
+
adapter(basic_request).delete(basic_request).should match_response(:body => Fixture.xml)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "settings:" do
|
57
|
+
before { stub_request(:get, basic_request.url.to_s) }
|
58
|
+
|
59
|
+
describe "use_ssl" do
|
60
|
+
it "should be set to false for non-SSL requests" do
|
61
|
+
net_http.expects(:use_ssl=).with(false)
|
62
|
+
adapter(basic_request).get(basic_request)
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should be set to true for SSL requests" do
|
66
|
+
request = basic_request { |request| request.ssl = true }
|
67
|
+
|
68
|
+
net_http.expects(:use_ssl=).with(true)
|
69
|
+
adapter(request).get(request)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
describe "open_timeout" do
|
74
|
+
it "should not be set if not specified" do
|
75
|
+
net_http.expects(:open_timeout=).never
|
76
|
+
adapter(basic_request).get(basic_request)
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should be set if specified" do
|
80
|
+
request = basic_request { |request| request.open_timeout = 30 }
|
81
|
+
|
82
|
+
net_http.expects(:open_timeout=).with(30)
|
83
|
+
adapter(request).get(request)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
describe "read_timeout" do
|
88
|
+
it "should not be set if not specified" do
|
89
|
+
net_http.expects(:read_timeout=).never
|
90
|
+
adapter(basic_request).get(basic_request)
|
91
|
+
end
|
92
|
+
|
93
|
+
it "should be set if specified" do
|
94
|
+
request = basic_request { |request| request.read_timeout = 30 }
|
95
|
+
|
96
|
+
net_http.expects(:read_timeout=).with(30)
|
97
|
+
adapter(request).get(request)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
describe "basic_auth" do
|
102
|
+
it "should be set for HTTP basic auth" do
|
103
|
+
request = basic_request { |request| request.auth.basic "username", "password" }
|
104
|
+
|
105
|
+
stub_request(:get, "http://username:password@example.com")
|
106
|
+
Net::HTTP::Get.any_instance.expects(:basic_auth).with(*request.auth.credentials)
|
107
|
+
adapter(request).get(request)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
context "(for SSL client auth)" do
|
112
|
+
let(:ssl_auth_request) do
|
113
|
+
basic_request do |request|
|
114
|
+
request.auth.ssl.cert_key_file = "spec/fixtures/client_key.pem"
|
115
|
+
request.auth.ssl.cert_file = "spec/fixtures/client_cert.pem"
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
it "key, cert and verify_mode should be set" do
|
120
|
+
net_http.expects(:cert=).with(ssl_auth_request.auth.ssl.cert)
|
121
|
+
net_http.expects(:key=).with(ssl_auth_request.auth.ssl.cert_key)
|
122
|
+
net_http.expects(:verify_mode=).with(ssl_auth_request.auth.ssl.openssl_verify_mode)
|
123
|
+
|
124
|
+
adapter(ssl_auth_request).get(ssl_auth_request)
|
125
|
+
end
|
126
|
+
|
127
|
+
it "should set the client_ca if specified" do
|
128
|
+
ssl_auth_request.auth.ssl.ca_cert_file = "spec/fixtures/client_cert.pem"
|
129
|
+
net_http.expects(:ca_file=).with(ssl_auth_request.auth.ssl.ca_cert_file)
|
130
|
+
|
131
|
+
adapter(ssl_auth_request).get(ssl_auth_request)
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def basic_request
|
137
|
+
request = HTTPI::Request.new "http://example.com"
|
138
|
+
yield request if block_given?
|
139
|
+
request
|
140
|
+
end
|
141
|
+
|
142
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "cs-httpi/adapter"
|
3
|
+
|
4
|
+
describe HTTPI::Adapter do
|
5
|
+
let(:adapter) { HTTPI::Adapter }
|
6
|
+
|
7
|
+
describe ".use" do
|
8
|
+
around do |example|
|
9
|
+
adapter.use = nil
|
10
|
+
example.run
|
11
|
+
adapter.use = nil
|
12
|
+
end
|
13
|
+
|
14
|
+
it "sets the adapter to use" do
|
15
|
+
adapter.use.should_not == :net_http
|
16
|
+
|
17
|
+
adapter.use = :net_http
|
18
|
+
adapter.use.should == :net_http
|
19
|
+
end
|
20
|
+
|
21
|
+
it "defaults to use the HTTPClient adapter" do
|
22
|
+
adapter.use.should == :httpclient
|
23
|
+
end
|
24
|
+
|
25
|
+
it "loads the adapter's client library" do
|
26
|
+
adapter.expects(:require).with("httpclient")
|
27
|
+
adapter.use = :httpclient
|
28
|
+
end
|
29
|
+
|
30
|
+
it "raises an ArgumentError in case of an invalid adapter" do
|
31
|
+
expect { adapter.use = :unknown }.to raise_error(ArgumentError)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe ".load" do
|
36
|
+
context "called with a valid adapter" do
|
37
|
+
it "returns the adapter's name and class" do
|
38
|
+
adapter.load(:curb).should == [:curb, HTTPI::Adapter::Curb]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context "called with nil" do
|
43
|
+
it "returns the default adapter's name and class" do
|
44
|
+
adapter.load(nil).should == [:httpclient, HTTPI::Adapter::HTTPClient]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
context "called with an invalid adapter" do
|
49
|
+
it "raises an ArgumentError" do
|
50
|
+
expect { adapter.use = :unknown }.to raise_error(ArgumentError)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "cs-httpi/auth/config"
|
3
|
+
|
4
|
+
describe HTTPI::Auth::Config do
|
5
|
+
let(:auth) { HTTPI::Auth::Config.new }
|
6
|
+
|
7
|
+
describe "#basic" do
|
8
|
+
it "lets you specify the basic auth credentials" do
|
9
|
+
auth.basic "username", "password"
|
10
|
+
auth.basic.should == ["username", "password"]
|
11
|
+
end
|
12
|
+
|
13
|
+
it "also accepts an Array of credentials" do
|
14
|
+
auth.basic ["username", "password"]
|
15
|
+
auth.basic.should == ["username", "password"]
|
16
|
+
end
|
17
|
+
|
18
|
+
it "sets the authentication type to :basic" do
|
19
|
+
auth.basic "username", "password"
|
20
|
+
auth.type.should == :basic
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "#basic?" do
|
25
|
+
it "defaults to return false" do
|
26
|
+
auth.should_not be_basic
|
27
|
+
end
|
28
|
+
|
29
|
+
it "returns true for HTTP basic auth" do
|
30
|
+
auth.basic "username", "password"
|
31
|
+
auth.should be_basic
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "#digest" do
|
36
|
+
it "lets you specify the digest auth credentials" do
|
37
|
+
auth.digest "username", "password"
|
38
|
+
auth.digest.should == ["username", "password"]
|
39
|
+
end
|
40
|
+
|
41
|
+
it "also accepts an Array of credentials" do
|
42
|
+
auth.digest ["username", "password"]
|
43
|
+
auth.digest.should == ["username", "password"]
|
44
|
+
end
|
45
|
+
|
46
|
+
it "sets the authentication type to :digest" do
|
47
|
+
auth.digest "username", "password"
|
48
|
+
auth.type.should == :digest
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe "#digest?" do
|
53
|
+
it "defaults to return false" do
|
54
|
+
auth.should_not be_digest
|
55
|
+
end
|
56
|
+
|
57
|
+
it "returns true for HTTP digest auth" do
|
58
|
+
auth.digest "username", "password"
|
59
|
+
auth.should be_digest
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "#http?" do
|
64
|
+
it "defaults to return false" do
|
65
|
+
auth.should_not be_http
|
66
|
+
end
|
67
|
+
|
68
|
+
it "returns true for HTTP basic auth" do
|
69
|
+
auth.basic "username", "password"
|
70
|
+
auth.should be_http
|
71
|
+
end
|
72
|
+
|
73
|
+
it "returns true for HTTP digest auth" do
|
74
|
+
auth.digest "username", "password"
|
75
|
+
auth.should be_http
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe "#ssl" do
|
80
|
+
it "returns the HTTPI::Auth::SSL object" do
|
81
|
+
auth.ssl.should be_a(HTTPI::Auth::SSL)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe "#ssl?" do
|
86
|
+
it "defaults to return false" do
|
87
|
+
auth.should_not be_ssl
|
88
|
+
end
|
89
|
+
|
90
|
+
it "returns true for SSL client auth" do
|
91
|
+
auth.ssl.cert_key_file = "spec/fixtures/client_key.pem"
|
92
|
+
auth.ssl.cert_file = "spec/fixtures/client_cert.pem"
|
93
|
+
|
94
|
+
auth.should be_ssl
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
describe "#type" do
|
99
|
+
it "returns the authentication type" do
|
100
|
+
auth.basic "username", "password"
|
101
|
+
auth.type.should == :basic
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
describe "#credentials" do
|
106
|
+
it "returns the credentials for HTTP basic auth" do
|
107
|
+
auth.basic "username", "basic"
|
108
|
+
auth.credentials.should == ["username", "basic"]
|
109
|
+
end
|
110
|
+
|
111
|
+
it "returns the credentials for HTTP digest auth" do
|
112
|
+
auth.digest "username", "digest"
|
113
|
+
auth.credentials.should == ["username", "digest"]
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|