Permalink
Browse files

Adds template http adapter

  • Loading branch information...
1 parent f9c7dc5 commit 21de4d6e6ba37d1b2da3d7ac9a98d6b9327c9348 @reggieb reggieb committed Mar 19, 2014
View
@@ -61,7 +61,12 @@ and specify it in config.ru:
It is recommend (but not essential) that your adapter inherits from HttpAdapter.
The adapter will need to replace HttpAdapter's methods with those specific to
the alternative HTTP gem. It should also be able to handle HTTP proxy
-settings.
+settings.
+
+Defining your own adapter also allows you to configure Geminabox to use the
+local systems SSL certificates.
+
+TemplateFaradayAdapter is provided as an example of an alternative HTTPAdapter.
## Client Usage
View
@@ -21,6 +21,7 @@ Gem::Specification.new do |s|
s.add_dependency('builder')
s.add_dependency('httpclient', [">= 2.2.7"])
s.add_dependency('nesty')
+ s.add_dependency('faraday')
s.add_development_dependency('rake')
s.add_development_dependency('pry')
end
@@ -0,0 +1,51 @@
+require 'faraday'
+
+module Geminabox
+
+ class TemplateFaradayAdapter < HttpAdapter
+
+ def get(*args)
+ adapter.get(*args)
+ end
+
+ def get_content(*args)
+ response = adapter.get(*args)
+ response.body
+ end
+
+ def post(*args)
+ adapter.post(*args)
+ end
+
+ # Note that this configuration turns SSL certificate verification off.
+ # To set up the adapter for your environment see:
+ # https://github.com/lostisland/faraday/wiki/Setting-up-SSL-certificates
+ def set_auth(uri, username = nil, password = nil)
+ connection = Faraday.new url: uri, ssl: {verify: false} do |faraday|
+ faraday.adapter http_engine
+ faraday.proxy(ENV['http_proxy']) if ENV['http_proxy']
+ end
+ connection.basic_auth username, password if username
+ connection
+ end
+
+ def adapter
+ @adapter ||= Faraday.new do |faraday|
+ faraday.adapter http_engine
+ faraday.proxy(ENV['http_proxy']) if ENV['http_proxy']
+ end
+ end
+
+ def http_engine
+ :net_http # make requests with Net::HTTP
+ end
+
+ def options
+ lambda {|faraday|
+ faraday.adapter http_engine
+ faraday.proxy(ENV['http_proxy']) if ENV['http_proxy']
+ }
+ end
+
+ end
+end
@@ -28,7 +28,7 @@ def push(gemfile, options = {})
if response.status < 300
response.body
else
- raise GeminaboxClient::Error, "Error (#{response.code} received)\n\n#{response.body}"
+ raise GeminaboxClient::Error, "Error (#{response.status} received)\n\n#{response.body}"
end
end
@@ -5,21 +5,21 @@ class HttpClientAdapterTest < Minitest::Test
def setup
@default = 'foo bar'
- @http_dummy = HttpClientAdapter.new
+ @http_adapter = HttpClientAdapter.new
end
def test_get_content
stub_request(:get, "http://example.com/").
to_return(:status => 200, :body => @default)
- assert_equal @default, @http_dummy.get_content('http://example.com')
+ assert_equal @default, @http_adapter.get_content('http://example.com')
end
def test_get
stub_request(:get, "http://example.com/").
to_return(:status => 200, :body => @default)
- response = @http_dummy.get('http://example.com')
+ response = @http_adapter.get('http://example.com')
assert_equal @default, response.body
assert_equal 200, response.status
assert_equal 200, response.code
@@ -29,7 +29,7 @@ def test_post
stub_request(:post, "http://example.com/").
to_return(:status => 200, :body => @default)
- response = @http_dummy.post('http://example.com')
+ response = @http_adapter.post('http://example.com')
assert_equal @default, response.body
assert_equal 200, response.status
assert_equal 200, response.code
@@ -40,8 +40,8 @@ def test_set_auth
with(:headers => {'Authorization'=>'Basic Zm9vOmJhcg=='}).
to_return(:status => 200, :body => @default, :headers => {})
- @http_dummy.set_auth('http://example.com', 'foo', 'bar')
- assert_equal @default, @http_dummy.get_content('http://example.com')
+ @http_adapter.set_auth('http://example.com', 'foo', 'bar')
+ assert_equal @default, @http_adapter.get_content('http://example.com')
end
end
@@ -0,0 +1,46 @@
+require 'test_helper'
+
+module Geminabox
+ class HttpClientAdapterTest < Minitest::Test
+
+ def setup
+ @default = 'foo bar'
+ @http_adapter = TemplateFaradayAdapter.new
+ end
+
+ def test_get_content
+ stub_request(:get, "http://example.com/").
+ to_return(:status => 200, :body => @default)
+
+ assert_equal @default, @http_adapter.get_content('http://example.com')
+ end
+
+ def test_get
+ stub_request(:get, "http://example.com/").
+ to_return(:status => 200, :body => @default)
+
+ response = @http_adapter.get('http://example.com')
+ assert_equal @default, response.body
+ assert_equal 200, response.status
+ end
+
+ def test_post
+ stub_request(:post, "http://example.com/").
+ to_return(:status => 200, :body => @default)
+
+ response = @http_adapter.post('http://example.com')
+ assert_equal @default, response.body
+ assert_equal 200, response.status
+ end
+
+ def test_set_auth
+ stub_request(:get, "http://foo:bar@example.com/").
+ to_return(:status => 200, :body => @default, :headers => {})
+
+ connection = @http_adapter.set_auth('http://example.com', 'foo', 'bar')
+ response = connection.get('http://example.com')
+ assert_equal @default, response.body
+ end
+
+ end
+end

0 comments on commit 21de4d6

Please sign in to comment.