httpi 0.9.3 → 0.9.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -133,6 +133,11 @@ module HTTPI
133
133
  send method, request, adapter
134
134
  end
135
135
 
136
+ # Shortcut for setting the default adapter to use.
137
+ def adapter=(adapter)
138
+ Adapter.use = adapter
139
+ end
140
+
136
141
  # Sets whether to log HTTP requests.
137
142
  attr_writer :log
138
143
 
@@ -14,30 +14,30 @@ module HTTPI
14
14
  module Adapter
15
15
 
16
16
  ADAPTERS = {
17
- :httpclient => HTTPClient,
18
- :curb => Curb,
19
- :net_http => NetHTTP
17
+ :httpclient => { :class => HTTPClient, :dependencies => ["httpclient"] },
18
+ :curb => { :class => Curb, :dependencies => ["curb"] },
19
+ :net_http => { :class => NetHTTP, :dependencies => ["net/https", "net/ntlm_http"] }
20
20
  }
21
21
 
22
- DEPENDENCIES = [
23
- [:httpclient, ["httpclient"]],
24
- [:curb, ["curb"]],
25
- [:net_http, ["net/https", "net/ntlm_http"]]
26
- ]
22
+ LOAD_ORDER = [:httpclient, :curb, :net_http]
27
23
 
28
24
  class << self
29
25
 
30
26
  def use=(adapter)
31
- @adapter = adapter.nil? ? nil : validate_adapter!(adapter)
27
+ return @adapter = nil if adapter.nil?
28
+
29
+ validate_adapter! adapter
30
+ load_dependencies adapter
31
+ @adapter = adapter
32
32
  end
33
33
 
34
34
  def use
35
35
  @adapter ||= default_adapter
36
36
  end
37
37
 
38
- def load(adapter = nil)
38
+ def load(adapter)
39
39
  adapter = adapter ? validate_adapter!(adapter) : use
40
- [adapter, ADAPTERS[adapter]]
40
+ [adapter, ADAPTERS[adapter][:class]]
41
41
  end
42
42
 
43
43
  private
@@ -48,9 +48,9 @@ module HTTPI
48
48
  end
49
49
 
50
50
  def default_adapter
51
- DEPENDENCIES.each do |(adapter, dependencies)|
51
+ LOAD_ORDER.each do |adapter|
52
52
  begin
53
- dependencies.each { |dependency| require dependency }
53
+ load_dependencies adapter
54
54
  return adapter
55
55
  rescue LoadError
56
56
  next
@@ -58,6 +58,10 @@ module HTTPI
58
58
  end
59
59
  end
60
60
 
61
+ def load_dependencies(adapter)
62
+ ADAPTERS[adapter][:dependencies].each { |dependency| require dependency }
63
+ end
64
+
61
65
  end
62
66
  end
63
67
  end
@@ -17,7 +17,7 @@ module HTTPI
17
17
  rescue TypeError, Errno::ENOENT
18
18
  false
19
19
  end
20
-
20
+
21
21
  # Accessor for the cert key file to validate SSL certificates.
22
22
  attr_accessor :cert_key_file
23
23
 
@@ -29,18 +29,18 @@ module HTTPI
29
29
 
30
30
  # Accessor for the cacert file to validate SSL certificates.
31
31
  attr_accessor :ca_cert_file
32
-
32
+
33
33
  # Returns the cert type to validate SSL certificates PEM|DER.
34
34
  def cert_type
35
35
  @cert_type ||= :pem
36
36
  end
37
-
37
+
38
38
  # Sets the cert type to validate SSL certificates PEM|DER.
39
39
  def cert_type=(type)
40
40
  raise ArgumentError, "Invalid SSL cert type: #{type}" unless CERT_TYPES.include? type
41
41
  @cert_type = type
42
42
  end
43
-
43
+
44
44
  # Returns the SSL verify mode. Defaults to <tt>:peer</tt>.
45
45
  def verify_mode
46
46
  @verify_mode ||= :peer
@@ -77,7 +77,7 @@ module HTTPI
77
77
  def mass_assign(args)
