subprotocol.test.js (2310B)
1 'use strict'; 2 3 const assert = require('assert'); 4 5 const { parse } = require('../lib/subprotocol'); 6 7 describe('subprotocol', () => { 8 describe('parse', () => { 9 it('parses a single subprotocol', () => { 10 assert.deepStrictEqual(parse('foo'), new Set(['foo'])); 11 }); 12 13 it('parses multiple subprotocols', () => { 14 assert.deepStrictEqual( 15 parse('foo,bar,baz'), 16 new Set(['foo', 'bar', 'baz']) 17 ); 18 }); 19 20 it('ignores the optional white spaces', () => { 21 const header = 'foo , bar\t, \tbaz\t , qux\t\t,norf'; 22 23 assert.deepStrictEqual( 24 parse(header), 25 new Set(['foo', 'bar', 'baz', 'qux', 'norf']) 26 ); 27 }); 28 29 it('throws an error if a subprotocol is empty', () => { 30 [ 31 [',', 0], 32 ['foo,,', 4], 33 ['foo, ,', 6] 34 ].forEach((element) => { 35 assert.throws( 36 () => parse(element[0]), 37 new RegExp( 38 `^SyntaxError: Unexpected character at index ${element[1]}$` 39 ) 40 ); 41 }); 42 }); 43 44 it('throws an error if a subprotocol is duplicated', () => { 45 ['foo,foo,bar', 'foo,bar,foo'].forEach((header) => { 46 assert.throws( 47 () => parse(header), 48 /^SyntaxError: The "foo" subprotocol is duplicated$/ 49 ); 50 }); 51 }); 52 53 it('throws an error if a white space is misplaced', () => { 54 [ 55 ['f oo', 2], 56 [' foo', 0] 57 ].forEach((element) => { 58 assert.throws( 59 () => parse(element[0]), 60 new RegExp( 61 `^SyntaxError: Unexpected character at index ${element[1]}$` 62 ) 63 ); 64 }); 65 }); 66 67 it('throws an error if a subprotocol contains invalid characters', () => { 68 [ 69 ['f@o', 1], 70 ['f\\oo', 1], 71 ['foo,b@r', 5] 72 ].forEach((element) => { 73 assert.throws( 74 () => parse(element[0]), 75 new RegExp( 76 `^SyntaxError: Unexpected character at index ${element[1]}$` 77 ) 78 ); 79 }); 80 }); 81 82 it('throws an error if the header value ends prematurely', () => { 83 ['foo ', 'foo, ', 'foo,bar ', 'foo,bar,'].forEach((header) => { 84 assert.throws( 85 () => parse(header), 86 /^SyntaxError: Unexpected end of input$/ 87 ); 88 }); 89 }); 90 }); 91 });