diff options
author | Jenkins <jenkins@review.openstack.org> | 2014-04-14 10:49:14 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2014-04-14 10:49:14 +0000 |
commit | ec538d37202c659c3702b499fc2f2ea962309746 (patch) | |
tree | 5fc5a45c67cacea32da421818fc9285e4ae6dd3f /tests/test_swiftclient.py | |
parent | cdd8d33ea70745bf477aae1d5609e6ee7170820a (diff) | |
parent | da2d8eef47ac875fcd72fbcb1e2f8ef9ee70d6d6 (diff) | |
download | python-swiftclient-ec538d37202c659c3702b499fc2f2ea962309746.tar.gz |
Merge "Add requests related unit-tests"
Diffstat (limited to 'tests/test_swiftclient.py')
-rw-r--r-- | tests/test_swiftclient.py | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/tests/test_swiftclient.py b/tests/test_swiftclient.py index f50dca3..a1e519c 100644 --- a/tests/test_swiftclient.py +++ b/tests/test_swiftclient.py @@ -23,6 +23,8 @@ except ImportError: import six import socket +import types +import StringIO import testtools import warnings from six.moves.urllib.parse import urlparse @@ -32,6 +34,7 @@ from six.moves import reload_module from .utils import fake_http_connect, fake_get_keystoneclient_2_0 from swiftclient import client as c +import swiftclient.utils class TestClientException(testtools.TestCase): @@ -152,6 +155,7 @@ class MockHttpResponse(): self.status_code = status self.reason = "OK" self.buffer = [] + self.requests_params = None class Raw: def read(): @@ -172,6 +176,7 @@ class MockHttpResponse(): def _fake_request(self, *arg, **kwarg): self.status = 200 + self.requests_params = kwarg # This simulate previous httplib implementation that would do a # putrequest() and then use putheader() to send header. for k, v in kwarg['headers'].items(): @@ -684,6 +689,58 @@ class TestPutObject(MockHttpTest): c.put_object('http://www.test.com', 'asdf', 'asdf', 'asdf', query_string="hello=20") + def test_raw_upload(self): + # Raw upload happens when content_length is passed to put_object + conn = c.http_connection(u'http://www.test.com/') + resp = MockHttpResponse(status=200) + conn[1].getresponse = resp.fake_response + conn[1]._request = resp._fake_request + mock_file = StringIO.StringIO('asdf') + + c.put_object(url='http://www.test.com', http_conn=conn, + contents=mock_file, content_length=4) + self.assertTrue(isinstance(resp.requests_params['data'], + swiftclient.utils.LengthWrapper)) + self.assertEqual(mock_file.len, 4) + + c.put_object(url='http://www.test.com', http_conn=conn, + headers={'Content-Length': '4'}, contents=mock_file) + self.assertTrue(isinstance(resp.requests_params['data'], + swiftclient.utils.LengthWrapper)) + self.assertEqual(mock_file.len, 4) + + def test_chunk_upload(self): + # Chunked upload happens when no content_length is passed to put_object + conn = c.http_connection(u'http://www.test.com/') + resp = MockHttpResponse(status=200) + conn[1].getresponse = resp.fake_response + conn[1]._request = resp._fake_request + raw_data = 'asdf' * 256 + chunk_size = 16 + mock_file = StringIO.StringIO(raw_data) + + c.put_object(url='http://www.test.com', http_conn=conn, + contents=mock_file, chunk_size=chunk_size) + request_data = resp.requests_params['data'] + self.assertTrue(isinstance(request_data, types.GeneratorType)) + data = '' + for chunk in request_data: + self.assertEqual(chunk_size, len(chunk)) + data += chunk + self.assertEqual(data, raw_data) + + def test_params(self): + conn = c.http_connection(u'http://www.test.com/') + resp = MockHttpResponse(status=200) + conn[1].getresponse = resp.fake_response + conn[1]._request = resp._fake_request + + c.put_object(url='http://www.test.com', http_conn=conn, + etag='1234-5678', content_type='text/plain') + request_header = resp.requests_params['headers'] + self.assertTrue(request_header['etag'], '1234-5678') + self.assertTrue(request_header['content-type'], 'text/plain') + class TestPostObject(MockHttpTest): @@ -755,6 +812,30 @@ class TestGetCapabilities(MockHttpTest): self.assertRaises(c.ClientException, c.get_capabilities, http_conn) +class TestHTTPConnection(MockHttpTest): + + def test_ok_proxy(self): + conn = c.http_connection(u'http://www.test.com/', + proxy='http://localhost:8080') + self.assertEquals(conn[1].requests_args['proxies']['http'], + 'http://localhost:8080') + + def test_bad_proxy(self): + try: + c.http_connection(u'http://www.test.com/', proxy='localhost:8080') + except c.ClientException as e: + self.assertEqual(e.msg, "Proxy's missing scheme") + + def test_cacert(self): + conn = c.http_connection(u'http://www.test.com/', + cacert='/dev/urandom') + self.assertEquals(conn[1].requests_args['verify'], '/dev/urandom') + + def test_insecure(self): + conn = c.http_connection(u'http://www.test.com/', insecure=True) + self.assertEquals(conn[1].requests_args['verify'], False) + + class TestConnection(MockHttpTest): def test_instance(self): |