httpi 2.0.2 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,96 @@
1
+ require "spec_helper"
2
+ require "integration/support/server"
3
+
4
+ describe HTTPI::Adapter::Excon do
5
+
6
+ subject(:adapter) { :excon }
7
+
8
+ context "http requests" do
9
+ before :all do
10
+ @server = IntegrationServer.run
11
+ end
12
+
13
+ after :all do
14
+ @server.stop
15
+ end
16
+
17
+ it "sends and receives HTTP headers" do
18
+ request = HTTPI::Request.new(@server.url + "x-header")
19
+ request.headers["X-Header"] = "HTTPI"
20
+
21
+ response = HTTPI.get(request, adapter)
22
+ response.body.should include("HTTPI")
23
+ end
24
+
25
+ it "executes GET requests" do
26
+ response = HTTPI.get(@server.url, adapter)
27
+ response.body.should eq("get")
28
+ response.headers["Content-Type"].should eq("text/plain")
29
+ end
30
+
31
+ it "executes POST requests" do
32
+ response = HTTPI.post(@server.url, "<some>xml</some>", adapter)
33
+ response.body.should eq("post")
34
+ response.headers["Content-Type"].should eq("text/plain")
35
+ end
36
+
37
+ it "executes HEAD requests" do
38
+ response = HTTPI.head(@server.url, adapter)
39
+ response.code.should == 200
40
+ response.headers["Content-Type"].should eq("text/plain")
41
+ end
42
+
43
+ it "executes PUT requests" do
44
+ response = HTTPI.put(@server.url, "<some>xml</some>", adapter)
45
+ response.body.should eq("put")
46
+ response.headers["Content-Type"].should eq("text/plain")
47
+ end
48
+
49
+ it "executes DELETE requests" do
50
+ response = HTTPI.delete(@server.url, adapter)
51
+ response.body.should eq("delete")
52
+ response.headers["Content-Type"].should eq("text/plain")
53
+ end
54
+
55
+ it "supports basic authentication" do
56
+ request = HTTPI::Request.new(@server.url + "basic-auth")
57
+ request.auth.basic("admin", "secret")
58
+
59
+ response = HTTPI.get(request, adapter)
60
+ response.body.should eq("basic-auth")
61
+ end
62
+
63
+ it "does not support ntlm authentication" do
64
+ request = HTTPI::Request.new(@server.url + "ntlm-auth")
65
+ request.auth.ntlm("tester", "vReqSoafRe5O")
66
+
67
+ expect { HTTPI.get(request, adapter) }.
68
+ to raise_error(HTTPI::NotSupportedError, /does not support NTLM authentication/)
69
+ end
70
+ end
71
+
72
+ # it does not support digest auth
73
+
74
+ if RUBY_PLATFORM =~ /java/
75
+ pending "Puma Server complains: SSL not supported on JRuby"
76
+ else
77
+ context "https requests" do
78
+ before :all do
79
+ @server = IntegrationServer.run(:ssl => true)
80
+ end
81
+ after :all do
82
+ @server.stop
83
+ end
84
+
85
+ # it does not raise when no certificate was set up
86
+ it "works when set up properly" do
87
+ request = HTTPI::Request.new(@server.url)
88
+ request.auth.ssl.ca_cert_file = IntegrationServer.ssl_ca_file
89
+
90
+ response = HTTPI.get(request, adapter)
91
+ expect(response.body).to eq("get")
92
+ end
93
+ end
94
+ end
95
+
96
+ end
@@ -113,6 +113,14 @@ describe HTTPI::Adapter::HTTPClient do
113
113
  request.auth.ssl.cert_file = "spec/fixtures/client_cert.pem"
114
114
  end
115
115
 
116
+ it "send certificate regardless of state of SSL verify mode" do
117
+ request.auth.ssl.verify_mode = :none
118
+ ssl_config.expects(:client_cert=).with(request.auth.ssl.cert)
119
+ ssl_config.expects(:client_key=).with(request.auth.ssl.cert_key)
120
+
121
+ adapter.request(:get)
122
+ end
123
+
116
124
  it "client_cert, client_key and verify_mode should be set" do
