Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added paremeter user to MockHttpRequest. Added tests for http module. #12

Merged
merged 2 commits into from

2 participants

@alej0varas

Patch for issue #11

mock_django/http.py
@@ -54,6 +54,8 @@ def MockHttpRequest(path='/', method='GET', GET=None, POST=None, META=None):
'HTTP_REFERER': '',
'SERVER_NAME': 'testserver',
}
+ if user is not None:
+ user = user()
@dcramer Owner
dcramer added a note

Wouldn't you want to be able to pass a user instance?

Yes, it's better a instance. I'm going to change it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@dcramer dcramer commented on the diff
mock_django/http.py
@@ -54,6 +54,8 @@ def MockHttpRequest(path='/', method='GET', GET=None, POST=None, META=None):
'HTTP_REFERER': '',
'SERVER_NAME': 'testserver',
}
+ if user is not None:
@dcramer Owner
dcramer added a note

Probably makes more sense to set it to AnonymousUser() here :)

Removing it from WsgiHttpRequest? I don't wanted to change the WsgiHttpRequest code. If it's safe I can doit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@dcramer dcramer merged commit 1036ab1 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
1  .gitignore
@@ -3,3 +3,4 @@
*.egg-info/
/dist
/build
+*~
View
5 mock_django/http.py
@@ -40,7 +40,7 @@ def _set_raw_post_data(self, data):
raw_post_data = property(_get_raw_post_data, _set_raw_post_data)
-def MockHttpRequest(path='/', method='GET', GET=None, POST=None, META=None):
+def MockHttpRequest(path='/', method='GET', GET=None, POST=None, META=None, user=None):
if GET is None:
GET = {}
if POST is None:
@@ -54,6 +54,8 @@ def MockHttpRequest(path='/', method='GET', GET=None, POST=None, META=None):
'HTTP_REFERER': '',
'SERVER_NAME': 'testserver',
}
+ if user is not None:
@dcramer Owner
dcramer added a note

Probably makes more sense to set it to AnonymousUser() here :)

Removing it from WsgiHttpRequest? I don't wanted to change the WsgiHttpRequest code. If it's safe I can doit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ user = user
request = WsgiHttpRequest()
request.path = request.path_info = path
@@ -61,4 +63,5 @@ def MockHttpRequest(path='/', method='GET', GET=None, POST=None, META=None):
request.META = META
request.GET = GET
request.POST = POST
+ request.user = user
return request
View
0  tests/mock_django/http/__init__.py
No changes.
View
79 tests/mock_django/http/tests.py
@@ -0,0 +1,79 @@
+from unittest2 import TestCase
+from urllib import urlencode
+
+from django.contrib.auth.models import AnonymousUser
+from django.utils.datastructures import MergeDict
+
+from mock import Mock
+
+from mock_django.http import MockHttpRequest
+from mock_django.http import WsgiHttpRequest
+
+
+class WsgiHttpRequestTest(TestCase):
+ def test_instance(self):
+ wsgi_r = WsgiHttpRequest()
+
+ self.assertTrue(isinstance(wsgi_r.user, AnonymousUser))
+ self.assertEqual({}, wsgi_r.session)
+ self.assertEqual({}, wsgi_r.META)
+ self.assertEqual({}, wsgi_r.GET)
+ self.assertEqual({}, wsgi_r.POST)
+
+ def test__get_request(self):
+ wsgi_r = WsgiHttpRequest()
+ expected_items = MergeDict({}, {}).items()
+
+ wsgi_r.GET = {}
+ wsgi_r.POST = {}
+
+ self.assertListEqual(sorted(expected_items),
+ sorted(wsgi_r._get_request().items()))
+
+ def test_REQUEST_property(self):
+ self.assertTrue(isinstance(WsgiHttpRequest.REQUEST, property))
+
+ def test__get_raw_post_data(self):
+ wsgi_r = WsgiHttpRequest()
+
+ wsgi_r._get_raw_post_data()
+
+ self.assertEqual(urlencode({}), wsgi_r._raw_post_data)
+
+ def test__set_raw_post_data(self):
+ wsgi_r = WsgiHttpRequest()
+
+ wsgi_r._set_raw_post_data('')
+
+ self.assertEqual({}, wsgi_r.POST)
+ self.assertEqual(urlencode({}), wsgi_r._raw_post_data)
+
+ def test_raw_post_data_property(self):
+ self.assertTrue(isinstance(WsgiHttpRequest.raw_post_data, property))
+
+
+class MockHttpRequestTest(TestCase):
+ def test_call(self):
+ result = MockHttpRequest()
+ meta = {
+ 'REMOTE_ADDR': '127.0.0.1',
+ 'SERVER_PORT': '8000',
+ 'HTTP_REFERER': '',
+ 'SERVER_NAME': 'testserver',
+ }
+
+ self.assertTrue(isinstance(result, WsgiHttpRequest))
+ self.assertEqual('/', result.path)
+ self.assertEqual('GET', result.method)
+ self.assertEqual(meta, result.META)
+ self.assertEqual({}, result.GET)
+ self.assertEqual({}, result.POST)
+ self.assertTrue(isinstance(result.user, AnonymousUser))
+
+ def test_call(self):
+ class MockUser:
+ pass
+
+ result = MockHttpRequest(user=MockUser())
+
+ self.assertTrue(isinstance(result.user, MockUser))
Something went wrong with that request. Please try again.