diff options
Diffstat (limited to 'tests/unit/test_connection.py')
-rw-r--r-- | tests/unit/test_connection.py | 69 |
1 files changed, 57 insertions, 12 deletions
diff --git a/tests/unit/test_connection.py b/tests/unit/test_connection.py index 3d4a57b3..ec9fe997 100644 --- a/tests/unit/test_connection.py +++ b/tests/unit/test_connection.py @@ -25,8 +25,9 @@ import socket from tests.compat import mock, unittest from httpretty import HTTPretty +from boto import UserAgent from boto.compat import json, parse_qs -from boto.connection import AWSQueryConnection, AWSAuthConnection +from boto.connection import AWSQueryConnection, AWSAuthConnection, HTTPRequest from boto.exception import BotoServerError from boto.regioninfo import RegionInfo @@ -72,6 +73,7 @@ class MockAWSService(AWSQueryConnection): """ APIVersion = '2012-01-01' + def _required_auth_capability(self): return ['sign-v2'] @@ -95,6 +97,7 @@ class MockAWSService(AWSQueryConnection): validate_certs=validate_certs, profile_name=profile_name) + class TestAWSAuthConnection(unittest.TestCase): def test_get_path(self): conn = AWSAuthConnection( @@ -129,6 +132,19 @@ class TestAWSAuthConnection(unittest.TestCase): self.assertEqual(conn.proxy_port, '8180') del os.environ['http_proxy'] + def test_get_proxy_url_with_auth(self): + conn = AWSAuthConnection( + 'mockservice.cc-zone-1.amazonaws.com', + aws_access_key_id='access_key', + aws_secret_access_key='secret', + suppress_consec_slashes=False, + proxy="127.0.0.1", + proxy_user="john.doe", + proxy_pass="p4ssw0rd", + proxy_port="8180" + ) + self.assertEqual(conn.get_proxy_url_with_auth(), 'http://john.doe:p4ssw0rd@127.0.0.1:8180') + def test_connection_behind_proxy_without_explicit_port(self): os.environ['http_proxy'] = "http://127.0.0.1" conn = AWSAuthConnection( @@ -169,8 +185,9 @@ class TestAWSAuthConnection(unittest.TestCase): 'testhost', aws_access_key_id='access_key', aws_secret_access_key='secret') - request = conn.build_base_http_request(method='POST', path='/', - auth_path=None, params=None, headers=None, data='', host=None) + request = conn.build_base_http_request( + method='POST', path='/', auth_path=None, params=None, headers=None, + data='', host=None) conn.set_host_header(request) self.assertEqual(request.headers['Host'], 'testhost') @@ -180,15 +197,17 @@ class TestAWSAuthConnection(unittest.TestCase): aws_access_key_id='access_key', aws_secret_access_key='secret', port=8773) - request = conn.build_base_http_request(method='POST', path='/', - auth_path=None, params=None, headers=None, data='', host=None) + request = conn.build_base_http_request( + method='POST', path='/', auth_path=None, params=None, headers=None, + data='', host=None) conn.set_host_header(request) self.assertEqual(request.headers['Host'], 'testhost:8773') + class V4AuthConnection(AWSAuthConnection): def __init__(self, host, aws_access_key_id, aws_secret_access_key, port=443): - AWSAuthConnection.__init__(self, host, aws_access_key_id, - aws_secret_access_key, port=port) + AWSAuthConnection.__init__( + self, host, aws_access_key_id, aws_secret_access_key, port=port) def _required_auth_capability(self): return ['hmac-v4'] @@ -196,15 +215,17 @@ class V4AuthConnection(AWSAuthConnection): class TestAWSQueryConnection(unittest.TestCase): def setUp(self): - self.region = RegionInfo(name='cc-zone-1', - endpoint='mockservice.cc-zone-1.amazonaws.com', - connection_cls=MockAWSService) + self.region = RegionInfo( + name='cc-zone-1', + endpoint='mockservice.cc-zone-1.amazonaws.com', + connection_cls=MockAWSService) HTTPretty.enable() def tearDown(self): HTTPretty.disable() + class TestAWSQueryConnectionSimple(TestAWSQueryConnection): def test_query_connection_basis(self): HTTPretty.register_uri(HTTPretty.POST, @@ -250,7 +271,7 @@ class TestAWSQueryConnectionSimple(TestAWSQueryConnection): aws_secret_access_key='secret', proxy="NON_EXISTENT_HOSTNAME", proxy_port="3128", - is_secure = False) + is_secure=False) resp = conn.make_request('myCmd', {'par1': 'foo', 'par2': 'baz'}, @@ -440,7 +461,7 @@ class TestAWSQueryStatus(TestAWSQueryConnection): content_type='text/xml') conn = self.region.connect(aws_access_key_id='access_key', - aws_secret_access_key='secret') + aws_secret_access_key='secret') with self.assertRaises(BotoServerError): resp = conn.get_status('getStatus', {'par1': 'foo', 'par2': 'baz'}, @@ -460,5 +481,29 @@ class TestAWSQueryStatus(TestAWSQueryConnection): {'par1': 'foo', 'par2': 'baz'}, 'status') + +class TestHTTPRequest(unittest.TestCase): + def test_user_agent_not_url_encoded(self): + headers = {'Some-Header': u'should be url encoded', + 'User-Agent': UserAgent} + request = HTTPRequest('PUT', 'https', 'amazon.com', 443, None, + None, {}, headers, 'Body') + mock_connection = mock.Mock() + + # Create a method that preserves the headers at the time of + # authorization. + def mock_add_auth(req, **kwargs): + mock_connection.headers_at_auth = req.headers.copy() + + mock_connection._auth_handler.add_auth = mock_add_auth + + request.authorize(mock_connection) + # Ensure the headers at authorization are as expected i.e. + # the user agent header was not url encoded but the other header was. + self.assertEqual(mock_connection.headers_at_auth, + {'Some-Header': 'should%20be%20url%20encoded', + 'User-Agent': UserAgent}) + + if __name__ == '__main__': unittest.main() |