如何在Mocha中创建具有特定大小的模拟发布对象

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

在我的nodejs代码中,我创建了一个函数,如果请求正文大于1MB(我用req.get(“ content-length”)检查大小)),然后执行某些操作。我想在模拟到1MB请求主体大小的POST对象的mocha中创建测试,以测试其功能是否正常。

node.js mocha
1个回答
0
投票

简短的回答,您可以像这样创建特定的尺寸请求正文:

const buf = Buffer.alloc(1024 * 1024, '.');

为您的案例创建一个1MB大小的请求正文。

长答案,这是一个使用express.js Web框架和supertest测试框架的工作示例。

index.ts

import express from 'express';
import bodyParser from 'body-parser';

function formatBytes(bytes, decimals = 2) {
  if (bytes === 0) return '0 Bytes';

  const k = 1024;
  const dm = decimals < 0 ? 0 : decimals;
  const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];

  const i = Math.floor(Math.log(bytes) / Math.log(k));

  return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}

function createServer() {
  const app = express();
  const port = 3000;

  app.use(bodyParser.text({ limit: 10 * 1024 * 1024 /** 10MB */ }));
  app.post('/', (req, res) => {
    console.log(JSON.stringify(req.body));
    const contentLength = req.get('content-length');
    console.log('content length: ', formatBytes(contentLength));
    res.sendStatus(200);
  });

  return app.listen(port, () => {
    console.log(`HTTP server is listening to http://localhost:${port}`);
  });
}

export { createServer };

index.test.ts

import { createServer } from './';
import request from 'supertest';

describe('60286487', () => {
  let server;
  beforeEach(() => {
    server = createServer();
  });
  afterEach((done) => {
    server.close(done);
  });
  it('should pass', (done) => {
    const buf = Buffer.alloc(1024 * 1024, '.');
    request(server)
      .post('/')
      .send(buf.toString())
      .set('Content-Type', 'text/plain')
      .set('Content-Length', buf.byteLength.toString())
      .expect(200, done);
  });
});

综合测试结果:

.........................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................."
content length:  1 MB
    ✓ should pass (3877ms)
© www.soinside.com 2019 - 2024. All rights reserved.