117
125
  ssl_config.expects(:client_cert=).with(request.auth.ssl.cert)
118
126
  ssl_config.expects(:client_key=).with(request.auth.ssl.cert_key)
@@ -130,7 +138,7 @@ describe HTTPI::Adapter::HTTPClient do
130
138
 
131
139
  it 'should set the ssl_version if specified' do
132
140
  request.auth.ssl.ssl_version = :SSLv3
133
- ssl_config.expects(:ssl_version=).with(request.auth.ssl.ssl_version)
141
+ ssl_config.expects(:ssl_version=).with('SSLv3')
134
142
 
135
143
  adapter.request(:get)
136
144
  end
@@ -147,19 +155,19 @@ describe HTTPI::Adapter::HTTPClient do
147
155
  adapter.request(:get)
148
156
  end
149
157
 
150
- it "does not set client_cert and client_key "do
151
- ssl_config.expects(:client_cert=).never
152
- ssl_config.expects(:client_key=).never
153
-
154
- adapter.request(:get)
155
- end
156
-
157
158
  it "does not raise an exception" do
158
159
  expect { adapter.request(:get) }.to_not raise_error
159
160
  end
160
161
  end
161
162
  end
162
163
 
164
+ it "does not support NTLM authentication" do
165
+ request.auth.ntlm("tester", "vReqSoafRe5O")
166
+
167
+ expect { adapter.request(:get) }.
168
+ to raise_error(HTTPI::NotSupportedError, /adapter does not support NTLM authentication/)
169
+ end
170
+
163
171
  def httpclient_expects(method)
164
172
  httpclient.expects(:request).
165
173
  with(method, request.url, nil, request.body, request.headers).
@@ -0,0 +1,96 @@
1
+ require "spec_helper"
2
+ require "integration/support/server"
3
+
4
+ describe HTTPI::Adapter::NetHTTPPersistent do
5
+
6
+ subject(:adapter) { :net_http_persistent }
7
+
8
+ context "http requests" do
9
+ before :all do
10
+ @server = IntegrationServer.run
11
+ end
12
+
13
+ after :all do
14
+ @server.stop
15
+ end
16
+
17
+ it "sends and receives HTTP headers" do
18
+ request = HTTPI::Request.new(@server.url + "x-header")
19
+ request.headers["X-Header"] = "HTTPI"
20
+
21
+ response = HTTPI.get(request, adapter)
22
+ response.body.should include("HTTPI")
23
+ end
24
+
25
+ it "executes GET requests" do
26
+ response = HTTPI.get(@server.url, adapter)
27
+ response.body.should eq("get")
28
+ response.headers["Content-Type"].should eq("text/plain")
29
+ end
30
+
31
+ it "executes POST requests" do
32
+ response = HTTPI.post(@server.url, "<some>xml</some>", adapter)
33
+ response.body.should eq("post")
34
+ response.headers["Content-Type"].should eq("text/plain")
35
+ end
36
+
37
+ it "executes HEAD requests" do
38
+ response = HTTPI.head(@server.url, adapter)
39
+ response.code.should == 200
40
+ response.headers["Content-Type"].should eq("text/plain")
41
+ end
42
+
43
+ it "executes PUT requests" do
44
+ response = HTTPI.put(@server.url, "<some>xml</some>", adapter)
45
+ response.body.should eq("put")
46
+ response.headers["Content-Type"].should eq("text/plain")
47
+ end
48
+
49
+ it "executes DELETE requests" do
50
+ response = HTTPI.delete(@server.url, adapter)
51
+ response.body.should eq("delete")
52
+ response.headers["Content-Type"].should eq("text/plain")
53
+ end
54
+
55
+ it "supports basic authentication" do
56
+ request = HTTPI::Request.new(@server.url + "basic-auth")
57
+ request.auth.basic("admin", "secret")
58
+
59
+ response = HTTPI.get(request, adapter)
60
+ response.body.should eq("basic-auth")
61
+ end
62
+
63
+ it "does not support ntlm authentication" do
64
+ request = HTTPI::Request.new(@server.url + "ntlm-auth")
65
+ request.auth.ntlm("tester", "vReqSoafRe5O")
66
+
67
+ expect { HTTPI.get(request, adapter) }.
68
+ to raise_error(HTTPI::NotSupportedError, /does not support NTLM authentication/)
69
+ end
70
+ end
71
+
72
+ # it does not support digest auth
73
+
74
+ if RUBY_PLATFORM =~ /java/
75
+ pending "Puma Server complains: SSL not supported on JRuby"
76
+ else
77
+ context "https requests" do
78
+ before :all do
79
+ @server = IntegrationServer.run(:ssl => true)
80
+ end
81
+ after :all do
82
+ @server.stop
83
+ end
84
+
85
+ # it does not raise when no certificate was set up
86
+ it "works when set up properly" do
87
+ request = HTTPI::Request.new(@server.url)
88
+ request.auth.ssl.ca_cert_file = IntegrationServer.ssl_ca_file
89
+
90
+ response = HTTPI.get(request, adapter)
91
+ expect(response.body).to eq("get")
92
+ end
93
+ end
94
+ end
95
+
96
+ end
@@ -59,173 +59,46 @@ describe HTTPI::Adapter::NetHTTP do
59
59
  response = HTTPI.get(request, adapter)
