1
0
Fork 0
mirror of https://github.com/rest-client/rest-client.git synced 2022-11-09 13:49:40 -05:00

Simplify the proxy stuff to just use the global proxy setting.

This commit is contained in:
Brian Donovan 2008-08-19 13:25:48 -07:00
parent 94a50a66ef
commit 3e0b928254
3 changed files with 35 additions and 69 deletions

View file

@ -31,8 +31,7 @@ module RestClient
:url => url,
:user => user,
:password => password,
:headers => headers,
:proxy => RestClient.proxy)
:headers => headers)
end
def post(payload, headers={})
@ -41,8 +40,7 @@ module RestClient
:payload => payload,
:user => user,
:password => password,
:headers => headers,
:proxy => RestClient.proxy)
:headers => headers)
end
def put(payload, headers={})
@ -51,8 +49,7 @@ module RestClient
:payload => payload,
:user => user,
:password => password,
:headers => headers,
:proxy => RestClient.proxy)
:headers => headers)
end
def delete(headers={})
@ -60,8 +57,7 @@ module RestClient
:url => url,
:user => user,
:password => password,
:headers => headers,
:proxy => RestClient.proxy)
:headers => headers)
end
# Construct a subresource, preserving authentication.

View file

@ -38,33 +38,29 @@ module RestClient
def self.get(url, headers={})
Request.execute(:method => :get,
:url => url,
:headers => headers,
:proxy => proxy)
:headers => headers)
end
def self.post(url, payload, headers={})
Request.execute(:method => :post,
:url => url,
:payload => payload,
:headers => headers,
:proxy => proxy)
:headers => headers)
end
def self.put(url, payload, headers={})
Request.execute(:method => :put,
:url => url,
:payload => payload,
:headers => headers,
:proxy => proxy)
:headers => headers)
end
def self.delete(url, headers={})
Request.execute(:method => :delete,
:url => url,
:headers => headers,
:proxy => proxy)
:headers => headers)
end
class <<self
attr_accessor :proxy
end
@ -83,7 +79,7 @@ module RestClient
# Internal class used to build and execute the request.
class Request
attr_reader :method, :url, :payload, :headers, :user, :password, :proxy
attr_reader :method, :url, :payload, :headers, :user, :password
def self.execute(args)
new(args).execute
@ -96,7 +92,6 @@ module RestClient
@payload = process_payload(args[:payload])
@user = args[:user]
@password = args[:password]
@proxy = args[:proxy]
end
def execute
@ -108,7 +103,7 @@ module RestClient
def execute_inner
uri = parse_url_with_auth(url)
transmit uri, net_http_class(method).new(uri.request_uri, make_headers(headers)), payload
transmit uri, net_http_request_class(method).new(uri.request_uri, make_headers(headers)), payload
end
def make_headers(user_headers)
@ -120,7 +115,16 @@ module RestClient
final
end
def net_http_class(method)
def net_http_class
if RestClient.proxy
proxy_uri = URI.parse(RestClient.proxy)
Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
else
Net::HTTP
end
end
def net_http_request_class(method)
Net::HTTP.const_get(method.to_s.capitalize)
end
@ -155,14 +159,8 @@ 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 = http_klass.new(uri.host, uri.port)
net = net_http_class.new(uri.host, uri.port)
net.use_ssl = uri.is_a?(URI::HTTPS)
display_log request_log

View file

@ -1,29 +1,24 @@
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 => {}, :proxy => nil)
RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => {})
RestClient.get('http://some/resource')
end
it "POST" do
RestClient::Request.should_receive(:execute).with(:method => :post, :url => 'http://some/resource', :payload => 'payload', :headers => {}, :proxy => nil)
RestClient::Request.should_receive(:execute).with(:method => :post, :url => 'http://some/resource', :payload => 'payload', :headers => {})
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 => {}, :proxy => nil)
RestClient::Request.should_receive(:execute).with(:method => :put, :url => 'http://some/resource', :payload => 'payload', :headers => {})
RestClient.put('http://some/resource', 'payload')
end
it "DELETE" do
RestClient::Request.should_receive(:execute).with(:method => :delete, :url => 'http://some/resource', :headers => {}, :proxy => nil)
RestClient::Request.should_receive(:execute).with(:method => :delete, :url => 'http://some/resource', :headers => {})
RestClient.delete('http://some/resource')
end
end
@ -117,7 +112,7 @@ describe RestClient do
end
it "determines the Net::HTTP class to instantiate by the method name" do
@request.net_http_class(:put).should == Net::HTTP::Put
@request.net_http_request_class(:put).should == Net::HTTP::Put
end
it "merges user headers with the default headers" do
@ -138,7 +133,7 @@ describe RestClient do
it "executes by constructing the Net::HTTP object, headers, and payload and calling transmit" do
@request.should_receive(:parse_url_with_auth).with('http://some/resource').and_return(@uri)
klass = mock("net:http class")
@request.should_receive(:net_http_class).with(:put).and_return(klass)
@request.should_receive(:net_http_request_class).with(:put).and_return(klass)
klass.should_receive(:new).and_return('result')
@request.should_receive(:transmit).with(@uri, 'result', 'payload')
@request.execute_inner
@ -261,37 +256,14 @@ 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)
RestClient.stub!(:proxy).and_return("http://example.com/")
@request.net_http_class.should include(Net::HTTP::ProxyDelta)
end
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
it "creates a non-proxy class if a proxy url is not given" do
@request.net_http_class.should_not include(Net::HTTP::ProxyDelta)
end
it "logs a get request" do
@ -323,7 +295,7 @@ describe RestClient do
it "logs a response with a nil Content-type" do
res = mock('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
res.stub!(:[]).with('Content-type').and_return(nil)
@request.response_log(res).should == "# => 200 OK | 4 bytes"
@request.response_log(res).should == "# => 200 OK | 4 bytes"
end
it "strips the charset from the response content type" do