Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Initial work on MockHttpRequest

  • Loading branch information...
commit d9648ff92adf2da3ee743beb3498571362d5cf04 1 parent 61c14f9
David Cramer authored February 27, 2012

Showing 1 changed file with 64 additions and 0 deletions. Show diff stats Hide diff stats

  1. 64  mock_django/http.py
64  mock_django/http.py
... ...
@@ -0,0 +1,64 @@
  1
+"""
  2
+mock_django.http
  3
+~~~~~~~~~~~~~~~~
  4
+
  5
+:copyright: (c) 2012 DISQUS.
  6
+:license: Apache License 2.0, see LICENSE for more details.
  7
+"""
  8
+
  9
+from mock import Mock
  10
+from django.contrib.auth.models import AnonymousUser
  11
+from django.http import HttpRequest
  12
+from django.utils.datastructures import MergeDict
  13
+from urllib2 import urlencode
  14
+
  15
+__all__ = ('MockHttpRequest',)
  16
+
  17
+
  18
+class WsgiHttpRequest(HttpRequest):
  19
+    def __init__(self, *args, **kwargs):
  20
+        super(MockHttpRequest, self).__init__(*args, **kwargs)
  21
+        self.user = AnonymousUser()
  22
+        self.session = {}
  23
+        self.url = '/'
  24
+
  25
+    def _get_request(self):
  26
+        if not hasattr(self, '_request'):
  27
+            self._request = MergeDict(self.POST, self.GET)
  28
+        return self._request
  29
+    REQUEST = property(_get_request)
  30
+
  31
+    def build_absolute_uri(self):
  32
+        return self.url
  33
+
  34
+    def _get_raw_post_data(self):
  35
+        if not hasattr(self, '_raw_post_data'):
  36
+            self._raw_post_data = urlencode(self.POST)
  37
+        return self._raw_post_data
  38
+
  39
+    def _set_raw_post_data(self, data):
  40
+        self._raw_post_data = data
  41
+        self.POST = {}
  42
+    raw_post_data = property(_set_raw_post_data)
  43
+
  44
+
  45
+def MockHttpRequest(url='/', method='GET', GET=None, POST=None, META=None):
  46
+    if GET is None:
  47
+        GET = {}
  48
+    if POST is None:
  49
+        POST = {}
  50
+    else:
  51
+        method = 'POST'
  52
+    if META is None:
  53
+        META = {
  54
+            'REMOTE_ADDR': '127.0.0.1',
  55
+            'HTTP_REFERER': '',
  56
+        }
  57
+
  58
+    request = Mock(spec=WsgiHttpRequest())
  59
+    request.url = url
  60
+    request.method = method
  61
+    request.META = META
  62
+    request.GET = GET
  63
+    request.POST = POST
  64
+    return request

0 notes on commit d9648ff

Please sign in to comment.
Something went wrong with that request. Please try again.