60
60
  response.body.should eq("basic-auth")
61
61
  end
62
- end
63
-
64
- # it does not support digest auth
65
62
 
66
- context "https requests" do
67
- before :all do
68
- @server = IntegrationServer.run(:ssl => true)
69
- end
63
+ it "does not support digest authentication" do
64
+ request = HTTPI::Request.new(@server.url + "digest-auth")
65
+ request.auth.digest("admin", "secret")
70
66
 
71
- after :all do
72
- @server.stop
67
+ expect { HTTPI.get(request, adapter) }.
68
+ to raise_error(HTTPI::NotSupportedError, /does not support HTTP digest authentication/)
73
69
  end
74
70
 
75
- # it does not raise when no certificate was set up
76
-
77
- it "works when set up properly" do
78
- request = HTTPI::Request.new(@server.url)
79
- request.auth.ssl.ca_cert_file = IntegrationServer.ssl_ca_file
71
+ it "supports ntlm authentication" do
72
+ request = HTTPI::Request.new(@server.url + "ntlm-auth")
73
+ request.auth.ntlm("tester", "vReqSoafRe5O")
80
74
 
81
75
  response = HTTPI.get(request, adapter)
82
- expect(response.body).to eq("get")
83
- end
84
- end
85
-
86
- end
87
-
88
-
89
- __END__
90
-
91
- describe "#request(:get)" do
92
- it "should return a valid HTTPI::Response" do
93
- stub_request(:get, request.url.to_s).to_return(basic_response)
94
- adapter.request(:get).should match_response(:body => Fixture.xml)
95
- end
96
- end
97
-
98
- describe "#request(:post)" do
99
- it "should return a valid HTTPI::Response" do
100
- request.body = Fixture.xml
101
- stub_request(:post, request.url.to_s).with(:body => request.body).to_return(basic_response)
102
-
103
- adapter.request(:post).should match_response(:body => Fixture.xml)
104
- end
105
- end
106
-
107
- describe "#request(:head)" do
108
- it "should return a valid HTTPI::Response" do
109
- stub_request(:head, request.url.to_s).to_return(basic_response)
110
- adapter.request(:head).should match_response(:body => Fixture.xml)
111
- end
112
- end
113
-
114
- describe "#request(:put)" do
115
- it "should return a valid HTTPI::Response" do
116
- request.url = "http://example.com"
117
- request.headers = { "Accept-encoding" => "utf-8" }
118
- request.body = Fixture.xml
119
-
120
- stub_request(:put, request.url.to_s).with(:body => request.body).to_return(basic_response)
121
-
122
- adapter.request(:put).should match_response(:body => Fixture.xml)
123
- end
124
- end
125
-
126
- describe "#request(:delete)" do
127
- it "should return a valid HTTPI::Response" do
128
- stub_request(:delete, request.url.to_s).to_return(basic_response)
129
- adapter.request(:delete).should match_response(:body => Fixture.xml)
76
+ response.body.should eq("ntlm-auth")
130
77
  end
