[英]How to set Content-Type of Supertest attachment
I came across a problem in my app that the Content-Type
associated with an uploaded file was wrong if the file was sent from Microsoft Windows, giving application/octet-stream
, while it was okay if sent from Mac, giving text/csv
. 我在应用程序中遇到一个问题:如果文件是从Microsoft Windows发送的,给出了
application/octet-stream
,则与上载的文件相关联的Content-Type
是错误的;而如果是从Mac发送的给出了text/csv
的文件,这是可以的。 After fixing my code to not rely on the mime type from the request, I would like to simulate that condition in one of my tests. 修复代码以使其不依赖请求中的mime类型后,我想在我的测试之一中模拟这种情况。
Given the following request, that includes JSON-stringified form fields and a file attachment, using supertest : 给定以下请求,其中包括使用supertest包括JSON字符串化的表单字段和文件附件:
request(app)
.post('/some/where')
.field('someFormData', JSON.stringify(formData))
.attach('someFile', 'someFile.csv')
.expect(400)
.end(done);
How can I change the Content-Type for the attached file? 如何更改附件的内容类型 ? Looking at Edge's Network tab, I would like to see the following for the request above:
查看“边缘”的“网络”选项卡,对于以上请求,我希望看到以下内容:
-----------------------------7e13121340602
Content-Disposition: form-data; name="someFormData"
{.............}
-----------------------------7e13121340602
Content-Disposition: form-data; name="someFile"; filename="someFile.csv"
Content-Type: application/octet-stream
ÿØÿà
(The JSON string has been omitted with dots) (JSON字符串已被点省略)
(Instead of showing text/csv
, I want to simulate the wrong content type.) (而不是显示
text/csv
,我想模拟错误的内容类型。)
Using attach's contentType option works for me: 使用attach的contentType选项对我有效:
request(app)
.post('/validateCertificate')
.set('Content-type', 'multipart/form-data')
.field('passphrase', '1234')
.attach(
'pfx',
'./src/build-request/test-certs/correct.pfx',
{ contentType: 'application/x-pkcs12' },
)
.expect(200))
Or if you're sending a buffer as the file: 或者,如果您要发送缓冲区作为文件:
request(app)
.post('/validateCertificate')
.set('Content-type', 'multipart/form-data')
.field('passphrase', '1234')
.attach(
'pfx',
buffer,
{ contentType: 'application/x-pkcs12', filename: 'correct.pfx' },
)
.expect(200))
Use .set() to set whatever Content-Type you want. 使用.set()设置所需的任何内容类型。 Or any kind of header.
或任何类型的标题。
request(app)
.post('/some/where')
.field('someFormData', JSON.stringify(formData))
.set('Content-Type', 'application/octet-stream')
.attach('someFile', 'someFile.csv')
.expect(400)
.end(done);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.