From 2ec4bbbc2e54944623b71fb023be53d90cb7d8cd Mon Sep 17 00:00:00 2001 From: Petr Blaho Date: Tue, 9 Jul 2013 18:38:07 +0200 Subject: [PATCH] Adds tests for HTTPClient.raw_client Imports mock in test file. Changes name for UrlGeneration tests Adds tests for HTTPClient.raw_client --- tuskarclient/tests/test_http.py | 69 ++++++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/tuskarclient/tests/test_http.py b/tuskarclient/tests/test_http.py index 0c3b1d7..29ae478 100644 --- a/tuskarclient/tests/test_http.py +++ b/tuskarclient/tests/test_http.py @@ -17,11 +17,12 @@ from tuskarclient.tests import utils as tutils from tuskarclient.common import http +import mock fixtures = {} -class HttpClientTest(tutils.TestCase): +class HttpClientUrlGenerationTest(tutils.TestCase): def test_url_generation_trailing_slash_in_base(self): client = http.HTTPClient('http://localhost/') @@ -46,3 +47,69 @@ class HttpClientTest(tutils.TestCase): url = client._make_connection_url('v1/resources') print client.connection_params self.assertEqual(url, '/v1/resources') + + +class HttpClientRawRequestTest(tutils.TestCase): + + def setUp(self): + super(HttpClientRawRequestTest, self).setUp() + + client = http.HTTPClient('http://localhost') + client._http_request = mock.MagicMock(name='_http_request') + self.client = client + + self.call_args = {'provided_method': 'method', + 'expected_method': 'method', + 'provided_url': 'url', + 'expected_url': 'url', + 'provided_args': { + 'headers': {}, + 'other': {}}, + 'expected_args': { + 'headers': { + 'Content-Type': 'application/octet-stream'}, + 'other': {}}} + + def raw_request_calls_http_request(self, + provided_method=None, + provided_url=None, + provided_args={}, + expected_method=None, + expected_url=None, + expected_args={}): + self.client.raw_request(provided_method, + provided_url, + **provided_args) + self.client._http_request.\ + assert_called_once_with(expected_url, + expected_method, + **expected_args) + + def test_raw_request_set_default_headers_with_empty_kwargs(self): + args = self.call_args.copy() + self.raw_request_calls_http_request(**args) + + def test_raw_request_set_default_headers_without_headers(self): + args = self.call_args.copy() + args['provided_args']['other'] = 'other_value' + args['expected_args']['other'] = 'other_value' + self.raw_request_calls_http_request(**args) + + def test_raw_request_set_default_headers_with_other_headers(self): + args = self.call_args.copy() + args['provided_args']['other'] = 'other_value' + args['expected_args']['other'] = 'other_value' + args['provided_args']['headers'] = {'other_header': 'other_value'} + args['expected_args']['headers'] = {'other_header': + 'other_value', + 'Content-Type': + 'application/octet-stream'} + self.raw_request_calls_http_request(**args) + + def test_raw_request_set_default_headers_with_conflicting_header(self): + args = self.call_args.copy() + args['provided_args']['headers'] = {'Content-Type': + 'conflicting_header_value'} + args['expected_args']['headers'] = {'Content-Type': + 'conflicting_header_value'} + self.raw_request_calls_http_request(**args)