utils.js
This file
Path
test/utils.js
115 lines · 3.7 KiB · JavaScript
Last changed by
test: add test for normalizeType fallback when mime lookup fails (#6894)Ayoub Mabroukbefore adoption6b7ccfc
History of this fileTake it
The walk stopped after 200 commits: lines older than af341b0 point at it.
Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 1why, one step back1'use strict'deps: body-parser@2.0.0-beta.1Douglas Christopher Wilsonbefore adoptionaf341b0
line 2why, one step back2fix(refactor): prefix built-in node module importsSzymon Łągiewkabefore adoption4111359
line 3why, one step back3var assert = require('node:assert');chore: enforce explicit Buffer import and add lint rule (#6525)Shivam Sharmabefore adoption98c85eb
line 4why, one step back4const { Buffer } = require('node:buffer');deps: body-parser@2.0.0-beta.1Douglas Christopher Wilsonbefore adoptionaf341b0
lines 5–8why, one step back5var utils = require('../lib/utils');67describe('utils.etag(body, encoding)', function(){8 it('should support strings', function(){Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
lines 9–10why, one step back9 assert.strictEqual(utils.etag('express!'),10 '"8-O2uVAFaQ1rZvlKLT14RnuvjPIdg"')deps: body-parser@2.0.0-beta.1Douglas Christopher Wilsonbefore adoptionaf341b0
lines 11–13why, one step back11 })1213 it('should support utf8 strings', function(){Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
lines 14–15why, one step back14 assert.strictEqual(utils.etag('express❤', 'utf8'),15 '"a-JBiXf7GyzxwcrxY4hVXUwa7tmks"')deps: body-parser@2.0.0-beta.1Douglas Christopher Wilsonbefore adoptionaf341b0
lines 16–18why, one step back16 })1718 it('should support buffer', function(){Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
lines 19–20why, one step back19 assert.strictEqual(utils.etag(Buffer.from('express!')),20 '"8-O2uVAFaQ1rZvlKLT14RnuvjPIdg"')deps: body-parser@2.0.0-beta.1Douglas Christopher Wilsonbefore adoptionaf341b0
lines 21–23why, one step back21 })2223 it('should support empty string', function(){Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
lines 24–25why, one step back24 assert.strictEqual(utils.etag(''),25 '"0-2jmj7l5rSw0yVb/vlWAYkK/YBwk"')deps: body-parser@2.0.0-beta.1Douglas Christopher Wilsonbefore adoptionaf341b0
lines 26–28why, one step back26 })27})28increased code coverage of utils.js file (#6386)Ashish Sharmabefore adoption5da5a11
lines 29–38why, one step back29describe('utils.normalizeType acceptParams method', () => {30 it('should handle a type with a malformed parameter and break the loop in acceptParams', () => {31 const result = utils.normalizeType('text/plain;invalid');32 assert.deepEqual(result,{33 value: 'text/plain',34 quality: 1,35 params: {} // No parameters are added since "invalid" has no "="36 });37 });38test: add test for normalizeType fallback when mime lookup fails (#6894)Ayoub Mabroukbefore adoption6b7ccfc
lines 39–46why, one step back39 it('should default to application/octet-stream when mime lookup fails', () => {40 const result = utils.normalizeType('unknown-extension-xyz');41 assert.deepEqual(result, {42 value: 'application/octet-stream',43 params: {}44 });45 });46});increased code coverage of utils.js file (#6386)Ashish Sharmabefore adoption5da5a11
line 47why, one step back47deps: body-parser@2.0.0-beta.1Douglas Christopher Wilsonbefore adoptionaf341b0
lines 48–71why, one step back48describe('utils.setCharset(type, charset)', function () {49 it('should do anything without type', function () {50 assert.strictEqual(utils.setCharset(), undefined);51 });5253 it('should return type if not given charset', function () {54 assert.strictEqual(utils.setCharset('text/html'), 'text/html');55 });5657 it('should keep charset if not given charset', function () {58 assert.strictEqual(utils.setCharset('text/html; charset=utf-8'), 'text/html; charset=utf-8');59 });6061 it('should set charset', function () {62 assert.strictEqual(utils.setCharset('text/html', 'utf-8'), 'text/html; charset=utf-8');63 });6465 it('should override charset', function () {66 assert.strictEqual(utils.setCharset('text/html; charset=iso-8859-1', 'utf-8'), 'text/html; charset=utf-8');67 });68});6970describe('utils.wetag(body, encoding)', function(){71 it('should support strings', function(){Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
lines 72–73why, one step back72 assert.strictEqual(utils.wetag('express!'),73 'W/"8-O2uVAFaQ1rZvlKLT14RnuvjPIdg"')deps: body-parser@2.0.0-beta.1Douglas Christopher Wilsonbefore adoptionaf341b0
lines 74–76why, one step back74 })7576 it('should support utf8 strings', function(){Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
lines 77–78why, one step back77 assert.strictEqual(utils.wetag('express❤', 'utf8'),78 'W/"a-JBiXf7GyzxwcrxY4hVXUwa7tmks"')deps: body-parser@2.0.0-beta.1Douglas Christopher Wilsonbefore adoptionaf341b0
lines 79–81why, one step back79 })8081 it('should support buffer', function(){Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
lines 82–83why, one step back82 assert.strictEqual(utils.wetag(Buffer.from('express!')),83 'W/"8-O2uVAFaQ1rZvlKLT14RnuvjPIdg"')deps: body-parser@2.0.0-beta.1Douglas Christopher Wilsonbefore adoptionaf341b0
lines 84–86why, one step back84 })8586 it('should support empty string', function(){Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
lines 87–88why, one step back87 assert.strictEqual(utils.wetag(''),88 'W/"0-2jmj7l5rSw0yVb/vlWAYkK/YBwk"')deps: body-parser@2.0.0-beta.1Douglas Christopher Wilsonbefore adoptionaf341b0
lines 89–90why, one step back89 })90})test: added unit tests for utils.compileETag to cover valid and invalid inputs (#6534)sukdev24before adoption912893c
lines 91–115why, one step back9192describe('utils.compileETag()', function () {93 it('should return generateETag for true', function () {94 const fn = utils.compileETag(true);95 assert.strictEqual(fn('express!'), utils.wetag('express!'));96 });9798 it('should return undefined for false', function () {99 assert.strictEqual(utils.compileETag(false), undefined);100 });101102 it('should return generateETag for string values "strong" and "weak"', function () {103 assert.strictEqual(utils.compileETag('strong')("express"), utils.etag("express"));104 assert.strictEqual(utils.compileETag('weak')("express"), utils.wetag("express"));105 });106107 it('should throw for unknown string values', function () {108 assert.throws(() => utils.compileETag('foo'), TypeError);109 });110111 it('should throw for unsupported types like arrays and objects', function () {112 assert.throws(() => utils.compileETag([]), TypeError);113 assert.throws(() => utils.compileETag({}), TypeError);114 });115});