Beispiel #1
0
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
Beispiel #2
0
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)
Beispiel #3
0
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)
Beispiel #4
0
def parse_part(value: bytes, default_charset: Optional[bytes]) -> FormPart:
    """Parses a single multipart/form-data part."""
    raw_headers, data = value.split(b"\r\n\r\n", 1)

    headers = dict(split_headers(raw_headers))

    content_disposition = headers.get(b"content-disposition")

    if not content_disposition:
        raise ValueError(
            "Missing Content-Disposition header in multipart/form-data part."
        )

    content_disposition_values = parse_content_disposition_values(content_disposition)

    field_name = content_disposition_values.get(b"name")

    if field_name == b"_charset_":
        # NB: handling charset...
        # https://tools.ietf.org/html/rfc7578#section-4.6
        raise CharsetPart(data)

    content_type = headers.get(b"content-type", None)

    return FormPart(
        field_name or b"",
        data,
        content_type,
        content_disposition_values.get(b"filename"),
        default_charset,
    )
Beispiel #5
0
def parse_part(value: bytes, default_charset: Optional[bytes]) -> FormPart:
    """Parses a single multipart/form-data part."""
    raw_headers, data = value.split(b'\r\n\r\n', 1)

    headers = dict(split_headers(raw_headers))

    content_disposition = headers.get(b'content-disposition')

    if not content_disposition:
        # what to do? raise makes sense
        raise Exception('Missing Content-Disposition header in multipart/form-data part.')

    content_disposition_values = parse_content_disposition_values(content_disposition)

    field_name = content_disposition_values.get(b'name')

    if field_name == b'_charset_':
        # NB: handling charset...
        # https://tools.ietf.org/html/rfc7578#section-4.6
        raise CharsetPart(data)

    content_type = headers.get(b'content-type', None)  # TODO: b'text/plain' default to text plain?

    return FormPart(field_name,
                    data,
                    content_type,
                    content_disposition_values.get(b'filename'),
                    default_charset)
Beispiel #6
0
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