Skip to content
Myr

utils.js

This file

Path

test/utils.js

250 lines · 7.5 KiB · JavaScript

Last changed by

Use etag module to generate ETag headersDouglas Christopher Wilsonbefore adoption4d1ee23

History of this file

Take it

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption766b3ae

lines 1–4why, one step back
1
2var utils = require('../lib/utils')3 , assert = require('assert');4

tests: add more etag testsDouglas Christopher Wilsonbefore adoption3e35845

line 5why, one step back
5describe('utils.etag(body, encoding)', function(){

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption766b3ae

line 6why, one step back
6 it('should support strings', function(){

tests: add more etag testsDouglas Christopher Wilsonbefore adoption3e35845

line 7why, one step back
7 utils.etag('express!')

improve etag control for res.sendDouglas Christopher Wilsonbefore adoptionc21226a

line 8why, one step back
8 .should.eql('"zZdv4imtWD49AHEviejT6A=="')

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption766b3ae

lines 9–11why, one step back
9 })
1011 it('should support utf8 strings', function(){

tests: add more etag testsDouglas Christopher Wilsonbefore adoption3e35845

line 12why, one step back
12 utils.etag('express❤', 'utf8')

improve etag control for res.sendDouglas Christopher Wilsonbefore adoptionc21226a

line 13why, one step back
13 .should.eql('"fsFba4IxwQS6h6Umb+FNxw=="')

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption766b3ae

lines 14–16why, one step back
14 })
1516 it('should support buffer', function(){

tests: add more etag testsDouglas Christopher Wilsonbefore adoption3e35845

lines 17–18why, one step back
17 var buf = new Buffer('express!')
18 utils.etag(buf)

improve etag control for res.sendDouglas Christopher Wilsonbefore adoptionc21226a

line 19why, one step back
19 .should.eql('"zZdv4imtWD49AHEviejT6A=="');

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption766b3ae

lines 20–21why, one step back
20 })
21

tests: add more etag testsDouglas Christopher Wilsonbefore adoption3e35845

lines 22–23why, one step back
22 it('should support empty string', function(){
23 utils.etag('')

improve etag control for res.sendDouglas Christopher Wilsonbefore adoptionc21226a

lines 24–30why, one step back
24 .should.eql('"1B2M2Y8AsgTpgAmY7PhCfg=="');
25 })26})2728describe('utils.wetag(body, encoding)', function(){29 it('should support strings', function(){30 utils.wetag('express!')

Use etag module to generate ETag headersDouglas Christopher Wilsonbefore adoption4d1ee23

line 31why, one step back
31 .should.eql('W/"8-b8aabac7"')

improve etag control for res.sendDouglas Christopher Wilsonbefore adoptionc21226a

lines 32–35why, one step back
32 })
3334 it('should support utf8 strings', function(){35 utils.wetag('express❤', 'utf8')

Use etag module to generate ETag headersDouglas Christopher Wilsonbefore adoption4d1ee23

line 36why, one step back
36 .should.eql('W/"a-686b0af1"')

improve etag control for res.sendDouglas Christopher Wilsonbefore adoptionc21226a

lines 37–41why, one step back
37 })
3839 it('should support buffer', function(){40 var buf = new Buffer('express!')41 utils.wetag(buf)

Use etag module to generate ETag headersDouglas Christopher Wilsonbefore adoption4d1ee23

line 42why, one step back
42 .should.eql('W/"8-b8aabac7"');

improve etag control for res.sendDouglas Christopher Wilsonbefore adoptionc21226a

lines 43–46why, one step back
43 })
4445 it('should support empty string', function(){46 utils.wetag('')

tests: add more etag testsDouglas Christopher Wilsonbefore adoption3e35845

lines 47–48why, one step back
47 .should.eql('W/"0-0"');
48 })

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption766b3ae

lines 49–56why, one step back
49})
5051describe('utils.isAbsolute()', function(){52 it('should support windows', function(){53 assert(utils.isAbsolute('c:\\'));54 assert(!utils.isAbsolute(':\\'));55 })56

tests: add more testsDouglas Christopher Wilsonbefore adoption8751d7e

lines 57–61why, one step back
57 it('should support windows unc', function(){
58 assert(utils.isAbsolute('\\\\foo\\bar'))59 })6061 it('should support unices', function(){

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption766b3ae

lines 62–250why, one step back
62 assert(utils.isAbsolute('/foo/bar'));
63 assert(!utils.isAbsolute('foo/bar'));64 })65})6667describe('utils.flatten(arr)', function(){68 it('should flatten an array', function(){69 var arr = ['one', ['two', ['three', 'four'], 'five']];70 utils.flatten(arr)71 .should.eql(['one', 'two', 'three', 'four', 'five']);72 })73})7475describe('utils.parseParams(str)', function(){76 it('should default quality to 1', function(){77 utils.parseParams('text/html')78 .should.eql([{ value: 'text/html', quality: 1, params: {}, originalIndex: 0 }]);79 })8081 it('should parse qvalues', function(){82 utils.parseParams('text/html; q=0.5')83 .should.eql([{ value: 'text/html', quality: 0.5, params: {}, originalIndex: 0 }]);8485 utils.parseParams('text/html; q=.2')86 .should.eql([{ value: 'text/html', quality: 0.2, params: {}, originalIndex: 0 }]);87 })8889 it('should parse accept parameters', function(){90 utils.parseParams('application/json; ver=2.0')91 .should.eql([{ value: 'application/json', quality: 1, params: {ver: "2.0"}, originalIndex: 0 }]);9293 utils.parseParams('text/html; q=0.5; level=2')94 .should.eql([{ value: 'text/html', quality: 0.5, params: {level: "2"}, originalIndex: 0 }]);95 utils.parseParams('text/html;q=.2;ver=beta')96 .should.eql([{ value: 'text/html', quality: 0.2, params: {ver: "beta"}, originalIndex: 0 }]);97 })9899 it('should work with messed up whitespace', function(){100 utils.parseParams('text/html ; q = .2')101 .should.eql([{ value: 'text/html', quality: 0.2, params: {}, originalIndex: 0 }]);102 })103104 it('should work with multiples', function(){105 var str = 'da, en;q=.5, en-gb;q=.8';106 var arr = utils.parseParams(str);107 arr[0].value.should.equal('da');108 arr[1].value.should.equal('en-gb');109 arr[2].value.should.equal('en');110 })111112 it('should work with long lists', function(){113 var str = 'en, nl, fr, de, ja, it, es, pt, pt-PT, da, fi, nb, sv, ko, zh-Hans, zh-Hant, ru, pl';114 var arr = utils.parseParams(str).map(function(o){ return o.value });115 arr.should.eql(str.split(', '));116 })117118 it('should sort by quality', function(){119 var str = 'text/plain;q=.2, application/json, text/html;q=0.5';120 var arr = utils.parseParams(str);121 arr[0].value.should.equal('application/json');122 arr[1].value.should.equal('text/html');123 arr[2].value.should.equal('text/plain');124 })125126 it('should exclude those with a quality of 0', function(){127 var str = 'text/plain;q=.2, application/json, text/html;q=0';128 var arr = utils.parseParams(str);129 arr.should.have.length(2);130 })131})132133describe('utils.parseAccept(str)', function(){134 it('should provide .type', function(){135 var arr = utils.parseAccept('text/html');136 arr[0].type.should.equal('text');137 })138139 it('should provide .subtype', function(){140 var arr = utils.parseAccept('text/html');141 arr[0].subtype.should.equal('html');142 })143})144145describe('utils.accepts(type, str)', function(){146 describe('when a string is not given', function(){147 it('should return the value', function(){148 utils.accepts('text/html')149 .should.equal('text/html');150 })151 })152153 describe('when a string is empty', function(){154 it('should return the value', function(){155 utils.accepts('text/html', '')156 .should.equal('text/html');157 })158 })159160 describe('when */* is given', function(){161 it('should return the value', function(){162 utils.accepts('text/html', 'text/plain, */*')163 .should.equal('text/html');164 })165 })166167 describe('when an array is given', function(){168 it('should return the best match', function(){169 utils.accepts(['html', 'json'], 'text/plain, application/json')170 .should.equal('json');171172 utils.accepts(['html', 'application/json'], 'text/plain, application/json')173 .should.equal('application/json');174175 utils.accepts(['text/html', 'application/json'], 'application/json;q=.5, text/html')176 .should.equal('text/html');177 })178 })179180 describe('when a comma-delimited list is give', function(){181 it('should behave like an array', function(){182 utils.accepts('html, json', 'text/plain, application/json')183 .should.equal('json');184185 utils.accepts('html, application/json', 'text/plain, application/json')186 .should.equal('application/json');187188 utils.accepts('text/html, application/json', 'application/json;q=.5, text/html')189 .should.equal('text/html');190 })191 })192193 describe('when accepting type/subtype', function(){194 it('should return the value when present', function(){195 utils.accepts('text/html', 'text/plain, text/html')196 .should.equal('text/html');197 })198199 it('should return undefined otherwise', function(){200 assert(null == utils.accepts('text/html', 'text/plain, application/json'));201 })202 })203204 describe('when accepting */subtype', function(){205 it('should return the value when present', function(){206 utils.accepts('text/html', 'text/*')207 .should.equal('text/html');208 })209210 it('should return undefined otherwise', function(){211 assert(null == utils.accepts('text/html', 'image/*'));212 })213 })214215 describe('when accepting type/*', function(){216 it('should return the value when present', function(){217 utils.accepts('text/html', '*/html')218 .should.equal('text/html');219 })220221 it('should return undefined otherwise', function(){222 assert(null == utils.accepts('text/html', '*/json'));223 })224 })225226 describe('when an extension is given', function(){227 it('should return the value when present', function(){228 utils.accepts('html', 'text/html, application/json')229 .should.equal('html');230 })231232 it('should return undefined otherwise', function(){233 assert(null == utils.accepts('html', 'text/plain, application/json'));234 })235236 it('should support *', function(){237 utils.accepts('html', 'text/*')238 .should.equal('html');239240 utils.accepts('html', '*/html')241 .should.equal('html');242 })243 })244245 describe('when params included', function(){246 it('should match params', function(){247 assert(null == utils.accepts('text/html; charset=us-ascii', 'text/html; charset=utf-8'));248 })249 })250})