Skip to content

Commit 5f7d795

Browse files
committed
Merge pull request #264 from ryanshow/master
Normalize the encryption parameter passed to the LDAP constructor
2 parents 771e11c + 8aaa96b commit 5f7d795

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

lib/net/ldap.rb

+15-8
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ def initialize(args = {})
539539
@auth = args[:auth] || DefaultAuth
540540
@base = args[:base] || DefaultTreebase
541541
@force_no_page = args[:force_no_page] || DefaultForceNoPage
542-
@encryption = args[:encryption] # may be nil
542+
@encryption = normalize_encryption(args[:encryption]) # may be nil
543543
@connect_timeout = args[:connect_timeout]
544544

545545
if pr = @auth[:password] and pr.respond_to?(:call)
@@ -609,13 +609,7 @@ def authenticate(username, password)
609609
def encryption(args)
610610
warn "Deprecation warning: please give :encryption option as a Hash to Net::LDAP.new"
611611
return if args.nil?
612-
return @encryption = args if args.is_a? Hash
613-
614-
case method = args.to_sym
615-
when :simple_tls, :start_tls
616-
args = { :method => method, :tls_options => {} }
617-
end
618-
@encryption = args
612+
@encryption = normalize_encryption(args)
619613
end
620614

621615
# #open takes the same parameters as #new. #open makes a network
@@ -1323,4 +1317,17 @@ def new_connection
13231317
}
13241318
raise e
13251319
end
1320+
1321+
# Normalize encryption parameter the constructor accepts, expands a few
1322+
# convenience symbols into recognizable hashes
1323+
def normalize_encryption(args)
1324+
return if args.nil?
1325+
return args if args.is_a? Hash
1326+
1327+
case method = args.to_sym
1328+
when :simple_tls, :start_tls
1329+
{ :method => method, :tls_options => {} }
1330+
end
1331+
end
1332+
13261333
end # class LDAP

test/test_ldap.rb

+20
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,24 @@ def test_encryption
9191

9292
assert_equal enc[:method], :start_tls
9393
end
94+
95+
def test_normalize_encryption_symbol
96+
enc = @subject.send(:normalize_encryption, :start_tls)
97+
assert_equal enc, {:method => :start_tls, :tls_options => {}}
98+
end
99+
100+
def test_normalize_encryption_nil
101+
enc = @subject.send(:normalize_encryption, nil)
102+
assert_equal enc, nil
103+
end
104+
105+
def test_normalize_encryption_string
106+
enc = @subject.send(:normalize_encryption, 'start_tls')
107+
assert_equal enc, {:method => :start_tls, :tls_options => {}}
108+
end
109+
110+
def test_normalize_encryption_hash
111+
enc = @subject.send(:normalize_encryption, {:method => :start_tls, :tls_options => {:foo => :bar}})
112+
assert_equal enc, {:method => :start_tls, :tls_options => {:foo => :bar}}
113+
end
94114
end

0 commit comments

Comments
 (0)