httpi 1.1.1 → 2.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,168 @@
1
+ require "spec_helper"
2
+ require "httpi/adapter/em_http"
3
+ require "httpi/request"
4
+
5
+ begin
6
+ HTTPI::Adapter.load_adapter(:em_http)
7
+
8
+ describe HTTPI::Adapter::EmHttpRequest do
9
+
10
+ around(:each) do |example|
11
+ EM.synchrony do
12
+ example.run
13
+ EM.stop
14
+ end
15
+ end
16
+
17
+ let(:adapter) { HTTPI::Adapter::EmHttpRequest.new(request) }
18
+ let(:em_http) { EventMachine::HttpConnection.any_instance }
19
+ let(:request) { HTTPI::Request.new("http://example.com") }
20
+
21
+ describe "#request(:get)" do
22
+ it "returns a valid HTTPI::Response" do
23
+ em_http.expects(:get).
24
+ with(:query => nil, :connect_timeout => nil, :inactivity_timeout => nil, :head => {}, :body => nil).
25
+ returns(http_message)
26
+
27
+ adapter.request(:get).should match_response(:body => Fixture.xml)
28
+ end
29
+ end
30
+
31
+ describe "#request(:post)" do
32
+ it "returns a valid HTTPI::Response" do
33
+ em_http.expects(:post).
34
+ with(:query => nil, :connect_timeout => nil, :inactivity_timeout => nil, :head => {}, :body => Fixture.xml).
35
+ returns(http_message)
36
+
37
+ request.body = Fixture.xml
38
+ adapter.request(:post).should match_response(:body => Fixture.xml)
39
+ end
40
+ end
41
+
42
+ describe "#request(:head)" do
43
+ it "returns a valid HTTPI::Response" do
44
+ em_http.expects(:head).
45
+ with(:query => nil, :connect_timeout => nil, :inactivity_timeout => nil, :head => {}, :body => nil).
46
+ returns(http_message)
47
+
48
+ adapter.request(:head).should match_response(:body => Fixture.xml)
49
+ end
50
+ end
51
+
52
+ describe "#request(:put)" do
53
+ it "returns a valid HTTPI::Response" do
54
+ em_http.expects(:put).
55
+ with(:query => nil, :connect_timeout => nil, :inactivity_timeout => nil, :head => {}, :body => Fixture.xml).
56
+ returns(http_message)
57
+
58
+ request.body = Fixture.xml
59
+ adapter.request(:put).should match_response(:body => Fixture.xml)
60
+ end
61
+ end
62
+
63
+ describe "#request(:delete)" do
64
+ it "returns a valid HTTPI::Response" do
65
+ em_http.expects(:delete).
66
+ with(:query => nil, :connect_timeout => nil, :inactivity_timeout => nil, :head => {}, :body => nil).
67
+ returns(http_message(""))
68
+
69
+ adapter.request(:delete).should match_response(:body => "")
70
+ end
71
+ end
72
+
73
+ describe "#request(:custom)" do
74
+ it "returns a valid HTTPI::Response" do
75
+ em_http.expects(:custom).
76
+ with(:query => nil, :connect_timeout => nil, :inactivity_timeout => nil, :head => {}, :body => nil).
77
+ returns(http_message(""))
78
+
79
+ adapter.request(:custom).should match_response(:body => "")
80
+ end
81
+ end
82
+
83
+ describe "settings:" do
84
+ describe "proxy" do
85
+ before do
86
+ request.proxy = "http://proxy-host.com:443"
87
+ request.proxy.user = "username"
88
+ request.proxy.password = "password"
89
+ end
90
+
91
+ it "sets host, port, and authorization" do
92
+ em_http.expects(:get).once.
93
+ with(has_entries(:proxy => { :host => "proxy-host.com", :port => 443, :authorization => %w( username password ) })).
94
+ returns(http_message)
95
+
96
+ adapter.request(:get)
97
+ end
98
+ end
99
+
100
+ describe "connect_timeout" do
101
+ it "is not set unless specified" do
102
+ em_http.expects(:get).once.with(has_entries(:connect_timeout => nil)).returns(http_message)
103
+ adapter.request(:get)
104
+ end
105
+
106
+ it "is set if specified" do
107
+ request.open_timeout = 30
108
+ em_http.expects(:get).once.with(has_entries(:connect_timeout => 30)).returns(http_message)
109
+ adapter.request(:get)
110
+ end
111
+ end
112
+
113
+ describe "receive_timeout" do
114
+ it "is not set unless specified" do
115
+ em_http.expects(:get).once.with(has_entries(:inactivity_timeout => nil)).returns(http_message)
116
+ adapter.request(:get)
117
+ end
118
+
119
+ it "is set if specified" do
120
+ request.read_timeout = 30
121
+ em_http.expects(:get).once.with(has_entries(:inactivity_timeout => 30)).returns(http_message)
122
+ adapter.request(:get)
123
+ end
124
+ end
125
+
126
+ describe "set_auth" do
127
+ it "is set for HTTP basic auth" do
128
+ request.auth.basic "username", "password"
129
+ em_http.expects(:get).once.with(has_entries(:head => { :authorization => %w( username password) })).returns(http_message)
130
+ adapter.request(:get)
131
+ end
132
+
133
+ it "raises an error for HTTP digest auth" do
134
+ request.auth.digest "username", "password"
135
+ expect { adapter.request(:get) }.to raise_error
136
+ end
137
+ end
138
+
139
+ context "(for SSL client auth)" do
140
+ before do
141
+ request.auth.ssl.cert_key_file = "spec/fixtures/client_key.pem"
142
+ request.auth.ssl.cert_file = "spec/fixtures/client_cert.pem"
143
+ end
144
+
145
+ it "is not supported" do
146
+ expect { adapter.request(:get) }.
147
+ to raise_error(HTTPI::NotSupportedError, "EM-HTTP-Request does not support SSL client auth")
148
+ end
149
+ end
150
+ end
151
+
152
+ def http_message(body = Fixture.xml)
153
+ message = EventMachine::HttpClient.new("http://example.com", {})
154
+ message.instance_variable_set :@response, body
155
+ message.instance_variable_set :@response_header, EventMachine::HttpResponseHeader.new
156
+ message.response_header['Accept-encoding'] = 'utf-8'
157
+ message.response_header.http_status = '200'
158
+ message
159
+ end
160
+
161
+ end
162
+ rescue LoadError => e
163
+ if e.message =~ /fiber/
164
+ warn "LoadError: #{e.message} (EventMachine requires fibers)"
165
+ else
166
+ raise e
167
+ end
168
+ end
@@ -2,54 +2,62 @@ require "spec_helper"
2
2
  require "httpi/adapter/httpclient"
