Preprocess_request in Sanic

I am wondering if there is a way to step into the request and response cycle, scenarios like constructing some tests using Unittest, to step into the request method, and do some validation.

with app.test_request_context('/test'):
        app.preprocess_request()
        # Do some stuff with the request

Hi there,

Have you check the document of middleware before? I think this what you are looking for.

yep, maybe I didn’t describe my problem clearly and I had read through the middleware documentation before. The thing is that in my code, I had setup two middleware, one in the request middleware and one in response middleware, and in the request middleware, I add something to the queue and dequeue in the response middleware. And in my unittest code, I want to validate some code logic inside the request middleware. Maybe I can setup another middleware in my unittest,?(depending on the executed order)

Oh, got your point.

I think in your case, use another middleware is an easier way to validate both request and response middleware. Maybe try to assert the content of your queue in this test middleware like:

yep, solved it already. Thanks for your help, Great community.