spatcherMiddleware包装的Flask应用程序不再

spatcherMiddleware包装的Flask应用程序不再

本文介绍了使用DispatcherMiddleware包装的Flask应用程序不再具有test_client的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



 来包装应用程序, / code>  - 我们会得到类似于
AttributeError的错误:'DispatcherMiddleware'对象没有'test_client'属性。



有没有办法测试烧瓶应用的组成?



我们希望能做到这样的事情:

  cls .client = my_app.all_apps.test_client()

当 all_apps 是中间件:

  all_apps = DispatcherMiddleware(my_app,{
'/ backend':backend_app,
})


解决方案

一个Flask应用程序,包装并替换应用程序的 wsgi_app 属性。您正在使用对其他WSGI应用程序的引用来替换对Flask应用程序的引用,该应用程序显然不具有相同的属性。通过替换 wsgi_app ,您保留对Flask应用程序的引用,但更改支持该应用程序的WSGI可调用。



<$ p $ app.wsgi_app = DispatcherMiddleware(app.wsgi_app,{
'/ backend':backend_app
})


We can obtain test_client for sample application in way like:

class MyTestCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        my_app.app.config['TESTING'] = True
        cls.client = my_app.app.test_client()

However, if we wrap app with DispatcherMiddleware - we will get error like AttributeError: 'DispatcherMiddleware' object has no attribute 'test_client'.

Are there way to test composition of flask applications?

We want to be able to do something like:

cls.client = my_app.all_apps.test_client()

When all_apps is middleware like:

all_apps = DispatcherMiddleware(my_app, {
    '/backend': backend_app,
})
解决方案

To add WSGI middleware to a Flask app, wrap and replace the app's wsgi_app attribute. You're replacing the reference to the Flask app with a reference to some other WSGI app, which obviously won't have the same properties. By replacing wsgi_app, you retain the reference to the Flask app but change the WSGI callable that backs it.

app.wsgi_app = DispatcherMiddleware(app.wsgi_app, {
    '/backend': backend_app
})

这篇关于使用DispatcherMiddleware包装的Flask应用程序不再具有test_client的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 17:27