Skip to content
Myr

utils.js

This file

Path

test/utils.js

221 lines · 6.9 KiB · JavaScript

Last changed by

fix long list params testTJ Holowaychukbefore adoption99bc628

History of this file

Take it

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

Merge pull request #1477 from gmethvin/set_arrayTJ Holowaychukbefore adoption57cda15

lines 1–8why, one step back
1
2var utils = require('../lib/utils')3 , assert = require('assert');45describe('utils.etag(body)', function(){67 var str = 'Hello CRC';8 var strUTF8 = '<!DOCTYPE html>\n<html>\n<head>\n</head>\n<body><p>自動販売</p></body></html>';

fix long list params testTJ Holowaychukbefore adoption99bc628

line 9why, one step back
9

Merge pull request #1477 from gmethvin/set_arrayTJ Holowaychukbefore adoption57cda15

lines 10–29why, one step back
10 it('should support strings', function(){
11 utils.etag(str).should.eql('"-2034458343"');12 })1314 it('should support utf8 strings', function(){15 utils.etag(strUTF8).should.eql('"1395090196"');16 })1718 it('should support buffer', function(){19 utils.etag(new Buffer(strUTF8)).should.eql('"1395090196"');20 utils.etag(new Buffer(str)).should.eql('"-2034458343"');21 })2223})2425describe('utils.isAbsolute()', function(){26 it('should support windows', function(){27 assert(utils.isAbsolute('c:\\'));28 assert(!utils.isAbsolute(':\\'));29 })

fix long list params testTJ Holowaychukbefore adoption99bc628

line 30why, one step back
30

Merge pull request #1477 from gmethvin/set_arrayTJ Holowaychukbefore adoption57cda15

lines 31–51why, one step back
31 it('should unices', function(){
32 assert(utils.isAbsolute('/foo/bar'));33 assert(!utils.isAbsolute('foo/bar'));34 })35})3637describe('utils.flatten(arr)', function(){38 it('should flatten an array', function(){39 var arr = ['one', ['two', ['three', 'four'], 'five']];40 utils.flatten(arr)41 .should.eql(['one', 'two', 'three', 'four', 'five']);42 })43})4445describe('utils.escape(html)', function(){46 it('should escape html entities', function(){47 utils.escape('<script>foo & "bar"')48 .should.equal('&lt;script&gt;foo &amp; &quot;bar&quot;')49 })50})51

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

