Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask app wrapped with DispatcherMiddleware no longer has test_client

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,
})
like image 299
Nikolay Fominyh Avatar asked Mar 25 '16 12:03

Nikolay Fominyh


1 Answers

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.wsgi_app,
})
like image 194
davidism Avatar answered Sep 28 '22 00:09

davidism