如何在 Mocha 中创建具有特定大小的模拟 post 对象
How to create mocked post object with specific size in Mocha
在我的 nodejs 代码中,我创建了一个函数,如果请求正文大于 1MB(我使用 req.get("content-length") 检查大小)然后执行某些操作。
我想在 mocha 中创建测试 POST 具有模拟 1MB 请求正文大小的对象,以测试我的功能是否有效。
简答,您可以像这样创建特定大小的请求正文:
const buf = Buffer.alloc(1024 * 1024, '.');
为您的案例创建一个 1MB 大小的请求正文。
长答案,这是一个使用 express.js
网络框架和 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)
在我的 nodejs 代码中,我创建了一个函数,如果请求正文大于 1MB(我使用 req.get("content-length") 检查大小)然后执行某些操作。 我想在 mocha 中创建测试 POST 具有模拟 1MB 请求正文大小的对象,以测试我的功能是否有效。
简答,您可以像这样创建特定大小的请求正文:
const buf = Buffer.alloc(1024 * 1024, '.');
为您的案例创建一个 1MB 大小的请求正文。
长答案,这是一个使用 express.js
网络框架和 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)