我想为 Django REST Framework 应用程序的视图编写单元测试。测试应该使用 PUT 上传文件,本质上相当于
http -a malkaouri PUT http://localhost:8000/data-packages/upload/ka @tmp/hello.py
到目前为止我写的代码是
factory = APIRequestFactory()
request = factory.put( '/data-packages/upload/ka',
data,
content_type='application/octet-stream',
content_disposition="attachment; filename=data.dump")
force_authenticate(request, user)
view = PackageView.as_view()
response = view(request, "k.py")
显然,它不会上传文件。运行测试时的具体错误是400:
{u'detail': u'缺少文件名。请求应包含带有文件名参数的 Content-Disposition 标头。'}
值得注意的是,我使用的是请求工厂来测试视图,而不是完整的客户端。这就是使诸如此问题中的解决方案之类的解决方案对我不起作用的原因。
设置内容处置标头的正确方法是什么?