用摩卡testing二进制file upload

我目前正在研究一个带有nodejs的小API,并且要求通过接收一个二进制string来完成file upload。

我不知道该怎么做,是testing它与摩卡,所以我一直在做一些search,发现堆栈溢出unit testingfile upload与摩卡 ,它是一个很好的开始,但它不会工作,因为它发送一个多部分的forms,什么我要求客户端发送的api文件就是一个stream。

inheritance人我的控制器:

exports.uploadVideo = function(req, res, next) { var video = "public/video/" + req.params.videoId + ".mp4", util = require('util'), exec = require('child_process').exec; var newFile = fs.createWriteStream("./uploads/" + video); req.pipe(newFile); req.on('end', function () { var cmd = 'qtfaststart ' + './uploads/' + video; var qtfaststart = exec(cmd, function(error, stdout, stderr){ if (error === "atom not found, is this a valid MOV/MP4 file?\n" || error !== null) { return next(new restify.ConflictError("Error: " + stdout)); } else { fs.chmodSync('./uploads/' + video, '644'); Video.findOne( { _id: req.params.videoId }, function(err, video) { if (err) return next(new restify.ConflictError(err)); if (!video) { newVideo = new Video({ _id: req.params.videoId, file: video}); newVideo.save() } else { video.file = video; video.increment(); video.save(); } }); } }); }); req.on('error', function(err){ return next(new restify.NetworkConnectTimeoutError(err)); }); }; 

所以给这个控制器接收一个stream(二进制文件),并把stream放在后端,我怎么会用摩卡testing这个控制器?

你可以使用http

 it('should be possible to upload a file', function(done) { var http = require('http'); var options = require('url').parse(YOUR_URL); options.method = 'POST'; var req = http.request(options, function(response) { // TODO: check for errors, correct response, etc... done(...); }); require('fs').createReadStream(YOUR_TEST_FILE).pipe(req); }); 

你想使用摩卡内的请求模块。 它支持多部分forms。