Hi,

I have a view which processes a multi-part form and whose behaviour
varies depending on the content types of the uploaded files. I've
written some tests for that view as follows:

        post_data = {
            'name1': 'blah',
            'file_field1': image_data,
        }
        response = self.client.post('/upload/', post_data)

But the problem is that (apparently by design) all files are
systematically encoded with the content type 'application/octet-
stream'.

I have found a way around that, but only by writing a very long piece
of code to create a customised request from scratch:

        payload = []
        payload.extend([
                '--' + client.BOUNDARY,
                'Content-Disposition: form-data; name="name1"',
                '',
                'blah'
            ])
        payload.extend([
                '--' + client.BOUNDARY,
                'Content-Disposition: form-data; name="file_field1";
filename="image1.png"',
                'Content-Type: image/png',
                '',
                image_data
            ])
        payload.extend([
                '--' + client.BOUNDARY + '--',
                '',
            ])
        payload = "\r\n".join(payload)

        r = {
            'CONTENT_LENGTH': len(payload),
            'CONTENT_TYPE':   client.MULTIPART_CONTENT,
            'PATH_INFO':      "/upload/",
            'REQUEST_METHOD': 'POST',
            'wsgi.input':     client.FakePayload(payload),
        }
        response = self.client.request(**r)

Is there a more concise or more elegant way to do?

Thanks a lot for your help,

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

Reply via email to