78
78
  ATTRIBUTES.each { |key| send("#{key}=", args[key]) if args[key] }
79
79
  end
80
-
80
+
81
81
  private
82
82
 
83
83
  # Expects a +url+, validates its validity and returns a +URI+ object.
@@ -1,5 +1,5 @@
1
1
  module HTTPI
2
2
 
3
- VERSION = "0.9.3"
3
+ VERSION = "0.9.4"
4
4
 
5
5
  end
@@ -2,228 +2,231 @@ require "spec_helper"
2
2
  require "httpi/adapter/curb"
3
3
  require "httpi/request"
4
4
 
5
- require "curb"
6
-
7
- describe HTTPI::Adapter::Curb do
8
- let(:adapter) { HTTPI::Adapter::Curb.new }
9
- let(:curb) { Curl::Easy.any_instance }
10
-
11
- describe "#get" do
12
- before do
13
- curb.expects(:http_get)
14
- curb.expects(:response_code).returns(200)
15
- curb.expects(:header_str).returns("Accept-encoding: utf-8")
16
- curb.expects(:body_str).returns(Fixture.xml)
17
- end
5
+ # curb does not run on jruby
6
+ unless RUBY_PLATFORM =~ /java/
7
+ require "curb"
18
8
 
19
- it "should return a valid HTTPI::Response" do
20
- adapter.get(basic_request).should match_response(:body => Fixture.xml)
21
- end
22
- end
9
+ describe HTTPI::Adapter::Curb do
10
+ let(:adapter) { HTTPI::Adapter::Curb.new }
11
+ let(:curb) { Curl::Easy.any_instance }
23
12
 
24
- describe "#post" do
25
- before do
26
- curb.expects(:http_post)
27
- curb.expects(:response_code).returns(200)
28
- curb.expects(:header_str).returns("Accept-encoding: utf-8")
29
- curb.expects(:body_str).returns(Fixture.xml)
30
- end
13
+ describe "#get" do
14
+ before do
15
+ curb.expects(:http_get)
16
+ curb.expects(:response_code).returns(200)
17
+ curb.expects(:header_str).returns("Accept-encoding: utf-8")
18
+ curb.expects(:body_str).returns(Fixture.xml)
19
+ end
31
20
 
32
- it "should return a valid HTTPI::Response" do
33
- adapter.post(basic_request).should match_response(:body => Fixture.xml)
21
+ it "returns a valid HTTPI::Response" do
22
+ adapter.get(basic_request).should match_response(:body => Fixture.xml)
23
+ end
34
24
  end
35
- end
36
25
 
37
- describe "#post includes body of request" do
38
- it "should send the body in the request" do
39
- curb.expects(:http_post).with('xml=hi&name=123')
40
- adapter.post(basic_request { |request| request.body = 'xml=hi&name=123' } )
41
- end
42
- end
26
+ describe "#post" do
27
+ before do
28
+ curb.expects(:http_post)
29
+ curb.expects(:response_code).returns(200)
30
+ curb.expects(:header_str).returns("Accept-encoding: utf-8")
31
+ curb.expects(:body_str).returns(Fixture.xml)
32
+ end
43
33
 
44
- describe "#head" do
45
- before do
46
- curb.expects(:http_head)
47
- curb.expects(:response_code).returns(200)
48
- curb.expects(:header_str).returns("Accept-encoding: utf-8")
49
- curb.expects(:body_str).returns(Fixture.xml)
34
+ it "returns a valid HTTPI::Response" do
35
+ adapter.post(basic_request).should match_response(:body => Fixture.xml)
36
+ end
50
37
  end
51
38
 
52
- it "should return a valid HTTPI::Response" do
53
- adapter.head(basic_request).should match_response(:body => Fixture.xml)
39
+ describe "#post" do
40
+ it "sends the body in the request" do
41
+ curb.expects(:http_post).with('xml=hi&name=123')
42
+ adapter.post(basic_request { |request| request.body = 'xml=hi&name=123' } )
43
+ end
54
44
  end
55
- end
56
45
 