3
3
  require "httpi/request"
4
4
 
5
- require "httpclient"
5
+ HTTPI::Adapter.load_adapter(:httpclient)
6
6
 
7
7
  describe HTTPI::Adapter::HTTPClient do
8
- let(:adapter) { HTTPI::Adapter::HTTPClient.new }
8
+ let(:adapter) { HTTPI::Adapter::HTTPClient.new(request) }
9
9
  let(:httpclient) { HTTPClient.any_instance }
10
10
  let(:ssl_config) { HTTPClient::SSLConfig.any_instance }
11
+ let(:request) { HTTPI::Request.new("http://example.com") }
11
12
 
12
- describe "#get" do
13
+ describe "#request(:get)" do
13
14
  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)
15
+ httpclient_expects(:get)
16
+ adapter.request(:get).should match_response(:body => Fixture.xml)
16
17
  end
17
18
  end
18
19
 
19
- describe "#post" do
20
+ describe "#request(:post)" do
20
21
  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)
22
+ request.body = Fixture.xml
23
+ httpclient_expects(:post)
23
24
 
24
- adapter.post(request).should match_response(:body => Fixture.xml)
25
+ adapter.request(:post).should match_response(:body => Fixture.xml)
25
26
  end
26
27
  end
27
28
 
28
- describe "#head" do
29
+ describe "#request(:head)" do
29
30
  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)
31
+ httpclient_expects(:head)
32
+ adapter.request(:head).should match_response(:body => Fixture.xml)
32
33
  end
33
34
  end
34
35
 
35
- describe "#put" do
36
+ describe "#request(:put)" do
36
37
  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)
38
+ request.body = Fixture.xml
39
+ httpclient_expects(:put)
39
40
 
40
- adapter.put(request).should match_response(:body => Fixture.xml)
41
+ adapter.request(:put).should match_response(:body => Fixture.xml)
41
42
  end
42
43
  end
