Improved auth tests.

This commit is contained in:
Jakub Roztocil 2014-04-25 13:10:01 +02:00
parent b10d973019
commit 3c2de34285
2 changed files with 22 additions and 35 deletions

View File

@ -15,8 +15,6 @@ from httpie.compat import bytes, str
TESTS_ROOT = os.path.abspath(os.path.dirname(__file__)) TESTS_ROOT = os.path.abspath(os.path.dirname(__file__))
HTTPBIN_URL = os.environ.get('HTTPBIN_URL',
'http://httpbin.org').rstrip('/')
CRLF = '\r\n' CRLF = '\r\n'
@ -29,16 +27,22 @@ HTTP_OK_COLOR = (
) )
def httpbin(path): def httpbin(path, auth=None,
base=os.environ.get('HTTPBIN_URL', 'http://httpbin.org')):
""" """
Return a fully-qualified httpbin URL for `path`. Return a fully-qualified httpbin URL for `path`.
>>> httpbin('/get') >>> httpbin('/get')
'http://httpbin.org/get' 'http://httpbin.org/get'
>>> httpbin('/get', auth='user:password')
'http://user:password@httpbin.org/get'
""" """
url = HTTPBIN_URL + path if auth:
return url proto, rest = base.split('://', 1)
base = proto + '://' + auth + '@' + rest
return base.rstrip('/') + path
class TestEnvironment(Environment): class TestEnvironment(Environment):

View File

@ -8,53 +8,36 @@ import httpie.input
class TestAuth: class TestAuth:
def test_basic_auth(self): def test_basic_auth(self):
r = http('--auth=user:password', 'GET', r = http('--auth=user:password',
httpbin('/basic-auth/user/password')) 'GET', httpbin('/basic-auth/user/password'))
assert HTTP_OK in r assert HTTP_OK in r
assert r.json == { assert r.json == {'authenticated': True, 'user': 'user'}
'authenticated': True,
'user': 'user'
}
@pytest.mark.skipif( @pytest.mark.skipif(
requests.__version__ == '0.13.6', requests.__version__ == '0.13.6',
reason='Redirects with prefetch=False are broken in Requests 0.13.6') reason='Redirects with prefetch=False are broken in Requests 0.13.6')
def test_digest_auth(self): def test_digest_auth(self):
r = http('--auth-type=digest', '--auth=user:password', 'GET', r = http('--auth-type=digest', '--auth=user:password',
httpbin('/digest-auth/auth/user/password')) 'GET', httpbin('/digest-auth/auth/user/password'))
assert HTTP_OK in r assert HTTP_OK in r
assert r.json == { assert r.json == {'authenticated': True, 'user': 'user'}
'authenticated': True,
'user': 'user'
}
def test_password_prompt(self): def test_password_prompt(self):
httpie.input.AuthCredentials._getpass = lambda self, prompt: 'password' httpie.input.AuthCredentials._getpass = lambda self, prompt: 'password'
r = http('--auth', 'user', 'GET', httpbin('/basic-auth/user/password')) r = http('--auth', 'user', 'GET', httpbin('/basic-auth/user/password'))
assert HTTP_OK in r assert HTTP_OK in r
assert r.json == { assert r.json == {'authenticated': True, 'user': 'user'}
'authenticated': True,
'user': 'user'
}
def test_credentials_in_url(self): def test_credentials_in_url(self):
url = httpbin('/basic-auth/user/password') r = http('GET', httpbin('/basic-auth/user/password',
url = 'http://user:password@' + url.split('http://', 1)[1] auth='user:password'))
r = http('GET', url)
assert HTTP_OK in r assert HTTP_OK in r
assert r.json == { assert r.json == {'authenticated': True, 'user': 'user'}
'authenticated': True,
'user': 'user'
}
def test_credentials_in_url_auth_flag_has_priority(self): def test_credentials_in_url_auth_flag_has_priority(self):
"""When credentials are passed in URL and via -a at the same time, """When credentials are passed in URL and via -a at the same time,
then the ones from -a are used.""" then the ones from -a are used."""
url = httpbin('/basic-auth/user/password') r = http('--auth=user:password', 'GET',
url = 'http://user:wrong_password@' + url.split('http://', 1)[1] httpbin('/basic-auth/user/password', auth='user:wrong'))
r = http('--auth=user:password', 'GET', url)
assert HTTP_OK in r assert HTTP_OK in r
assert r.json == { assert r.json == {'authenticated': True, 'user': 'user'}
'authenticated': True,
'user': 'user'
}