Skip to content

Instantly share code, notes, and snippets.

@majgis
Created November 28, 2012 21:08
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save majgis/4164503 to your computer and use it in GitHub Desktop.
Save majgis/4164503 to your computer and use it in GitHub Desktop.
Fake Django WSGIRequest Object
from django.core.handlers.wsgi import WSGIRequest
from StringIO import StringIO
from django.contrib.auth.models import AnonymousUser
def GetFakeRequest(path='/', user=None):
""" Construct a fake request(WSGIRequest) object"""
req = WSGIRequest({
'REQUEST_METHOD': 'GET',
'PATH_INFO': path,
'wsgi.input': StringIO()})
req.user = AnonymousUser() if user is None else user
return req
@AlfreddGco
Copy link

This is great! Thank you so much!

@jartaud
Copy link

jartaud commented Jun 28, 2021

Thanks

*py 3: from io import StringIO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment