async def test_multipart_form_data(): data = MultiPartFormData([ FormPart(b'text1', b'text default'), FormPart(b'text2', 'aωb'.encode('utf8')), FormPart(b'file1', b'Content of a.txt.\r\n', b'text/plain', b'a.txt'), FormPart(b'file2', b'<!DOCTYPE html><title>Content of a.html.</title>\r\n', b'text/html', b'a.html'), FormPart(b'file3', 'aωb'.encode('utf8'), b'application/octet-stream', b'binary'), ]) whole = b'' async for chunk in data.get_parts(): whole += chunk expected_result_lines = [ b'--' + data.boundary, b'Content-Disposition: form-data; name="text1"', b'', b'text default', b'--' + data.boundary, b'Content-Disposition: form-data; name="text2"', b'', 'aωb'.encode('utf8'), b'--' + data.boundary, b'Content-Disposition: form-data; name="file1"; filename="a.txt"', b'Content-Type: text/plain', b'', b'Content of a.txt.', b'', b'--' + data.boundary, b'Content-Disposition: form-data; name="file2"; filename="a.html"', b'Content-Type: text/html', b'', b'<!DOCTYPE html><title>Content of a.html.</title>', b'', b'--' + data.boundary, b'Content-Disposition: form-data; name="file3"; filename="binary"', b'Content-Type: application/octet-stream', b'', 'aωb'.encode('utf8'), b'--' + data.boundary + b'--', b'' ] assert whole == b'\r\n'.join(expected_result_lines)
def test_request_content_type_is_read_from_content(): request = Request('POST', b'/', []).with_content(MultiPartFormData([ FormPart(b'a', b'world'), FormPart(b'b', b'9000'), ])) assert request.content_type() == request.content.type
async def test_multipart_form_data(): data = MultiPartFormData( [ FormPart(b"text1", b"text default"), FormPart(b"text2", "aωb".encode("utf8")), FormPart(b"file1", b"Content of a.txt.\r\n", b"text/plain", b"a.txt"), FormPart( b"file2", b"<!DOCTYPE html><title>Content of a.html.</title>\r\n", b"text/html", b"a.html", ), FormPart( b"file3", "aωb".encode("utf8"), b"application/octet-stream", b"binary" ), ] ) whole = data.body expected_result_lines = [ b"--" + data.boundary, b'Content-Disposition: form-data; name="text1"', b"", b"text default", b"--" + data.boundary, b'Content-Disposition: form-data; name="text2"', b"", "aωb".encode("utf8"), b"--" + data.boundary, b'Content-Disposition: form-data; name="file1"; filename="a.txt"', b"Content-Type: text/plain", b"", b"Content of a.txt.", b"", b"--" + data.boundary, b'Content-Disposition: form-data; name="file2"; filename="a.html"', b"Content-Type: text/html", b"", b"<!DOCTYPE html><title>Content of a.html.</title>", b"", b"--" + data.boundary, b'Content-Disposition: form-data; name="file3"; filename="binary"', b"Content-Type: application/octet-stream", b"", "aωb".encode("utf8"), b"--" + data.boundary + b"--", b"", ] assert whole == b"\r\n".join(expected_result_lines)
def test_request_content_type_is_read_from_content(): request = Request("POST", b"/", []).with_content( MultiPartFormData([FormPart(b"a", b"world"), FormPart(b"b", b"9000")])) assert request.content_type() == request.content.type