github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/public/libs/sockjs-client-1.1.0/tests/lib/utils.js (about) 1 'use strict'; 2 3 var expect = require('expect.js') 4 , JSON3 = require('json3') 5 ; 6 7 describe('utils', function () { 8 describe('random', function () { 9 var random = require('../../lib/utils/random'); 10 describe('string', function () { 11 it('should generate unique outputs', function () { 12 expect(random.string(8)).not.to.equal(random.string(8)); 13 }); 14 15 it('should have the correct length', function () { 16 var lengths = [1, 2, 3, 128]; 17 lengths.forEach(function (len) { 18 expect(random.string(len).length).to.equal(len); 19 }); 20 }); 21 }); 22 23 describe('numberString', function () { 24 it('should have the correct length based on the max', function () { 25 expect(random.numberString(10).length).to.equal(1); 26 expect(random.numberString(100).length).to.equal(2); 27 expect(random.numberString(1000).length).to.equal(3); 28 expect(random.numberString(10000).length).to.equal(4); 29 expect(random.numberString(100000).length).to.equal(5); 30 }); 31 }); 32 }); 33 34 describe('url', function () { 35 var urlUtils = require('../../lib/utils/url'); 36 it('getOrigin', function () { 37 expect(urlUtils.getOrigin('http://a.b/')).to.equal('http://a.b:80'); 38 expect(urlUtils.getOrigin('http://a.b/c')).to.equal('http://a.b:80'); 39 expect(urlUtils.getOrigin('http://a.b:123/c')).to.equal('http://a.b:123'); 40 expect(urlUtils.getOrigin('https://a.b/')).to.equal('https://a.b:443'); 41 expect(urlUtils.getOrigin('file://a.b/')).to.equal(null); 42 }); 43 44 it('isOriginEqual', function () { 45 expect(urlUtils.isOriginEqual('http://localhost', 'http://localhost/')).to.be.ok(); 46 expect(urlUtils.isOriginEqual('http://localhost', 'http://localhost/abc')).to.be.ok(); 47 expect(urlUtils.isOriginEqual('http://localhost/', 'http://localhost')).to.be.ok(); 48 expect(urlUtils.isOriginEqual('http://localhost', 'http://localhost')).to.be.ok(); 49 expect(urlUtils.isOriginEqual('http://localhost', 'http://localhost:8080')).to.not.be.ok(); 50 expect(urlUtils.isOriginEqual('http://localhost:8080', 'http://localhost')).to.not.be.ok(); 51 expect(urlUtils.isOriginEqual('http://localhost:8080', 'http://localhost:8080/')).to.be.ok(); 52 expect(urlUtils.isOriginEqual('http://127.0.0.1:80/', 'http://127.0.0.1:80/a')).to.be.ok(); 53 expect(urlUtils.isOriginEqual('http://127.0.0.1:80', 'http://127.0.0.1:80/a')).to.be.ok(); 54 expect(urlUtils.isOriginEqual('http://localhost', 'http://localhost:80')).to.be.ok(); 55 expect(urlUtils.isOriginEqual('http://127.0.0.1/', 'http://127.0.0.1:80/a')).to.be.ok(); 56 expect(urlUtils.isOriginEqual('http://127.0.0.1:9', 'http://127.0.0.1:9999')).to.not.be.ok(); 57 expect(urlUtils.isOriginEqual('http://127.0.0.1:99', 'http://127.0.0.1:9999')).to.not.be.ok(); 58 expect(urlUtils.isOriginEqual('http://127.0.0.1:999', 'http://127.0.0.1:9999')).to.not.be.ok(); 59 expect(urlUtils.isOriginEqual('http://127.0.0.1:9999', 'http://127.0.0.1:9999')).to.be.ok(); 60 expect(urlUtils.isOriginEqual('http://127.0.0.1:99999', 'http://127.0.0.1:9999')).to.not.be.ok(); 61 }); 62 63 it('isSchemeEqual', function () { 64 expect(urlUtils.isSchemeEqual('http://localhost', 'http://localhost/')).to.be.ok(); 65 expect(urlUtils.isSchemeEqual('http://localhost', 'https://localhost/')).to.not.be.ok(); 66 expect(urlUtils.isSchemeEqual('http://localhost', 'file://localhost/')).to.not.be.ok(); 67 }); 68 }); 69 70 describe('escape', function () { 71 var escape = require('../../lib/utils/escape'); 72 describe('quote', function () { 73 it('handles empty string', function () { 74 expect(escape.quote('')).to.equal('""'); 75 }); 76 77 it('handles non-empty string', function () { 78 expect(escape.quote('a')).to.equal('"a"'); 79 }); 80 81 it('handles tab and newline', function () { 82 expect(['"\\t"', '"\\u0009"']).to.contain(escape.quote('\t')); 83 expect(['"\\n"', '"\\u000a"']).to.contain(escape.quote('\n')); 84 }); 85 86 it('handles unicode', function () { 87 expect(escape.quote('\x00\udfff\ufffe\uffff')).to.equal('"\\u0000\\udfff\\ufffe\\uffff"'); 88 expect(escape.quote('\ud85c\udff7\ud800\ud8ff')).to.equal('"\\ud85c\\udff7\\ud800\\ud8ff"'); 89 expect(escape.quote('\u2000\u2001\u0300\u0301')).to.equal('"\\u2000\\u2001\\u0300\\u0301"'); 90 }); 91 92 it.skip('handles all 64K characters round-trip', function () { 93 var c = []; 94 for (var i = 0; i <= 65536; i++) { 95 c.push(String.fromCharCode(i)); 96 } 97 var allChars = c.join(''); 98 expect(JSON3.parse(escape.quote(allChars))).to.equal(allChars); 99 }); 100 }); 101 }); 102 103 describe('object', function () { 104 var objectUtils = require('../../lib/utils/object'); 105 it('extend', function () { 106 var a, b; 107 expect(objectUtils.extend({}, {})).to.eql({}); 108 a = { 109 a: 1 110 }; 111 expect(objectUtils.extend(a, {})).to.eql(a); 112 expect(objectUtils.extend(a, { 113 b: 1 114 })).to.eql(a); 115 a = { 116 a: 1 117 }; 118 b = { 119 b: 2 120 }; 121 expect(objectUtils.extend(a, b)).to.eql({ 122 a: 1, 123 b: 2 124 }); 125 expect(a).to.eql({ 126 a: 1, 127 b: 2 128 }); 129 expect(b).to.eql({ 130 b: 2 131 }); 132 }); 133 }); 134 });