43
44
 
44
- describe "#delete" do
45
+ describe "#request(:delete)" do
45
46
  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 => "")
47
+ httpclient_expects(:delete)
48
+ adapter.request(:delete).should match_response(:body => Fixture.xml)
49
+ end
50
+ end
51
+
52
+ describe "#request(:custom)" do
53
+ it "returns a valid HTTPI::Response" do
54
+ httpclient_expects(:custom)
55
+ adapter.request(:custom).should match_response(:body => Fixture.xml)
48
56
  end
49
57
  end
50
58
 
51
59
  describe "settings:" do
52
- before { httpclient.stubs(:get).returns(http_message) }
60
+ before { httpclient.stubs(:request).returns(http_message) }
53
61
 
54
62
  describe "proxy" do
55
63
  it "have should specs"
@@ -58,107 +66,110 @@ describe HTTPI::Adapter::HTTPClient do
58
66
  describe "connect_timeout" do
59
67
  it "is not set unless specified" do
60
68
  httpclient.expects(:connect_timeout=).never
61
- adapter.get(basic_request)
69
+ adapter.request(:get)
62
70
  end
63
71
 
64
72
  it "is set if specified" do
65
- request = basic_request { |request| request.open_timeout = 30 }
73
+ request.open_timeout = 30
66
74
 
67
75
  httpclient.expects(:connect_timeout=).with(30)
68
- adapter.get(request)
76
+ adapter.request(:get)
69
77
  end
70
78
  end
71
79
 
72
80
  describe "receive_timeout" do
73
81
  it "is not set unless specified" do
74
82
  httpclient.expects(:receive_timeout=).never
75
- adapter.get(basic_request)
83
+ adapter.request(:get)
76
84
  end
77
85
 
78
86
  it "is set if specified" do
79
- request = basic_request { |request| request.read_timeout = 30 }
87
+ request.read_timeout = 30
80
88
 
81
89
  httpclient.expects(:receive_timeout=).with(30)
82
- adapter.get(request)
90
+ adapter.request(:get)
83
91
  end
84
92
  end
85
93
 
86
94
  describe "set_auth" do
87
95
  it "is set for HTTP basic auth" do
88
- request = basic_request { |request| request.auth.basic "username", "password" }
96
+ request.auth.basic "username", "password"
89
97
 
90
98
  httpclient.expects(:set_auth).with(request.url, *request.auth.credentials)
91
- adapter.get(request)
99
+ adapter.request(:get)
92
100
  end
93
101
 
94
102
  it "is set for HTTP digest auth" do
95
- request = basic_request { |request| request.auth.digest "username", "password" }
103
+ request.auth.digest "username", "password"
96
104
 
97
105
  httpclient.expects(:set_auth).with(request.url, *request.auth.credentials)
98
- adapter.get(request)
106
+ adapter.request(:get)
99
107
  end
100
108
  end
101
109
 
102
110
  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
111
+ before do
112
+ request.auth.ssl.cert_key_file = "spec/fixtures/client_key.pem"
113
+ request.auth.ssl.cert_file = "spec/fixtures/client_cert.pem"
108
114
  end
109
115
 
110
116
  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)
117
+ ssl_config.expects(:client_cert=).with(request.auth.ssl.cert)
118
+ ssl_config.expects(:client_key=).with(request.auth.ssl.cert_key)
119
+ ssl_config.expects(:verify_mode=).with(request.auth.ssl.openssl_verify_mode)
114
120
 
115
- adapter.get(ssl_auth_request)
121
+ adapter.request(:get)
116
122
  end
117
123
 
118
124
  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)
125
+ request.auth.ssl.ca_cert_file = "spec/fixtures/client_cert.pem"
126
+ ssl_config.expects(:add_trust_ca).with(request.auth.ssl.ca_cert_file)
121
127
 
122
- adapter.get(ssl_auth_request)
128
+ adapter.request(:get)
129
+ end
130
+
131
+ it 'should set the ssl_version if specified' do
132
+ request.auth.ssl.ssl_version = :SSLv3
133
+ ssl_config.expects(:ssl_version=).with(request.auth.ssl.ssl_version)
134
+
135
+ adapter.request(:get)
123
136
  end
124
137
  end
125
138
 
126
139
  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
140
+ before do
141
+ request.auth.ssl.verify_mode = :none
131
142
  end
