Skip to content

Commit

Permalink
removed base64 encoding/decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
annatisch committed May 17, 2016
1 parent 914f60f commit 1d37145
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,9 @@ def test_array(self):
with self.assertRaises(DeserializationError):
client.array.get_date_time_invalid_chars()

test_array = ['a string that gets encoded with base64url', 'test string', 'Lorem ipsum']
test_array = ['a string that gets encoded with base64url'.encode(),
'test string'.encode(),
'Lorem ipsum'.encode()]
self.assertEqual(client.array.get_base64_url(), test_array)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,9 @@ def test_dictionary_primitive_types(self):
bytes_result = self.client.dictionary.get_byte_invalid_null()
self.assertEqual(bytes_null, bytes_result)

test_dict = {'0': 'a string that gets encoded with base64url',
'1': 'test string',
'2': 'Lorem ipsum'}
test_dict = {'0': 'a string that gets encoded with base64url'.encode(),
'1': 'test string'.encode(),
'2': 'Lorem ipsum'.encode()}
self.assertEqual(self.client.dictionary.get_base64_url(), test_dict)

def test_basic_dictionary_parsing(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ def test_string(self):
self.assertEqual(Colors.redcolor, client.enum.get_not_expandable())
client.enum.put_not_expandable(Colors.redcolor)

self.assertEqual(client.string.get_base64_encoded(), 'a string that gets encoded with base64')
self.assertEqual(client.string.get_base64_url_encoded(), 'a string that gets encoded with base64url')
self.assertEqual(client.string.get_base64_encoded(), 'a string that gets encoded with base64'.encode())
self.assertEqual(client.string.get_base64_url_encoded(), 'a string that gets encoded with base64url'.encode())
self.assertIsNone(client.string.get_null_base64_url_encoded())
client.string.put_base64_url_encoded('a string that gets encoded with base64url')
client.string.put_base64_url_encoded('a string that gets encoded with base64url'.encode())


if __name__ == '__main__':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def test_url_path(self):
with self.assertRaises(ValidationError):
self.client.paths.enum_null(None)

self.client.paths.base64_url("lorem")
self.client.paths.base64_url("lorem".encode())

def test_url_query(self):

Expand Down
13 changes: 8 additions & 5 deletions ClientRuntimes/Python/msrest/msrest/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,10 @@ def serialize_unicode(self, data):
:param data: Object to be serialized.
:rtype: str
"""
try:
return data.value
except AttributeError:
pass
try:
if isinstance(data, unicode):
return data.encode(encoding='utf-8')
Expand Down Expand Up @@ -549,7 +553,7 @@ def serialize_base64(attr, **kwargs):
:param attr: Object to be serialized.
:rtype: str
"""
encoded = b64encode(attr.encode()).decode()
encoded = b64encode(attr).decode('ascii')
return encoded.strip('=').replace('+', '-').replace('/', '_')

@staticmethod
Expand Down Expand Up @@ -1005,11 +1009,10 @@ def deserialize_base64(attr):
:rtype: bytearray
:raises: TypeError if string format invalid.
"""
pad_count = 3 - (len(attr) + 3) % 4
padding = ['='] * pad_count
attr = attr + ''.join(padding)
padding = '=' * (3 - (len(attr) + 3) % 4)
attr = attr + padding
encoded = attr.replace('-', '+').replace('_', '/')
return b64decode(encoded).decode()
return b64decode(encoded)

@staticmethod
def deserialize_decimal(attr):
Expand Down

0 comments on commit 1d37145

Please sign in to comment.