Beispiel #1
0
    async def test_files_attribute_expects_correctly_parsed(self):
        app = Vibora()

        @app.route('/', methods=['POST'])
        async def home(request: Request):
            uploaded_files = {}
            for file in await request.files():
                uploaded_files[file.filename] = await file.read()
            return JsonResponse(uploaded_files)

        with app.test_client() as client:
            response = await client.post(
                '/',
                form={
                    'a': FileUpload(content=b'a', name='a'),
                    'b': FileUpload(content=b'b', name='b'),
                    'c': FileUpload(content=b'c', name='c'),
                    'd': 1,
                },
            )
            self.assertEqual(response.status_code, 200)
            self.assertDictEqual(response.json(), {
                'a': 'a',
                'b': 'b',
                'c': 'c'
            })
Beispiel #2
0
    async def test_files_upload_expects_correctly_parsed(self):
        app = Vibora()

        @app.route('/', methods=['POST'])
        async def home(request: Request):
            form = await request.form()
            return JsonResponse({
                'a': await form['a'].read(),
                'b': await form['b'].read(),
                'c': await form['c'].read(),
                'd': form['d'],
            })

        with app.test_client() as client:
            response = await client.post(
                '/',
                form={
                    'a': FileUpload(content=b'a'),
                    'b': FileUpload(content=b'b'),
                    'c': FileUpload(content=b'c'),
                    'd': 1,
                },
            )
            self.assertEqual(response.status_code, 200)
            self.assertDictEqual(response.json(), {
                'a': 'a',
                'b': 'b',
                'c': 'c',
                'd': '1'
            })
Beispiel #3
0
    async def test_files_upload_expects_correctly_parsed(self):
        app = Vibora()

        @app.route("/", methods=["POST"])
        async def home(request: Request):
            form = await request.form()
            return JsonResponse(
                {
                    "a": (await form["a"].read()).decode(),
                    "b": (await form["b"].read()).decode(),
                    "c": (await form["c"].read()).decode(),
                    "d": form["d"],
                }
            )

        async with app.test_client() as client:
            response = await client.post(
                "/",
                form={
                    "a": FileUpload(content=b"a"),
                    "b": FileUpload(content=b"b"),
                    "c": FileUpload(content=b"c"),
                    "d": 1,
                },
            )
            self.assertEqual(response.status_code, 200)
            self.assertDictEqual(response.json(), {"a": "a", "b": "b", "c": "c", "d": "1"})
Beispiel #4
0
    async def test_file_upload_with_another_values(self):
        app = Vibora()

        @app.route('/', methods=['POST'])
        async def home(request: Request):
            form = (await request.form())
            return JsonResponse({'a': form['a'], 'b': await form['b'].read()})

        with app.test_client() as client:
            response = await client.post('/', form={'a': 1, 'b': FileUpload(content=b'uploaded_file')})
            self.assertDictEqual(response.json(), {'a': '1', 'b': 'uploaded_file'})
Beispiel #5
0
    async def test_files_attribute_expects_correctly_parsed(self):
        app = Vibora()

        @app.route("/", methods=["POST"])
        async def home(request: Request):
            uploaded_files = {}
            for file in await request.files():
                uploaded_files[file.filename] = (await file.read()).decode()
            return JsonResponse(uploaded_files)

        async with app.test_client() as client:
            response = await client.post(
                "/",
                form={
                    "a": FileUpload(content=b"a", name="a"),
                    "b": FileUpload(content=b"b", name="b"),
                    "c": FileUpload(content=b"c", name="c"),
                    "d": 1,
                },
            )
            self.assertEqual(response.status_code, 200)
            self.assertDictEqual(response.json(), {"a": "a", "b": "b", "c": "c"})
Beispiel #6
0
async def test_file_upload_with_another_values():
    app = Vibora()

    @app.route("/", methods=["POST"])
    async def home(request: Request):
        form = await request.form()
        return JsonResponse({
            "a": form["a"],
            "b": (await form["b"].read()).decode()
        })

    async with app.test_client() as client:
        response = await client.post("/",
                                     form={
                                         "a": 1,
                                         "b":
                                         FileUpload(content=b"uploaded_file")
                                     })
        assert response.json() == {"a": "1", "b": "uploaded_file"}