Skip to content
Myr

utils.js

This file

Path

test/utils.js

164 lines · 4.5 KiB · JavaScript

Last changed by

Fixed parseQuality(), exclude q=0TJ Holowaychukbefore adoptione44c874

History of this file

Take it

utils.isAbsolute() testsTj Holowaychukbefore adoption5221ecc

lines 1–16why, one step back
1
23var utils = require('../lib/utils')4 , assert = require('assert');56describe('utils.isAbsolute()', function(){7 it('should support windows', function(){8 assert(utils.isAbsolute('c:\\'));9 assert(!utils.isAbsolute(':\\'));10 })11 12 it('should unices', function(){13 assert(utils.isAbsolute('/foo/bar'));14 assert(!utils.isAbsolute('foo/bar'));15 })16})

Added utils.flatten(arr) testsTj Holowaychukbefore adoptiond56e5d6

lines 17–20why, one step back
17
18describe('utils.flatten(arr)', function(){19 it('should flatten an array', function(){20 var arr = ['one', ['two', ['three', 'four'], 'five']];

Added utils.escape(html) testTj Holowaychukbefore adoptionce2ec94

line 21why, one step back
21 utils.flatten(arr)

Added utils.flatten(arr) testsTj Holowaychukbefore adoptiond56e5d6

line 22why, one step back
22 .should.eql(['one', 'two', 'three', 'four', 'five']);

Added utils.escape(html) testTj Holowaychukbefore adoptionce2ec94

lines 23–29why, one step back
23 })
24})2526describe('utils.escape(html)', function(){27 it('should escape html entities', function(){28 utils.escape('<script>foo & "bar"')29 .should.equal('&lt;script&gt;foo &amp; &quot;bar&quot;')

Added utils.flatten(arr) testsTj Holowaychukbefore adoptiond56e5d6

lines 30–31why, one step back
30 })
31})

Added utils.parseAccepts(str)TJ Holowaychukbefore adoptionfe5efa5

line 32why, one step back
32

renamed prev util to parseQuality()TJ Holowaychukbefore adoption29d4ffd

line 33why, one step back
33describe('utils.parseQuality(str)', function(){

Added utils.parseAccepts(str)TJ Holowaychukbefore adoptionfe5efa5

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

renamed prev util to parseQuality()TJ Holowaychukbefore adoption29d4ffd

lines 35–36why, one step back
35 utils.parseQuality('text/html')
36 .should.eql([{ value: 'text/html', quality: 1 }]);

Added utils.parseAccepts(str)TJ Holowaychukbefore adoptionfe5efa5

lines 37–39why, one step back
37 })
38 39 it('should parse qvalues', function(){

renamed prev util to parseQuality()TJ Holowaychukbefore adoption29d4ffd

lines 40–41why, one step back
40 utils.parseQuality('text/html; q=0.5')
41 .should.eql([{ value: 'text/html', quality: 0.5 }]);

Added utils.parseAccepts(str)TJ Holowaychukbefore adoptionfe5efa5

line 42why, one step back
42

renamed prev util to parseQuality()TJ Holowaychukbefore adoption29d4ffd

lines 43–44why, one step back
43 utils.parseQuality('text/html; q=.2')
44 .should.eql([{ value: 'text/html', quality: 0.2 }]);

Added utils.parseAccepts(str)TJ Holowaychukbefore adoptionfe5efa5

lines 45–47why, one step back
45 })
46 47 it('should work with messed up whitespace', function(){

renamed prev util to parseQuality()TJ Holowaychukbefore adoption29d4ffd

lines 48–49why, one step back
48 utils.parseQuality('text/html ; q = .2')
49 .should.eql([{ value: 'text/html', quality: 0.2 }]);

Added utils.parseAccepts(str)TJ Holowaychukbefore adoptionfe5efa5

lines 50–51why, one step back
50 })
51

more testsTJ Holowaychukbefore adoptionfd06084

lines 52–59why, one step back
52 it('should work with multiples', function(){
53 var str = 'da, en;q=.5, en-gb;q=.8';54 var arr = utils.parseQuality(str);55 arr[0].value.should.equal('da');56 arr[1].value.should.equal('en-gb');57 arr[2].value.should.equal('en');58 })59

