1
0
mirror of https://github.com/httpie/cli.git synced 2024-11-24 08:22:22 +02:00
httpie-cli/tests/test_auth.py

53 lines
1.9 KiB
Python
Raw Normal View History

2014-04-24 15:07:31 +03:00
"""HTTP authentication-related tests."""
from unittest import TestCase
2014-04-24 15:07:31 +03:00
import requests
2014-04-24 16:17:23 +03:00
import pytest
2014-04-24 15:07:31 +03:00
2014-04-24 16:17:23 +03:00
from tests import http, httpbin, HTTP_OK
2014-04-24 15:07:31 +03:00
import httpie.input
class AuthTest(TestCase):
2014-04-24 15:07:31 +03:00
def test_basic_auth(self):
2014-04-24 16:48:01 +03:00
r = http('--auth=user:password', 'GET',
httpbin('/basic-auth/user/password'))
assert HTTP_OK in r
assert '"authenticated": true' in r
assert '"user": "user"' in r
2014-04-24 15:07:31 +03:00
2014-04-24 16:17:23 +03:00
@pytest.mark.skipif(
requests.__version__ == '0.13.6',
reason='Redirects with prefetch=False are broken in Requests 0.13.6')
2014-04-24 15:07:31 +03:00
def test_digest_auth(self):
2014-04-24 16:48:01 +03:00
r = http('--auth-type=digest', '--auth=user:password', 'GET',
httpbin('/digest-auth/auth/user/password'))
assert HTTP_OK in r
assert r'"authenticated": true' in r
assert r'"user": "user"', r
2014-04-24 15:07:31 +03:00
def test_password_prompt(self):
httpie.input.AuthCredentials._getpass = lambda self, prompt: 'password'
2014-04-24 16:48:01 +03:00
r = http('--auth', 'user', 'GET', httpbin('/basic-auth/user/password'))
assert HTTP_OK in r
assert '"authenticated": true' in r
assert '"user": "user"' in r
2014-04-24 15:07:31 +03:00
def test_credentials_in_url(self):
url = httpbin('/basic-auth/user/password')
url = 'http://user:password@' + url.split('http://', 1)[1]
2014-04-24 16:48:01 +03:00
r = http('GET', url)
assert HTTP_OK in r
2014-04-24 16:48:01 +03:00
assert '"authenticated": true' in r
assert '"user": "user"' in r
2014-04-24 15:07:31 +03:00
def test_credentials_in_url_auth_flag_has_priority(self):
"""When credentials are passed in URL and via -a at the same time,
then the ones from -a are used."""
url = httpbin('/basic-auth/user/password')
url = 'http://user:wrong_password@' + url.split('http://', 1)[1]
2014-04-24 16:48:01 +03:00
r = http('--auth=user:password', 'GET', url)
assert HTTP_OK in r
assert '"authenticated": true' in r
assert '"user": "user"' in r