def test01MPEncoder(self): """ Test for _MPEncoder class, two parts. """ data = [dict(name='name1', data='data1', content_type='text/plain'), dict(name='name2', data='data2=1&data22=test', content_type='application/x-www-form-urlencoded')] encoder = client._MPEncoder(data, boundary='1234567890') body = _readAll(encoder) lines = body.split('\r\n') expected = ['--1234567890', 'Content-Disposition: form-data; name="name1"', 'Content-Type: text/plain', 'Content-Transfer-Encoding: binary', '', 'data1', '--1234567890', 'Content-Disposition: form-data; name="name2"', 'Content-Type: application/x-www-form-urlencoded', 'Content-Transfer-Encoding: binary', '', 'data2=1&data22=test', '--1234567890--', '' ] expectedLen = sum(len(x) for x in expected) + 2 * (len(expected) - 1) self.assertEqual(lines, expected) self.assertEqual(len(encoder), expectedLen)
def test02MPEncoder(self): """ Test for _MPEncoder class, reading data from file.""" fobj = io.BytesIO('line,1,\\N\nline,2,\0x0\nline,3,\0x1') data = [dict(name='load-options', data='data2=1&data22=test', content_type='application/x-www-form-urlencoded'), dict(name='table-data', data=fobj, filename='tabledata.dat', content_type='binary/octet-stream')] encoder = client._MPEncoder(data) body = _readAll(encoder) lines = body.split('\r\n') expected = ['--part-X-of-the-multipart-request', 'Content-Disposition: form-data; name="load-options"', 'Content-Type: application/x-www-form-urlencoded', 'Content-Transfer-Encoding: binary', '', 'data2=1&data22=test', '--part-X-of-the-multipart-request', 'Content-Disposition: form-data; name="table-data"; filename="tabledata.dat"', 'Content-Type: binary/octet-stream', 'Content-Transfer-Encoding: binary', '', 'line,1,\\N\nline,2,\0x0\nline,3,\0x1', '--part-X-of-the-multipart-request--', '' ] expectedLen = sum(len(x) for x in expected) + 2 * (len(expected) - 1) self.assertEqual(lines, expected) self.assertEqual(len(encoder), expectedLen)
def test01MPEncoder(self): """ Test for _MPEncoder class, two parts. """ data = [ dict(name='name1', data='data1', content_type='text/plain'), dict(name='name2', data='data2=1&data22=test', content_type='application/x-www-form-urlencoded') ] encoder = client._MPEncoder(data, boundary='1234567890') body = _readAll(encoder) lines = body.split('\r\n') expected = [ '--1234567890', 'Content-Disposition: form-data; name="name1"', 'Content-Type: text/plain', 'Content-Transfer-Encoding: binary', '', 'data1', '--1234567890', 'Content-Disposition: form-data; name="name2"', 'Content-Type: application/x-www-form-urlencoded', 'Content-Transfer-Encoding: binary', '', 'data2=1&data22=test', '--1234567890--', '' ] expectedLen = sum(len(x) for x in expected) + 2 * (len(expected) - 1) self.assertEqual(lines, expected) self.assertEqual(len(encoder), expectedLen)
def test02MPEncoder(self): """ Test for _MPEncoder class, reading data from file.""" fobj = io.BytesIO('line,1,\\N\nline,2,\0x0\nline,3,\0x1') data = [ dict(name='load-options', data='data2=1&data22=test', content_type='application/x-www-form-urlencoded'), dict(name='table-data', data=fobj, filename='tabledata.dat', content_type='binary/octet-stream') ] encoder = client._MPEncoder(data) body = _readAll(encoder) lines = body.split('\r\n') expected = [ '--part-X-of-the-multipart-request', 'Content-Disposition: form-data; name="load-options"', 'Content-Type: application/x-www-form-urlencoded', 'Content-Transfer-Encoding: binary', '', 'data2=1&data22=test', '--part-X-of-the-multipart-request', 'Content-Disposition: form-data; name="table-data"; filename="tabledata.dat"', 'Content-Type: binary/octet-stream', 'Content-Transfer-Encoding: binary', '', 'line,1,\\N\nline,2,\0x0\nline,3,\0x1', '--part-X-of-the-multipart-request--', '' ] expectedLen = sum(len(x) for x in expected) + 2 * (len(expected) - 1) self.assertEqual(lines, expected) self.assertEqual(len(encoder), expectedLen)
def test00MPEncoder(self): """ Test for _MPEncoder class, single part. """ data = [dict(name='name1', data='data1')] encoder = client._MPEncoder(data, boundary='1234567890') body = _readAll(encoder) lines = body.split('\r\n') expected = [ '--1234567890', 'Content-Disposition: form-data; name="name1"', 'Content-Transfer-Encoding: binary', '', 'data1', '--1234567890--', '' ] expectedLen = sum(len(x) for x in expected) + 2 * (len(expected) - 1) self.assertEqual(len(encoder), expectedLen) self.assertEqual(lines, expected)
def test00MPEncoder(self): """ Test for _MPEncoder class, single part. """ data = [dict(name='name1', data='data1')] encoder = client._MPEncoder(data, boundary='1234567890') body = _readAll(encoder) lines = body.split('\r\n') expected = ['--1234567890', 'Content-Disposition: form-data; name="name1"', 'Content-Transfer-Encoding: binary', '', 'data1', '--1234567890--', '' ] expectedLen = sum(len(x) for x in expected) + 2 * (len(expected) - 1) self.assertEqual(len(encoder), expectedLen) self.assertEqual(lines, expected)