57
- describe "#put" do
58
- before do
59
- curb.expects(:http_put)
60
- curb.expects(:response_code).returns(200)
61
- curb.expects(:header_str).returns("Accept-encoding: utf-8")
62
- curb.expects(:body_str).returns(Fixture.xml)
63
- end
46
+ describe "#head" do
47
+ before do
48
+ curb.expects(:http_head)
49
+ curb.expects(:response_code).returns(200)
50
+ curb.expects(:header_str).returns("Accept-encoding: utf-8")
51
+ curb.expects(:body_str).returns(Fixture.xml)
52
+ end
64
53
 
65
- it "should return a valid HTTPI::Response" do
66
- adapter.put(basic_request).should match_response(:body => Fixture.xml)
54
+ it "returns a valid HTTPI::Response" do
55
+ adapter.head(basic_request).should match_response(:body => Fixture.xml)
56
+ end
67
57
  end
68
- end
69
58
 
70
- describe "#put includes body of request" do
71
- it "should send the body in the request" do
72
- curb.expects(:http_put).with('xml=hi&name=123')
73
- adapter.put(basic_request { |request| request.body = 'xml=hi&name=123' } )
74
- end
75
- end
59
+ describe "#put" do
60
+ before do
61
+ curb.expects(:http_put)
62
+ curb.expects(:response_code).returns(200)
63
+ curb.expects(:header_str).returns("Accept-encoding: utf-8")
64
+ curb.expects(:body_str).returns(Fixture.xml)
65
+ end
76
66
 
77
- describe "#delete" do
78
- before do
79
- curb.expects(:http_delete)
80
- curb.expects(:response_code).returns(200)
81
- curb.expects(:header_str).returns("Accept-encoding: utf-8")
82
- curb.expects(:body_str).returns("")
67
+ it "returns a valid HTTPI::Response" do
68
+ adapter.put(basic_request).should match_response(:body => Fixture.xml)
69
+ end
83
70
  end
84
71
 
85
- it "should return a valid HTTPI::Response" do
86
- adapter.delete(basic_request).should match_response(:body => "")
72
+ describe "#put" do
73
+ it "sends the body in the request" do
74
+ curb.expects(:http_put).with('xml=hi&name=123')
75
+ adapter.put(basic_request { |request| request.body = 'xml=hi&name=123' } )
76
+ end
87
77
  end
88
- end
89
78
 
90
- describe "settings:" do
91
- before { curb.stubs(:http_get) }
79
+ describe "#delete" do
80
+ before do
81
+ curb.expects(:http_delete)
82
+ curb.expects(:response_code).returns(200)
83
+ curb.expects(:header_str).returns("Accept-encoding: utf-8")
84
+ curb.expects(:body_str).returns("")
85
+ end
92
86
 
93
- describe "url" do
94
- it "should always set the request url" do
95
- curb.expects(:url=).with(basic_request.url.to_s)
96
- adapter.get(basic_request)
87
+ it "returns a valid HTTPI::Response" do
88
+ adapter.delete(basic_request).should match_response(:body => "")
97
89
  end
98
90
  end
99
91
 
100
- describe "proxy_url" do
101
- it "should not be set if not specified" do
102
- curb.expects(:proxy_url=).never
103
- adapter.get(basic_request)
92
+ describe "settings:" do
93
+ before { curb.stubs(:http_get) }
94
+
95
+ describe "url" do
96
+ it "always sets the request url" do
97
+ curb.expects(:url=).with(basic_request.url.to_s)
98
+ adapter.get(basic_request)
99
+ end
104
100
  end
105
101
 
106
- it "should be set if specified" do
107
- request = basic_request { |request| request.proxy = "http://proxy.example.com" }
102
+ describe "proxy_url" do
103
+ it "is not set unless it's specified" do
104
+ curb.expects(:proxy_url=).never
105
+ adapter.get(basic_request)
106
+ end
108
107
 
109
- curb.expects(:proxy_url=).with(request.proxy.to_s)
110
- adapter.get(request)
111
- end
112
- end
108
+ it "is set if specified" do
109
+ request = basic_request { |request| request.proxy = "http://proxy.example.com" }
113
110
 
