I would like to unit test an Express middleware function which, in turn, uses node-formidable to process a multipart file upload.
我想对Express中间件函数进行单元测试,而后者又使用node-formidable来处理多部分文件上传。
Here’s a contrived example:
这是一个人为的例子:
function doUpload(req, res, next) {
const form = new formidable.IncomingForm();
form.uploadDir = this.mediaDir;
form.keepExtensions = true;
form.type = 'multipart';
form.multiples = true;
form.parse(req, function(err, fields, files) {
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
res.end(util.inspect({fields: fields, files: files}));
});
}
This code works for me when tested with Chrome and a running Express app.
在使用Chrome和正在运行的Express应用程序进行测试后,此代码适用于我。
I want my test code to look like the following where I am mocking the request object, but I can't figure out how to mock the request object with the form data. The formidable callback is not firing:
我希望我的测试代码看起来像我在嘲笑请求对象,但我无法弄清楚如何使用表单数据模拟请求对象。强大的回调没有触发:
it(‘handles uploads’, (done) => {
const mockReq = http.request({
host: 'example.org',
});
mockReq.url = ‘/upload’;
const res = jasmine.createSpyObj('response', [ 'json', 'send', 'status', 'render', 'header', ‘redirect’, ‘end’, ‘write;]);
const next = jasmine.createSpy('next');
//*how to emulate a request with a multipart file upload)
doUpload(req, res, next);
res.write.and.callFake(done);
});
I’ve tried using the form-data library to create a FormData object and pipe it to the request, but no luck, I’m not sure if I’m on the right track or way off. Something like this:
我已经尝试使用表单数据库创建一个FormData对象并将其传递给请求,但没有运气,我不确定我是否在正确的轨道上或离开。像这样的东西:
var form = new FormData();
const buff = fs.readFileSync(path.join(__dirname, '../fixtures/logo.png'));
form.append('file', buff, {
filename: 'logo.jpg',
contentType: 'image/png',
knownLength: buff.length
});
form.append('name', 'Logo');
req.headers = form.getHeaders();
form.pipe(req);
doUpload(req, res, next);
1 个解决方案
#1
0
You could use some request tester like supertest, to do it. Here is an example, assuming that your main file is named app.js:
您可以使用像supertest这样的请求测试程序来执行此操作。这是一个示例,假设您的主文件名为app.js:
const request = require('supertest');
const app = require('app.js');
it('Uploads a file', function(){
request(app)
.post('/upload')
.field('name', 'Logo') //adds a field 'name' and sets its value to 'Logo'
.attach('file', '/path/to/file') // attaches the file to the form
.then(function(response){
// response from the server
console.log(response.status);
console.log(response.body);
})
})
#1
0
You could use some request tester like supertest, to do it. Here is an example, assuming that your main file is named app.js:
您可以使用像supertest这样的请求测试程序来执行此操作。这是一个示例,假设您的主文件名为app.js:
const request = require('supertest');
const app = require('app.js');
it('Uploads a file', function(){
request(app)
.post('/upload')
.field('name', 'Logo') //adds a field 'name' and sets its value to 'Logo'
.attach('file', '/path/to/file') // attaches the file to the form
.then(function(response){
// response from the server
console.log(response.status);
console.log(response.body);
})
})