0

I am trying to overwrite HTTP_POST to certain string of a request object inside a view:

with patch('django.core.handlers.wsgi.WSGIRequest') as request:
    request.META = {'HTTP_HOST': 'www.abc.com'}
    resp = self.client.get('/')

But obviously it's not working since it's complaining HTTP_POST KeyError

James Lin
  • 25,028
  • 36
  • 133
  • 233

1 Answers1

1

Rather than using a mock object, you should probably use django.test.client.RequestFactory to generate a request object.

oxfn
  • 6,590
  • 2
  • 26
  • 34
knbk
  • 52,111
  • 9
  • 124
  • 122
  • I don't have a particular view to test since this is a library, I just want to test the middleware, but looks like I might just test the middleware directly without going through the request – James Lin Mar 31 '15 at 00:21
  • You'd still need a `request` object. Another option is to simply use `django.http.request.HttpRequest`, which is easier to construct than a `WSGIRequest`. Both methods are specifically tailored to testing, and are frequently used throughout Django's test suite. – knbk Mar 31 '15 at 00:39