Skip to content
Myr

utils.js

This file

Path

test/utils.js

215 lines · 6.5 KiB · JavaScript

Last changed by

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

History of this file

Take it

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

Added another example to content-negotiationTJ Holowaychukbefore adoptionf25aaf1

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

mergeTJ Holowaychukbefore adoptione2ad0d3

lines 5–24why, one step back
5describe('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>';9 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})24

Added another example to content-negotiationTJ Holowaychukbefore adoptionf25aaf1

lines 25–51why, one step back
25describe('utils.isAbsolute()', function(){
26 it('should support windows', function(){27 assert(utils.isAbsolute('c:\\'));28 assert(!utils.isAbsolute(':\\'));29 })30 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(){

Added another example to content-negotiationTJ Holowaychukbefore adoptionf25aaf1

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

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

lines 54–55why, one step back
54 utils.parseParams('text/html')
55 .should.eql([{ value: 'text/html', quality: 1, params: {}}]);

Added another example to content-negotiationTJ Holowaychukbefore adoptionf25aaf1

lines 56–58why, one step back
56 })
57 58 it('should parse qvalues', function(){

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

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

Added another example to content-negotiationTJ Holowaychukbefore adoptionf25aaf1

line 61why, one step back
61

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

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

Added another example to content-negotiationTJ Holowaychukbefore adoptionf25aaf1

line 64why, one step back
64 })

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

lines 65–75why, one step back
65
66 it('should parse accept parameters', function(){67 utils.parseParams('application/json; ver=2.0')68 .should.eql([{ value: 'application/json', quality: 1, params: {ver: "2.0"}}]);6970 utils.parseParams('text/html; q=0.5; level=2')71 .should.eql([{ value: 'text/html', quality: 0.5, params: {level: "2"}}]);72 utils.parseParams('text/html;q=.2;ver=beta')73 .should.eql([{ value: 'text/html', quality: 0.2, params: {ver: "beta"}}]);74 })75

Added another example to content-negotiationTJ Holowaychukbefore adoptionf25aaf1

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

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

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

Added another example to content-negotiationTJ Holowaychukbefore adoptionf25aaf1

lines 79–82why, one step back
79 })
80 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);

Added another example to content-negotiationTJ Holowaychukbefore adoptionf25aaf1

lines 84–90why, 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');87 })88 89 it('should sort by quality', function(){90 var str = 'text/plain;q=.2, application/json, text/html;q=0.5';

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

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

Added another example to content-negotiationTJ Holowaychukbefore adoptionf25aaf1

lines 92–98why, one step back
92 arr[0].value.should.equal('application/json');
93 arr[1].value.should.equal('text/html');94 arr[2].value.should.equal('text/plain');95 })96 97 it('should exclude those with a quality of 0', function(){98 var str = 'text/plain;q=.2, application/json, text/html;q=0';

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

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

Added another example to content-negotiationTJ Holowaychukbefore adoptionf25aaf1

lines 100–215why, one step back
100 arr.should.have.length(2);
101 })102})103104describe('utils.parseAccept(str)', function(){105 it('should provide .type', function(){106 var arr = utils.parseAccept('text/html');107 arr[0].type.should.equal('text');108 })109 110 it('should provide .subtype', function(){111 var arr = utils.parseAccept('text/html');112 arr[0].subtype.should.equal('html');113 })114})115116describe('utils.accepts(type, str)', function(){117 describe('when a string is not given', function(){118 it('should return the value', function(){119 utils.accepts('text/html')120 .should.equal('text/html');121 })122 })123 124 describe('when a string is empty', function(){125 it('should return the value', function(){126 utils.accepts('text/html', '')127 .should.equal('text/html');128 })129 })130131 describe('when */* is given', function(){132 it('should return the value', function(){133 utils.accepts('text/html', 'text/plain, */*')134 .should.equal('text/html');135 })136 })137138 describe('when an array is given', function(){139 it('should return the best match', function(){140 utils.accepts(['html', 'json'], 'text/plain, application/json')141 .should.equal('json');142143 utils.accepts(['html', 'application/json'], 'text/plain, application/json')144 .should.equal('application/json');145146 utils.accepts(['text/html', 'application/json'], 'application/json;q=.5, text/html')147 .should.equal('text/html');148 })149 })150151 describe('when a comma-delimited list is give', function(){152 it('should behave like an array', function(){153 utils.accepts('html, json', 'text/plain, application/json')154 .should.equal('json');155156 utils.accepts('html, application/json', 'text/plain, application/json')157 .should.equal('application/json');158159 utils.accepts('text/html, application/json', 'application/json;q=.5, text/html')160 .should.equal('text/html');161 })162 })163164 describe('when accepting type/subtype', function(){165 it('should return the value when present', function(){166 utils.accepts('text/html', 'text/plain, text/html')167 .should.equal('text/html');168 })169 170 it('should return undefined otherwise', function(){171 assert(null == utils.accepts('text/html', 'text/plain, application/json'));172 })173 })174 175 describe('when accepting */subtype', function(){176 it('should return the value when present', function(){177 utils.accepts('text/html', 'text/*')178 .should.equal('text/html');179 })180 181 it('should return undefined otherwise', function(){182 assert(null == utils.accepts('text/html', 'image/*'));183 })184 })185 186 describe('when accepting type/*', function(){187 it('should return the value when present', function(){188 utils.accepts('text/html', '*/html')189 .should.equal('text/html');190 })191 192 it('should return undefined otherwise', function(){193 assert(null == utils.accepts('text/html', '*/json'));194 })195 })196 197 describe('when an extension is given', function(){198 it('should return the value when present', function(){199 utils.accepts('html', 'text/html, application/json')200 .should.equal('html');201 })202 203 it('should return undefined otherwise', function(){204 assert(null == utils.accepts('html', 'text/plain, application/json'));205 })206 207 it('should support *', function(){208 utils.accepts('html', 'text/*')209 .should.equal('html');210211 utils.accepts('html', '*/html')212 .should.equal('html');213 })214 })215})