【问题标题】:Django Rest Framework api test upload text file application/x-www-form-urlencodedDjango Rest Framework api 测试上传文本文件 application/x-www-form-urlencoded
【发布时间】:2018-10-05 03:38:26
【问题描述】:

我正在尝试测试我的 Django REST API 以进行文件上传。

问题是我的服务器尝试验证文件,以便文件具有可识别的文件类型。目前只允许使用基于文本的文件类型,例如:docs、pdf、txt。

我尝试过使用临时文件,现在我只是尝试从磁盘读取文件然后放弃了。

每当我使用临时文件时,服务器都会响应:

    {
        "cover_letter": [
            "The submitted data was not a file. Check the encoding type on the form."
        ],
        "manuscript": [
            "The submitted data was not a file. Check the encoding type on the form."
        ]
    }

这是我的测试:

 def test_user_can_submit_a_paper(self):
     """
         Ensure that an user is able to upload a paper.

         This test is not working.. yet
     """
     tmp_file = open("__init__.py", "w")
     data = {
         "title": "paper",
         "authors": "me",
         "description": "ma detailed description",
         "manuscript": base64.b64encode(tmp_file.read()).decode(),
         "cover_letter": base64.b64encode(tmp_file.read()).decode()
     }
     tmp_file.close()


     response = self.client.post(self.papers_submitted, data=urlencode(MultiValueDict(data)), content_type='application/x-www-form-urlencoded',
                                 HTTP_AUTHORIZATION=self.authorization_header)
     print(response.data)
     self.assertEqual(response.status_code, status.HTTP_200_OK)

     del data["cover_letter"]
     response = self.client.post(self.papers_submitted, data=urlencode(MultiValueDict(data)), content_type='application/x-www-form-urlencoded',
                                 HTTP_AUTHORIZATION=self.authorization_header)
     print(response.data)
     self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

我已经通过邮递员成功测试了这个端点,但我不知道如何在 Python 中进行。

【问题讨论】:

    标签: django django-rest-framework


    【解决方案1】:

    感谢 Mark 的回答,我设法想出了一个解决方案。 如果有人感兴趣,这是代码:

    def test_user_can_submit_a_paper(self):
        from api.journal import PaperListSubmitted
        from django.test.client import RequestFactory
        from django.core.files import temp as tempfile
        """
            Ensure that an user is able to upload a paper.
        """
    
        request_factory = RequestFactory()
        manuscript = tempfile.NamedTemporaryFile(suffix=".txt")
        cover_letter = tempfile.NamedTemporaryFile(suffix=".txt")
        manuscript.write(b"This is my stupid paper that required me to research writing this test for over 5h")
        cover_letter.write(b"This is my stupid paper that required me to research writing this test for over 5h")
        manuscript.seek(0)
        cover_letter.seek(0)
    
        post_data = {
            "title": "My post title",
            "description": "this is my paper description",
            "authors": "no authors",
            "manuscript": manuscript,
            "cover_letter": cover_letter
        }
    
        request = request_factory.post(self.papers_submitted, HTTP_AUTHORIZATION=self.authorization_header,
                                       data=post_data)
    
        response = PaperListSubmitted.as_view()(request)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
    

    【讨论】:

      【解决方案2】:

      如果我理解您的要求,您想使用 python 上传文件来测试您的 DRF 端点。你应该使用RequestFactory,它模拟一个请求——它更像是使用 Postman。

      这个答案Django: simulate HTTP requests in shell有一个使用RequestFactory的例子,这里有多个答案django RequestFactory file upload关于如何用RequestFactory包含上传文件的具体解决方案。

      【讨论】:

        猜你喜欢
        • 2011-11-15
        • 2018-11-17
        • 1970-01-01
        • 2017-09-11
        • 1970-01-01
        • 2017-07-19
        • 1970-01-01
        • 1970-01-01
        • 2020-09-06
        相关资源
        最近更新 更多