本文整理匯總了Python中django.test.client.FakePayload方法的典型用法代碼示例。如果您正苦於以下問題:Python client.FakePayload方法的具體用法?Python client.FakePayload怎麽用?Python client.FakePayload使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.test.client
的用法示例。
在下文中一共展示了client.FakePayload方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: patch
# 需要導入模塊: from django.test import client [as 別名]
# 或者: from django.test.client import FakePayload [as 別名]
def patch(self, path, data={}, content_type=MULTIPART_CONTENT, **extra):
"""
Construct a PATCH request."
:param path:
:param data:
:param content_type:
:param extra:
:return:
"""
patch_data = self._encode_data(data, content_type)
parsed = urlparse(path)
r = {
'CONTENT_LENGTH': len(patch_data),
'CONTENT_TYPE': content_type,
'PATH_INFO': self._get_path(parsed),
'QUERY_STRING': parsed[4],
'REQUEST_METHOD': 'PATCH',
'wsgi.input': FakePayload(patch_data),
}
r.update(extra)
return self.request(**r)
示例2: compilesTo
# 需要導入模塊: from django.test import client [as 別名]
# 或者: from django.test.client import FakePayload [as 別名]
def compilesTo(self, input, expected_output):
"""
Asserts that the given template string compiles to the given output.
"""
input = '{% load seo %}' + input
environ = {
'PATH_INFO': self.path,
'REQUEST_METHOD': 'GET',
'wsgi.input': FakePayload(''),
}
# Create a fake request for our purposes
request = WSGIRequest(environ)
context = RequestContext(request)
context.update(self.context)
self.assertEqual(Template(input).render(context).strip(),
expected_output.strip())
示例3: _test_base64_upload
# 需要導入模塊: from django.test import client [as 別名]
# 或者: from django.test.client import FakePayload [as 別名]
def _test_base64_upload(self, content, encode=base64.b64encode):
payload = client.FakePayload("\r\n".join([
'--' + client.BOUNDARY,
'Content-Disposition: form-data; name="file"; filename="test.txt"',
'Content-Type: application/octet-stream',
'Content-Transfer-Encoding: base64',
'']))
payload.write(b'\r\n' + encode(content.encode()) + b'\r\n')
payload.write('--' + client.BOUNDARY + '--\r\n')
r = {
'CONTENT_LENGTH': len(payload),
'CONTENT_TYPE': client.MULTIPART_CONTENT,
'PATH_INFO': "/echo_content/",
'REQUEST_METHOD': 'POST',
'wsgi.input': payload,
}
response = self.client.request(**r)
self.assertEqual(response.json()['file'], content)
示例4: test_unicode_file_name_rfc2231
# 需要導入模塊: from django.test import client [as 別名]
# 或者: from django.test.client import FakePayload [as 別名]
def test_unicode_file_name_rfc2231(self):
"""
Test receiving file upload when filename is encoded with RFC2231
(#22971).
"""
payload = client.FakePayload()
payload.write('\r\n'.join([
'--' + client.BOUNDARY,
'Content-Disposition: form-data; name="file_unicode"; filename*=UTF-8\'\'%s' % quote(UNICODE_FILENAME),
'Content-Type: application/octet-stream',
'',
'You got pwnd.\r\n',
'\r\n--' + client.BOUNDARY + '--\r\n'
]))
r = {
'CONTENT_LENGTH': len(payload),
'CONTENT_TYPE': client.MULTIPART_CONTENT,
'PATH_INFO': "/unicode_name/",
'REQUEST_METHOD': 'POST',
'wsgi.input': payload,
}
response = self.client.request(**r)
self.assertEqual(response.status_code, 200)
示例5: test_truncated_multipart_handled_gracefully
# 需要導入模塊: from django.test import client [as 別名]
# 或者: from django.test.client import FakePayload [as 別名]
def test_truncated_multipart_handled_gracefully(self):
"""
If passed an incomplete multipart message, MultiPartParser does not
attempt to read beyond the end of the stream, and simply will handle
the part that can be parsed gracefully.
"""
payload_str = "\r\n".join([
'--' + client.BOUNDARY,
'Content-Disposition: form-data; name="file"; filename="foo.txt"',
'Content-Type: application/octet-stream',
'',
'file contents'
'--' + client.BOUNDARY + '--',
'',
])
payload = client.FakePayload(payload_str[:-10])
r = {
'CONTENT_LENGTH': len(payload),
'CONTENT_TYPE': client.MULTIPART_CONTENT,
'PATH_INFO': '/echo/',
'REQUEST_METHOD': 'POST',
'wsgi.input': payload,
}
self.assertEqual(self.client.request(**r).json(), {})
示例6: test_body_after_POST_multipart_form_data
# 需要導入模塊: from django.test import client [as 別名]
# 或者: from django.test.client import FakePayload [as 別名]
def test_body_after_POST_multipart_form_data(self):
"""
Reading body after parsing multipart/form-data is not allowed
"""
# Because multipart is used for large amounts of data i.e. file uploads,
# we don't want the data held in memory twice, and we don't want to
# silence the error by setting body = '' either.
payload = FakePayload("\r\n".join([
'--boundary',
'Content-Disposition: form-data; name="name"',
'',
'value',
'--boundary--'
'']))
request = WSGIRequest({'REQUEST_METHOD': 'POST',
'CONTENT_TYPE': 'multipart/form-data; boundary=boundary',
'CONTENT_LENGTH': len(payload),
'wsgi.input': payload})
self.assertEqual(request.POST, {'name': ['value']})
with self.assertRaises(RawPostDataException):
request.body
示例7: test_body_after_POST_multipart_related
# 需要導入模塊: from django.test import client [as 別名]
# 或者: from django.test.client import FakePayload [as 別名]
def test_body_after_POST_multipart_related(self):
"""
Reading body after parsing multipart that isn't form-data is allowed
"""
# Ticket #9054
# There are cases in which the multipart data is related instead of
# being a binary upload, in which case it should still be accessible
# via body.
payload_data = b"\r\n".join([
b'--boundary',
b'Content-ID: id; name="name"',
b'',
b'value',
b'--boundary--'
b''])
payload = FakePayload(payload_data)
request = WSGIRequest({'REQUEST_METHOD': 'POST',
'CONTENT_TYPE': 'multipart/related; boundary=boundary',
'CONTENT_LENGTH': len(payload),
'wsgi.input': payload})
self.assertEqual(request.POST, {})
self.assertEqual(request.body, payload_data)
示例8: test_POST_after_body_read_and_stream_read_multipart
# 需要導入模塊: from django.test import client [as 別名]
# 或者: from django.test.client import FakePayload [as 別名]
def test_POST_after_body_read_and_stream_read_multipart(self):
"""
POST should be populated even if body is read first, and then
the stream is read second. Using multipart/form-data instead of urlencoded.
"""
payload = FakePayload("\r\n".join([
'--boundary',
'Content-Disposition: form-data; name="name"',
'',
'value',
'--boundary--'
'']))
request = WSGIRequest({'REQUEST_METHOD': 'POST',
'CONTENT_TYPE': 'multipart/form-data; boundary=boundary',
'CONTENT_LENGTH': len(payload),
'wsgi.input': payload})
request.body # evaluate
# Consume enough data to mess up the parsing:
self.assertEqual(request.read(13), b'--boundary\r\nC')
self.assertEqual(request.POST, {'name': ['value']})
示例9: test_POST_immutable_for_mutipart
# 需要導入模塊: from django.test import client [as 別名]
# 或者: from django.test.client import FakePayload [as 別名]
def test_POST_immutable_for_mutipart(self):
"""
MultiPartParser.parse() leaves request.POST immutable.
"""
payload = FakePayload("\r\n".join([
'--boundary',
'Content-Disposition: form-data; name="name"',
'',
'value',
'--boundary--',
]))
request = WSGIRequest({
'REQUEST_METHOD': 'POST',
'CONTENT_TYPE': 'multipart/form-data; boundary=boundary',
'CONTENT_LENGTH': len(payload),
'wsgi.input': payload,
})
self.assertFalse(request.POST._mutable)
示例10: test_file_passes
# 需要導入模塊: from django.test import client [as 別名]
# 或者: from django.test.client import FakePayload [as 別名]
def test_file_passes(self):
payload = FakePayload("\r\n".join([
'--boundary',
'Content-Disposition: form-data; name="file1"; filename="test.file"',
'',
'value',
'--boundary--'
''
]))
request = WSGIRequest({
'REQUEST_METHOD': 'POST',
'CONTENT_TYPE': 'multipart/form-data; boundary=boundary',
'CONTENT_LENGTH': len(payload),
'wsgi.input': payload,
})
with self.settings(DATA_UPLOAD_MAX_MEMORY_SIZE=1):
request._load_post_and_files()
self.assertIn('file1', request.FILES, "Upload file not present")
示例11: setUp
# 需要導入模塊: from django.test import client [as 別名]
# 或者: from django.test.client import FakePayload [as 別名]
def setUp(self):
payload = FakePayload("\r\n".join([
'--boundary',
'Content-Disposition: form-data; name="name1"',
'',
'value1',
'--boundary',
'Content-Disposition: form-data; name="name2"',
'',
'value2',
'--boundary--'
''
]))
self.request = WSGIRequest({
'REQUEST_METHOD': 'POST',
'CONTENT_TYPE': 'multipart/form-data; boundary=boundary',
'CONTENT_LENGTH': len(payload),
'wsgi.input': payload,
})
示例12: compilesTo
# 需要導入模塊: from django.test import client [as 別名]
# 或者: from django.test.client import FakePayload [as 別名]
def compilesTo(self, input, expected_output):
""" Asserts that the given template string compiles to the given output.
"""
input = '{% load seo %}' + input
environ = {
'PATH_INFO': self.path,
'REQUEST_METHOD': 'GET',
'wsgi.input': FakePayload(''),
}
# Create a fake request for our purposes
request = WSGIRequest(environ)
context = RequestContext(request)
context.update(self.context)
self.assertEqual(Template(input).render(context).strip(), expected_output.strip())
示例13: _base_environ
# 需要導入模塊: from django.test import client [as 別名]
# 或者: from django.test.client import FakePayload [as 別名]
def _base_environ(self, **request):
'''
Override the default values for the wsgi environment variables.
'''
# This is a minimal valid WSGI environ dictionary, plus:
# - HTTP_COOKIE: for cookie support,
# - REMOTE_ADDR: often useful, see #8551.
# See http://www.python.org/dev/peps/pep-3333/#environ-variables
environ = {
'HTTP_COOKIE': self.cookies.output(header='', sep='; '),
'PATH_INFO': str('/'),
'REMOTE_ADDR': str('127.0.0.1'),
'REQUEST_METHOD': str('GET'),
'SCRIPT_NAME': str(''),
'SERVER_NAME': str('localhost'),
'SERVER_PORT': str('8000'),
'SERVER_PROTOCOL': str('HTTP/1.1'),
'wsgi.version': (1, 0),
'wsgi.url_scheme': str('http'),
'wsgi.input': FakePayload(b''),
'wsgi.errors': self.errors,
'wsgi.multiprocess': True,
'wsgi.multithread': True,
'wsgi.run_once': False,
}
environ.update(self.defaults)
environ.update(request)
return environ
示例14: encode_data
# 需要導入模塊: from django.test import client [as 別名]
# 或者: from django.test.client import FakePayload [as 別名]
def encode_data(self, http_method, path, data, content_type, **extra):
patch_data = self._encode_data(data, content_type)
parsed = urlparse(path)
request = {
"CONTENT_LENGTH": len(patch_data),
"CONTENT_TYPE": content_type,
"PATH_INFO": self._get_path(parsed),
"QUERY_STRING": parsed[4],
"REQUEST_METHOD": http_method,
"wsgi.input": FakePayload(patch_data),
}
request.update(extra)
return request
示例15: test_unicode_name_rfc2231
# 需要導入模塊: from django.test import client [as 別名]
# 或者: from django.test.client import FakePayload [as 別名]
def test_unicode_name_rfc2231(self):
"""
Test receiving file upload when filename is encoded with RFC2231
(#22971).
"""
payload = client.FakePayload()
payload.write(
'\r\n'.join([
'--' + client.BOUNDARY,
'Content-Disposition: form-data; name*=UTF-8\'\'file_unicode; filename*=UTF-8\'\'%s' % quote(
UNICODE_FILENAME
),
'Content-Type: application/octet-stream',
'',
'You got pwnd.\r\n',
'\r\n--' + client.BOUNDARY + '--\r\n'
])
)
r = {
'CONTENT_LENGTH': len(payload),
'CONTENT_TYPE': client.MULTIPART_CONTENT,
'PATH_INFO': "/unicode_name/",
'REQUEST_METHOD': 'POST',
'wsgi.input': payload,
}
response = self.client.request(**r)
self.assertEqual(response.status_code, 200)