Skip to content

Commit

Permalink
all relevant tests covered
Browse files Browse the repository at this point in the history
  • Loading branch information
hacksparrow committed Jul 19, 2014
1 parent dcfc80d commit ba9a31a
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
*.DS_Store
temp/
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "multer",
"version": "0.1.2",
"version": "0.1.3",
"description": "Connect middleware for handling multipart/form-data",
"main": "index.js",
"repository": {
Expand Down Expand Up @@ -34,6 +34,7 @@
"co": "^3.0.6",
"express": "*",
"mocha": "*",
"rimraf": "^2.2.8",
"supertest": "^0.13.0"
},
"author": {
Expand Down
9 changes: 3 additions & 6 deletions test/test.js → test/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ app.use(multer());
app.post('/', handler);
app.put('/', handler);

function handler(req, res) {
function handler(req, res) {
var form = {
body: req.body,
files: req.files
Expand All @@ -27,7 +27,7 @@ app2.post('/', function (req, res) {
res.send(form);
});

describe('Basic Functionality', function () {
describe('Form', function () {

it('should process multipart/form-data POST request', function (done) {

Expand Down Expand Up @@ -90,15 +90,12 @@ describe('Basic Functionality', function () {
it('should not process non-multipart POST request', function (done) {
request(app)
.post('/')
.attach('small0', __dirname + '/files/small0.dat')
.field('name', 'Multer')
.send({name: 'Multer'})
.expect(200)
.end(function (err, res) {
var form = res.body;
expect(err).to.be.null;
expect(form.body).to.be.an('object');
expect(Object.keys(form.body).length).to.equal(0);
expect(form.files).to.be.an('object');
expect(Object.keys(form.files).length).to.equal(0);
done();
})
Expand Down
59 changes: 59 additions & 0 deletions test/functionality.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
var fs = require('fs');
var rimraf = require('rimraf');
var expect = require('chai').expect
var request = require('supertest');
var express = require('express');
var multer = require('../');

describe('Functionality', function () {

// delete the temp dir after the tests are run
after(function (done) { rimraf('./temp', done); });

var app = express();
app.use(multer({
dest: './temp',
rename: function (fieldname, filename) {
return fieldname + filename;
}
}));
app.post('/', function (req, res) {
var form = {
body: req.body,
files: req.files
}
res.send(form);
});


it('should upload the file to the `dest` dir', function (done) {
request(app)
.post('/')
.type('form')
.attach('small0', __dirname + '/files/small0.dat')
.expect(200)
.end(function (err, res) {
var form = res.body;
expect(err).to.be.null;
expect(fs.existsSync('./temp/small0small0.dat')).to.equal(true);
done();
})
})


it('should rename the uploaded file', function (done) {
request(app)
.post('/')
.type('form')
.attach('small0', __dirname + '/files/small0.dat')
.expect(200)
.end(function (err, res) {
var form = res.body;
expect(err).to.be.null;
expect(form.files.small0.name).to.equal('small0small0.dat');
done();
})
})


})

0 comments on commit ba9a31a

Please sign in to comment.