New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added paremeter user to MockHttpRequest. Added tests for http module. #12
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
*.egg-info/ | ||
/dist | ||
/build | ||
*~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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=AnonymousUser): | ||
if GET is None: | ||
GET = {} | ||
if POST is None: | ||
|
@@ -54,11 +54,14 @@ def MockHttpRequest(path='/', method='GET', GET=None, POST=None, META=None): | |
'HTTP_REFERER': '', | ||
'SERVER_NAME': 'testserver', | ||
} | ||
if user is not None: | ||
user = user() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't you want to be able to pass a user instance? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it's better a instance. I'm going to change it. |
||
|
||
request = WsgiHttpRequest() | ||
request.path = request.path_info = path | ||
request.method = method | ||
request.META = META | ||
request.GET = GET | ||
request.POST = POST | ||
request.user = user | ||
return request |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
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): | ||
mock_user = Mock() | ||
|
||
result = MockHttpRequest(user=mock_user) | ||
|
||
self.assertTrue(isinstance(result.user, Mock)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably makes more sense to set it to AnonymousUser() here :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing it from WsgiHttpRequest? I don't wanted to change the WsgiHttpRequest code. If it's safe I can doit.