132
143
 
133
144
  it "verify_mode should be set" do
134
- ssl_config.expects(:verify_mode=).with(ssl_auth_request.auth.ssl.openssl_verify_mode)
145
+ ssl_config.expects(:verify_mode=).with(request.auth.ssl.openssl_verify_mode)
135
146
 
136
- adapter.get(ssl_auth_request)
147
+ adapter.request(:get)
137
148
  end
138
149
 
139
150
  it "does not set client_cert and client_key "do
140
151
  ssl_config.expects(:client_cert=).never
141
152
  ssl_config.expects(:client_key=).never
142
153
 
143
- adapter.get(ssl_auth_request)
154
+ adapter.request(:get)
144
155
  end
145
156
 
146
157
  it "does not raise an exception" do
147
- expect { adapter.get(ssl_auth_request) }.to_not raise_error
158
+ expect { adapter.request(:get) }.to_not raise_error
148
159
  end
149
160
  end
150
161
  end
151
162
 
163
+ def httpclient_expects(method)
164
+ httpclient.expects(:request).
165
+ with(method, request.url, nil, request.body, request.headers).
166
+ returns(http_message)
167
+ end
168
+
152
169
  def http_message(body = Fixture.xml)
153
170
  message = HTTP::Message.new_response body
154
171
  message.header.set "Accept-encoding", "utf-8"
155
172
  message
156
173
  end
157
174
 
158
- def basic_request
159
- request = HTTPI::Request.new "http://example.com"
160
- yield request if block_given?
161
- request
162
- end
163
-
164
175
  end
@@ -2,133 +2,148 @@ require "spec_helper"
2
2
  require "httpi/adapter/net_http"
3
3
  require "httpi/request"
4
4
 
5
+ HTTPI::Adapter.load_adapter(:net_http)
6
+
5
7
  describe HTTPI::Adapter::NetHTTP do
6
- let(:net_http) { Net::HTTP.any_instance }
8
+
9
+ let(:net_http) { Net::HTTP.any_instance }
10
+ let(:request) { HTTPI::Request.new("http://example.com") }
7
11
  let(:basic_response) { { :body => Fixture.xml, :headers => { "Accept-encoding" => "utf-8" } } }
8
12
 
9
- def adapter(request)
10
- @adapter ||= HTTPI::Adapter::NetHTTP.new request
11
- end
13
+ let(:adapter) {
14
+ HTTPI::Adapter::NetHTTP.new(request)
15
+ }
12
16
 
13
- describe "#get" do
17
+ describe "#request(:get)" do
14
18
  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)
19
+ stub_request(:get, request.url.to_s).to_return(basic_response)
20
+ adapter.request(:get).should match_response(:body => Fixture.xml)
17
21
  end
18
22
  end
19
23
 
20
- describe "#post" do
24
+ describe "#request(:post)" do
21
25
  it "should return a valid HTTPI::Response" do
22
- request = HTTPI::Request.new :url => "http://example.com", :body => Fixture.xml
26
+ request.body = Fixture.xml
23
27
  stub_request(:post, request.url.to_s).with(:body => request.body).to_return(basic_response)
24
28
 
25
- adapter(request).post(request).should match_response(:body => Fixture.xml)
29
+ adapter.request(:post).should match_response(:body => Fixture.xml)
26
30
  end
27
31
  end
28
32
 
29
- describe "#head" do
33
+ describe "#request(:head)" do
30
34
  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)
35
+ stub_request(:head, request.url.to_s).to_return(basic_response)
36
+ adapter.request(:head).should match_response(:body => Fixture.xml)
33
37
  end
34
38
  end
35
39
 
36
- describe "#put" do
40
+ describe "#request(:put)" do
37
41
  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
- )
42
+ request.url = "http://example.com"
43
+ request.headers = { "Accept-encoding" => "utf-8" }
44
+ request.body = Fixture.xml
45
+
43
46
  stub_request(:put, request.url.to_s).with(:body => request.body).to_return(basic_response)
44
47
 
45
- adapter(request).put(request).should match_response(:body => Fixture.xml)
48
+ adapter.request(:put).should match_response(:body => Fixture.xml)
46
49
  end
47
50
  end
48
51
 
49
- describe "#delete" do
52
+ describe "#request(:delete)" do
50
53
  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)
