PageRenderTime 80ms CodeModel.GetById 69ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/js/lib/Socket.IO-node/tests/transports.htmlfile.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 217 lines | 190 code | 24 blank | 3 comment | 24 complexity | af0c159e3b7443330e12005ef09e00b5 MD5 | raw file
  1var io = require('socket.io')
  2  , net = require('net')
  3  , assert = require('assert')
  4  , http = require('http')
  5  , querystring = require('querystring')
  6  , port = 7600
  7  , encode = require('socket.io/utils').encode
  8  , decode = require('socket.io/utils').decode
  9  , EventEmitter = require('events').EventEmitter
 10  , HTMLFile = require('socket.io/transports/htmlfile');
 11  
 12function server(callback){
 13  return http.createServer(function(){});
 14};
 15
 16function listen(s, callback){
 17  s._port = port;
 18  s.listen(port, callback);
 19  port++;
 20  return s;
 21};
 22
 23function client(s){
 24  return http.createClient(s._port, 'localhost');
 25};
 26
 27function socket(server, options){
 28  if (!options) options = {};
 29  options.log = false;
 30  if (!options.transportOptions) options.transportOptions = { 
 31    'htmlfile': {
 32      // disable heartbeats for tests, re-enabled in heartbeat test below
 33      timeout: null 
 34    }
 35  };
 36  return io.listen(server, options);
 37};
 38
 39function get(server, url, assert, callback){
 40  var client = http.createClient(server._port)
 41    , request = client.request('GET', url, {host: 'localhost'})
 42    , emitter = new EventEmitter
 43    , messages = 0;
 44  
 45  request.end();
 46  request.on('response', function(resp){
 47    callback(emitter);
 48    resp.on('data', function(data){
 49      var msg = data.toString();
 50      if (++messages == 1){
 51        // make sure to send 256 bytes of data IE needs
 52        assert.ok(data.length >= 256); 
 53        assert.ok(msg.indexOf('<html>') != -1);
 54        assert.ok(msg.indexOf('<body>') != -1);
 55      } else {
 56        // make sure element is wrapped in a script tag and a javascript call
 57        assert.ok(/^<script>parent.s\._\("/.test(msg));
 58        assert.ok(/", document\);<\/script>$/.test(msg));
 59        
 60        emitter.emit('data', msg.substr(20, msg.length - 42));
 61      }
 62    });
 63  });
 64  
 65  client.end = function(){
 66    if (request.abort)
 67      request.abort();
 68    else
 69      request.connection.destroy();
 70  };
 71
 72  return client;
 73};
 74
 75function post(client, url, data, callback){
 76  var query = querystring.stringify(data)
 77    , request = client.request('POST', url, {'Content-Length': Buffer.byteLength(query)});
 78  request.write(query);
 79  request.end();
 80};
 81
 82module.exports = {
 83  
 84  'test connection and handshake': function(){
 85    var _server = server()
 86      , _socket = socket(_server)
 87      , trips = 2;
 88
 89    listen(_server, function(){
 90      var _client = get(_server, '/socket.io/htmlfile', assert, function(response){
 91        var i = 0;
 92        response.on('data', function(data){
 93          var msg = decode(data);
 94          switch (i++){
 95            case 0:
 96              assert.ok(Object.keys(_socket.clients).length == 1);
 97              assert.ok(msg == Object.keys(_socket.clients)[0]);
 98              assert.ok(_socket.clients[msg] instanceof HTMLFile);
 99              _socket.clients[msg].send('from server');
100              post(client(_server), '/socket.io/htmlfile/' + msg + '/send', {data: encode('from client')});
101              break;
102            case 1:
103              assert.ok(msg == 'from server');
104              --trips || close();
105          }
106        });
107      });
108      
109      _socket.on('connection', function(client){
110        assert.ok(client instanceof HTMLFile);
111        client.on('message', function(msg){
112          --trips || close();
113        });
114      });
115      
116      function close(){
117        _client.end();
118        _server.close();
119      };
120    });
121  },
122  
123  'test clients tracking': function(){
124    var _server = server()
125      , _socket = socket(_server);
126    
127    listen(_server, function(){
128      var _client = get(_server, '/socket.io/htmlfile', assert, function(response){
129        var once = false
130        response.on('data', function(){
131          if (!once){
132            assert.ok(Object.keys(_socket.clients).length == 1);
133            once = true;
134            var _client2 = get(_server, '/socket.io/htmlfile', assert, function(response){
135              response.on('data', function(){
136                assert.ok(Object.keys(_socket.clients).length == 2);
137                _client.end();
138                _client2.end();
139                _server.close();
140              });
141            });
142          }
143        });
144      });
145    });
146  },
147  
148  'test buffered messages': function(){
149    var _server = server()
150      , _socket = socket(_server, { transportOptions: { 
151        'htmlfile': {
152          closeTimeout: 100
153        }
154      } });
155      
156    listen(_server, function(){
157      var _client = get(_server, '/socket.io/htmlfile', assert, function(response){
158        var once = false;
159        response.on('data', function(data){
160          if (!once){
161            var sessid = decode(data);
162            assert.ok(_socket.clients[sessid]._open === true);
163            assert.ok(_socket.clients[sessid].connected);
164            
165            _socket.clients[sessid].connection.addListener('end', function(){
166              assert.ok(_socket.clients[sessid]._open === false);
167              assert.ok(_socket.clients[sessid].connected);
168              
169              _socket.clients[sessid].send('from server');
170              
171              _client = get(_server, '/socket.io/htmlfile/' + sessid, assert, function(response){
172                response.on('data', function(data){
173                  assert.ok(decode(data) == 'from server');
174                  _client.end();
175                  _server.close();
176                });
177              });
178            });
179            _client.end();
180            once = true;
181          }
182        });
183      });
184    });
185  },
186  
187  'test hearbeat timeout': function(){
188    var _server = server()
189      , _socket = socket(_server, {
190          transportOptions: {
191            'htmlfile': {
192              timeout: 100,
193              heartbeatInterval: 1
194            }
195          }
196        });
197    listen(_server, function(){
198      var client = get(_server, '/socket.io/htmlfile', assert, function(response){
199        var messages = 0;
200        response.on('data', function(data){
201          ++messages;
202          var msg = decode(data);
203          if (msg[0].substr(0, 3) == '~h~'){
204            assert.ok(messages == 2);
205            assert.ok(Object.keys(_socket.clients).length == 1);
206            setTimeout(function(){
207              assert.ok(Object.keys(_socket.clients).length == 0);
208              client.end();
209              _server.close();
210            }, 150);
211          }
212        });
213      });
214    });
215  }
216  
217};