131
78
  end
132
79
 
133
- describe "#request(:custom)" do
134
- it "raises a NotSupportedError" do
135
- expect { adapter.request(:custom) }.
136
- to raise_error(HTTPI::NotSupportedError, "Net::HTTP does not support custom HTTP methods")
137
- end
138
- end
139
-
140
- describe "settings:" do
141
- before { stub_request(:get, request.url.to_s) }
142
-
143
- describe "use_ssl" do
144
- it "should be set to false for non-SSL requests" do
145
- net_http.expects(:use_ssl=).with(false)
146
- adapter.request(:get)
147
- end
148
-
149
- it "should be set to true for SSL requests" do
150
- request.ssl = true
151
-
152
- net_http.expects(:use_ssl=).with(true)
153
- adapter.request(:get)
154
- end
155
- end
156
-
157
- describe "open_timeout" do
158
- it "should not be set if not specified" do
159
- net_http.expects(:open_timeout=).never
160
- adapter.request(:get)
161
- end
162
-
163
- it "should be set if specified" do
164
- request.open_timeout = 30
165
-
166
- net_http.expects(:open_timeout=).with(30)
167
- adapter.request(:get)
168
- end
169
- end
170
-
171
- describe "read_timeout" do
172
- it "should not be set if not specified" do
173
- net_http.expects(:read_timeout=).never
174
- adapter.request(:get)
175
- end
176
-
177
- it "should be set if specified" do
178
- request.read_timeout = 30
179
-
180
- net_http.expects(:read_timeout=).with(30)
181
- adapter.request(:get)
182
- end
183
- end
184
-
185
- describe "basic_auth" do
186
- it "should be set for HTTP basic auth" do
187
- request.auth.basic "username", "password"
188
-
189
- stub_request(:get, "http://username:password@example.com")
190
- Net::HTTP::Get.any_instance.expects(:basic_auth).with(*request.auth.credentials)
191
- adapter.request(:get)
192
- end
193
- end
194
-
195
- context "(for SSL client auth)" do
196
- before do
197
- request.auth.ssl.cert_key_file = "spec/fixtures/client_key.pem"
198
- request.auth.ssl.cert_file = "spec/fixtures/client_cert.pem"
199
- end
200
-
201
- it "key, cert and verify_mode should be set" do
202
- net_http.expects(:cert=).with(request.auth.ssl.cert)
203
- net_http.expects(:key=).with(request.auth.ssl.cert_key)
204
- net_http.expects(:verify_mode=).with(request.auth.ssl.openssl_verify_mode)
80
+ # it does not support digest auth
205
81
 
206
- adapter.request(:get)
82
+ if RUBY_PLATFORM =~ /java/
83
+ pending "Puma Server complains: SSL not supported on JRuby"
84
+ else
85
+ context "https requests" do
86
+ before :all do
87
+ @server = IntegrationServer.run(:ssl => true)
207
88
  end
208
-
209
- it "should set the client_ca if specified" do
210
- request.auth.ssl.ca_cert_file = "spec/fixtures/client_cert.pem"
211
- net_http.expects(:ca_file=).with(request.auth.ssl.ca_cert_file)
212
-
213
- adapter.request(:get)
89
+ after :all do
90
+ @server.stop
214
91
  end
215
92
 
216
- it 'should set the ssl_version if specified' do
217
- request.auth.ssl.ssl_version = :SSLv3
218
- net_http.expects(:ssl_version=).with(request.auth.ssl.ssl_version)
93
+ # it does not raise when no certificate was set up
94
+ it "works when set up properly" do
95
+ request = HTTPI::Request.new(@server.url)
96
+ request.auth.ssl.ca_cert_file = IntegrationServer.ssl_ca_file
219
97
 
220
- adapter.request(:get)
98
+ response = HTTPI.get(request, adapter)
99
+ expect(response.body).to eq("get")
221
100
  end