114
- describe "timeout" do
115
- it "should not be set if not specified" do
116
- curb.expects(:timeout=).never
117
- adapter.get(basic_request)
111
+ curb.expects(:proxy_url=).with(request.proxy.to_s)
112
+ adapter.get(request)
113
+ end
118
114
  end
119
115
 
120
- it "should be set if specified" do
121
- request = basic_request { |request| request.read_timeout = 30 }
116
+ describe "timeout" do
117
+ it "is not set unless it's specified" do
118
+ curb.expects(:timeout=).never
119
+ adapter.get(basic_request)
120
+ end
122
121
 
123
- curb.expects(:timeout=).with(30)
124
- adapter.get(request)
125
- end
126
- end
122
+ it "is set if specified" do
123
+ request = basic_request { |request| request.read_timeout = 30 }
127
124
 
128
- describe "connect_timeout" do
129
- it "should not be set if not specified" do
130
- curb.expects(:connect_timeout=).never
131
- adapter.get(basic_request)
125
+ curb.expects(:timeout=).with(30)
126
+ adapter.get(request)
127
+ end
132
128
  end
133
129
 
134
- it "should be set if specified" do
135
- request = basic_request { |request| request.open_timeout = 30 }
130
+ describe "connect_timeout" do
131
+ it "is not set unless it's specified" do
132
+ curb.expects(:connect_timeout=).never
133
+ adapter.get(basic_request)
134
+ end
135
+
136
+ it "is set if specified" do
137
+ request = basic_request { |request| request.open_timeout = 30 }
136
138
 
137
- curb.expects(:connect_timeout=).with(30)
138
- adapter.get(request)
139
+ curb.expects(:connect_timeout=).with(30)
140
+ adapter.get(request)
141
+ end
139
142
  end
140
- end
141
143
 
142
- describe "headers" do
143
- it "should always be set" do
144
- curb.expects(:headers=).with({})
145
- adapter.get(basic_request)
144
+ describe "headers" do
145
+ it "is always set" do
146
+ curb.expects(:headers=).with({})
147
+ adapter.get(basic_request)
148
+ end
146
149
  end
147
- end
148
150
 
149
- describe "verbose" do
150
- it "should always be set to false" do
151
- curb.expects(:verbose=).with(false)
152
- adapter.get(basic_request)
151
+ describe "verbose" do
152
+ it "is always set to false" do
153
+ curb.expects(:verbose=).with(false)
154
+ adapter.get(basic_request)
155
+ end
153
156
  end
154
- end
155
157
 
156
- describe "http_auth_types" do
157
- it "should be set to :basic for HTTP basic auth" do
158
- request = basic_request { |request| request.auth.basic "username", "password" }
158
+ describe "http_auth_types" do
159
+ it "is set to :basic for HTTP basic auth" do
160
+ request = basic_request { |request| request.auth.basic "username", "password" }
159
161
 
160
- curb.expects(:http_auth_types=).with(:basic)
161
- adapter.get(request)
162
- end
162
+ curb.expects(:http_auth_types=).with(:basic)
163
+ adapter.get(request)
164
+ end
163
165
 
164
- it "should be set to :digest for HTTP digest auth" do
165
- request = basic_request { |request| request.auth.digest "username", "password" }
166
+ it "is set to :digest for HTTP digest auth" do
167
+ request = basic_request { |request| request.auth.digest "username", "password" }
166
168
 
167
- curb.expects(:http_auth_types=).with(:digest)
168
- adapter.get(request)
169
+ curb.expects(:http_auth_types=).with(:digest)
170
+ adapter.get(request)
171
+ end
169
172
  end
170
- end
171
173
 
172
- describe "username and password" do
173
- it "should be set for HTTP basic auth" do
174
- request = basic_request { |request| request.auth.basic "username", "password" }
174
+ describe "username and password" do
175
+ it "is set for HTTP basic auth" do
176
+ request = basic_request { |request| request.auth.basic "username", "password" }
175
177
 
