diff options
| author | tom christie tom@tomchristie.com | 2011-02-04 21:52:21 +0000 |
|---|---|---|
| committer | tom christie tom@tomchristie.com | 2011-02-04 21:52:21 +0000 |
| commit | fcd7f414c407c8dab7c0badbfa16476826dad3ae (patch) | |
| tree | 4d532d354e5f63b0ab31212d2dfc26d937c2720d /djangorestframework/tests/utils.py | |
| parent | eebcdc4dc0e3b34fa76eca638c469b1e79240844 (diff) | |
| download | django-rest-framework-fcd7f414c407c8dab7c0badbfa16476826dad3ae.tar.bz2 | |
Huge stack of refactoring getting stuff into Mixin classes, and loads of tests. Kickass.
Diffstat (limited to 'djangorestframework/tests/utils.py')
| -rw-r--r-- | djangorestframework/tests/utils.py | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/djangorestframework/tests/utils.py b/djangorestframework/tests/utils.py new file mode 100644 index 00000000..ef0cb59c --- /dev/null +++ b/djangorestframework/tests/utils.py @@ -0,0 +1,40 @@ +from django.test import Client +from django.core.handlers.wsgi import WSGIRequest + +# From: http://djangosnippets.org/snippets/963/ +# Lovely stuff +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) |
