Hi all,

I am writing some unit tests using the django.test.client.Client to
send requests to my views. I'm using django 1.0 final

I am having problems when sending a post request though. Here is my
code:

post_content_type = "application/x-www-form-urlencoded"

data = {}
data['username'] = 'testuser'
data['email'] = '[EMAIL PROTECTED]'
data['password1'] = 'testpassword'
data['password2'] = 'testpassword2'

response = self.client.post(self.registration_url, data,
                                            self.post_content_type)

And the exception I get when I run the test:

======================================================================
ERROR: test_post_valid_form
(registration.unittests.views_tests.RegistrationViewsTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/....../registration/unittests/views_tests.py", line 34, in
test_post_valid_form
    self.post_content_type)
  File "/usr/lib/python2.5/site-packages/django/test/client.py", line
281, in post
    'wsgi.input':     FakePayload(post_data),
  File "/usr/lib/python2.5/site-packages/django/test/client.py", line
34, in __init__
    self.__content = StringIO(content)
TypeError: expected read buffer, dict found


I had a look at the client.py code and found that it does:

try:
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO

then, in the post method:

def post(self, path, data={}, content_type=MULTIPART_CONTENT,
**extra):
  if content_type is MULTIPART_CONTENT:
            post_data = encode_multipart(BOUNDARY, data)
  else:
            post_data = data

  r = {
           ...
           ...
            'wsgi.input':     FakePayload(post_data),
        }

Now in the FakePayload class we have :

def __init__(self, content):
        self.__content = StringIO(content)

which is line 34, pointed as the culprit in the exception trace


I tried from the interpreter to do:

>>>d = {}
>>>d['a'] = 'b'

>>>StringIO.StringIO(d)

and it works

If I then do

>>>cStringIO.StringIO(d)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: expected read buffer, dict found

My conclusion is that my client.py imports StringIO from cStringIO,
which doesn't accept a dict as content in its __init__ method. But I
can't be the only person using django to have cStringIO installed,
right? So someone would have seen and reported that problem already.

So, am I doing something wrong in creating my dict for the post data?

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to