mirror of
https://github.com/rest-client/rest-client.git
synced 2022-11-09 13:49:40 -05:00
Add support for proxies.
This commit is contained in:
parent
4371c0ee49
commit
a959ae2b67
2 changed files with 63 additions and 11 deletions
|
@ -9,32 +9,40 @@ module RestClient
|
|||
def self.get(url, headers={})
|
||||
Request.execute(:method => :get,
|
||||
:url => url,
|
||||
:headers => headers)
|
||||
:headers => headers,
|
||||
:proxy => proxy)
|
||||
end
|
||||
|
||||
def self.post(url, payload, headers={})
|
||||
Request.execute(:method => :post,
|
||||
:url => url,
|
||||
:payload => payload,
|
||||
:headers => headers)
|
||||
:headers => headers,
|
||||
:proxy => proxy)
|
||||
end
|
||||
|
||||
def self.put(url, payload, headers={})
|
||||
Request.execute(:method => :put,
|
||||
:url => url,
|
||||
:payload => payload,
|
||||
:headers => headers)
|
||||
:headers => headers,
|
||||
:proxy => proxy)
|
||||
end
|
||||
|
||||
def self.delete(url, headers={})
|
||||
Request.execute(:method => :delete,
|
||||
:url => url,
|
||||
:headers => headers)
|
||||
:headers => headers,
|
||||
:proxy => proxy)
|
||||
end
|
||||
|
||||
|
||||
class <<self
|
||||
attr_accessor :proxy
|
||||
end
|
||||
|
||||
# Internal class used to build and execute the request.
|
||||
class Request
|
||||
attr_reader :method, :url, :payload, :headers, :user, :password
|
||||
attr_reader :method, :url, :payload, :headers, :user, :password, :proxy
|
||||
|
||||
def self.execute(args)
|
||||
new(args).execute
|
||||
|
@ -47,6 +55,7 @@ module RestClient
|
|||
@payload = process_payload(args[:payload])
|
||||
@user = args[:user]
|
||||
@password = args[:password]
|
||||
@proxy = args[:proxy]
|
||||
end
|
||||
|
||||
def execute
|
||||
|
@ -100,8 +109,14 @@ module RestClient
|
|||
|
||||
def transmit(uri, req, payload)
|
||||
setup_credentials(req)
|
||||
if proxy
|
||||
proxy_uri = URI.parse(proxy)
|
||||
http_klass = Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
|
||||
else
|
||||
http_klass = Net::HTTP
|
||||
end
|
||||
|
||||
net = Net::HTTP.new(uri.host, uri.port)
|
||||
net = http_klass.new(uri.host, uri.port)
|
||||
net.use_ssl = uri.is_a?(URI::HTTPS)
|
||||
|
||||
net.start do |http|
|
||||
|
|
|
@ -1,24 +1,29 @@
|
|||
require File.dirname(__FILE__) + '/base'
|
||||
|
||||
describe RestClient do
|
||||
before do
|
||||
# reset the proxy for other tests
|
||||
RestClient.proxy = nil
|
||||
end
|
||||
|
||||
context "public API" do
|
||||
it "GET" do
|
||||
RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => {})
|
||||
RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => {}, :proxy => nil)
|
||||
RestClient.get('http://some/resource')
|
||||
end
|
||||
|
||||
it "POST" do
|
||||
RestClient::Request.should_receive(:execute).with(:method => :post, :url => 'http://some/resource', :payload => 'payload', :headers => {})
|
||||
RestClient::Request.should_receive(:execute).with(:method => :post, :url => 'http://some/resource', :payload => 'payload', :headers => {}, :proxy => nil)
|
||||
RestClient.post('http://some/resource', 'payload')
|
||||
end
|
||||
|
||||
it "PUT" do
|
||||
RestClient::Request.should_receive(:execute).with(:method => :put, :url => 'http://some/resource', :payload => 'payload', :headers => {})
|
||||
RestClient::Request.should_receive(:execute).with(:method => :put, :url => 'http://some/resource', :payload => 'payload', :headers => {}, :proxy => nil)
|
||||
RestClient.put('http://some/resource', 'payload')
|
||||
end
|
||||
|
||||
it "DELETE" do
|
||||
RestClient::Request.should_receive(:execute).with(:method => :delete, :url => 'http://some/resource', :headers => {})
|
||||
RestClient::Request.should_receive(:execute).with(:method => :delete, :url => 'http://some/resource', :headers => {}, :proxy => nil)
|
||||
RestClient.delete('http://some/resource')
|
||||
end
|
||||
end
|
||||
|
@ -209,5 +214,37 @@ describe RestClient do
|
|||
res = mock('response', :code => '500')
|
||||
lambda { @request.process_result(res) }.should raise_error(RestClient::RequestFailed)
|
||||
end
|
||||
|
||||
it "sets proxy based on initializer argument" do
|
||||
RestClient::Request.new(
|
||||
:method => :get,
|
||||
:proxy => "http://test.com/",
|
||||
:url => "http://google.com/"
|
||||
).proxy.should == "http://test.com/"
|
||||
end
|
||||
|
||||
it "uses an http proxy if a proxy url is given" do
|
||||
RestClient.proxy = 'http://test.com/'
|
||||
RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => {}, :proxy => 'http://test.com/')
|
||||
RestClient.get('http://some/resource')
|
||||
end
|
||||
|
||||
it "creates a proxy class if a proxy url is given" do
|
||||
@http.should_receive(:request).with(@net, '')
|
||||
Net::HTTP::Get.stub!(:new).and_return(@net)
|
||||
|
||||
proxy = stub(:proxy_class)
|
||||
proxy.should_receive(:new).with('google.com', 80).and_return(@net)
|
||||
|
||||
Net::HTTP.should_receive(:Proxy).with('test.com', 80, nil, nil).and_return(proxy)
|
||||
request = RestClient::Request.new(
|
||||
:method => :get,
|
||||
:proxy => "http://test.com/",
|
||||
:url => "http://google.com/"
|
||||
)
|
||||
|
||||
request.stub!(:process_result)
|
||||
request.execute
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue