Skip to content

Commit

Permalink
net_http adapter instanciate a SocksClient is socks is specified
Browse files Browse the repository at this point in the history
  • Loading branch information
mdidato committed Jul 16, 2015
1 parent f0cfd69 commit 4f9c39f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 2 additions & 0 deletions httpi.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ Gem::Specification.new do |s|
s.license = 'MIT'

s.add_dependency 'rack'
s.add_dependency 'socksify'

s.add_development_dependency 'rubyntlm', '~> 0.3.2'
s.add_development_dependency 'rake', '~> 10.0'
s.add_development_dependency 'rspec', '~> 2.14'
s.add_development_dependency 'mocha', '~> 0.13'
s.add_development_dependency 'puma', '~> 2.3.2'
s.add_development_dependency 'webmock'

s.files = `git ls-files`.split("\n")
s.require_path = 'lib'
Expand Down
8 changes: 7 additions & 1 deletion lib/httpi/adapter/net_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
require "httpi/response"
require 'kconv'
require 'socket'
require "socksify"
require 'socksify/http'

module HTTPI
module Adapter
Expand Down Expand Up @@ -67,7 +69,11 @@ def perform(http, http_request, &block)

def create_client
proxy_url = @request.proxy || URI("")
proxy = Net::HTTP::Proxy(proxy_url.host, proxy_url.port, proxy_url.user, proxy_url.password)
if URI(proxy_url).scheme == 'socks'
proxy =Net::HTTP.SOCKSProxy(proxy_url.host, proxy_url.port)
else
proxy = Net::HTTP::Proxy(proxy_url.host, proxy_url.port, proxy_url.user, proxy_url.password)
end
proxy.new(@request.url.host, @request.url.port)
end

Expand Down
16 changes: 16 additions & 0 deletions spec/httpi/adapter/net_http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@
@server.stop
end

context 'when socks is specified' do

let(:socks_client) { mock('socks_client') }
let(:request){HTTPI::Request.new(@server.url)}

it 'uses Net::HTTP.SOCKSProxy as client' do
socks_client.expects(:new).with(URI(@server.url).host, URI(@server.url).port).returns(:socks_client_instance)
Net::HTTP.expects(:SOCKSProxy).with('localhost', 8080).returns socks_client

request.proxy = 'socks://localhost:8080'
adapter = HTTPI::Adapter::NetHTTP.new(request)

expect(adapter.client).to eq(:socks_client_instance)
end
end

it "sends and receives HTTP headers" do
request = HTTPI::Request.new(@server.url + "x-header")
request.headers["X-Header"] = "HTTPI"
Expand Down

0 comments on commit 4f9c39f

Please sign in to comment.