Added --auth-type and tests for basic/digest auth.

Closes #38.
This commit is contained in:
Jakub Roztocil 2012-04-11 13:47:47 +02:00
parent ee598d304d
commit 0c4c6c4753
3 changed files with 28 additions and 5 deletions

View File

@ -134,10 +134,11 @@ def main(args=None,
# Fire the request.
try:
credentials = None
if args.auth and args.digest:
credentials = requests.auth.HTTPDigestAuth(args.auth.key, args.auth.value)
elif args.auth:
credentials = requests.auth.HTTPBasicAuth(args.auth.key, args.auth.value)
if args.auth:
auth_type = (requests.auth.HTTPDigestAuth
if args.auth_type == 'digest'
else requests.auth.HTTPBasicAuth)
credentials = auth_type(args.auth.key, args.auth.value)
response = requests.request(
method=args.method.lower(),

View File

@ -100,6 +100,7 @@ class HTTPieArgumentParser(argparse.ArgumentParser):
def parse_args(self, args=None, namespace=None):
args = super(HTTPieArgumentParser, self).parse_args(args, namespace)
self._validate_output_options(args)
self._validate_auth_options(args)
return args
def _validate_output_options(self, args):
@ -107,6 +108,11 @@ class HTTPieArgumentParser(argparse.ArgumentParser):
if unknown_output_options:
self.error('Unknown output options: %s' % ','.join(unknown_output_options))
def _validate_auth_options(self, args):
if args.auth_type and not args.auth:
self.error('--auth-type can only be used with --auth')
parser = HTTPieArgumentParser(description=doc.strip(),)
parser.add_argument('--version', action='version', version=version)
@ -217,7 +223,8 @@ parser.add_argument(
)
parser.add_argument(
'--digest', '-d', action='store_true', help=_('Use Digest authentication')
'--auth-type', choices=['basic', 'digest'],
help=_('The type of authentication ("basic" or "digest"). Defaults to "basic".')
)
parser.add_argument(

View File

@ -139,5 +139,20 @@ class TestFileUpload(BaseTest):
self.assertIn('"test-file": "__test_file_content__', r)
class TestAuth(BaseTest):
def test_basic_auth(self):
r = http('--auth', 'user:password',
'GET', 'httpbin.org/basic-auth/user/password')
self.assertIn('"authenticated": true', r)
self.assertIn('"user": "user"', r)
def test_digest_auth(self):
r = http('--auth-type=digest', '--auth', 'user:password',
'GET', 'httpbin.org/digest-auth/auth/user/password')
self.assertIn('"authenticated": true', r)
self.assertIn('"user": "user"', r)
if __name__ == '__main__':
unittest.main()