line 52why, one step back
52describe('utils.parseParams(str)', function(){

Merge pull request #1477 from gmethvin/set_arrayTJ Holowaychukbefore adoption57cda15

line 53why, one step back
53 it('should default quality to 1', function(){

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

line 54why, one step back
54 utils.parseParams('text/html')

Merge pull request #1578 from Notificare/masterTJ Holowaychukbefore adoption5ba6c30

line 55why, one step back
55 .should.eql([{ value: 'text/html', quality: 1, params: {}, originalIndex: 0 }]);

Merge pull request #1477 from gmethvin/set_arrayTJ Holowaychukbefore adoption57cda15

line 56why, one step back
56 })

fix long list params testTJ Holowaychukbefore adoption99bc628

line 57why, one step back
57

Merge pull request #1477 from gmethvin/set_arrayTJ Holowaychukbefore adoption57cda15

line 58why, one step back
58 it('should parse qvalues', function(){

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

line 59why, one step back
59 utils.parseParams('text/html; q=0.5')

Merge pull request #1578 from Notificare/masterTJ Holowaychukbefore adoption5ba6c30

line 60why, one step back
60 .should.eql([{ value: 'text/html', quality: 0.5, params: {}, originalIndex: 0 }]);

Merge pull request #1477 from gmethvin/set_arrayTJ Holowaychukbefore adoption57cda15

line 61why, one step back
61

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

line 62why, one step back
62 utils.parseParams('text/html; q=.2')

Merge pull request #1578 from Notificare/masterTJ Holowaychukbefore adoption5ba6c30

line 63why, one step back
63 .should.eql([{ value: 'text/html', quality: 0.2, params: {}, originalIndex: 0 }]);

Merge pull request #1477 from gmethvin/set_arrayTJ Holowaychukbefore adoption57cda15

line 64why, one step back
64 })

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

lines 65–67why, one step back
65
66 it('should parse accept parameters', function(){67 utils.parseParams('application/json; ver=2.0')

Merge pull request #1578 from Notificare/masterTJ Holowaychukbefore adoption5ba6c30

line 68why, one step back
68 .should.eql([{ value: 'application/json', quality: 1, params: {ver: "2.0"}, originalIndex: 0 }]);

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

lines 69–70why, one step back
69
70 utils.parseParams('text/html; q=0.5; level=2')

Merge pull request #1578 from Notificare/masterTJ Holowaychukbefore adoption5ba6c30

line 71why, one step back
71 .should.eql([{ value: 'text/html', quality: 0.5, params: {level: "2"}, originalIndex: 0 }]);

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

line 72why, one step back
72 utils.parseParams('text/html;q=.2;ver=beta')

Merge pull request #1578 from Notificare/masterTJ Holowaychukbefore adoption5ba6c30

line 73why, one step back
73 .should.eql([{ value: 'text/html', quality: 0.2, params: {ver: "beta"}, originalIndex: 0 }]);

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

lines 74–75why, one step back
74 })
75

Merge pull request #1477 from gmethvin/set_arrayTJ Holowaychukbefore adoption57cda15

line 76why, one step back
76 it('should work with messed up whitespace', function(){

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

line 77why, one step back
77 utils.parseParams('text/html ; q = .2')

Merge pull request #1578 from Notificare/masterTJ Holowaychukbefore adoption5ba6c30

line 78why, one step back
78 .should.eql([{ value: 'text/html', quality: 0.2, params: {}, originalIndex: 0 }]);

Merge pull request #1477 from gmethvin/set_arrayTJ Holowaychukbefore adoption57cda15

line 79why, one step back
79 })

fix long list params testTJ Holowaychukbefore adoption99bc628

line 80why, one step back
80

Merge pull request #1477 from gmethvin/set_arrayTJ Holowaychukbefore adoption57cda15

lines 81–82why, one step back
81 it('should work with multiples', function(){
82 var str = 'da, en;q=.5, en-gb;q=.8';

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

line 83why, one step back
83 var arr = utils.parseParams(str);

Merge pull request #1477 from gmethvin/set_arrayTJ Holowaychukbefore adoption57cda15

lines 84–86why, one step back
84 arr[0].value.should.equal('da');
85 arr[1].value.should.equal('en-gb');86 arr[2].value.should.equal('en');

Merge pull request #1578 from Notificare/masterTJ Holowaychukbefore adoption5ba6c30

lines 87–90why, one step back
87 })
8889 it('should work with long lists', function(){90 var str = 'en, nl, fr, de, ja, it, es, pt, pt-PT, da, fi, nb, sv, ko, zh-Hans, zh-Hant, ru, pl';

fix long list params testTJ Holowaychukbefore adoption99bc628

lines 91–92why, one step back
91 var arr = utils.parseParams(str).map(function(o){ return o.value });
92 arr.should.eql(str.split(', '));

Merge pull request #1477 from gmethvin/set_arrayTJ Holowaychukbefore adoption57cda15

line 93why, one step back
93 })

fix long list params testTJ Holowaychukbefore adoption99bc628

line 94why, one step back
94

Merge pull request #1477 from gmethvin/set_arrayTJ Holowaychukbefore adoption57cda15

lines 95–96why, one step back
95 it('should sort by quality', function(){
96 var str = 'text/plain;q=.2, application/json, text/html;q=0.5';

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

line 97why, one step back
97 var arr = utils.parseParams(str);

Merge pull request #1477 from gmethvin/set_arrayTJ Holowaychukbefore adoption57cda15

lines 98–101why, one step back
98 arr[0].value.should.equal('application/json');
99 arr[1].value.should.equal('text/html');100 arr[2].value.should.equal('text/plain');101 })

fix long list params testTJ Holowaychukbefore adoption99bc628

line 102why, one step back
102

Merge pull request #1477 from gmethvin/set_arrayTJ Holowaychukbefore adoption57cda15

lines 103–104why, one step back
103 it('should exclude those with a quality of 0', function(){
104 var str = 'text/plain;q=.2, application/json, text/html;q=0';

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

line 105why, one step back
105 var arr = utils.parseParams(str);

Merge pull request #1477 from gmethvin/set_arrayTJ Holowaychukbefore adoption57cda15

lines 106–114why, one step back
106 arr.should.have.length(2);
107 })108})109110describe('utils.parseAccept(str)', function(){111 it('should provide .type', function(){112 var arr = utils.parseAccept('text/html');113 arr[0].type.should.equal('text');114 })

fix long list params testTJ Holowaychukbefore adoption99bc628

line 115why, one step back
115

Merge pull request #1477 from gmethvin/set_arrayTJ Holowaychukbefore adoption57cda15

lines 116–128why, one step back
116 it('should provide .subtype', function(){
117 var arr = utils.parseAccept('text/html');118 arr[0].subtype.should.equal('html');119 })120})121122describe('utils.accepts(type, str)', function(){123 describe('when a string is not given', function(){124 it('should return the value', function(){125 utils.accepts('text/html')126 .should.equal('text/html');127 })128 })

fix long list params testTJ Holowaychukbefore adoption99bc628

line 129why, one step back
129

Merge pull request #1477 from gmethvin/set_arrayTJ Holowaychukbefore adoption57cda15

lines 130–174why, one step back
130 describe('when a string is empty', function(){
131 it('should return the value', function(){132 utils.accepts('text/html', '')133 .should.equal('text/html');134 })135 })136137 describe('when */* is given', function(){138 it('should return the value', function(){139 utils.accepts('text/html', 'text/plain, */*')140 .should.equal('text/html');141 })142 })143144 describe('when an array is given', function(){145 it('should return the best match', function(){146 utils.accepts(['html', 'json'], 'text/plain, application/json')147 .should.equal('json');148149 utils.accepts(['html', 'application/json'], 'text/plain, application/json')150 .should.equal('application/json');151152 utils.accepts(['text/html', 'application/json'], 'application/json;q=.5, text/html')153 .should.equal('text/html');154 })155 })156157 describe('when a comma-delimited list is give', function(){158 it('should behave like an array', function(){159 utils.accepts('html, json', 'text/plain, application/json')160 .should.equal('json');161162 utils.accepts('html, application/json', 'text/plain, application/json')163 .should.equal('application/json');164165 utils.accepts('text/html, application/json', 'application/json;q=.5, text/html')166 .should.equal('text/html');167 })168 })169170 describe('when accepting type/subtype', function(){171 it('should return the value when present', function(){172 utils.accepts('text/html', 'text/plain, text/html')173 .should.equal('text/html');174 })

fix long list params testTJ Holowaychukbefore adoption99bc628

line 175why, one step back
175

Merge pull request #1477 from gmethvin/set_arrayTJ Holowaychukbefore adoption57cda15

lines 176–179why, one step back
176 it('should return undefined otherwise', function(){
177 assert(null == utils.accepts('text/html', 'text/plain, application/json'));178 })179 })

fix long list params testTJ Holowaychukbefore adoption99bc628

line 180why, one step back
180

Merge pull request #1477 from gmethvin/set_arrayTJ Holowaychukbefore adoption57cda15

lines 181–185why, one step back
181 describe('when accepting */subtype', function(){
182 it('should return the value when present', function(){183 utils.accepts('text/html', 'text/*')184 .should.equal('text/html');185 })

fix long list params testTJ Holowaychukbefore adoption99bc628

line 186why, one step back
186

Merge pull request #1477 from gmethvin/set_arrayTJ Holowaychukbefore adoption57cda15

lines 187–190why, one step back
187 it('should return undefined otherwise', function(){
188 assert(null == utils.accepts('text/html', 'image/*'));189 })190 })

fix long list params testTJ Holowaychukbefore adoption99bc628

line 191why, one step back
191

Merge pull request #1477 from gmethvin/set_arrayTJ Holowaychukbefore adoption57cda15

lines 192–196why, one step back
192 describe('when accepting type/*', function(){
193 it('should return the value when present', function(){194 utils.accepts('text/html', '*/html')195 .should.equal('text/html');196 })

fix long list params testTJ Holowaychukbefore adoption99bc628

line 197why, one step back
197

Merge pull request #1477 from gmethvin/set_arrayTJ Holowaychukbefore adoption57cda15

lines 198–201why, one step back
198 it('should return undefined otherwise', function(){
199 assert(null == utils.accepts('text/html', '*/json'));200 })201 })

fix long list params testTJ Holowaychukbefore adoption99bc628

line 202why, one step back
202

Merge pull request #1477 from gmethvin/set_arrayTJ Holowaychukbefore adoption57cda15

lines 203–207why, one step back
203 describe('when an extension is given', function(){
204 it('should return the value when present', function(){205 utils.accepts('html', 'text/html, application/json')206 .should.equal('html');207 })

fix long list params testTJ Holowaychukbefore adoption99bc628

line 208why, one step back
208

Merge pull request #1477 from gmethvin/set_arrayTJ Holowaychukbefore adoption57cda15

lines 209–211why, one step back
209 it('should return undefined otherwise', function(){
210 assert(null == utils.accepts('html', 'text/plain, application/json'));211 })

fix long list params testTJ Holowaychukbefore adoption99bc628

line 212why, one step back
212

Merge pull request #1477 from gmethvin/set_arrayTJ Holowaychukbefore adoption57cda15

lines 213–221why, one step back
213 it('should support *', function(){
214 utils.accepts('html', 'text/*')215 .should.equal('html');216217 utils.accepts('html', '*/html')218 .should.equal('html');219 })220 })221})