Skip to content
Myr

utils.js

This file

Path

test/utils.js

158 lines · 4.3 KiB · JavaScript

Last changed by

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

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

lines 66–67why, one step back
66 })
67})

Added parseAccepts() utilTJ Holowaychukbefore adoption2855004

line 68why, one step back
68

Added req.acceptedTJ Holowaychukbefore adoptiona8fd8cb

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

Added parseAccepts() utilTJ Holowaychukbefore adoption2855004

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

Added req.acceptedTJ Holowaychukbefore adoptiona8fd8cb

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

Added parseAccepts() utilTJ Holowaychukbefore adoption2855004

lines 72–75why, one step back
72 arr[0].type.should.equal('text');
73 })74 75 it('should provide .subtype', function(){

Added req.acceptedTJ Holowaychukbefore adoptiona8fd8cb

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

Added parseAccepts() utilTJ Holowaychukbefore adoption2855004

lines 77–79why, one step back
77 arr[0].subtype.should.equal('html');
78 })79})

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

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