-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Requests client #4406
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
Closed
Closed
Requests client #4406
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
08c7853
Start test case
lovelydinosaur 5abac93
Merge branch 'master' into requests-client
lovelydinosaur 3d1fff3
Added 'requests' test client
lovelydinosaur e76ca6e
Address typos
lovelydinosaur 6ede654
Graceful fallback if requests is not installed.
lovelydinosaur 049a39e
Add cookie support
lovelydinosaur 64e19c7
Tests for auth and CSRF
lovelydinosaur da47c34
Py3 compat
lovelydinosaur 5311769
py3 compat
lovelydinosaur 0b3db02
py3 compat
lovelydinosaur 0cc3f50
Add get_requests_client
lovelydinosaur 37b3475
API client (#4424)
lovelydinosaur File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Added 'requests' test client
- Loading branch information
commit 3d1fff3f26835612be17b6624d766a56520880ec
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -4,7 +4,10 @@ | |
| # to make it harder for the user to import the wrong thing without realizing. | ||
| from __future__ import unicode_literals | ||
|
|
||
| import io | ||
|
|
||
| from django.conf import settings | ||
| from django.core.handlers.wsgi import WSGIHandler | ||
| from django.test import testcases | ||
| from django.test.client import Client as DjangoClient | ||
| from django.test.client import RequestFactory as DjangoRequestFactory | ||
|
|
@@ -13,6 +16,10 @@ | |
| from django.utils.encoding import force_bytes | ||
| from django.utils.http import urlencode | ||
| from requests import Session | ||
| from requests.adapters import BaseAdapter | ||
| from requests.models import Response | ||
| from requests.structures import CaseInsensitiveDict | ||
| from requests.utils import get_encoding_from_headers | ||
|
|
||
| from rest_framework.settings import api_settings | ||
|
|
||
|
|
@@ -22,6 +29,83 @@ def force_authenticate(request, user=None, token=None): | |
| request._force_auth_token = token | ||
|
|
||
|
|
||
| class DjangoTestAdapter(BaseAdapter): | ||
| """ | ||
| A transport adaptor for `requests`, that makes requests via the | ||
| Django WSGI app, rather than making actual HTTP requests ovet the network. | ||
|
||
| """ | ||
| def __init__(self): | ||
| self.app = WSGIHandler() | ||
| self.factory = DjangoRequestFactory() | ||
|
|
||
| def get_environ(self, request): | ||
| """ | ||
| Given a `requests.PreparedRequest` instance, return a WSGI environ dict. | ||
| """ | ||
| method = request.method | ||
| url = request.url | ||
| kwargs = {} | ||
|
|
||
| # Set request content, if any exists. | ||
| if request.body is not None: | ||
| kwargs['data'] = request.body | ||
| if 'content-type' in request.headers: | ||
| kwargs['content_type'] = request.headers['content-type'] | ||
|
|
||
| # Set request headers. | ||
| for key, value in request.headers.items(): | ||
| key = key.upper() | ||
| if key in ('CONNECTION', 'CONTENT_LENGTH', 'CONTENT-TYPE'): | ||
| continue | ||
| kwargs['HTTP_%s' % key] = value | ||
|
|
||
| return self.factory.generic(method, url, **kwargs).environ | ||
|
|
||
| def send(self, request, *args, **kwargs): | ||
| """ | ||
| Make an outgoing request to the Django WSGI application. | ||
| """ | ||
| response = Response() | ||
|
|
||
| def start_response(status, headers): | ||
| status_code, _, reason_phrase = status.partition(' ') | ||
| response.status_code = int(status_code) | ||
| response.reason = reason_phrase | ||
| response.headers = CaseInsensitiveDict(headers) | ||
| response.encoding = get_encoding_from_headers(response.headers) | ||
|
|
||
| environ = self.get_environ(request) | ||
| raw_bytes = self.app(environ, start_response) | ||
|
|
||
| response.request = request | ||
| response.url = request.url | ||
| response.raw = io.BytesIO(b''.join(raw_bytes)) | ||
|
|
||
| return response | ||
|
|
||
| def close(self): | ||
| pass | ||
|
|
||
|
|
||
| class DjangoTestSession(Session): | ||
| def __init__(self, *args, **kwargs): | ||
| super(DjangoTestSession, self).__init__(*args, **kwargs) | ||
|
|
||
| adapter = DjangoTestAdapter() | ||
| hostnames = list(settings.ALLOWED_HOSTS) + ['testserver'] | ||
|
|
||
| for hostname in hostnames: | ||
| if hostname == '*': | ||
| hostname = '' | ||
| self.mount('http://%s' % hostname, adapter) | ||
| self.mount('https://%s' % hostname, adapter) | ||
|
|
||
| def request(self, method, url, *args, **kwargs): | ||
| if ':' not in url: | ||
| url = 'http://testserver/' + url.lstrip('/') | ||
| return super(DjangoTestSession, self).request(method, url, *args, **kwargs) | ||
|
|
||
|
|
||
| class APIRequestFactory(DjangoRequestFactory): | ||
| renderer_classes_list = api_settings.TEST_REQUEST_RENDERER_CLASSES | ||
| default_format = api_settings.TEST_REQUEST_DEFAULT_FORMAT | ||
|
|
@@ -224,7 +308,7 @@ class APITestCase(testcases.TestCase): | |
|
|
||
| def _pre_setup(self): | ||
| super(APITestCase, self)._pre_setup() | ||
| self.requests = Session() | ||
| self.requests = DjangoTestSession() | ||
|
|
||
|
|
||
| class APISimpleTestCase(testcases.SimpleTestCase): | ||
|
|
||
This file contains hidden or 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
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.
Seems like typo:
"adaptor" -> "adapter"