我从 Cypress 6.8.0 升级到 7.0.1。升级后,当 Cypress 测试之一调用此函数时
async saveTask (task, file) {
const requestBody = new FormData()
requestBody.append('file', file)
return await http.post('/api/endpoint', requestBody, {
headers: {
'Content-Type': 'multipart/form-data'
}
})
},
我收到以下错误
TypeError [ERR_INVALID_ARG_TYPE] [ERR_INVALID_ARG_TYPE]:
The first argument must be of type string or an instance of Buffer or Uint8Array. Received type number (45)
at write_ (_http_outgoing.js:696:11)
at ClientRequest.write (_http_outgoing.js:661:15)
at Request.write (/Users/donal/Library/Caches/Cypress/7.0.1/Cypress.app/Contents/Resources/app/packages/server/node_modules/@cypress/request/request.js:1496:27)
at /Users/donal/Library/Caches/Cypress/7.0.1/Cypress.app/Contents/Resources/app/packages/server/node_modules/@cypress/request/request.js:546:20
at Array.forEach (<anonymous>:null:null)
at end (/Users/donal/Library/Caches/Cypress/7.0.1/Cypress.app/Contents/Resources/app/packages/server/node_modules/@cypress/request/request.js:545:23)
at Immediate._onImmediate (/Users/donal/Library/Caches/Cypress/7.0.1/Cypress.app/Contents/Resources/app/packages/server/node_modules/@cypress/request/request.js:578:7)
at processImmediate (internal/timers.js:461:21)
{
code: 'ERR_INVALID_ARG_TYPE'
}
The http
我用来发出 POST 请求的对象是 Axios 实例,file
我附加到请求正文的对象是File https://developer.mozilla.org/en-US/docs/Web/API/File。文件对象是问题的原因,因为如果我不将其附加到请求正文,则不会发生错误。
仅当该函数由 Cypress 测试运行时才会发生该错误。 Cypress 使用 Node.js,从上面的错误消息来看,似乎File
类型是不允许的。此外,Axios 请求配置文档 https://github.com/axios/axios#request-config表明当axios运行在Node下时,File
不允许。
// `data` is the data to be sent as the request body
// Only applicable for request methods 'PUT', 'POST', 'DELETE , and 'PATCH'
// When no `transformRequest` is set, must be of one of the following types:
// - string, plain object, ArrayBuffer, ArrayBufferView, URLSearchParams
// - Browser only: FormData, File, Blob
// - Node only: Stream, Buffer
data: {
firstName: 'Fred'
},
所以我想我需要转换File
反对别的东西,这样该函数既可以在应用程序本身内运行,也可以在由 Cypress 运行时运行。
在服务器端(Spring Boot 应用程序),此文件绑定到MultipartFile
public void handlePost(
RequestPart(value = "file") MultipartFile file) {
// controller action body
}
如果我的理论File
类型是问题,我应该使用什么来代替,我应该如何进行转换?