#30101: Recommended middleware syntax fails for some testing cases (when not
using
Client)
-------------------------------------+-------------------------------------
Reporter: Theodore | Owner: nobody
Diamantidis |
Type: | Status: closed
Cleanup/optimization |
Component: Documentation | Version: 2.1
Severity: Normal | Resolution: invalid
Keywords: middleware, unit | Triage Stage: Accepted
test, requestfactory, request, |
layering |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):
* status: new => closed
* resolution: => invalid
Comment:
This usage isn't correct:
{{{
view = MyMiddleware(MyView)
}}}
It treats views and middlewares as if they were equivalent, which they are
not.
Django's `BaseHandler` provides a `_get_response()` method that uses the
`URLConf ` and `URLResolver` to determine the callback (i.e. view
function) and parameters to pass to the view.
([https://github.com/django/django/blob/d02b2aa11e5b6c351a9a2c0673c23569889f90d6/django/core/handlers/base.py#L100-L101
django/core/handlers/base.py].)
It's this `_get_response()` that sits at the center of the ''Middleware
Onion™''. You can't just pass in a view.
To simulate the full dispatch you'd need to do something like this, as a
minimum:
{{{
request = RequestFactory().get('/articles/4/')
def _get_response(request):
return MyView(request, pk=4)
middleware_chain = MyMiddleware(_get_response)
}}}
(Lots of ways we might make that better...)
I've thought about adding something on this to the
[https://docs.djangoproject.com/en/2.1/topics/http/middleware/ middleware
usage guide] but, by the time you're jumping through these hoops, I think
the best advice is to **use the provided test client** and let Django
handle the mapping between your middleware and views.
--
Ticket URL: <https://code.djangoproject.com/ticket/30101#comment:4>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/063.1672240d53f07dc07f3a3383a971afb1%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.