2

我正在使用推荐的插件cypress-file-upload从我的灯具文件夹中附加和上传图像文件。当用户完成此文件上传时,此文件上传完美,但当赛普拉斯尝试此过程时,服务器会发回以下错误(即使它模仿用户将采取的相同操作):

Error: Unexpected end of multipart data
api_1       |     at /app/node_modules/dicer/lib/Dicer.js:62:28
api_1       |     at processTicksAndRejections (internal/process/task_queues.js:75:11) {
api_1       |   storageErrors: []
api_1       | }

奇怪的是,我仔细检查了来自赛普拉斯的请求,并将其与浏览器发送的请求进行了比较,我没有发现任何明显的差异。这是上传文件的赛普拉斯片段:

// attach file through cypress-file-upload
cy.getBySel('avatar-modal-input').attachFile('images/quinoa.png') // <- this method is from cypress-file-upload
cy.getBySel('avatar-modal-button').click() // submits the request to the server
cy.wait('@updateUser')

在后端,它到达这个端点:

// Endpoint to upload image
router.post('/upload', upload.single('image'), ensureAuthorized, (req, res) => res.status(200).json({ file: req.file.location }));

和上传功能实用程序:

const aws = require('aws-sdk');
const multer = require('multer');
const multerS3 = require('multer-s3');

aws.config.update({
  secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
  accessKeyId: process.env.AWS_ACCESS_KEY_ID,
  region: process.env.AWS_REGION,
});

const s3 = new aws.S3();

const fileFilter = (req, file, cb) => {
  if (req.body.gif === 'true' && (file.mimetype === 'image/jpeg' || file.mimetype === 'image/png' || file.mimetype === 'image/gif')) {
    cb(null, true);
  } else if (file.mimetype === 'image/jpeg' || file.mimetype === 'image/png') {
    cb(null, true);
  } else {
    cb(new Error('Invalid file type, only JPEG and PNG are allowed!'), false);
  }
};

const upload = multer({
  fileFilter,
  storage: multerS3({
    acl: 'public-read',
    s3,
    bucket: process.env.AWS_BUCKET,
    key(req, file, cb) {
      cb(null, Date.now().toString());
    },
  }),
});

module.exports = upload;
4

1 回答 1

0

I am facing a similar issue. I wonder if this may have resolve the problem:

6.2.1 6 months ago

"multipart/form-data is no longer wrongly encoded when using cy.intercept(). This should prevent Unexpected end of multipart data errors on the back end service where the request was pointing to. Fixes #9359."

https://openbase.com/js/cypress/versions

于 2021-07-01T16:42:30.307 回答