带有mulipart / form-data的Mocha测试帖子请求不起作用

问题描述 投票:2回答:1

我正在尝试使用mocha测试发布请求,发布请求主体包含一个image字段以及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响应缺少徽标2016-01-22T04:08:20.044Z-错误:2016年1月22日星期五,格林尼治标准时间uncaughtException:预期为200个“确定”,得到了400个“错误请求”

附件存在时的响应是

2016-01-22T04:13:44.849Z-信息:[beta]公司API已启动并在端口4000上运行2016-01-22T04:13:45.916Z-信息:公司工人准备好了!npm ERR!测试失败。有关更多详细信息,请参见上文。npm ERR!错误代码0

node.js mocha supertest
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.