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

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

问题描述

我目前正在使用来自 http://flask.pocoo.org/docs / testing / ,但是我想添加一个标题到一个post请求。

我的请求是:

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

但是我想添加一个content-md5到请求。这是可能的吗?



我的调查:

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

正如您所看到的, post 使用。但是打开只有:

$ $ $ $ $ $ $ $ $ $ $ $ $ $ as_tuple - 以(environ,result)形式返回元组
buffered - 将其设置为True以缓冲应用程序运行。这也会自动关闭你的应用程序。
follow_redirects - 如果客户端应该遵循HTTP重定向,则将其设置为True。

所以看起来不支持。但是我怎么能得到这样一个功能,虽然?

解决方案

open 还可以用 * args ** kwargs 来作为 EnvironBuilder 参数。因此,您可以在您的第一个发布请求中添加标头参数:

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


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.

My request is currently:

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

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

My investigations:

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

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 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天全站免登陆