54
+ stub_request(:delete, request.url.to_s).to_return(basic_response)
55
+ adapter.request(:delete).should match_response(:body => Fixture.xml)
56
+ end
57
+ end
58
+
59
+ describe "#request(:custom)" do
60
+ it "raises a NotSupportedError" do
61
+ expect { adapter.request(:custom) }.
62
+ to raise_error(HTTPI::NotSupportedError, "Net::HTTP does not support custom HTTP methods")
53
63
  end
54
64
  end
55
65
 
56
66
  describe "settings:" do
57
- before { stub_request(:get, basic_request.url.to_s) }
67
+ before { stub_request(:get, request.url.to_s) }
58
68
 
59
69
  describe "use_ssl" do
60
70
  it "should be set to false for non-SSL requests" do
61
71
  net_http.expects(:use_ssl=).with(false)
62
- adapter(basic_request).get(basic_request)
72
+ adapter.request(:get)
63
73
  end
64
74
 
65
75
  it "should be set to true for SSL requests" do
66
- request = basic_request { |request| request.ssl = true }
76
+ request.ssl = true
67
77
 
68
78
  net_http.expects(:use_ssl=).with(true)
69
- adapter(request).get(request)
79
+ adapter.request(:get)
70
80
  end
71
81
  end
72
82
 
73
83
  describe "open_timeout" do
74
84
  it "should not be set if not specified" do
75
85
  net_http.expects(:open_timeout=).never
76
- adapter(basic_request).get(basic_request)
86
+ adapter.request(:get)
77
87
  end
78
88
 
79
89
  it "should be set if specified" do
80
- request = basic_request { |request| request.open_timeout = 30 }
90
+ request.open_timeout = 30
81
91
 
82
92
  net_http.expects(:open_timeout=).with(30)
83
- adapter(request).get(request)
93
+ adapter.request(:get)
84
94
  end
85
95
  end
86
96
 
87
97
  describe "read_timeout" do
88
98
  it "should not be set if not specified" do
89
99
  net_http.expects(:read_timeout=).never
90
- adapter(basic_request).get(basic_request)
100
+ adapter.request(:get)
91
101
  end
92
102
 
93
103
  it "should be set if specified" do
94
- request = basic_request { |request| request.read_timeout = 30 }
104
+ request.read_timeout = 30
95
105
 
96
106
  net_http.expects(:read_timeout=).with(30)
97
- adapter(request).get(request)
107
+ adapter.request(:get)
98
108
  end
99
109
  end
100
110
 
101
111
  describe "basic_auth" do
102
112
  it "should be set for HTTP basic auth" do
103
- request = basic_request { |request| request.auth.basic "username", "password" }
113
+ request.auth.basic "username", "password"
104
114
 
105
115
  stub_request(:get, "http://username:password@example.com")
106
116
  Net::HTTP::Get.any_instance.expects(:basic_auth).with(*request.auth.credentials)
107
- adapter(request).get(request)
117
+ adapter.request(:get)
108
118
  end
109
119
  end
110
120
 
111
121
  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
122
+ before do
123
+ request.auth.ssl.cert_key_file = "spec/fixtures/client_key.pem"
124
+ request.auth.ssl.cert_file = "spec/fixtures/client_cert.pem"
117
125
  end
118
126
 
119
127
  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)
128
+ net_http.expects(:cert=).with(request.auth.ssl.cert)
129
+ net_http.expects(:key=).with(request.auth.ssl.cert_key)
130
+ net_http.expects(:verify_mode=).with(request.auth.ssl.openssl_verify_mode)
123
131
 
124
- adapter(ssl_auth_request).get(ssl_auth_request)
132
+ adapter.request(:get)
125
133
  end
126
134
 
127
135
  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)
136
+ request.auth.ssl.ca_cert_file = "spec/fixtures/client_cert.pem"
137
+ net_http.expects(:ca_file=).with(request.auth.ssl.ca_cert_file)
138
+
139
+ adapter.request(:get)
140
+ end
141
+
142
+ it 'should set the ssl_version if specified' do
143
+ request.auth.ssl.ssl_version = :SSLv3
144
+ net_http.expects(:ssl_version=).with(request.auth.ssl.ssl_version)
130
145
 
131
- adapter(ssl_auth_request).get(ssl_auth_request)
146
+ adapter.request(:get)
132
147
  end
133
148
  end
134
149
  end