mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Fixed #3160 -- Added the ability to control the content type in a test client POST request. This is to allow easier testing of json-rpc/xml-rpc/soap etc interfaces. Thanks to Mikeal Rogers for the suggestion, and Ben <afternoon@uk2.net> for the patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@4529 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -9,6 +9,9 @@ from django.http import urlencode, SimpleCookie
|
||||
from django.test import signals
|
||||
from django.utils.functional import curry
|
||||
|
||||
BOUNDARY = 'BoUnDaRyStRiNg'
|
||||
MULTIPART_CONTENT = 'multipart/form-data; boundary=%s' % BOUNDARY
|
||||
|
||||
class ClientHandler(BaseHandler):
|
||||
"""
|
||||
A HTTP Handler that can be used for testing purposes.
|
||||
@@ -184,19 +187,20 @@ class Client:
|
||||
|
||||
return self.request(**r)
|
||||
|
||||
def post(self, path, data={}, **extra):
|
||||
def post(self, path, data={}, content_type=MULTIPART_CONTENT, **extra):
|
||||
"Request a response from the server using POST."
|
||||
|
||||
BOUNDARY = 'BoUnDaRyStRiNg'
|
||||
if content_type is MULTIPART_CONTENT:
|
||||
post_data = encode_multipart(BOUNDARY, data)
|
||||
else:
|
||||
post_data = data
|
||||
|
||||
encoded = encode_multipart(BOUNDARY, data)
|
||||
stream = StringIO(encoded)
|
||||
r = {
|
||||
'CONTENT_LENGTH': len(encoded),
|
||||
'CONTENT_TYPE': 'multipart/form-data; boundary=%s' % BOUNDARY,
|
||||
'CONTENT_LENGTH': len(post_data),
|
||||
'CONTENT_TYPE': content_type,
|
||||
'PATH_INFO': path,
|
||||
'REQUEST_METHOD': 'POST',
|
||||
'wsgi.input': stream,
|
||||
'wsgi.input': StringIO(post_data),
|
||||
}
|
||||
r.update(extra)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user