svanoort / pyresttest

Python Rest Testing
Apache License 2.0
1.15k stars 325 forks source link

Can you upload multi-part form data? #158

Open pinventado opened 8 years ago

pinventado commented 8 years ago

Does pyresttest allow tests on the submission of multi-part form data? Specifically, sending some values and an image file. This is how I send the request via cURL:

curl -X POST -F "data1=value1" -F "data2=value2" -F "file=@1.gif" "http://localhost:5000/endpoint"
svanoort commented 8 years ago

Hi @pinventado, thank you for reaching out with your question. PyRestTest doesn't have any explicit support for multipart form data, but it is possible to construct one manually and specify it as the post body.

This is similar to constructing a manual form request: https://github.com/svanoort/pyresttest/tree/master/examples/dummyapp-posting-forms.yaml

There's a standing enhancement request for greater support of form data and other custom request bodies, in https://github.com/svanoort/pyresttest/issues/115.

I'll go ahead and convert this to a proposed enhancement to tackle in the next couple releases.

pinventado commented 8 years ago

Ok, looking forward to see this implemented. In case anyone is interested in testing multi-part form data, here's what I did:

Create a file with the multipart-form data. For example, store this into formdata.txt:

--------------------------4935875673ad6222
Content-Disposition: form-data; name="username"
george
--------------------------4935875673ad6222
Content-Disposition: form-data; name="file"; filename="image.jpg"
Content-Type: image/jpeg
Content-Transfer-Encoding: base64

/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDA <rest of base64 string>
--------------------------4935875673ad6222--

Set the appropriate headers and load the data into the body of the request.

- test:
  - group: "Group test"
  - name: "Test name"
  - url: "/endpoint"
  - method: "POST"
  - headers: {'Content-Type': 'multipart/form-data; boundary=------------------------4935875673ad6222'}
  - body: {'file':'formdata.txt'}
  - expected_status: [200]

You could place the multipart/form-data directly in the YAML file, but it takes up a lot of space and the extra line spacing requirements make it look messy.

svanoort commented 8 years ago

Clue - https://automatthias.wordpress.com/2013/04/07/http-put-with-multipartform-data-using-pycurl/