-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor server.js to allow for different options to be provided during testing Bumped the version number to 0.1.3
- Loading branch information
Showing
8 changed files
with
266 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "express-fileupload", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"author": "Richard Girges <[email protected]>", | ||
"description": "Simple express file upload middleware that wraps around Busboy", | ||
"main": "./lib/index", | ||
|
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const request = require('supertest'); | ||
const server = require('./server'); | ||
// const clearUploadsDir = server.clearUploadsDir; | ||
const fileDir = server.fileDir; | ||
const uploadDir = server.uploadDir; | ||
|
||
describe('SafeFileNames', function() { | ||
it(`Does nothing to your filename when disabled.`, function(done) { | ||
const app = server.setup({safeFileNames: false}); | ||
|
||
request(app) | ||
.post('/upload/single') | ||
.attach('testFile', path.join(fileDir, 'my$Invalid#fileName.png123')) | ||
.expect(200) | ||
.end(function(err, res) { | ||
if (err) | ||
return done(err); | ||
|
||
let uploadedFilePath = path.join(uploadDir, 'my$Invalid#fileName.png123'); | ||
|
||
fs.stat(uploadedFilePath, done); | ||
}); | ||
}); | ||
|
||
it(`Strips away all illegal characters (including spaces) when enabled.`, function(done) { | ||
const app = server.setup({safeFileNames: true}); | ||
|
||
request(app) | ||
.post('/upload/single') | ||
.attach('testFile', path.join(fileDir, 'my$Invalid#fileName.png123')) | ||
.expect(200) | ||
.end(function(err, res) { | ||
if (err) | ||
return done(err); | ||
|
||
let uploadedFilePath = path.join(uploadDir, 'myInvalidfileNamepng123'); | ||
|
||
fs.stat(uploadedFilePath, done); | ||
}); | ||
}); | ||
|
||
it(`Respects a regex for stripping 'invalid' characters from filename.`, function(done) { | ||
const app = server.setup({safeFileNames: /[\$#]/g}); | ||
|
||
request(app) | ||
.post('/upload/single') | ||
.attach('testFile', path.join(fileDir, 'my$Invalid#fileName.png123')) | ||
.expect(200) | ||
.end(function(err, res) { | ||
if (err) | ||
return done(err); | ||
|
||
let uploadedFilePath = path.join(uploadDir, 'myInvalidfileName.png123'); | ||
|
||
fs.stat(uploadedFilePath, done); | ||
}); | ||
}); | ||
}); | ||
|
||
describe(`preserveExtension`, function() { | ||
it(`Does nothing to your filename when disabled.`, function(done) { | ||
const app = server.setup({safeFileNames: true, preserveExtension: false}); | ||
|
||
request(app) | ||
.post('/upload/single') | ||
.attach('testFile', path.join(fileDir, 'my$Invalid#fileName.png123')) | ||
.expect(200) | ||
.end(function(err, res) { | ||
if (err) | ||
return done(err); | ||
|
||
let uploadedFilePath = path.join(uploadDir, 'myInvalidfileNamepng123'); | ||
|
||
fs.stat(uploadedFilePath, done); | ||
}); | ||
}); | ||
|
||
it(`Shortens your extension to the default(3) when enabled, if the extension found is larger`, | ||
function(done) { | ||
const app = server.setup({safeFileNames: true, preserveExtension: true}); | ||
|
||
request(app) | ||
.post('/upload/single') | ||
.attach('testFile', path.join(fileDir, 'my$Invalid#fileName.png123')) | ||
.expect(200) | ||
.end(function(err, res) { | ||
if (err) | ||
return done(err); | ||
|
||
let uploadedFilePath = path.join(uploadDir, 'myInvalidfileNamepng.123'); | ||
|
||
fs.stat(uploadedFilePath, done); | ||
}); | ||
}); | ||
|
||
it(`Leaves your extension alone when enabled, if the extension found is <= default(3) length`, | ||
function(done) { | ||
const app = server.setup({safeFileNames: true, preserveExtension: true}); | ||
|
||
request(app) | ||
.post('/upload/single') | ||
.attach('testFile', path.join(fileDir, 'car.png')) | ||
.expect(200) | ||
.end(function(err, res) { | ||
if (err) | ||
return done(err); | ||
|
||
let uploadedFilePath = path.join(uploadDir, 'car.png'); | ||
|
||
fs.stat(uploadedFilePath, done); | ||
}); | ||
}); | ||
|
||
it(`Leaves your extension alone when set to a number >= the extension length.`, | ||
function(done) { | ||
const app = server.setup({safeFileNames: true, preserveExtension: 7}); | ||
|
||
request(app) | ||
.post('/upload/single') | ||
.attach('testFile', path.join(fileDir, 'my$Invalid#fileName.png123')) | ||
.expect(200) | ||
.end(function(err, res) { | ||
if (err) | ||
return done(err); | ||
|
||
let uploadedFilePath = path.join(uploadDir, 'myInvalidfileName.png123'); | ||
|
||
fs.stat(uploadedFilePath, done); | ||
}); | ||
}); | ||
|
||
it(`Only considers the last dotted part the extension.`, | ||
function(done) { | ||
const app = server.setup({safeFileNames: true, preserveExtension: true}); | ||
|
||
request(app) | ||
.post('/upload/single') | ||
.attach('testFile', path.join(fileDir, 'basket.ball.bp')) | ||
.expect(200) | ||
.end(function(err, res) { | ||
if (err) | ||
return done(err); | ||
|
||
let uploadedFilePath = path.join(uploadDir, 'basketball.bp'); | ||
|
||
fs.stat(uploadedFilePath, done); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.