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

support authentication in the URI, i.e. http://user:pass@example.com/resource

This commit is contained in:
Adam Wiggins 2008-06-20 19:44:36 -07:00
parent 7511d49770
commit 7c7e62515c
2 changed files with 24 additions and 2 deletions

View file

@ -57,7 +57,7 @@ module RestClient
end
def execute_inner
uri = parse_url(url)
uri = parse_url_with_auth(url)
transmit uri, net_http_class(method).new(uri.request_uri, make_headers(headers)), payload
end
@ -79,6 +79,13 @@ module RestClient
URI.parse(url)
end
def parse_url_with_auth(url)
uri = parse_url(url)
@user = uri.user if uri.user
@password = uri.password if uri.password
uri
end
def process_payload(p=nil)
unless p.is_a?(Hash)
p

View file

@ -60,6 +60,21 @@ describe RestClient do
@request.parse_url('example.com/resource')
end
it "extracts the username and password when parsing http://user:password@example.com/" do
URI.stub!(:parse).and_return(mock('uri', :user => 'joe', :password => 'pass1'))
@request.parse_url_with_auth('http://joe:pass1@example.com/resource')
@request.user.should == 'joe'
@request.password.should == 'pass1'
end
it "doesn't overwrite user and password (which may have already been set by the Resource constructor) if there is no user/password in the url" do
URI.stub!(:parse).and_return(mock('uri', :user => nil, :password => nil))
@request = RestClient::Request.new(:method => 'get', :url => 'example.com', :user => 'beth', :password => 'pass2')
@request.parse_url_with_auth('http://example.com/resource')
@request.user.should == 'beth'
@request.password.should == 'pass2'
end
it "determines the Net::HTTP class to instantiate by the method name" do
@request.net_http_class(:put).should == Net::HTTP::Put
end
@ -80,7 +95,7 @@ describe RestClient do
end
it "executes by constructing the Net::HTTP object, headers, and payload and calling transmit" do
@request.should_receive(:parse_url).with('http://some/resource').and_return(@uri)
@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)
klass.should_receive(:new).and_return('result')