Flask and Werkzeug: Testing a post request with custom headers

烂漫一生 提交于 2019-11-28 11:52:38

open also take *args and **kwargs which used as EnvironBuilder arguments. So you can add just headers argument to your first post request:

with self.app.test_client() as client:
    client.post('/v0/scenes/test/foo',
                data=dict(image=(StringIO('fake image'), 'image.png')),
                headers={'content-md5': 'some hash'});

Werkzeug to the rescue!

from werkzeug.test import EnvironBuilder, run_wsgi_app
from werkzeug.wrappers import Request

builder = EnvironBuilder(path='/v0/scenes/bucket/foo', method='POST', data={'image': (StringIO('fake image'), 'image.png')}, \
    headers={'content-md5': 'some hash'})
env = builder.get_environ()

(app_iter, status, headers) = run_wsgi_app(http.app.wsgi_app, env)
status = int(status[:3]) # output will be something like 500 INTERNAL SERVER ERROR
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!