def multipart(rq, **values): frame = '\n--{}\nContent-Disposition: form-data; name="{}"' boundary = uuid4().hex files = [name for name, data in values.items() if 'mimetype' in data] output = bytes() for name, value in values.items(): if name in files: fname = value.get('filename', getattr(value['value'], 'name', uuid4().hex)) this_frame = frame + '; filename="{}"\nContent-Type: {}\n\n' this_data = encode(value['value'], value['mimetype']) args = (boundary, name, fname, value['mimetype']) else: this_frame = frame + '\n\n' this_data = value['value'].encode('ascii') args = (boundary, name) output += this_frame.format(*args).encode('ascii') + this_data output += '\n--{}--'.format(boundary).encode('ascii') rq.set_data(output) content_type_header = 'multipart/form-data; boundary={}'.format(boundary) set_content_type(rq, content_type_header)
def render_data(rq, **data): for val in data.values(): set_content_type(rq, val['mimetype']) rq.set_data(encode(val['value'], val['mimetype']))
def test_encode(self): self.assertEqual(encode(self.test_params, 'application/x-www-form-urlencoded'), self.proper_output)
def test_encode(self): self.assertEqual(encode(self.string, 'text/plain'), self.bytes)
def test_encode(self): self.assertEqual(encode(self.to_test, 'fake-mime'), self.to_test)