Skip to content

Commit e5e7225

Browse files
committed
adding tests for .mv() using Promise
1 parent 1d13af9 commit e5e7225

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

test/multipartUploads.spec.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,77 @@ describe('Test Single File Upload', function() {
102102
});
103103
});
104104

105+
describe('Test Single File Upload w/ .mv() Promise', function() {
106+
for (let i = 0; i < mockFiles.length; i++) {
107+
let fileName = mockFiles[i];
108+
109+
it(`upload ${fileName} with POST w/ .mv() Promise`, function(done) {
110+
let filePath = path.join(fileDir, fileName);
111+
let uploadedFilePath = path.join(uploadDir, fileName);
112+
113+
clearUploadsDir();
114+
115+
request(app)
116+
.post('/upload/single/promise')
117+
.attach('testFile', filePath)
118+
.expect(200)
119+
.end(function(err, res) {
120+
if (err) {
121+
return done(err);
122+
}
123+
124+
fs.stat(uploadedFilePath, done);
125+
});
126+
});
127+
128+
it(`upload ${fileName} with PUT w/ .mv() Promise`, function(done) {
129+
let filePath = path.join(fileDir, fileName);
130+
let uploadedFilePath = path.join(uploadDir, fileName);
131+
132+
clearUploadsDir();
133+
134+
request(app)
135+
.post('/upload/single/promise')
136+
.attach('testFile', filePath)
137+
.expect(200)
138+
.end(function(err, res) {
139+
if (err) {
140+
return done(err);
141+
}
142+
143+
fs.stat(uploadedFilePath, done);
144+
});
145+
});
146+
}
147+
148+
it('fail when no files were attached', function(done) {
149+
request(app)
150+
.post('/upload/single')
151+
.expect(400)
152+
.end(done);
153+
});
154+
155+
it('fail when using GET', function(done) {
156+
let filePath = path.join(fileDir, mockFiles[0]);
157+
158+
request(app)
159+
.get('/upload/single')
160+
.attach('testFile', filePath)
161+
.expect(400)
162+
.end(done);
163+
});
164+
165+
it('fail when using HEAD', function(done) {
166+
let filePath = path.join(fileDir, mockFiles[0]);
167+
168+
request(app)
169+
.head('/upload/single')
170+
.attach('testFile', filePath)
171+
.expect(400)
172+
.end(done);
173+
});
174+
});
175+
105176
describe('Test Multi-File Upload', function() {
106177
it('upload multiple files with POST', function(done) {
107178
let upload1 = path.join(fileDir, mockFiles[0]);

test/server.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,23 @@ const setup = function(fileUploadOptions) {
3838
});
3939
});
4040

41+
app.all('/upload/single/promise', function(req, res) {
42+
if (!req.files) {
43+
return res.status(400).send('No files were uploaded.');
44+
}
45+
46+
let testFile = req.files.testFile;
47+
let uploadPath = path.join(uploadDir, testFile.name);
48+
49+
testFile.mv(uploadPath)
50+
.then(() => {
51+
res.send('File uploaded to ' + uploadPath);
52+
})
53+
.catch((err) => {
54+
res.status(500).send(err);
55+
});
56+
});
57+
4158
app.all('/upload/single/withfields', function(req, res) {
4259
if (!req.files) {
4360
return res.status(400).send('No files were uploaded.');

0 commit comments

Comments
 (0)