From e4040942ab55689f88625c8a048d83d84627d6e0 Mon Sep 17 00:00:00 2001 From: Steve Kemp Date: Tue, 28 Mar 2017 13:14:10 +0300 Subject: Support HTTP BASIC-AUthentication. Supply this like so: http://example.com/ must run http with auth 'username:passw0rd' with status 200 otherwise 'failure' --- lib/custodian/protocoltest/http.rb | 44 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/lib/custodian/protocoltest/http.rb b/lib/custodian/protocoltest/http.rb index 89d8f18..34e4b6e 100644 --- a/lib/custodian/protocoltest/http.rb +++ b/lib/custodian/protocoltest/http.rb @@ -63,6 +63,11 @@ module Custodian # @redirect = true + # + # No basic-authentication by default + # + @username = nil + @password = nil # # Ensure we've got a HTTP/HTTPS url. @@ -98,6 +103,18 @@ module Custodian end + # + # Look for username & password + # + if line =~ /with auth '([^']+)'/ + data = $1.dup + @username, @password = data.split( ":" ) + end + if line =~ /with auth "([^"]+)"/ + data = $1.dup + @username, @password = data.split( ":" ) + end + # # Expected status # @@ -180,6 +197,24 @@ module Custodian @cache_busting end + # + # Do we have basic auth? + # + def basic_auth? + ( @username.nil? == false ) && ( @password.nil? == false ) + end + + # + # Get the username/password for basic-auth + # + def basic_auth_username + @username + end + + def basic_auth_password + @password + end + # # Allow this test to be serialized. # @@ -239,6 +274,15 @@ module Custodian c.resolve_mode = resolve_mode + # + # Should we use HTTP basic-auth? + # + if basic_auth? + c.http_auth_types = :basic + c.username = basic_auth_username + c.password = basic_auth_password + end + # # Should we follow redirections? # -- cgit v1.2.1 From 334c67fa0e39f657dfe59b4a6c6445de20d5706b Mon Sep 17 00:00:00 2001 From: Steve Kemp Date: Tue, 28 Mar 2017 13:14:54 +0300 Subject: Added testcases for HTTP basic-auth. --- t/test-custodian-parser.rb | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/t/test-custodian-parser.rb b/t/test-custodian-parser.rb index 1bcf5bc..843dc80 100755 --- a/t/test-custodian-parser.rb +++ b/t/test-custodian-parser.rb @@ -429,6 +429,46 @@ EOF end end + # + # Test that the parser works for basic-auth. + # + def test_http_user_password + + # + # test data + # + data = { + 'http://example must run http with auth "bob:steve".' => + { username: 'bob', password: 'steve'}, + 'http://example must run http with auth "stee\':steve".' => + { username: 'stee\'', password: 'steve'}, + 'http://example must run http with auth \'e"e:pa$$w0rd\'.' => + { username: 'e"e', password: 'pa$$w0rd'}, + } + + data.each do |str, hash | + assert_nothing_raised do + + # + # Create the new parser + # + obj = Custodian::TestFactory.create(str) + + assert(!obj.nil?) + assert(obj.kind_of?(Array)) + assert(obj.size == 1) + assert_equal(obj[0].to_s, str) + + # There should be auth-enabled + assert(obj[0].basic_auth?) + assert(obj[0].basic_auth_username == hash[:username] ) + assert(obj[0].basic_auth_password == hash[:password] ) + + end + end + end + + # # HTTP/HTTPS tests might specify custom expiry # -- cgit v1.2.1 From 69ac6cfbdaf9cbb25a0f73561d92a3d69accb434 Mon Sep 17 00:00:00 2001 From: Steve Kemp Date: Mon, 10 Apr 2017 10:41:50 +0300 Subject: Use standard URL username/password holders. Rather than: with auth 'username:password' We use: http://user:pass@example.com/ --- lib/custodian/protocoltest/http.rb | 14 ++++---------- t/test-custodian-parser.rb | 8 ++++---- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/lib/custodian/protocoltest/http.rb b/lib/custodian/protocoltest/http.rb index 34e4b6e..a4c4adb 100644 --- a/lib/custodian/protocoltest/http.rb +++ b/lib/custodian/protocoltest/http.rb @@ -102,18 +102,12 @@ module Custodian raise ArgumentError, "The test case has a different protocol in the URI than that which we're testing: #{@line} - \"#{test_type} != #{u.scheme}\"" end - # - # Look for username & password + # Save username/password if they were specified # - if line =~ /with auth '([^']+)'/ - data = $1.dup - @username, @password = data.split( ":" ) - end - if line =~ /with auth "([^"]+)"/ - data = $1.dup - @username, @password = data.split( ":" ) - end + @username = u.user if ( u.user ) + @password = u.password if ( u.password ) + # # Expected status diff --git a/t/test-custodian-parser.rb b/t/test-custodian-parser.rb index 843dc80..2239503 100755 --- a/t/test-custodian-parser.rb +++ b/t/test-custodian-parser.rb @@ -438,12 +438,12 @@ EOF # test data # data = { - 'http://example must run http with auth "bob:steve".' => + 'http://bob:steve@example must run http.' => { username: 'bob', password: 'steve'}, - 'http://example must run http with auth "stee\':steve".' => + 'http://stee\':steve@example must run http.' => { username: 'stee\'', password: 'steve'}, - 'http://example must run http with auth \'e"e:pa$$w0rd\'.' => - { username: 'e"e', password: 'pa$$w0rd'}, + 'http://e\'e:pa$$w0rd@example must run http.' => + { username: 'e\'e', password: 'pa$$w0rd'}, } data.each do |str, hash | -- cgit v1.2.1