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

credentials sent via http basic auth

This commit is contained in:
Adam Wiggins 2008-03-10 16:52:59 -07:00
parent a9f62bf573
commit 15c4bab527
3 changed files with 38 additions and 1 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
rdoc
pkg

View file

@ -31,7 +31,7 @@ module RestClient
# Internal class used to build and execute the request.
class Request
attr_reader :method, :url, :payload, :headers
attr_reader :method, :url, :payload, :headers, :user, :password
def self.execute(args)
new(args).execute
@ -42,6 +42,8 @@ module RestClient
@url = args[:url] or raise ArgumentError, "must pass :url"
@payload = args[:payload]
@headers = args[:headers] || {}
@user = args[:user]
@password = args[:password]
end
def execute
@ -84,11 +86,17 @@ module RestClient
class Unauthorized < Exception; end
def transmit(uri, req, payload)
setup_credentials(req)
Net::HTTP.start(uri.host, uri.port) do |http|
process_result http.request(req, payload || "")
end
end
def setup_credentials(req)
req.basic_auth(user, password) if user
end
def process_result(res)
if %w(200 201 202).include? res.code
res.body

View file

@ -98,6 +98,34 @@ describe RestClient do
@request.transmit(@uri, 'req', nil)
end
it "sets up the credentials prior to the request" do
http = mock("net::http connection")
Net::HTTP.should_receive(:start).and_yield(http)
http.stub!(:request)
@request.stub!(:process_result)
@request.stub!(:user).and_return('joe')
@request.stub!(:password).and_return('mypass')
@request.should_receive(:setup_credentials).with('req')
@request.transmit(@uri, 'req', nil)
end
it "does not attempt to send any credentials if user is nil" do
@request.stub!(:user).and_return(nil)
req = mock("request")
req.should_not_receive(:basic_auth)
@request.setup_credentials(req)
end
it "does not attempt to send any credentials if user is nil" do
@request.stub!(:user).and_return('joe')
@request.stub!(:password).and_return('mypass')
req = mock("request")
req.should_receive(:basic_auth).with('joe', 'mypass')
@request.setup_credentials(req)
end
it "execute calls execute_inner" do
@request.should_receive(:execute_inner)
@request.execute