class LegacySignedRequestFactoryTests(TestCase): if six.PY3: urllib_mock = 'urllib.request.Request.__init__' else: urllib_mock = 'urllib2.Request.__init__' def setUp(self): self.client_id = 'client_id' self.private_key = 'oVB_b3qrP3R6IDApALqehQzFy3DpMfob6Y4627WEK5A=' self.raw_data = {'some': 'data'} self.sut = SignedRequestFactory('GET', self.client_id, self.private_key, self.raw_data) def test_sets_client_id_in_init(self): self.assertEqual(self.client_id, self.sut.client_id) def test_sets_private_key_in_init(self): self.assertEqual(self.private_key, self.sut.private_key) def test_adds_client_id_to_url(self): url = 'http://example.com/my/url' self.sut.raw_data = {} request = self.sut.create_request(url) querystring = "?{}={}".format(constants.CLIENT_ID_PARAM_NAME, self.client_id) querystring += "&{}={}".format(constants.SIGNATURE_PARAM_NAME, 'N1WOdyaBUVlPjKVyL3ionapOLAasFdvagfotfCdCW-Y=') self.assertEqual(url + querystring, request.get_full_url()) def test_adds_signature_to_url(self): url = 'http://example.com/my/url' self.sut.raw_data = {} request = self.sut.create_request(url) querystring = "?{}={}".format(constants.CLIENT_ID_PARAM_NAME, self.client_id) querystring += "&{}={}".format(constants.SIGNATURE_PARAM_NAME, 'N1WOdyaBUVlPjKVyL3ionapOLAasFdvagfotfCdCW-Y=') self.assertEqual(url + querystring, request.get_full_url()) @mock.patch(urllib_mock) def test_urlencodes_data_as_part_of_url_when_method_is_get(self, urllib_request): self.sut.raw_data = {'some': 'da ta', 'goes': 'he re'} self.sut.create_request('http://www.myurl.com') self.assertEqual(None, urllib_request.call_args[0][1]) url = "http://www.myurl.com?{}={}&goes=he+re&some=da+ta&{}={}".format( constants.CLIENT_ID_PARAM_NAME, self.client_id, constants.SIGNATURE_PARAM_NAME, 'oQDdntI8I-YKvMBPlyphnLFoS7xswqdkGNGh0I5uPN8=' ) self.assertEqual(url, urllib_request.call_args[0][0]) @mock.patch(urllib_mock) def test_urlencodes_data_as_part_of_url_when_method_is_delete(self, urllib2_request): self.sut.http_method = 'DELETE' self.sut.raw_data = {'some': 'da ta', 'goes': 'he re'} self.sut.create_request('http://www.myurl.com') self.assertEqual(None, urllib2_request.call_args[0][1]) url = "http://www.myurl.com?{}={}&goes=he+re&some=da+ta&{}={}".format( constants.CLIENT_ID_PARAM_NAME, self.client_id, constants.SIGNATURE_PARAM_NAME, 'oQDdntI8I-YKvMBPlyphnLFoS7xswqdkGNGh0I5uPN8=' ) self.assertEqual(url, urllib2_request.call_args[0][0]) @mock.patch(urllib_mock) def test_passes_data_to_urllib_request_when_method_is_not_get_correctly_for_py2_and_py3(self, urllib2_request): self.sut.raw_data = {'some': 'da ta', 'goes': 'he re'} self.sut.http_method = 'POST' self.sut.create_request('https://www.myurl.com') if six.PY2: self.assertEqual(urlencode(OrderedDict(sorted(self.sut.raw_data.items()))), urllib2_request.call_args[0][1]) else: self.assertEqual(urlencode(OrderedDict( sorted(self.sut.raw_data.items()))).encode(), urllib2_request.call_args[0][1]) url = "https://www.myurl.com?{}={}&{}={}".format( constants.CLIENT_ID_PARAM_NAME, self.client_id, constants.SIGNATURE_PARAM_NAME, 'qlOmQQGhbHtfu8i7UMaS_233z1550YN0JDkS1FeOzRA=' ) self.assertEqual(url, urllib2_request.call_args[0][0]) def test_payload_is_empty_on_get_request_when_signed(self): url = "http://www.myurl.com?asdf=1234" self.sut.raw_data = {'asdf': '1234'} first_request = self.sut._build_signed_url(url, {}) second_request = self.sut._build_signed_url(url, {}) self.assertEqual(first_request, second_request) def test_get_data_payload_returns_none_when_no_raw_data(self): self.sut.raw_data = None payload_data = self.sut._get_data_payload({}) self.assertEqual(None, payload_data) def test_get_data_payload_returns_none_when_get_request(self): self.sut.http_method = "GET" payload_data = self.sut._get_data_payload({}) self.assertEqual(None, payload_data) def test_get_data_payload_returns_proper_data_in_py2_and_bytes_in_py3_and_data_is_str(self): self.sut.http_method = "POST" request_headers = {"Content-Type": "application/json"} self.sut.raw_data = "{'some': 'data'}" payload_data = self.sut._get_data_payload(request_headers) if six.PY2: self.assertEqual("{'some': 'data'}", payload_data) else: self.assertEqual(b"{'some': 'data'}", payload_data) def test_get_data_payload_returns_properly_encoded_data_when_content_type_header_present(self): self.sut.http_method = "POST" request_headers = {"Content-Type": "application/json"} payload_data = self.sut._get_data_payload(request_headers) self.assertEqual({'some': 'data'}, payload_data) def test_get_data_payload_returns_properly_encoded_data_when_content_type_header_present_for_jsonapi(self): self.sut.http_method = "POST" request_headers = {"Content-Type": "application/vnd.api+json"} payload_data = self.sut._get_data_payload(request_headers) self.assertEqual({'some': 'data'}, payload_data) def test_get_data_payload_returns_default_encoded_data_when_no_content_type_header_correctly_for_py2_and_py3(self): self.sut.http_method = "POST" payload_data = self.sut._get_data_payload(self.sut.raw_data) if six.PY2: self.assertEqual(payload_data, 'some=data') else: self.assertEqual(payload_data, b'some=data') def test_create_request_sends_header_data_to_get_data_payload(self): request_kwargs = {"headers": {"Content-Type": "application/json"}} with mock.patch.object(self.sut, "_get_data_payload") as get_payload: self.sut.create_request("http://google.com/", **request_kwargs) get_payload.assert_called_once_with(request_kwargs["headers"]) def test_create_request_sends_empty_dict_to_get_data_payload_when_no_header(self): with mock.patch.object(self.sut, "_get_data_payload") as get_payload: self.sut.create_request("http://google.com/") get_payload.assert_called_once_with({}) def test_input_files_property_will_wrap_single_file_in_list(self): files = {'file': ('name', 'file data')} self.sut.files = files self.assertEqual([files], self.sut.input_files) def test_input_files_property_will_not_wrap_list_of_files_in_list(self): files = [{'file': ('name', 'file data')}] self.sut.files = files self.assertEqual(files, self.sut.input_files) def test_will_flatten_nested_list_on_querystring_for_get_requests(self): self.sut.raw_data = {'items': ['a', 'b', 'c', 'd']} url = 'http://bit.ly/' self.sut.http_method = 'GET' self.sut.client_id = 'foobar' result = self.sut.build_request_url(url, {}) self.assertIn('&items=a&items=b&items=c&items=d', result)
class LegacySignedRequestFactoryTests(unittest.TestCase): def setUp(self): self.client_id = 'client_id' self.private_key = 'oVB_b3qrP3R6IDApALqehQzFy3DpMfob6Y4627WEK5A=' self.raw_data = {'some': 'data'} self.sut = SignedRequestFactory('GET', self.client_id, self.private_key, self.raw_data) def test_sets_client_id_in_init(self): self.assertEqual(self.client_id, self.sut.client_id) def test_sets_private_key_in_init(self): self.assertEqual(self.private_key, self.sut.private_key) def test_adds_client_id_to_url(self): url = 'http://example.com/my/url' self.sut.raw_data = {} request = self.sut.create_request(url) querystring = "?{}={}".format(constants.CLIENT_ID_PARAM_NAME, self.client_id) querystring += "&{}={}".format(constants.SIGNATURE_PARAM_NAME, 'N1WOdyaBUVlPjKVyL3ionapOLAasFdvagfotfCdCW-Y=') self.assertEqual(url + querystring, request.get_full_url()) def test_adds_signature_to_url(self): url = 'http://example.com/my/url' self.sut.raw_data = {} request = self.sut.create_request(url) querystring = "?{}={}".format(constants.CLIENT_ID_PARAM_NAME, self.client_id) querystring += "&{}={}".format(constants.SIGNATURE_PARAM_NAME, 'N1WOdyaBUVlPjKVyL3ionapOLAasFdvagfotfCdCW-Y=') self.assertEqual(url + querystring, request.get_full_url()) @mock.patch('urllib2.Request.__init__') def test_urlencodes_data_as_part_of_url_when_method_is_get(self, urllib2_request): self.sut.raw_data = {'some': 'da ta', 'goes': 'he re'} self.sut.create_request('http://www.myurl.com') self.assertEqual(None, urllib2_request.call_args[0][1]) url = "http://www.myurl.com?{}={}&some=da+ta&goes=he+re&{}={}".format( constants.CLIENT_ID_PARAM_NAME, self.client_id, constants.SIGNATURE_PARAM_NAME, 'Npe9c-jKl2KhwGqvI8-DYxLQMqEm41swdGkQfQ9--lM=' ) self.assertEqual(url, urllib2_request.call_args[0][0]) @mock.patch('urllib2.Request.__init__') def test_urlencodes_data_as_part_of_url_when_method_is_delete(self, urllib2_request): self.sut.http_method = 'DELETE' self.sut.raw_data = {'some': 'da ta', 'goes': 'he re'} self.sut.create_request('http://www.myurl.com') self.assertEqual(None, urllib2_request.call_args[0][1]) url = "http://www.myurl.com?{}={}&some=da+ta&goes=he+re&{}={}".format( constants.CLIENT_ID_PARAM_NAME, self.client_id, constants.SIGNATURE_PARAM_NAME, 'Npe9c-jKl2KhwGqvI8-DYxLQMqEm41swdGkQfQ9--lM=' ) self.assertEqual(url, urllib2_request.call_args[0][0]) @mock.patch('urllib2.Request.__init__') def test_passes_data_to_urllib_request_when_method_is_not_get(self, urllib2_request): self.sut.raw_data = {'some': 'da ta', 'goes': 'he re'} self.sut.http_method = 'POST' self.sut.create_request('https://www.myurl.com') self.assertEqual(urlencode(self.sut.raw_data), urllib2_request.call_args[0][1]) url = "https://www.myurl.com?{}={}&{}={}".format( constants.CLIENT_ID_PARAM_NAME, self.client_id, constants.SIGNATURE_PARAM_NAME, 'cbseuxu6jVikia-u_Qxf5a4v3DKvyrkxjFSj4pnIHVw=' ) self.assertEqual(url, urllib2_request.call_args[0][0]) def test_payload_is_empty_on_get_request_when_signed(self): url = "http://www.myurl.com?asdf=1234" self.sut.raw_data = {'asdf': '1234'} first_request = self.sut._build_signed_url(url, {}) second_request = self.sut._build_signed_url(url, {}) self.assertEqual(first_request, second_request) def test_get_data_payload_returns_none_when_no_raw_data(self): self.sut.raw_data = None payload_data = self.sut._get_data_payload({}) self.assertEqual(None, payload_data) def test_get_data_payload_returns_none_when_get_request(self): self.sut.http_method = "GET" payload_data = self.sut._get_data_payload({}) self.assertEqual(None, payload_data) def test_get_data_payload_returns_properly_encoded_data_when_content_type_header_present(self): self.sut.http_method = "POST" request_headers = {"Content-Type": "application/json"} payload_data = self.sut._get_data_payload(request_headers) self.assertEqual({'some': 'data'}, payload_data) def test_get_data_payload_returns_default_encoded_data_when_no_content_type_header(self): self.sut.http_method = "POST" payload_data = self.sut._get_data_payload(self.sut.raw_data) self.assertEqual('some=data', payload_data) def test_create_request_sends_header_data_to_get_data_payload(self): request_kwargs = {"headers": {"Content-Type": "application/json"}} with mock.patch.object(self.sut, "_get_data_payload") as get_payload: self.sut.create_request("http://google.com/", **request_kwargs) get_payload.assert_called_once_with(request_kwargs["headers"]) def test_create_request_sends_empty_dict_to_get_data_payload_when_no_header(self): with mock.patch.object(self.sut, "_get_data_payload") as get_payload: self.sut.create_request("http://google.com/") get_payload.assert_called_once_with({}) def test_input_files_property_will_wrap_single_file_in_list(self): files = {'file': ('name', 'file data')} self.sut.files = files self.assertEqual([files], self.sut.input_files) def test_input_files_property_will_not_wrap_list_of_files_in_list(self): files = [{'file': ('name', 'file data')}] self.sut.files = files self.assertEqual(files, self.sut.input_files) def test_will_flatten_nested_list_on_querystring_for_get_requests(self): self.sut.raw_data = {'items': ['a', 'b', 'c', 'd']} url = 'http://bit.ly/' self.sut.http_method = 'GET' self.sut.client_id = 'foobar' result = self.sut.build_request_url(url, {}) self.assertIn('&items=a&items=b&items=c&items=d', result)
class LegacySignedRequestFactoryTests(unittest.TestCase): def setUp(self): self.client_id = 'client_id' self.private_key = 'oVB_b3qrP3R6IDApALqehQzFy3DpMfob6Y4627WEK5A=' self.raw_data = {'some': 'data'} self.sut = SignedRequestFactory('GET', self.client_id, self.private_key, self.raw_data) def test_sets_client_id_in_init(self): self.assertEqual(self.client_id, self.sut.client_id) def test_sets_private_key_in_init(self): self.assertEqual(self.private_key, self.sut.private_key) def test_adds_client_id_to_url(self): url = 'http://example.com/my/url' self.sut.raw_data = {} request = self.sut.create_request(url) querystring = "?{}={}".format(constants.CLIENT_ID_PARAM_NAME, self.client_id) querystring += "&{}={}".format( constants.SIGNATURE_PARAM_NAME, 'N1WOdyaBUVlPjKVyL3ionapOLAasFdvagfotfCdCW-Y=') self.assertEqual(url + querystring, request.get_full_url()) def test_adds_signature_to_url(self): url = 'http://example.com/my/url' self.sut.raw_data = {} request = self.sut.create_request(url) querystring = "?{}={}".format(constants.CLIENT_ID_PARAM_NAME, self.client_id) querystring += "&{}={}".format( constants.SIGNATURE_PARAM_NAME, 'N1WOdyaBUVlPjKVyL3ionapOLAasFdvagfotfCdCW-Y=') self.assertEqual(url + querystring, request.get_full_url()) @mock.patch('urllib2.Request.__init__') def test_urlencodes_data_as_part_of_url_when_method_is_get( self, urllib2_request): self.sut.raw_data = {'some': 'da ta', 'goes': 'he re'} self.sut.create_request('http://www.myurl.com') self.assertEqual(None, urllib2_request.call_args[0][1]) url = "http://www.myurl.com?{}={}&some=da+ta&goes=he+re&{}={}".format( constants.CLIENT_ID_PARAM_NAME, self.client_id, constants.SIGNATURE_PARAM_NAME, 'Npe9c-jKl2KhwGqvI8-DYxLQMqEm41swdGkQfQ9--lM=') self.assertEqual(url, urllib2_request.call_args[0][0]) @mock.patch('urllib2.Request.__init__') def test_urlencodes_data_as_part_of_url_when_method_is_delete( self, urllib2_request): self.sut.http_method = 'DELETE' self.sut.raw_data = {'some': 'da ta', 'goes': 'he re'} self.sut.create_request('http://www.myurl.com') self.assertEqual(None, urllib2_request.call_args[0][1]) url = "http://www.myurl.com?{}={}&some=da+ta&goes=he+re&{}={}".format( constants.CLIENT_ID_PARAM_NAME, self.client_id, constants.SIGNATURE_PARAM_NAME, 'Npe9c-jKl2KhwGqvI8-DYxLQMqEm41swdGkQfQ9--lM=') self.assertEqual(url, urllib2_request.call_args[0][0]) @mock.patch('urllib2.Request.__init__') def test_passes_data_to_urllib_request_when_method_is_not_get( self, urllib2_request): self.sut.raw_data = {'some': 'da ta', 'goes': 'he re'} self.sut.http_method = 'POST' self.sut.create_request('https://www.myurl.com') self.assertEqual(urlencode(self.sut.raw_data), urllib2_request.call_args[0][1]) url = "https://www.myurl.com?{}={}&{}={}".format( constants.CLIENT_ID_PARAM_NAME, self.client_id, constants.SIGNATURE_PARAM_NAME, 'cbseuxu6jVikia-u_Qxf5a4v3DKvyrkxjFSj4pnIHVw=') self.assertEqual(url, urllib2_request.call_args[0][0]) def test_payload_is_empty_on_get_request_when_signed(self): url = "http://www.myurl.com?asdf=1234" self.sut.raw_data = {'asdf': '1234'} first_request = self.sut._build_signed_url(url, {}) second_request = self.sut._build_signed_url(url, {}) self.assertEqual(first_request, second_request) def test_get_data_payload_returns_none_when_no_raw_data(self): self.sut.raw_data = None payload_data = self.sut._get_data_payload({}) self.assertEqual(None, payload_data) def test_get_data_payload_returns_none_when_get_request(self): self.sut.http_method = "GET" payload_data = self.sut._get_data_payload({}) self.assertEqual(None, payload_data) def test_get_data_payload_returns_properly_encoded_data_when_content_type_header_present( self): self.sut.http_method = "POST" request_headers = {"Content-Type": "application/json"} payload_data = self.sut._get_data_payload(request_headers) self.assertEqual({'some': 'data'}, payload_data) def test_get_data_payload_returns_default_encoded_data_when_no_content_type_header( self): self.sut.http_method = "POST" payload_data = self.sut._get_data_payload(self.sut.raw_data) self.assertEqual('some=data', payload_data) def test_create_request_sends_header_data_to_get_data_payload(self): request_kwargs = {"headers": {"Content-Type": "application/json"}} with mock.patch.object(self.sut, "_get_data_payload") as get_payload: self.sut.create_request("http://google.com/", **request_kwargs) get_payload.assert_called_once_with(request_kwargs["headers"]) def test_create_request_sends_empty_dict_to_get_data_payload_when_no_header( self): with mock.patch.object(self.sut, "_get_data_payload") as get_payload: self.sut.create_request("http://google.com/") get_payload.assert_called_once_with({}) def test_input_files_property_will_wrap_single_file_in_list(self): files = {'file': ('name', 'file data')} self.sut.files = files self.assertEqual([files], self.sut.input_files) def test_input_files_property_will_not_wrap_list_of_files_in_list(self): files = [{'file': ('name', 'file data')}] self.sut.files = files self.assertEqual(files, self.sut.input_files) def test_will_flatten_nested_list_on_querystring_for_get_requests(self): self.sut.raw_data = {'items': ['a', 'b', 'c', 'd']} url = 'http://bit.ly/' self.sut.http_method = 'GET' self.sut.client_id = 'foobar' result = self.sut.build_request_url(url, {}) self.assertIn('&items=a&items=b&items=c&items=d', result)
class LegacySignedRequestFactoryTests(TestCase): if six.PY3: urllib_mock = 'urllib.request.Request.__init__' else: urllib_mock = 'urllib2.Request.__init__' def setUp(self): self.client_id = 'client_id' self.private_key = 'oVB_b3qrP3R6IDApALqehQzFy3DpMfob6Y4627WEK5A=' self.raw_data = {'some': 'data'} self.sut = SignedRequestFactory('GET', self.client_id, self.private_key, self.raw_data) def test_sets_client_id_in_init(self): self.assertEqual(self.client_id, self.sut.client_id) def test_sets_private_key_in_init(self): self.assertEqual(self.private_key, self.sut.private_key) def test_adds_client_id_to_url(self): url = 'http://example.com/my/url' self.sut.raw_data = {} request = self.sut.create_request(url) querystring = "?{}={}".format(constants.CLIENT_ID_PARAM_NAME, self.client_id) querystring += "&{}={}".format(constants.SIGNATURE_PARAM_NAME, 'N1WOdyaBUVlPjKVyL3ionapOLAasFdvagfotfCdCW-Y=') self.assertEqual(url + querystring, request.get_full_url()) def test_adds_signature_to_url(self): url = 'http://example.com/my/url' self.sut.raw_data = {} request = self.sut.create_request(url) querystring = "?{}={}".format(constants.CLIENT_ID_PARAM_NAME, self.client_id) querystring += "&{}={}".format(constants.SIGNATURE_PARAM_NAME, 'N1WOdyaBUVlPjKVyL3ionapOLAasFdvagfotfCdCW-Y=') self.assertEqual(url + querystring, request.get_full_url()) @mock.patch(urllib_mock) def test_urlencodes_data_as_part_of_url_when_method_is_get(self, urllib_request): self.sut.raw_data = {'some': 'da ta', 'goes': 'he re'} self.sut.create_request('http://www.myurl.com') self.assertEqual(None, urllib_request.call_args[0][1]) url = "http://www.myurl.com?{}={}&goes=he+re&some=da+ta&{}={}".format( constants.CLIENT_ID_PARAM_NAME, self.client_id, constants.SIGNATURE_PARAM_NAME, 'oQDdntI8I-YKvMBPlyphnLFoS7xswqdkGNGh0I5uPN8=' ) self.assertEqual(url, urllib_request.call_args[0][0]) @mock.patch(urllib_mock) def test_urlencodes_data_as_part_of_url_when_method_is_delete(self, urllib2_request): self.sut.http_method = 'DELETE' self.sut.raw_data = {'some': 'da ta', 'goes': 'he re'} self.sut.create_request('http://www.myurl.com') self.assertEqual(None, urllib2_request.call_args[0][1]) url = "http://www.myurl.com?{}={}&goes=he+re&some=da+ta&{}={}".format( constants.CLIENT_ID_PARAM_NAME, self.client_id, constants.SIGNATURE_PARAM_NAME, 'oQDdntI8I-YKvMBPlyphnLFoS7xswqdkGNGh0I5uPN8=' ) self.assertEqual(url, urllib2_request.call_args[0][0]) @mock.patch(urllib_mock) def test_passes_data_to_urllib_request_when_method_is_not_get_correctly_for_py2_and_py3(self, urllib2_request): self.sut.raw_data = {'some': 'da ta', 'goes': 'he re'} self.sut.http_method = 'POST' self.sut.create_request('https://www.myurl.com') if six.PY2: self.assertEqual(urlencode(OrderedDict(sorted(self.sut.raw_data.items()))), urllib2_request.call_args[0][1]) else: self.assertEqual( urlencode(OrderedDict(sorted(self.sut.raw_data.items()))).encode(), urllib2_request.call_args[0][1] ) url = "https://www.myurl.com?{}={}&{}={}".format( constants.CLIENT_ID_PARAM_NAME, self.client_id, constants.SIGNATURE_PARAM_NAME, 'qlOmQQGhbHtfu8i7UMaS_233z1550YN0JDkS1FeOzRA=' ) self.assertEqual(url, urllib2_request.call_args[0][0]) def test_payload_is_empty_on_get_request_when_signed(self): url = "http://www.myurl.com?asdf=1234" self.sut.raw_data = {'asdf': '1234'} first_request = self.sut._build_signed_url(url, {}) second_request = self.sut._build_signed_url(url, {}) self.assertEqual(first_request, second_request) def test_get_data_payload_returns_none_when_no_raw_data(self): self.sut.raw_data = None payload_data = self.sut._get_data_payload({}) self.assertEqual(None, payload_data) def test_get_data_payload_returns_none_when_get_request(self): self.sut.http_method = "GET" payload_data = self.sut._get_data_payload({}) self.assertEqual(None, payload_data) def test_get_data_payload_returns_proper_data_in_py2_and_bytes_in_py3_and_data_is_str(self): self.sut.http_method = "POST" request_headers = {"Content-Type": "application/json"} self.sut.raw_data = "{'some': 'data'}" payload_data = self.sut._get_data_payload(request_headers) if six.PY2: self.assertEqual("{'some': 'data'}", payload_data) else: self.assertEqual(b"{'some': 'data'}", payload_data) def test_get_data_payload_returns_properly_encoded_data_when_content_type_header_present(self): self.sut.http_method = "POST" request_headers = {"Content-Type": "application/json"} payload_data = self.sut._get_data_payload(request_headers) self.assertEqual({'some': 'data'}, payload_data) def test_get_data_payload_returns_properly_encoded_data_when_content_type_header_present_for_jsonapi(self): self.sut.http_method = "POST" request_headers = {"Content-Type": "application/vnd.api+json"} payload_data = self.sut._get_data_payload(request_headers) self.assertEqual({'some': 'data'}, payload_data) def test_get_data_payload_returns_default_encoded_data_when_no_content_type_header_correctly_for_py2_and_py3(self): self.sut.http_method = "POST" payload_data = self.sut._get_data_payload(self.sut.raw_data) if six.PY2: self.assertEqual(payload_data, 'some=data') else: self.assertEqual(payload_data, b'some=data') def test_create_request_sends_header_data_to_get_data_payload(self): request_kwargs = {"headers": {"Content-Type": "application/json"}} with mock.patch.object(self.sut, "_get_data_payload") as get_payload: self.sut.create_request("http://google.com/", **request_kwargs) get_payload.assert_called_once_with(request_kwargs["headers"]) def test_create_request_sends_empty_dict_to_get_data_payload_when_no_header(self): with mock.patch.object(self.sut, "_get_data_payload") as get_payload: self.sut.create_request("http://google.com/") get_payload.assert_called_once_with({}) def test_input_files_property_will_wrap_single_file_in_list(self): files = {'file': ('name', 'file data')} self.sut.files = files self.assertEqual([files], self.sut.input_files) def test_input_files_property_will_not_wrap_list_of_files_in_list(self): files = [{'file': ('name', 'file data')}] self.sut.files = files self.assertEqual(files, self.sut.input_files) def test_will_flatten_nested_list_on_querystring_for_get_requests(self): self.sut.raw_data = {'items': ['a', 'b', 'c', 'd']} url = 'http://bit.ly/' self.sut.http_method = 'GET' self.sut.client_id = 'foobar' result = self.sut.build_request_url(url, {}) self.assertIn('&items=a&items=b&items=c&items=d', result)