как передать почтовый запрос с несколькими файлами в метод сокола simulate_post - PullRequest
0 голосов
/ 05 февраля 2020

У меня есть запрос curl для сокола API, для которого я хочу создать юнит-тест. Я могу успешно вызвать API с помощью следующей команды curl.

curl -F "file1=@path/to/file1.wav" -F "file2=@path/to/wavfile2.wav" -F "metajson=@path/to/meta.json" http://0.0.0.0:8000/upload_session

Я могу получить доступ к двоичным файлам через атрибут req.params:

class SessionUploader(object):
    def on_post(self, req, resp):
        print(req.params['file1'].filename)  # filename of file1
        print(req.params['file1'].value)  # binary of file1
        print(req.params['file2'].filename)  # filename of file2
        print(req.params['file2'].value)  # binary of file2
        do_work_with_files(req.params)
        resp.status = falcon.HTTP_200


def create():
    # falcon.API instances are callable WSGI apps
    app = falcon.API(middleware=[MultipartMiddleware()])

    # things will handle all requests to the '/things' URL path
    things = ThingsResource()
    app.add_route('/things', things)

    # uploader will handle all requests to the '/upload_session' URL path
    uploader = SessionUploader()
    app.add_route('/upload_session', uploader)
    return app

app = create()

Я пытался следовать примеру тестирования в документации по соколу , но не уверен, как сопоставить вызов curl интерфейсу сокола:

import unittest

from falcon import testing
from speechmetrics.api import api
from speechmetrics.utils import unittest_utils


class ApiTests(testing.TestCase):
    def setUp(self):
        super().setUp()
        self.app = api.create()


class TestApi(ApiTests):

    def test_post(self):
        files = get_files()  # <======== function that package the files

        result = self.simulate_post('/upload_session', body=files)
        self.assertTrue(result.status=='200, 'api has failed')


if __name__ == '__main__':
    unittest.main()
...