Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than ee40a88 point at it.

call callback once on listen errorWesbefore adoptionee40a88

lines 1–2why, one step back
1'use strict'
2

fix(refactor): prefix built-in node module importsSzymon Łągiewkabefore adoption4111359

line 3why, one step back
3var assert = require('node:assert');

chore: enforce explicit Buffer import and add lint rule (#6525)Shivam Sharmabefore adoption98c85eb

line 4why, one step back
4const { Buffer } = require('node:buffer');

call callback once on listen errorWesbefore adoptionee40a88

lines 5–28why, one step back
5var utils = require('../lib/utils');
67describe('utils.etag(body, encoding)', function(){8 it('should support strings', function(){9 assert.strictEqual(utils.etag('express!'),10 '"8-O2uVAFaQ1rZvlKLT14RnuvjPIdg"')11 })1213 it('should support utf8 strings', function(){14 assert.strictEqual(utils.etag('express❤', 'utf8'),15 '"a-JBiXf7GyzxwcrxY4hVXUwa7tmks"')16 })1718 it('should support buffer', function(){19 assert.strictEqual(utils.etag(Buffer.from('express!')),20 '"8-O2uVAFaQ1rZvlKLT14RnuvjPIdg"')21 })2223 it('should support empty string', function(){24 assert.strictEqual(utils.etag(''),25 '"0-2jmj7l5rSw0yVb/vlWAYkK/YBwk"')26 })27})28

increased code coverage of utils.js file (#6386)Ashish Sharmabefore adoption5da5a11

lines 29–38why, one step back
29describe('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 });38

test: add test for normalizeType fallback when mime lookup fails (#6894)Ayoub Mabroukbefore adoption6b7ccfc

lines 39–46why, one step back
39 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 back
47

call callback once on listen errorWesbefore adoptionee40a88

lines 48–90why, one step back
48describe('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(){72 assert.strictEqual(utils.wetag('express!'),73 'W/"8-O2uVAFaQ1rZvlKLT14RnuvjPIdg"')74 })7576 it('should support utf8 strings', function(){77 assert.strictEqual(utils.wetag('express❤', 'utf8'),78 'W/"a-JBiXf7GyzxwcrxY4hVXUwa7tmks"')79 })8081 it('should support buffer', function(){82 assert.strictEqual(utils.wetag(Buffer.from('express!')),83 'W/"8-O2uVAFaQ1rZvlKLT14RnuvjPIdg"')84 })8586 it('should support empty string', function(){87 assert.strictEqual(utils.wetag(''),88 'W/"0-2jmj7l5rSw0yVb/vlWAYkK/YBwk"')89 })90})

test: added unit tests for utils.compileETag to cover valid and invalid inputs (#6534)sukdev24before adoption912893c

lines 91–115why, one step back
91
92describe('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});