-
-
Notifications
You must be signed in to change notification settings - Fork 681
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: part of #415, start converting to Jest tests
Signed-off-by: Charlike Mike Reagent <[email protected]>
- Loading branch information
1 parent
7970ab1
commit a3bffa6
Showing
10 changed files
with
2,229 additions
and
252 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 |
---|---|---|
|
@@ -2,5 +2,6 @@ | |
|
||
require('urun')(__dirname, { | ||
verbose: true, | ||
include: /test-.+/, | ||
reporter: 'BashTapReporter', | ||
}); |
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,31 @@ | ||
'use strict'; | ||
|
||
// const assert = require('assert'); | ||
// const test = require('utest'); | ||
|
||
const { File } = require('../../src/index'); | ||
|
||
const now = new Date(); | ||
const file = new File({ | ||
size: 1024, | ||
path: '/tmp/cat.png', | ||
name: 'cat.png', | ||
type: 'image/png', | ||
lastModifiedDate: now, | ||
filename: 'cat.png', | ||
mime: 'image/png', | ||
}); | ||
|
||
test('File#toJSON()', () => { | ||
const obj = file.toJSON(); | ||
const len = Object.keys(obj).length; | ||
|
||
expect(1024).toBe(obj.size); | ||
expect('/tmp/cat.png').toBe(obj.path); | ||
expect('cat.png').toBe(obj.name); | ||
expect('image/png').toBe(obj.type); | ||
expect('image/png').toBe(obj.mime); | ||
expect('cat.png').toBe(obj.filename); | ||
expect(now).toBe(obj.mtime); | ||
expect(len).toBe(8); | ||
}); |
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,96 @@ | ||
/* eslint-disable no-underscore-dangle */ | ||
|
||
'use strict'; | ||
|
||
const path = require('path'); | ||
// const assert = require('assert'); | ||
const Request = require('http').ClientRequest; | ||
|
||
// const test = require('utest'); | ||
const mod = require('../../src/index'); | ||
|
||
function getForm(name, opts) { | ||
return name === 'formidable' ? mod.formidable(opts) : new mod[name](opts); | ||
} | ||
function makeHeader(filename) { | ||
return `Content-Disposition: form-data; name="upload"; filename="${filename}"`; | ||
} | ||
|
||
['IncomingForm', 'Formidable', 'formidable'].forEach((name) => { | ||
test(`${name}#_fileName with regular characters`, () => { | ||
const filename = 'foo.txt'; | ||
const form = getForm(name); | ||
|
||
expect(form._fileName(makeHeader(filename))).toBe('foo.txt'); | ||
}); | ||
|
||
test(`${name}#_fileName with unescaped quote`, () => { | ||
const filename = 'my".txt'; | ||
const form = getForm(name); | ||
|
||
expect(form._fileName(makeHeader(filename))).toBe('my".txt'); | ||
}); | ||
|
||
test(`${name}#_fileName with escaped quote`, () => { | ||
const filename = 'my%22.txt'; | ||
const form = getForm(name); | ||
|
||
expect(form._fileName(makeHeader(filename))).toBe('my".txt'); | ||
}); | ||
|
||
test(`${name}#_fileName with bad quote and additional sub-header`, () => { | ||
const filename = 'my".txt'; | ||
const form = getForm(name); | ||
|
||
const header = `${makeHeader(filename)}; foo="bar"`; | ||
expect(form._fileName(header)).toBe(filename); | ||
}); | ||
|
||
test(`${name}#_fileName with semicolon`, () => { | ||
const filename = 'my;.txt'; | ||
const form = getForm(name); | ||
|
||
expect(form._fileName(makeHeader(filename))).toBe('my;.txt'); | ||
}); | ||
|
||
test(`${name}#_fileName with utf8 character`, () => { | ||
const filename = 'my☃.txt'; | ||
const form = getForm(name); | ||
|
||
expect(form._fileName(makeHeader(filename))).toBe('my☃.txt'); | ||
}); | ||
|
||
test(`${name}#_uploadPath strips harmful characters from extension when keepExtensions`, () => { | ||
const form = getForm(name, { keepExtensions: true }); | ||
|
||
let ext = path.extname(form._uploadPath('fine.jpg?foo=bar')); | ||
expect(ext).toBe('.jpg'); | ||
|
||
ext = path.extname(form._uploadPath('fine?foo=bar')); | ||
expect(ext).toBe(''); | ||
|
||
ext = path.extname(form._uploadPath('super.cr2+dsad')); | ||
expect(ext).toBe('.cr2'); | ||
|
||
ext = path.extname(form._uploadPath('super.bar')); | ||
expect(ext).toBe('.bar'); | ||
|
||
ext = path.extname(form._uploadPath('file.aAa')); | ||
expect(ext).toBe('.aAa'); | ||
}); | ||
|
||
test(`${name}#_Array parameters support`, () => { | ||
const form = getForm(name, { multiples: true }); | ||
|
||
const req = new Request(); | ||
req.headers = 'content-type: json; content-length:8'; | ||
form.parse(req, (error, fields) => { | ||
expect(Array.isArray(fields.a)).toBe(true); | ||
expect(fields.a[0]).toBe(1); | ||
expect(fields.a[1]).toBe(2); | ||
}); | ||
form.emit('field', 'a[]', 1); | ||
form.emit('field', 'a[]', 2); | ||
form.emit('end'); | ||
}); | ||
}); |
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,73 @@ | ||
'use strict'; | ||
|
||
const { MultipartParser } = require('../../src/index'); | ||
|
||
test('on constructor', () => { | ||
const parser = new MultipartParser(); | ||
expect(parser.boundary).toBeNull(); | ||
expect(parser.state).toBe(0); | ||
expect(parser.flags).toBe(0); | ||
expect(parser.boundaryChars).toBeNull(); | ||
expect(parser.index).toBeNull(); | ||
expect(parser.lookbehind).toBeNull(); | ||
expect(parser.constructor.name).toBe('MultipartParser'); | ||
}); | ||
|
||
test('initWithBoundary', () => { | ||
const boundary = 'abc'; | ||
const parser = new MultipartParser(); | ||
parser.initWithBoundary(boundary); | ||
|
||
expect(Array.prototype.slice.call(parser.boundary)).toMatchObject([ | ||
13, | ||
10, | ||
45, | ||
45, | ||
97, | ||
98, | ||
99, | ||
]); | ||
expect(parser.state).toBe(MultipartParser.STATES.START); | ||
|
||
expect(parser.boundaryChars).toMatchObject({ | ||
10: true, | ||
13: true, | ||
45: true, | ||
97: true, | ||
98: true, | ||
99: true, | ||
}); | ||
}); | ||
|
||
test('initWithBoundary failing', () => { | ||
const parser = new MultipartParser(); | ||
const boundary = 'abc'; | ||
const buffer = Buffer.alloc(5); | ||
|
||
parser.initWithBoundary(boundary); | ||
buffer.write('--ad', 0); | ||
expect(parser.bufferLength).toBe(0); | ||
|
||
parser.write(buffer); | ||
expect(parser.bufferLength).toBe(5); | ||
}); | ||
|
||
test('on .end() throwing', () => { | ||
const parser = new MultipartParser(); | ||
parser.once('error', () => {}); | ||
|
||
const res = parser.end(); | ||
expect(res.state).toBe(0); | ||
|
||
// expect(() => { parser.end() }).toThrow(/MultipartParser/); | ||
// expect(() => { parser.end() }).toThrow(/stream ended unexpectedly/); | ||
// expect(() => { parser.end() }).toThrow(parser.explain()); | ||
}); | ||
|
||
test('on .end() successful', () => { | ||
const parser = new MultipartParser(); | ||
parser.state = MultipartParser.STATES.END; | ||
|
||
const res = parser.end(); | ||
expect(res.state).toBe(12); | ||
}); |
26 changes: 10 additions & 16 deletions
26
test/unit/test-querystring-parser.js → test/unit/querystring-parser.test.js
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.