RequestFactory: Easily create mock request objects, for use in testing

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from django.test import Client
from django.core.handlers.wsgi import WSGIRequest

class RequestFactory(Client):
    """
    Class that lets you create mock Request objects for use in testing.
    
    Usage:
    
    rf = RequestFactory()
    get_request = rf.get('/hello/')
    post_request = rf.post('/submit/', {'foo': 'bar'})
    
    This class re-uses the django.test.client.Client interface, docs here:
    http://www.djangoproject.com/documentation/testing/#the-test-client
    
    Once you have a request object you can pass it to any view function, 
    just as if that view had been hooked up using a URLconf.
    
    """
    def request(self, **request):
        """
        Similar to parent class, but returns the request object as soon as it
        has created it.
        """
        environ = {
            'HTTP_COOKIE': self.cookies,
            'PATH_INFO': '/',
            'QUERY_STRING': '',
            'REQUEST_METHOD': 'GET',
            'SCRIPT_NAME': '',
            'SERVER_NAME': 'testserver',
            'SERVER_PORT': 80,
            'SERVER_PROTOCOL': 'HTTP/1.1',
        }
        environ.update(self.defaults)
        environ.update(request)
        return WSGIRequest(environ)

More like this

  1. Bulk Insert - updated 5/9/2008 by coolie 6 years, 6 months ago
  2. create and authenticate an anonymous user by chr15m 4 years, 7 months ago
  3. Modify requests in your unit tests (improvement on RequestFactory) by vaughnkoch 3 years, 6 months ago
  4. object-oriented generic views by carljm 5 years, 7 months ago
  5. FirstRun Middleware by TheMysteriousX 2 years, 11 months ago

Comments

akaihola (on February 16, 2009):

What would be a good way to have request.user included? Just assign a User object to the created request? It would be nice to have AnonymousUser there by default.

#

akaihola (on February 16, 2009):

Answering myself, replace the last line with the following:

        request = WSGIRequest(environ)
        handler = BaseHandler()
        handler.load_middleware()
        for middleware_method in handler._request_middleware:
            if middleware_method(request):
                raise Exception("Couldn't create request mock object - "
                                "request middleware returned a response")
        return request

#

peterbe (on September 29, 2009):

Is this still sufficient? You cut away large chunks of Client.request I notice by checking the SVN version.

#

cooncesean (on February 24, 2011):

Wondering if there's any solution for sending this request to a view that has the @csrf_protect_m decorator.

#

cooncesean (on February 24, 2011):

@cooncesean: this looks like it works ->

....

request.csrf_processing_done = True

....

#

(Forgotten your password?)