Complexity of Browser#proxy_auth= reduced
This commit is contained in:
@@ -109,25 +109,18 @@ class Browser
|
|||||||
|
|
||||||
def proxy_auth=(auth)
|
def proxy_auth=(auth)
|
||||||
unless auth.nil?
|
unless auth.nil?
|
||||||
if auth.is_a?(Hash)
|
if auth.is_a?(Hash) && auth.include?(:proxy_username) && auth.include?(:proxy_password)
|
||||||
if !auth.include?(:proxy_username) or !auth.include?(:proxy_password)
|
|
||||||
raise_invalid_proxy_format()
|
|
||||||
end
|
|
||||||
@proxy_auth = auth[:proxy_username] + ':' + auth[:proxy_password]
|
@proxy_auth = auth[:proxy_username] + ':' + auth[:proxy_password]
|
||||||
elsif auth.is_a?(String)
|
elsif auth.is_a?(String) && auth.index(':') != nil
|
||||||
if auth.index(':') != nil
|
|
||||||
@proxy_auth = auth
|
@proxy_auth = auth
|
||||||
else
|
else
|
||||||
raise_invalid_proxy_auth_format()
|
raise invalid_proxy_auth_format
|
||||||
end
|
|
||||||
else
|
|
||||||
raise_invalid_proxy_auth_format()
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def raise_invalid_proxy_auth_format
|
def invalid_proxy_auth_format
|
||||||
raise 'Invalid proxy auth format, expected username:password or {proxy_username: username, proxy_password: password}'
|
'Invalid proxy auth format, expected username:password or {proxy_username: username, proxy_password: password}'
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO reload hydra (if the .load_config is called on a browser object,
|
# TODO reload hydra (if the .load_config is called on a browser object,
|
||||||
|
|||||||
@@ -84,29 +84,54 @@ describe Browser do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should raise an error if the format is not correct' do
|
context 'when the auth supplied is' do
|
||||||
@proxy_auth = 'invaludauthformat'
|
|
||||||
@raise_error = true
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'should raise an error if the hash does not contain :proxy_username and :proxy_password' do
|
context 'not a String or a Hash' do
|
||||||
@proxy_auth = { proxy_password: 'hello' }
|
it 'raises an error' do
|
||||||
@raise_error = true
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'should raise an error if the auth if not a string or a hash' do
|
|
||||||
@proxy_auth = 10
|
@proxy_auth = 10
|
||||||
@raise_error = true
|
@raise_error = true
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
it 'should set the correct credentials' do
|
context 'a String with' do
|
||||||
|
context 'invalid format' do
|
||||||
|
it 'raises an error' do
|
||||||
|
@proxy_auth = 'invaludauthformat'
|
||||||
|
@raise_error = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'valid format' do
|
||||||
|
it 'sets the auth' do
|
||||||
|
@proxy_auth = 'username:passwd'
|
||||||
|
@expected = @proxy_auth
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'a Hash with' do
|
||||||
|
context 'only :proxy_username' do
|
||||||
|
it 'raises an error' do
|
||||||
|
@proxy_auth = { proxy_username: 'username' }
|
||||||
|
@raise_error = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'only :proxy_password' do
|
||||||
|
it 'raises an error' do
|
||||||
|
@proxy_auth = { proxy_password: 'hello' }
|
||||||
|
@raise_error = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context ':proxy_username and :proxy_password' do
|
||||||
|
it 'sets the auth' do
|
||||||
@proxy_auth = { proxy_username: 'user', proxy_password: 'pass' }
|
@proxy_auth = { proxy_username: 'user', proxy_password: 'pass' }
|
||||||
@expected = 'user:pass'
|
@expected = 'user:pass'
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
it 'should set the correct credentials' do
|
|
||||||
@proxy_auth = 'username:passwd'
|
|
||||||
@expected = @proxy_auth
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user