为Flask应用程序单元测试设置(模拟)请求标头 [英] Setting (mocking) request headers for Flask app unit test

查看:470
本文介绍了为Flask应用程序单元测试设置(模拟)请求标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有没有人知道在单元测试中设置(模拟)由FLask(Werkzeug)提供的请求对象的User-Agent的方法?当我尝试获取request.headers ['User-Agent']等细节时,会引发KeyError,因为Flask test_client()不会设置这些值。 (请参阅下面的部分堆栈跟踪)

在单元测试期间尝试从Flask项目中的请求对象中获取User-Agent时,会引发KeyError。

 文件/Users/me/app/rest/app.py,第515行,在登录
中,如果request.headers ['User-Agent']:
文件/Users/me/.virtualenvs/app/lib/python2.7/site-packages/werkzeug/datastructures.py,行1229,在__getitem__
返回self.environ ['HTTP_'+ key]
KeyError:'HTTP_USER_AGENT'

- - 更新 -



随着下面的(接受的)解决方案,environ_base提示将我带到这个 SO解决方案。此解决方案的前提是为Flask应用程序创建一个包装类,并覆盖调用方法来自动设置环境变量。这样,所有的调用都设置了变量。所以,我最终实现的解决方案是创建这个代理类:

$ p $ class FlaskTestClientProxy(object):
def __init__ (self,app):
self.app = app

def __call __(self,environ,start_response):
environ ['REMOTE_ADDR'] = environ.get('REMOTE_ADDR ','127.0.0.1')
environ ['HTTP_USER_AGENT'] = environ.get('HTTP_USER_AGENT','Chrome')
返回self.app(environ,start_response)

然后使用该代理包装WSGI容器:

  app.wsgi_app = FlaskTestClientProxy(app.wsgi_app)
test_client = app.test_client()


当您调用 get()时,您需要传入 environ_base post()。例如,

  client = app.test_client()
response = client.get('/ your / url /' ,
environ_base = {'HTTP_USER_AGENT':'Chrome等'))

你可以通过 request.headers ['User-Agent'] 来访问它。 c>。



请参阅 http:// werkzeug。 pocoo.org/docs/test/#testing-api 了解更多信息。


Does anyone know of a way to set (mock) the User-Agent of the request object provided by FLask (Werkzeug) during unit testing?

As it currently stands, when I attempt to obtain details such as the request.headers['User-Agent'] a KeyError is raised as the Flask test_client() doesn't set these up. (See partial stack trace below)

When attempting to get the User-Agent from the request object in a Flask project during unit testing, a KeyError is raised.

File "/Users/me/app/rest/app.py", line 515, in login
    if request.headers['User-Agent']:
File "/Users/me/.virtualenvs/app/lib/python2.7/site-packages/werkzeug/datastructures.py", line 1229, in __getitem__
    return self.environ['HTTP_' + key]
    KeyError: 'HTTP_USER_AGENT'

-- UPDATE --

Along with the (accepted) solution below, the environ_base hint lead me to this other SO solution. The premise of this solution is to create a wrapper class for the Flask app and override the call method to automatically set the environment variables. This way, the variables are set for all calls. So, the solution I ended up implementing is creating this proxy class:

class FlaskTestClientProxy(object):
    def __init__(self, app):
        self.app = app

    def __call__(self, environ, start_response):
        environ['REMOTE_ADDR'] = environ.get('REMOTE_ADDR', '127.0.0.1')
        environ['HTTP_USER_AGENT'] = environ.get('HTTP_USER_AGENT', 'Chrome')
        return self.app(environ, start_response)

And then wrapping the WSGI container with that proxy:

app.wsgi_app = FlaskTestClientProxy(app.wsgi_app)
test_client = app.test_client()

解决方案

You need to pass in environ_base when you call get() or post(). E.g.,

client = app.test_client()
response = client.get('/your/url/', 
                      environ_base={'HTTP_USER_AGENT': 'Chrome, etc'})

Then your request.user_agent should be whatever you pass in, and you can access it via request.headers['User-Agent'].

See http://werkzeug.pocoo.org/docs/test/#testing-api for more info.

这篇关于为Flask应用程序单元测试设置(模拟)请求标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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