Flask 和 Werkzeug:使用自定义标头测试发布请求 [英] Flask and Werkzeug: Testing a post request with custom headers

查看:23
本文介绍了Flask 和 Werkzeug:使用自定义标头测试发布请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在根据 http://flask.pocoo.org/docs 的建议测试我的应用/testing/,但我想向发布请求添加标头.

I'm currently testing my app with suggestions from http://flask.pocoo.org/docs/testing/, but I would like to add a header to a post request.

我的请求目前是:

self.app.post('/v0/scenes/test/foo', data=dict(image=(StringIO('fake image'), 'image.png')))

但我想在请求中添加一个 content-md5.这可能吗?

but I would like to add a content-md5 to the request. Is this possible?

我的调查:

Flask 客户端(在flask/testing.py 中)扩展了Werkzeug 的客户端,记录在这里:http://werkzeug.pocoo.org/docs/test/

Flask Client (in flask/testing.py) extends Werkzeug's Client, documented here: http://werkzeug.pocoo.org/docs/test/

如您所见,post 使用 open.但是open只有:

As you can see, post uses open. But open only has:

Parameters: 
 as_tuple – Returns a tuple in the form (environ, result)
 buffered – Set this to True to buffer the application run. This will automatically close the application for you as well.
 follow_redirects – Set this to True if the Client should follow HTTP redirects.

所以它看起来不受支持.但是,我如何才能使这样的功能正常工作?

So it looks like it's not supported. How might I get such a feature working, though?

推荐答案

open 也把 *args**kwargs 用作EnvironBuilder 参数.所以你可以在你的第一个 post 请求中添加 headers 参数:

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'});

这篇关于Flask 和 Werkzeug:使用自定义标头测试发布请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