-
-
Notifications
You must be signed in to change notification settings - Fork 744
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: normalize common options for all parsers
- Loading branch information
1 parent
4a9ed7e
commit d93b76d
Showing
7 changed files
with
177 additions
and
73 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
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
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,123 @@ | ||
'use strict' | ||
|
||
const assert = require('node:assert') | ||
const { normalizeOptions } = require('../lib/utils.js') | ||
|
||
describe('normalizeOptions(options, defaultType)', () => { | ||
it('should return default options when no options are provided', () => { | ||
for (const options of [undefined, null, {}]) { | ||
const result = normalizeOptions(options, 'application/json') | ||
assert.strictEqual(result.inflate, true) | ||
assert.strictEqual(result.limit, 100 * 1024) // 100kb in bytes | ||
assert.strictEqual(result.verify, false) | ||
assert.strictEqual(typeof result.shouldParse, 'function') | ||
} | ||
}) | ||
|
||
it('should override default options with provided options', () => { | ||
const options = { | ||
inflate: false, | ||
limit: '200kb', | ||
type: 'application/xml', | ||
verify: () => {} | ||
} | ||
const result = normalizeOptions(options, 'application/json') | ||
assert.strictEqual(result.inflate, false) | ||
assert.strictEqual(result.limit, 200 * 1024) // 200kb in bytes | ||
assert.strictEqual(result.verify, options.verify) | ||
assert.strictEqual(typeof result.shouldParse, 'function') | ||
}) | ||
|
||
it('should remove additional options', () => { | ||
const options = { | ||
inflate: false, | ||
limit: '200kb', | ||
type: 'application/xml', | ||
verify: () => {}, | ||
additional: 'option', | ||
something: 'weird' | ||
} | ||
const result = normalizeOptions(options, 'application/json') | ||
assert.strictEqual(result.inflate, false) | ||
assert.strictEqual(result.limit, 200 * 1024) // 200kb in bytes | ||
assert.strictEqual(result.verify, options.verify) | ||
assert.strictEqual(typeof result.shouldParse, 'function') | ||
assert.strictEqual(result.additional, undefined) | ||
assert.strictEqual(result.something, undefined) | ||
}) | ||
|
||
describe('options', () => { | ||
describe('verify', () => { | ||
it('should throw an error if verify is not a function', () => { | ||
assert.throws(() => { | ||
normalizeOptions({ verify: 'not a function' }, 'application/json') | ||
}, /option verify must be function/) | ||
}) | ||
|
||
it('should accept a verify function', () => { | ||
const verify = () => {} | ||
const result = normalizeOptions({ verify }, 'application/json') | ||
assert.strictEqual(result.verify, verify) | ||
}) | ||
}) | ||
|
||
describe('limit', () => { | ||
it('should return the default limit if limit is not provided', () => { | ||
const result = normalizeOptions({}, 'application/json') | ||
assert.strictEqual(result.limit, 100 * 1024) // 100kb in bytes | ||
}) | ||
|
||
it('should accept a number limit', () => { | ||
const result = normalizeOptions({ limit: 1234 }, 'application/json') | ||
assert.strictEqual(result.limit, 1234) | ||
}) | ||
|
||
it('should parse a string limit', () => { | ||
const result = normalizeOptions({ limit: '200kb' }, 'application/json') | ||
assert.strictEqual(result.limit, 200 * 1024) // 200kb in bytes | ||
}) | ||
|
||
it('should return null for an invalid limit', () => { | ||
const result = normalizeOptions({ limit: 'invalid' }, 'application/json') | ||
assert.strictEqual(result.limit, null) | ||
}) | ||
}) | ||
|
||
describe('type', () => { | ||
it('should return the default type if type is not provided', () => { | ||
const result = normalizeOptions({}, 'application/json') | ||
assert.strictEqual(result.shouldParse({ headers: { 'content-type': 'application/json', 'content-length': '1024' } }), true) | ||
assert.strictEqual(result.shouldParse({ headers: { 'content-type': 'application/xml', 'content-length': '1024' } }), false) | ||
}) | ||
|
||
it('should accept a string type', () => { | ||
const result = normalizeOptions({ type: 'application/xml' }, 'application/json') | ||
assert.strictEqual(result.shouldParse({ headers: { 'content-type': 'application/xml', 'content-length': '1024' } }), true) | ||
assert.strictEqual(result.shouldParse({ headers: { 'content-type': 'application/json', 'content-length': '1024' } }), false) | ||
}) | ||
|
||
it('should accept a type checking function', () => { | ||
const result = normalizeOptions({ type: () => true }, 'application/json') | ||
assert.strictEqual(result.shouldParse({ headers: { 'content-type': 'application/xml' } }), true) | ||
assert.strictEqual(result.shouldParse({ headers: { 'content-type': 'application/json' } }), true) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('defaultType', () => { | ||
it('should throw an error if defaultType is not provided', () => { | ||
assert.throws(() => { | ||
normalizeOptions({}) | ||
}, /defaultType must be provided/) | ||
assert.throws(() => { | ||
normalizeOptions({}, undefined) | ||
}, /defaultType must be provided/) | ||
}) | ||
|
||
it('should throw an error if defaultType is not a string', () => { | ||
assert.throws(() => { | ||
normalizeOptions({}, 123) | ||
}, /defaultType must be provided/) | ||
}) | ||
}) | ||
}) |