gpt4 book ai didi

node.js - 使用 multipart/form-data 的 Mocha 测试发布请求不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 22:32:18 26 4
gpt4 key购买 nike

我正在尝试使用 mocha 测试 post 请求,post 请求正文包含一个图像字段以及 json 数据。当图像与 json 一起附加时,测试不会执行。测试正在执行,但在没有附件的情况下引发 400 错误。在此附上我的代码。

var request = require('supertest');
var app = require('../server')

request(app).post('/companies/')
.set({apikey: 'TestHashKey',
'app-token': process.env.API_TOKEN,
'app-key': process.env.API_KEY})
.field('Content-Type', 'multipart/form-data')
.field('name', 'sample_companyx')
.field('phoneNumber','+963014311354')
.attach('logo', '/app/test/images/test_logo.jpg')
.expect(200)
.end(function (err, res) {
if (err) {
throw err;
}
done();
});

不带附件的回复粘贴在下面,

POST /companies/ 400 12ms - 12b response Missing logo 2016-01-22T04:08:20.044Z - error: Fri, 22 Jan 2016 04:08:20 GMT uncaughtException: expected 200 "OK", got 400 "Bad Request"

附件存在时的响应是

2016-01-22T04:13:44.849Z - info: [beta] Companies API up and running on port 4000 2016-01-22T04:13:45.916Z - info: Companies worker ready! npm ERR! Test failed. See above for more details. npm ERR! not ok code 0

最佳答案

这是一个最小的工作示例:

app.js:

const express = require("express");
const multer = require("multer");
const path = require("path");
const crypto = require("crypto");
const app = express();

const storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, path.resolve(__dirname, "uploads/"));
},
filename: function(req, file, cb) {
crypto.pseudoRandomBytes(16, function(err, raw) {
if (err) return cb(err);

cb(null, raw.toString("hex") + path.extname(file.originalname));
});
},
});
const upload = multer({ storage });

app.post("/companies/", upload.single("logo"), (req, res) => {
console.log("req.body: ", req.body);
console.log("req.file:", req.file);
res.sendStatus(200);
});

module.exports = app;

app.test.js:

const app = require("./app");
const request = require("supertest");
const path = require("path");

describe("34939199", () => {
it("should pass", (done) => {
process.env.API_TOKEN = "123";
process.env.API_KEY = "abc";
request(app)
.post("/companies/")
.set({ apikey: "TestHashKey", "app-token": process.env.API_TOKEN, "app-key": process.env.API_KEY })
.field("Content-Type", "multipart/form-data")
.field("name", "sample_companyx")
.field("phoneNumber", "+963014311354")
.attach("logo", path.resolve(__dirname, "fixtures/test_logo.jpg"))
.expect(200)
.end(function(err, res) {
if (err) {
throw err;
}
done();
});
});
});

集成测试结果与覆盖率报告:

 34939199
req.body: [Object: null prototype] {
'Content-Type': 'multipart/form-data',
name: 'sample_companyx',
phoneNumber: '+963014311354' }
req.file: { fieldname: 'logo',
originalname: 'test_logo.jpg',
encoding: '7bit',
mimetype: 'image/jpeg',
destination:
'/Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/src/stackoverflow/34939199/uploads',
filename: 'da39e06c1cd2a97f98a66eb6d832aa74.jpg',
path:
'/Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/src/stackoverflow/34939199/uploads/da39e06c1cd2a97f98a66eb6d832aa74.jpg',
size: 0 }
✓ should pass (50ms)


1 passing (56ms)

-------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
-------------|----------|----------|----------|----------|-------------------|
All files | 93.1 | 50 | 100 | 96.43 | |
app.js | 94.12 | 50 | 100 | 100 | 13 |
app.test.js | 91.67 | 50 | 100 | 91.67 | 20 |
-------------|----------|----------|----------|----------|-------------------|

文件夹结构:

.
├── app.js
├── app.test.js
├── fixtures
│ └── test_logo.jpg
└── uploads
└── da39e06c1cd2a97f98a66eb6d832aa74.jpg

2 directories, 4 files

源代码:https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/34939199

关于node.js - 使用 multipart/form-data 的 Mocha 测试发布请求不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34939199/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com