222
101
  end
223
102
  end
224
103
 
225
- def basic_request
226
- request = HTTPI::Request.new "http://example.com"
227
- yield request if block_given?
228
- request
229
- end
230
-
231
104
  end
@@ -0,0 +1,111 @@
1
+ require "spec_helper"
2
+ require "integration/support/application"
3
+
4
+ describe HTTPI::Adapter::NetHTTP do
5
+
6
+ subject(:adapter) { :rack }
7
+
8
+ context "http requests" do
9
+ before :all do
10
+ @app = 'app'
11
+ @url = "http://#{@app}/"
12
+
13
+ HTTPI::Adapter::Rack.mount @app, IntegrationServer::Application
14
+ end
15
+
16
+ it "sends and receives HTTP headers" do
17
+ request = HTTPI::Request.new(@url + "x-header")
18
+ request.headers["X-Header"] = "HTTPI"
19
+
20
+ response = HTTPI.get(request, adapter)
21
+ response.body.should include("HTTPI")
22
+ end
23
+
24
+ it "executes GET requests" do
25
+ response = HTTPI.get(@url, adapter)
26
+ response.body.should eq("get")
27
+ response.headers["Content-Type"].should eq("text/plain")
28
+ end
29
+
30
+ it "executes POST requests" do
31
+ response = HTTPI.post(@url, "<some>xml</some>", adapter)
32
+ response.body.should eq("post")
33
+ response.headers["Content-Type"].should eq("text/plain")
34
+ end
35
+
36
+ it "executes HEAD requests" do
37
+ response = HTTPI.head(@url, adapter)
38
+ response.code.should == 200
39
+ response.headers["Content-Type"].should eq("text/plain")
40
+ end
41
+
42
+ it "executes PUT requests" do
43
+ response = HTTPI.put(@url, "<some>xml</some>", adapter)
44
+ response.body.should eq("put")
45
+ response.headers["Content-Type"].should eq("text/plain")
46
+ end
47
+
48
+ it "executes DELETE requests" do
49
+ response = HTTPI.delete(@url, adapter)
50
+ response.body.should eq("delete")
51
+ response.headers["Content-Type"].should eq("text/plain")
52
+ end
53
+
54
+ describe "settings:" do
55
+
56
+ let(:request) { HTTPI::Request.new("http://#{@app}") }
57
+ let(:client) { HTTPI::Adapter::Rack.new(request) }
58
+
59
+ describe "proxy" do
60
+ before do
61
+ request.proxy = "http://proxy-host.com:443"
62
+ request.proxy.user = "username"
63
+ request.proxy.password = "password"
64
+ end
65
+
66
+ it "is not supported" do
67
+ expect { client.request(:get) }.
68
+ to raise_error(HTTPI::NotSupportedError, "Rack adapter does not support proxying")
69
+ end
70
+ end
71
+
72
+ describe "on_body" do
73
+ before do
74
+ request.on_body do
75
+ # ola-la!
76
+ end
77
+ end
78
+
79
+ it "is not supported" do
80
+ expect { client.request(:get) }.
81
+ to raise_error(HTTPI::NotSupportedError, "Rack adapter does not support response streaming")
82
+ end
83
+ end
84
+
85
+ describe "set_auth" do
86
+ before do
87
+ request.auth.basic "username", "password"
88
+ end
89
+
90
+ it "is not supported" do
91
+ expect { client.request(:get) }.
92
+ to raise_error(HTTPI::NotSupportedError, "Rack adapter does not support HTTP auth")
93
+ end
94
+ end
95
+
96
+ context "(for SSL client auth)" do
97
+ before do
98
+ request.auth.ssl.cert_key_file = "spec/fixtures/client_key.pem"
99
+ request.auth.ssl.cert_file = "spec/fixtures/client_cert.pem"
100
+ end
101
+
102
+ it "is not supported" do
103
+ expect { client.request(:get) }.
104
+ to raise_error(HTTPI::NotSupportedError, "Rack adapter does not support SSL client auth")
105
+ end
106
+ end
107
+ end
108
+
109
+ end
110
+
111
+ end