13

I'm trying to make a load test for Django-based website.

I use Locust 0.7.3 and python 2.7.10

Here I make POST - filling the form and attaching some file:

class WebsiteTasks(TaskSet):
    def on_start(self):
        self.client.get("/")

    @task
    def submit(self):
        response = self.client.get("/submit/")
        csrftoken = response.cookies['csrftoken']
        attach = open('file.pdf', 'rb')

        r = self.client.post("/submit/", {
           'csrfmiddlewaretoken': csrftoken,
           'password': smart_str(u'wkefjgui'),
           'payload': smart_str(u'kjsdgfljdsh'),
           'docfile': attach,
           'commit': smart_str(u'Вкрапить / Embed'),
        })

Everything's seemed to be ok, but on the server's upload folder there's no file!

What I'm doing wrong?

heyman
  • 4,845
  • 3
  • 26
  • 19
Nikolay Matkheev
  • 375
  • 1
  • 2
  • 10

3 Answers3

11

Well, I found the solution and I hope it will be useful for someone:

Here was described how Django handles file: How to send a "multipart/form-data" with requests in python?

And recipe is to define 'files' param in post function:

    r = self.client.post("/submit/", data={
        'csrfmiddlewaretoken': csrftoken,
        'password': smart_str(u'wkefjgui'),
        'payload': smart_str(u'kjsdgfljdsh'),
        'commit': smart_str(u'Вкрапить / Embed'),
         }, files={'docfile': attach})
Community
  • 1
  • 1
Nikolay Matkheev
  • 375
  • 1
  • 2
  • 10
3

handle multipart file

 def _get_image_part(self, file_path, file_content_type='image/jpeg'):
        import os
        file_name = os.path.basename(file_path)
        file_content = open(file_path, 'rb')
        return file_name, file_content, file_content_type

multipart test case


class OpenDeviceFrontApi(TaskSet):

    @task(2)
    def rec_log_upload(self):
        payload = {
            "device_key": device_key
        }
        files = {
            "scene_img": self._get_image_part("data/face/rec1.jpg"),
            "face_img": self._get_image_part("data/face/rec2.jpg")
        }
        r = self.client.post("/log/rec_log_upload", data=payload, files=files, verify=False)
        assert r.status_code == 200
        rData = json.loads(r.text, encoding="utf-8")

geosmart
  • 518
  • 4
  • 15
0

How to test a file upload in Locust over Django Server:

def post_img(self):
    files = {'media': open('img.png', 'rb')}
    response=self.client.post("/upload",files=files)
    print('Response is -: ',response)
mousetail
  • 7,009
  • 4
  • 25
  • 45
  • 1
    Welcome to StackOverflow. While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. Have a look here → [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – Federico Baù Jan 15 '21 at 08:36