3 var assert = require('assert');
4 var Buffer = require('safe-buffer').Buffer
5 var utils = require('../lib/utils');
7 describe('utils.etag(body, encoding)', function(){
8 it('should support strings', function(){
9 assert.strictEqual(utils.etag('express!'),
10 '"8-O2uVAFaQ1rZvlKLT14RnuvjPIdg"')
13 it('should support utf8 strings', function(){
14 assert.strictEqual(utils.etag('express❤', 'utf8'),
15 '"a-JBiXf7GyzxwcrxY4hVXUwa7tmks"')
18 it('should support buffer', function(){
19 assert.strictEqual(utils.etag(Buffer.from('express!')),
20 '"8-O2uVAFaQ1rZvlKLT14RnuvjPIdg"')
23 it('should support empty string', function(){
24 assert.strictEqual(utils.etag(''),
25 '"0-2jmj7l5rSw0yVb/vlWAYkK/YBwk"')
29 describe('utils.setCharset(type, charset)', function () {
30 it('should do anything without type', function () {
31 assert.strictEqual(utils.setCharset(), undefined);
34 it('should return type if not given charset', function () {
35 assert.strictEqual(utils.setCharset('text/html'), 'text/html');
38 it('should keep charset if not given charset', function () {
39 assert.strictEqual(utils.setCharset('text/html; charset=utf-8'), 'text/html; charset=utf-8');
42 it('should set charset', function () {
43 assert.strictEqual(utils.setCharset('text/html', 'utf-8'), 'text/html; charset=utf-8');
46 it('should override charset', function () {
47 assert.strictEqual(utils.setCharset('text/html; charset=iso-8859-1', 'utf-8'), 'text/html; charset=utf-8');
51 describe('utils.wetag(body, encoding)', function(){
52 it('should support strings', function(){
53 assert.strictEqual(utils.wetag('express!'),
54 'W/"8-O2uVAFaQ1rZvlKLT14RnuvjPIdg"')
57 it('should support utf8 strings', function(){
58 assert.strictEqual(utils.wetag('express❤', 'utf8'),
59 'W/"a-JBiXf7GyzxwcrxY4hVXUwa7tmks"')
62 it('should support buffer', function(){
63 assert.strictEqual(utils.wetag(Buffer.from('express!')),
64 'W/"8-O2uVAFaQ1rZvlKLT14RnuvjPIdg"')
67 it('should support empty string', function(){
68 assert.strictEqual(utils.wetag(''),
69 'W/"0-2jmj7l5rSw0yVb/vlWAYkK/YBwk"')
73 describe('utils.isAbsolute()', function(){
74 it('should support windows', function(){
75 assert(utils.isAbsolute('c:\\'));
76 assert(utils.isAbsolute('c:/'));
77 assert(!utils.isAbsolute(':\\'));
80 it('should support windows unc', function(){
81 assert(utils.isAbsolute('\\\\foo\\bar'))
84 it('should support unices', function(){
85 assert(utils.isAbsolute('/foo/bar'));
86 assert(!utils.isAbsolute('foo/bar'));
90 describe('utils.flatten(arr)', function(){
91 it('should flatten an array', function(){
92 var arr = ['one', ['two', ['three', 'four'], 'five']];
93 var flat = utils.flatten(arr)
95 assert.strictEqual(flat.length, 5)
96 assert.strictEqual(flat[0], 'one')
97 assert.strictEqual(flat[1], 'two')
98 assert.strictEqual(flat[2], 'three')
99 assert.strictEqual(flat[3], 'four')
100 assert.strictEqual(flat[4], 'five')
101 assert.ok(flat.every(function (v) { return typeof v === 'string' }))