176
- curb.expects(:username=).with("username")
177
- curb.expects(:password=).with("password")
178
- adapter.get(request)
179
- end
178
+ curb.expects(:username=).with("username")
179
+ curb.expects(:password=).with("password")
180
+ adapter.get(request)
181
+ end
180
182
 
181
- it "should be set for HTTP digest auth" do
182
- request = basic_request { |request| request.auth.digest "username", "password" }
183
+ it "is set for HTTP digest auth" do
184
+ request = basic_request { |request| request.auth.digest "username", "password" }
183
185
 
184
- curb.expects(:username=).with("username")
185
- curb.expects(:password=).with("password")
186
- adapter.get(request)
186
+ curb.expects(:username=).with("username")
187
+ curb.expects(:password=).with("password")
188
+ adapter.get(request)
189
+ end
187
190
  end
188
- end
189
191
 
190
- context "(for SSL client auth)" do
191
- let(:ssl_auth_request) do
192
- basic_request do |request|
193
- request.auth.ssl.cert_key_file = "spec/fixtures/client_key.pem"
194
- request.auth.ssl.cert_file = "spec/fixtures/client_cert.pem"
192
+ context "(for SSL client auth)" do
193
+ let(:ssl_auth_request) do
194
+ basic_request do |request|
195
+ request.auth.ssl.cert_key_file = "spec/fixtures/client_key.pem"
196
+ request.auth.ssl.cert_file = "spec/fixtures/client_cert.pem"
197
+ end
195
198
  end
196
- end
197
199
 
198
- it "cert_key, cert and ssl_verify_peer should be set" do
199
- curb.expects(:cert_key=).with(ssl_auth_request.auth.ssl.cert_key_file)
200
- curb.expects(:cert=).with(ssl_auth_request.auth.ssl.cert_file)
201
- curb.expects(:ssl_verify_peer=).with(true)
202
- curb.expects(:certtype=).with(ssl_auth_request.auth.ssl.cert_type.to_s.upcase)
200
+ it "cert_key, cert and ssl_verify_peer should be set" do
201
+ curb.expects(:cert_key=).with(ssl_auth_request.auth.ssl.cert_key_file)
202
+ curb.expects(:cert=).with(ssl_auth_request.auth.ssl.cert_file)
203
+ curb.expects(:ssl_verify_peer=).with(true)
204
+ curb.expects(:certtype=).with(ssl_auth_request.auth.ssl.cert_type.to_s.upcase)
203
205
 
204
- adapter.get(ssl_auth_request)
205
- end
206
+ adapter.get(ssl_auth_request)
207
+ end
206
208
 
207
- it "should set the cert_type to DER if specified" do
208
- ssl_auth_request.auth.ssl.cert_type = :der
209
- curb.expects(:certtype=).with(:der.to_s.upcase)
209
+ it "sets the cert_type to DER if specified" do
210
+ ssl_auth_request.auth.ssl.cert_type = :der
211
+ curb.expects(:certtype=).with(:der.to_s.upcase)
210
212
 
211
- adapter.get(ssl_auth_request)
212
- end
213
+ adapter.get(ssl_auth_request)
214
+ end
213
215
 
214
- it "should set the cacert if specified" do
215
- ssl_auth_request.auth.ssl.ca_cert_file = "spec/fixtures/client_cert.pem"
216
- curb.expects(:cacert=).with(ssl_auth_request.auth.ssl.ca_cert_file)
216
+ it "sets the cacert if specified" do
217
+ ssl_auth_request.auth.ssl.ca_cert_file = "spec/fixtures/client_cert.pem"
218
+ curb.expects(:cacert=).with(ssl_auth_request.auth.ssl.ca_cert_file)
217
219
 
218
- adapter.get(ssl_auth_request)
220
+ adapter.get(ssl_auth_request)
221
+ end
219
222
  end
220
223
  end
221
- end
222
224
 
223
- def basic_request
224
- request = HTTPI::Request.new :url => "http://example.com"
225
- yield request if block_given?
226
- request
227
- end
225
+ def basic_request
226
+ request = HTTPI::Request.new :url => "http://example.com"
227
+ yield request if block_given?
228
+ request
229
+ end
228
230
 
231
+ end
229
232
  end