How to set multipart / form-data part headers using django.test.Client?

Each part of the document multipart/form-datamay have its own headings, for example. a section may have a title Content-Type: text/plain. These parts can be downloaded from a web form, for example.

In the documentation for the Django class,UploadedFile I read

UploadedFile.content_type

The content header to be uploaded to the file (for example, text / regular or application / pdf). Like any data provided by the user, you should not trust that the downloaded file is actually a type. You still need to verify that the file contains content that the title of the content header is “trust but verify.”

Well, I have to check the file against the declared content type. So of course, now I need to write some tests that check to see if my server really checks the type of content correctly. One such test would be to make a request to my server using content-type: multipart/form-datawhere at least one part has content that does not match its content type.

How can i do this? The django.test.Client class has a methodpost that can send requests with a type multipart/form-data. Several parts of the request body are passed to the method as a dictionary. The keys for this dictionary are strings, and the values ​​are either strings or “file objects”.

I want to understand:

  • how this dictionary is converted to the request body multipart/form-data. What are the headings of each piece?
  • . , , Content-Type: text/plain?
+4
2
  • Django , application/octet-stream . , , encode_file .
  • : mimetype, content_type . , , Content-Type.
+3

Client django.test.client.RequestFactory (src) , Client post super, RequestFactory . _ encode_data, encode_multipart, , , "multipart/form-data".

, (, Content-Disposition), Content-Type ( ) .

+2

All Articles