def test_encode_blank(self): data, headers = maas_api_helper.encode_multipart_data({}, {}) m = re.search('boundary=([a-zA-Z]+)', headers['Content-Type']) boundary = m.group(1) self.assertEqual({ 'Content-Type': "multipart/form-data; boundary=%s" % boundary, 'Content-Length': str(len(data)), }, headers) self.assertIsInstance(data, bytes) self.assertEquals("--%s--\r\n" % boundary, data.decode('utf-8'))
def test_encode_blank(self): data, headers = maas_api_helper.encode_multipart_data({}, {}) m = re.search("boundary=([a-zA-Z]+)", headers["Content-Type"]) boundary = m.group(1) self.assertEqual( { "Content-Type": "multipart/form-data; boundary=%s" % boundary, "Content-Length": str(len(data)), }, headers, ) self.assertIsInstance(data, bytes) self.assertEquals("--%s--\r\n" % boundary, data.decode("utf-8"))
def test_encode_data(self): key = factory.make_name('key') value = factory.make_name('value') params = {key.encode('utf-8'): value.encode('utf-8')} data, headers = maas_api_helper.encode_multipart_data(params, {}) m = re.search('boundary=([a-zA-Z]+)', headers['Content-Type']) boundary = m.group(1) self.assertEqual({ 'Content-Type': "multipart/form-data; boundary=%s" % boundary, 'Content-Length': str(len(data)), }, headers) self.assertIsInstance(data, bytes) self.assertEquals( '--%s\r\nContent-Disposition: form-data; name="%s"' '\r\n\r\n%s\r\n--%s--\r\n' % (boundary, key, value, boundary), data.decode('utf-8'))
def test_encode_file(self): file = factory.make_name('file') content = factory.make_name('content') files = {file: content.encode('utf-8')} data, headers = maas_api_helper.encode_multipart_data({}, files) m = re.search('boundary=([a-zA-Z]+)', headers['Content-Type']) boundary = m.group(1) self.assertEqual({ 'Content-Type': "multipart/form-data; boundary=%s" % boundary, 'Content-Length': str(len(data)), }, headers) self.assertIsInstance(data, bytes) self.assertEquals( '--%s\r\nContent-Disposition: form-data; name="%s"; filename="%s"' '\r\nContent-Type: application/octet-stream\r\n\r\n%s\r\n--%s--' '\r\n' % (boundary, file, file, content, boundary), data.decode('utf-8'))
def test_encode_data(self): key = factory.make_name("key") value = factory.make_name("value") params = {key.encode("utf-8"): value.encode("utf-8")} data, headers = maas_api_helper.encode_multipart_data(params, {}) m = re.search("boundary=([a-zA-Z]+)", headers["Content-Type"]) boundary = m.group(1) self.assertEqual( { "Content-Type": "multipart/form-data; boundary=%s" % boundary, "Content-Length": str(len(data)), }, headers, ) self.assertIsInstance(data, bytes) self.assertEquals( '--%s\r\nContent-Disposition: form-data; name="%s"' "\r\n\r\n%s\r\n--%s--\r\n" % (boundary, key, value, boundary), data.decode("utf-8"), )
def test_encode_file(self): file = factory.make_name("file") content = factory.make_name("content") files = {file: content.encode("utf-8")} data, headers = maas_api_helper.encode_multipart_data({}, files) m = re.search("boundary=([a-zA-Z]+)", headers["Content-Type"]) boundary = m.group(1) self.assertEqual( { "Content-Type": "multipart/form-data; boundary=%s" % boundary, "Content-Length": str(len(data)), }, headers, ) self.assertIsInstance(data, bytes) self.assertEquals( '--%s\r\nContent-Disposition: form-data; name="%s"; filename="%s"' "\r\nContent-Type: application/octet-stream\r\n\r\n%s\r\n--%s--" "\r\n" % (boundary, file, file, content, boundary), data.decode("utf-8"), )