def test_render_parts(self): field = RequestField('somename', 'data') parts = field._render_parts({'name': 'value', 'filename': 'value'}) self.assertTrue('name="value"' in parts) self.assertTrue('filename="value"' in parts) parts = field._render_parts([('name', 'value'), ('filename', 'value')]) self.assertEqual(parts, 'name="value"; filename="value"')
def _encode_files(files, data): """Build the body for a multipart/form-data request. Will successfully encode files when passed as a dict or a list of 2-tuples. Order is retained if data is a list of 2-tuples but arbitrary if parameters are supplied as a dict. """ if (not files): raise ValueError("Files must be provided.") elif isinstance(data, basestring): raise ValueError("Data must not be a string.") new_fields = [] fields = to_key_val_list(data or {}) files = to_key_val_list(files or {}) for field, val in fields: if isinstance(val, basestring) or not hasattr(val, '__iter__'): val = [val] for v in val: if v is not None: # Don't call str() on bytestrings: in Py3 it all goes wrong. if not isinstance(v, bytes): v = str(v) new_fields.append( (field.decode('utf-8') if isinstance(field, bytes) else field, v.encode('utf-8') if isinstance(v, str) else v)) for (k, v) in files: # support for explicit filename ft = None fh = None if isinstance(v, (tuple, list)): if len(v) == 2: fn, fp = v elif len(v) == 3: fn, fp, ft = v else: fn, fp, ft, fh = v else: fn = guess_filename(v) or k fp = v if isinstance(fp, str): fp = StringIO(fp) if isinstance(fp, bytes): fp = BytesIO(fp) rf = RequestField(name=k, data=fp.read(), filename=fn, headers=fh) rf.make_multipart(content_type=ft) new_fields.append(rf) body, content_type = encode_multipart_formdata(new_fields) return body, content_type
def test_make_multipart(self): field = RequestField('somename', 'data') field.make_multipart(content_type='image/jpg', content_location='/test') self.assertEqual( field.render_headers(), 'Content-Disposition: form-data; name="somename"\r\n' 'Content-Type: image/jpg\r\n' 'Content-Location: /test\r\n' '\r\n')
def test_create(self): simple_field = RequestField('somename', 'data') self.assertEqual(simple_field.render_headers(), '\r\n') filename_field = RequestField('somename', 'data', filename='somefile.txt') self.assertEqual(filename_field.render_headers(), '\r\n') headers_field = RequestField('somename', 'data', headers={'Content-Length': 4}) self.assertEqual(headers_field.render_headers(), 'Content-Length: 4\r\n\r\n')
def test_request_fields(self): fields = [RequestField('k', b'v', filename='somefile.txt', headers={'Content-Type': 'image/jpeg'})] encoded, content_type = encode_multipart_formdata(fields, boundary=BOUNDARY) self.assertEqual(encoded, b'--' + b(BOUNDARY) + b'\r\n' b'Content-Type: image/jpeg\r\n' b'\r\n' b'v\r\n' b'--' + b(BOUNDARY) + b'--\r\n' )
def test_create(self): simple_field = RequestField('somename', 'data') self.assertEqual(simple_field.render_headers(), '\r\n') filename_field = RequestField('somename', 'data', filename='somefile.txt') self.assertEqual(filename_field.render_headers(), '\r\n') headers_field = RequestField('somename', 'data', headers={'Content-Length': 4}) self.assertEqual( headers_field.render_headers(), 'Content-Length: 4\r\n\r\n')
def test_render_part(self): field = RequestField('somename', 'data') param = field._render_part('filename', u('n\u00e4me')) self.assertEqual(param, "filename*=utf-8''n%C3%A4me")