From 4f9c39f2d1a4cf0d2d7fa59ebdd0ce3ba26d0e4f Mon Sep 17 00:00:00 2001 From: mdidato Date: Thu, 16 Jul 2015 12:40:59 +0100 Subject: [PATCH] net_http adapter instanciate a SocksClient is socks is specified --- httpi.gemspec | 2 ++ lib/httpi/adapter/net_http.rb | 8 +++++++- spec/httpi/adapter/net_http_spec.rb | 16 ++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/httpi.gemspec b/httpi.gemspec index a9ca20d..5e448a7 100644 --- a/httpi.gemspec +++ b/httpi.gemspec @@ -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' diff --git a/lib/httpi/adapter/net_http.rb b/lib/httpi/adapter/net_http.rb index 355001c..8dda0de 100644 --- a/lib/httpi/adapter/net_http.rb +++ b/lib/httpi/adapter/net_http.rb @@ -4,6 +4,8 @@ require "httpi/response" require 'kconv' require 'socket' +require "socksify" +require 'socksify/http' module HTTPI module Adapter @@ -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 diff --git a/spec/httpi/adapter/net_http_spec.rb b/spec/httpi/adapter/net_http_spec.rb index b40fa1b..95bd918 100644 --- a/spec/httpi/adapter/net_http_spec.rb +++ b/spec/httpi/adapter/net_http_spec.rb @@ -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"