Esempio n. 1
0
async def test_post_multipart_form_with_images(session):

    if os.path.exists("out"):
        shutil.rmtree("out")

    file_one_path = get_static_path("pexels-photo-126407.jpeg")
    file_two_path = get_static_path("pexels-photo-923360.jpeg")

    # NB: Flask api to handle parts with equal name is quite uncomfortable,
    # here for simplicity we set two parts with different names
    response = await session.post(
        "/upload-files",
        MultiPartFormData([
            FormPart(
                b"images1",
                get_file_bytes(file_one_path),
                b"image/jpeg",
                b"three.jpg",
            ),
            FormPart(
                b"images2",
                get_file_bytes(file_two_path),
                b"image/jpeg",
                b"four.jpg",
            ),
        ]),
    )
    ensure_success(response)

    assert_files_equals(f"./out/three.jpg", file_one_path)
    assert_files_equals(f"./out/four.jpg", file_two_path)
Esempio n. 2
0
async def test_post_multipart_form_with_files(session):

    if os.path.exists("out"):
        shutil.rmtree("out")

    response = await session.post(
        "/upload-files",
        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",
            ),
        ]),
    )
    ensure_success(response)

    assert_file_content_equals("./out/a.txt", "Content of a.txt.\n")
    assert_file_content_equals(
        "./out/a.html", "<!DOCTYPE html><title>Content of a.html.</title>\n")
    assert_file_content_equals("./out/binary", "aωb")
Esempio n. 3
0
async def test_from_body_form_binding_multipart():

    request = Request("POST", b"/", []).with_content(
        MultiPartFormData([FormPart(b"a", b"world"),
                           FormPart(b"b", b"9000")]))

    parameter = FormBinder(ExampleOne)
    value = await parameter.get_value(request)

    assert isinstance(value, ExampleOne)
    assert value.a == "world"
    assert value.b == 9000
Esempio n. 4
0
async def test_from_body_form_binding_multipart():

    request = Request('POST', b'/', []).with_content(
        MultiPartFormData([
            FormPart(b'a', b'world'),
            FormPart(b'b', b'9000'),
        ]))

    parameter = FromForm(ExampleOne)
    value = await parameter.get_value(request)

    assert isinstance(value, ExampleOne)
    assert value.a == 'world'
    assert value.b == 9000
Esempio n. 5
0
async def test_from_body_form_binding_multipart_keys_duplicates():

    request = Request("POST", b"/", []).with_content(
        MultiPartFormData([
            FormPart(b"a", b"world"),
            FormPart(b"b", b"one"),
            FormPart(b"b", b"two"),
            FormPart(b"b", b"three"),
        ]))

    parameter = FormBinder(ExampleThree)

    value = await parameter.get_value(request)

    assert isinstance(value, ExampleThree)
    assert value.a == "world"
    assert value.b == ["one", "two", "three"]
Esempio n. 6
0
async def test_from_body_form_binding_multipart_keys_duplicates():

    request = Request('POST', b'/', []).with_content(
        MultiPartFormData([
            FormPart(b'a', b'world'),
            FormPart(b'b', b'one'),
            FormPart(b'b', b'two'),
            FormPart(b'b', b'three')
        ]))

    parameter = FromForm(ExampleThree)

    value = await parameter.get_value(request)

    assert isinstance(value, ExampleThree)
    assert value.a == 'world'
    assert value.b == ['one', 'two', 'three']
Esempio n. 7
0
async def test_post_multipart_form_with_images(session):

    if os.path.exists('out'):
        shutil.rmtree('out')

    # NB: Flask api to handle parts with equal name is quite uncomfortable, here for simplicity
    # we set two parts with different names
    response = await session.post(
        '/upload-files',
        MultiPartFormData([
            FormPart(b'images1',
                     get_file_bytes('static/pexels-photo-126407.jpeg'),
                     b'image/jpeg', b'three.jpg'),
            FormPart(b'images2',
                     get_file_bytes('static/pexels-photo-923360.jpeg'),
                     b'image/jpeg', b'four.jpg')
        ]))
    ensure_success(response)

    assert_files_equals(f'./out/three.jpg', 'static/pexels-photo-126407.jpeg')
    assert_files_equals(f'./out/four.jpg', 'static/pexels-photo-923360.jpeg')
Esempio n. 8
0
async def test_post_multipart_form_with_files(session):

    if os.path.exists('out'):
        shutil.rmtree('out')

    response = await session.post(
        '/upload-files',
        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'),
        ]))
    ensure_success(response)

    assert_file_content_equals('./out/a.txt', 'Content of a.txt.\n')
    assert_file_content_equals(
        './out/a.html', '<!DOCTYPE html><title>Content of a.html.</title>\n')
    assert_file_content_equals('./out/binary', 'aωb')