Added utils.parseAccepts(str)TJ Holowaychukbefore adoptionfe5efa5

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

renamed prev util to parseQuality()TJ Holowaychukbefore adoption29d4ffd

lines 62–65why, one step back
62 var arr = utils.parseQuality(str);
63 arr[0].value.should.equal('application/json');64 arr[1].value.should.equal('text/html');65 arr[2].value.should.equal('text/plain');

Added utils.parseAccepts(str)TJ Holowaychukbefore adoptionfe5efa5

line 66why, one step back
66 })

Fixed parseQuality(), exclude q=0TJ Holowaychukbefore adoptione44c874

lines 67–72why, one step back
67
68 it('should exclude those with a quality of 0', function(){69 var str = 'text/plain;q=.2, application/json, text/html;q=0';70 var arr = utils.parseQuality(str);71 arr.should.have.length(2);72 })

Added utils.parseAccepts(str)TJ Holowaychukbefore adoptionfe5efa5

line 73why, one step back
73})

Added parseAccepts() utilTJ Holowaychukbefore adoption2855004

line 74why, one step back
74

Added req.acceptedTJ Holowaychukbefore adoptiona8fd8cb

line 75why, one step back
75describe('utils.parseAccept(str)', function(){

Added parseAccepts() utilTJ Holowaychukbefore adoption2855004

line 76why, one step back
76 it('should provide .type', function(){

Added req.acceptedTJ Holowaychukbefore adoptiona8fd8cb

line 77why, one step back
77 var arr = utils.parseAccept('text/html');

Added parseAccepts() utilTJ Holowaychukbefore adoption2855004

lines 78–81why, one step back
78 arr[0].type.should.equal('text');
79 })80 81 it('should provide .subtype', function(){

Added req.acceptedTJ Holowaychukbefore adoptiona8fd8cb

line 82why, one step back
82 var arr = utils.parseAccept('text/html');

Added parseAccepts() utilTJ Holowaychukbefore adoption2855004

lines 83–85why, one step back
83 arr[0].subtype.should.equal('html');
84 })85})

Added utils.accepts(type, str)TJ Holowaychukbefore adoption36be2b0

lines 86–164why, one step back
86
87describe('utils.accepts(type, str)', function(){88 describe('when a string is not given', function(){89 it('should return true', function(){90 utils.accepts('text/html')91 .should.be.true;92 })93 })94 95 describe('when a string is empty', function(){96 it('should return true', function(){97 utils.accepts('text/html', '')98 .should.be.true;99 })100 })101 102 describe('when */* is given', function(){103 it('should return true', function(){104 utils.accepts('text/html', 'text/plain, */*')105 .should.be.true;106 })107 })108109 describe('when accepting type/subtype', function(){110 it('should return true when present', function(){111 utils.accepts('text/html', 'text/plain, text/html')112 .should.be.true;113 })114 115 it('should return false otherwise', function(){116 utils.accepts('text/html', 'text/plain, application/json')117 .should.be.false;118 })119 })120 121 describe('when accepting */subtype', function(){122 it('should return true when present', function(){123 utils.accepts('text/html', 'text/*')124 .should.be.true;125 })126 127 it('should return false otherwise', function(){128 utils.accepts('text/html', 'image/*')129 .should.be.false;130 })131 })132 133 describe('when accepting type/*', function(){134 it('should return true when present', function(){135 utils.accepts('text/html', '*/html')136 .should.be.true;137 })138 139 it('should return false otherwise', function(){140 utils.accepts('text/html', '*/json')141 .should.be.false;142 })143 })144 145 describe('when an extension is given', function(){146 it('should return true when present', function(){147 utils.accepts('html', 'text/html, application/json')148 .should.be.true;149 })150 151 it('should return false otherwise', function(){152 utils.accepts('html', 'text/plain, application/json')153 .should.be.false;154 })155 156 it('should support *', function(){157 utils.accepts('html', 'text/*')158 .should.be.true;159160 utils.accepts('html', '*/html')161 .should.be.true;162 })163 })164})