Example #1
0
def test_parse_multipart_two_fields():
    content = b'--------28cbeda4cdd04d1595b71933e31928cd\r\nContent-Disposition: form-data; name="a"\r\n\r\nworld\r\n' \
              b'--------28cbeda4cdd04d1595b71933e31928cd\r\nContent-Disposition: form-data; name="b"\r\n\r\n9000\r\n' \
              b'--------28cbeda4cdd04d1595b71933e31928cd--\r\n'

    data = list(parse_multipart(content))  # type: List[FormPart]

    assert data is not None
    assert len(data) == 2

    assert data[0].name == b'a'
    assert data[0].data == b'world'
    assert data[1].name == b'b'
    assert data[1].data == b'9000'
Example #2
0
def test_parse_multipart():
    boundary = b'---------------------0000000000000000000000001'

    content = b'\r\n'.join([
        boundary, b'Content-Disposition: form-data; name="text1"', b'',
        b'text default', boundary,
        b'Content-Disposition: form-data; name="text2"', b'',
        'aωb'.encode('utf8'), boundary,
        b'Content-Disposition: form-data; name="file1"; filename="a.txt"',
        b'Content-Type: text/plain', b'', b'Content of a.txt.', b'', 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'', boundary,
        b'Content-Disposition: form-data; name="file3"; filename="binary"',
        b'Content-Type: application/octet-stream', b'', 'aωb'.encode('utf8'),
        boundary + b'--'
    ])

    data = list(parse_multipart(content))  # type: List[FormPart]

    assert data is not None
    assert len(data) == 5

    assert data[0].name == b'text1'
    assert data[0].file_name is None
    assert data[0].content_type is None
    assert data[0].data == b'text default'

    assert data[1].name == b'text2'
    assert data[1].file_name is None
    assert data[1].content_type is None
    assert data[1].data == 'aωb'.encode('utf8')

    assert data[2].name == b'file1'
    assert data[2].file_name == b'a.txt'
    assert data[2].content_type == b'text/plain'
    assert data[2].data == b'Content of a.txt.\r\n'

    assert data[3].name == b'file2'
    assert data[3].file_name == b'a.html'
    assert data[3].content_type == b'text/html'
    assert data[
        3].data == b'<!DOCTYPE html><title>Content of a.html.</title>\r\n'

    assert data[4].name == b'file3'
    assert data[4].file_name == b'binary'
    assert data[4].content_type == b'application/octet-stream'
    assert data[4].data == 'aωb'.encode('utf8')
Example #3
0
def test_function(value: bytes, expected_value):
    values = list(parse_multipart(value))
    assert values == expected_value
Example #4
0
def test_parse_multipart():
    boundary = b"---------------------0000000000000000000000001"

    content = b"\r\n".join([
        boundary,
        b'Content-Disposition: form-data; name="text1"',
        b"",
        b"text default",
        boundary,
        b'Content-Disposition: form-data; name="text2"',
        b"",
        "aωb".encode("utf8"),
        boundary,
        b'Content-Disposition: form-data; name="file1"; filename="a.txt"',
        b"Content-Type: text/plain",
        b"",
        b"Content of a.txt.",
        b"",
        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"",
        boundary,
        b'Content-Disposition: form-data; name="file3"; filename="binary"',
        b"Content-Type: application/octet-stream",
        b"",
        "aωb".encode("utf8"),
        boundary + b"--",
    ])

    data = list(parse_multipart(content))  # type: List[FormPart]

    assert data is not None
    assert len(data) == 5

    assert data[0].name == b"text1"
    assert data[0].file_name is None
    assert data[0].content_type is None
    assert data[0].data == b"text default"

    assert data[1].name == b"text2"
    assert data[1].file_name is None
    assert data[1].content_type is None
    assert data[1].data == "aωb".encode("utf8")

    assert data[2].name == b"file1"
    assert data[2].file_name == b"a.txt"
    assert data[2].content_type == b"text/plain"
    assert data[2].data == b"Content of a.txt.\r\n"

    assert data[3].name == b"file2"
    assert data[3].file_name == b"a.html"
    assert data[3].content_type == b"text/html"
    assert data[
        3].data == b"<!DOCTYPE html><title>Content of a.html.</title>\r\n"

    assert data[4].name == b"file3"
    assert data[4].file_name == b"binary"
    assert data[4].content_type == b"application/octet-stream"
    assert data[4].data == "aωb".encode("utf8")