UNPKG

4.62 kBJavaScriptView Raw
1
2/*!
3 * socket.io-node
4 * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
5 * MIT Licensed
6 */
7
8(function (module, io, should) {
9
10 module.exports = {
11
12 'parse uri': function () {
13 var http = io.util.parseUri('http://google.com')
14 , https = io.util.parseUri('https://www.google.com:80')
15 , query = io.util.parseUri('google.com:8080/foo/bar?foo=bar');
16
17 http.protocol.should().eql('http');
18 http.port.should().eql('');
19 http.host.should().eql('google.com');
20 https.protocol.should().eql('https');
21 https.port.should().eql('80');
22 https.host.should().eql('www.google.com');
23 query.port.should().eql('8080');
24 query.query.should().eql('foo=bar');
25 query.path.should().eql('/foo/bar');
26 query.relative.should().eql('/foo/bar?foo=bar');
27 },
28
29 'unique uri': function () {
30 var protocol = io.util.parseUri('http://google.com')
31 , noprotocol = io.util.parseUri('google.com')
32 , https = io.util.parseUri('https://google.com')
33 , path = io.util.parseUri('https://google.com/google.com/com/?foo=bar');
34
35 if ('object' == typeof window) {
36 io.util.uniqueUri(protocol).should().eql('http://google.com:3000');
37 io.util.uniqueUri(noprotocol).should().eql('http://google.com:3000');
38 } else {
39 io.util.uniqueUri(protocol).should().eql('http://google.com:80');
40 io.util.uniqueUri(noprotocol).should().eql('http://google.com:80');
41 }
42
43 io.util.uniqueUri(https).should().eql('https://google.com:443');
44 io.util.uniqueUri(path).should().eql('https://google.com:443');
45 },
46
47 'chunk query string': function () {
48 io.util.chunkQuery('foo=bar').should().be.a('object');
49 io.util.chunkQuery('foo=bar').foo.should().eql('bar');
50 },
51
52 'merge query strings': function () {
53 var base = io.util.query('foo=bar', 'foo=baz')
54 , add = io.util.query('foo=bar', 'bar=foo')
55
56 base.should().eql('?foo=baz');
57 add.should().eql('?foo=bar&bar=foo');
58
59 io.util.query('','').should().eql('');
60 io.util.query('foo=bar', '').should().eql('?foo=bar');
61 io.util.query('', 'foo=bar').should().eql('?foo=bar');
62 },
63
64 'request': function () {
65 var type = typeof io.util.request();
66 type.should().eql('object');
67 },
68
69 'is array': function () {
70 io.util.isArray([]).should().be_true;
71 io.util.isArray({}).should().be_false;
72 io.util.isArray('str').should().be_false;
73 io.util.isArray(new Date).should().be_false;
74 io.util.isArray(true).should().be_false;
75 io.util.isArray(arguments).should().be_false;
76 },
77
78 'merge, deep merge': function () {
79 var start = {
80 foo: 'bar'
81 , bar: 'baz'
82 }
83 , duplicate = {
84 foo: 'foo'
85 , bar: 'bar'
86 }
87 , extra = {
88 ping: 'pong'
89 }
90 , deep = {
91 level1:{
92 foo: 'bar'
93 , level2: {
94 foo: 'bar'
95 , level3:{
96 foo: 'bar'
97 , rescursive: deep
98 }
99 }
100 }
101 }
102 // same structure, but changed names
103 , deeper = {
104 foo: 'bar'
105 , level1:{
106 foo: 'baz'
107 , level2: {
108 foo: 'foo'
109 , level3:{
110 foo: 'pewpew'
111 , rescursive: deep
112 }
113 }
114 }
115 };
116
117 io.util.merge(start, duplicate);
118
119 start.foo.should().eql('foo');
120 start.bar.should().eql('bar');
121
122 io.util.merge(start, extra);
123 start.ping.should().eql('pong');
124 start.foo.should().eql('foo');
125
126 io.util.merge(deep, deeper);
127
128 deep.foo.should().eql('bar');
129 deep.level1.foo.should().eql('baz');
130 deep.level1.level2.foo.should().eql('foo');
131 deep.level1.level2.level3.foo.should().eql('pewpew');
132 },
133
134 'defer': function (next) {
135 var now = +new Date;
136
137 io.util.defer(function () {
138 ((new Date - now) >= ( io.util.webkit ? 100 : 0 )).should().be_true();
139 next();
140 })
141 },
142
143 'indexOf': function () {
144 var data = ['socket', 2, 3, 4, 'socket', 5, 6, 7, 'io'];
145 io.util.indexOf(data, 'socket', 1).should().eql(4);
146 io.util.indexOf(data, 'socket').should().eql(0);
147 io.util.indexOf(data, 'waffles').should().eql(-1);
148 }
149
150 };
151
152})(
153 'undefined' == typeof module ? module = {} : module
154 , 'undefined' == typeof io ? require('socket.io-client') : io
155 , 'undefined' == typeof should ? require('should') : should
156);