Skip to content
Browse files

Add support for passing basic auth credentials as part of the URL for…

… the CLI

This is just a porting of the work done by @raggi in pull request #34
  • Loading branch information...
1 parent 2c55d05 commit 5ddfe401fdbd02be8c4d6bf5ce523ad5b33d5aeb @rtyler rtyler committed Jul 29, 2012
Showing with 16 additions and 2 deletions.
  1. +5 −1 ruby-tools/cli/lib/jenkins/api.rb
  2. +11 −1 ruby-tools/cli/spec/api_spec.rb
View
6 ruby-tools/cli/lib/jenkins/api.rb
@@ -26,6 +26,8 @@ def self.setup_base_url(options = {})
# Thor's HashWithIndifferentAccess is based on string keys which URI::HTTP.build ignores
options = options.inject({}) { |mem, (key, val)| mem[key.to_sym] = val; mem }
+ options = setup_authentication(options)
+
# Handle URL style hosts by parsing the URL
if options.keys.length == 1 && options.key?(:host)
parsed_uri = URI::parse(options[:host])
@@ -35,9 +37,11 @@ def self.setup_base_url(options = {})
:path => parsed_uri.path,
:ssl => parsed_uri.scheme == 'https'
}
+ if parsed_uri.user && parsed_uri.password
+ basic_auth parsed_uri.user, parsed_uri.password
+ end
end
- options = setup_authentication(options)
options[:host] ||= ENV['JENKINS_HOST']
options[:port] ||= ENV['JENKINS_PORT']
options[:port] &&= options[:port].to_i
View
12 ruby-tools/cli/spec/api_spec.rb
@@ -24,6 +24,16 @@
uri.path.should == ''
end
+ it "should accept basic auth parameters in the :host argument" do
+ uri = Jenkins::Api.setup_base_url :host => 'http://foo:bar@string.example.com:2'
+ uri.host.should == 'string.example.com'
+ uri.port.should == 2
+ uri.path.should == ''
+ auth = Jenkins::Api.default_options[:basic_auth]
+ auth[:username].should == 'foo'
+ auth[:password].should == 'bar'
+ end
+
context "with environment variables" do
after :each do
ENV.delete 'JENKINS_HOST'
@@ -55,4 +65,4 @@
end
end
end
-end
+end

0 comments on commit 5ddfe40

Please sign in to comment.
Something went wrong with that request. Please try again.