PageRenderTime 34ms CodeModel.GetById 18ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/js/lib/Socket.IO-node/tests/transports.jsonp-polling.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 149 lines | 129 code | 19 blank | 1 comment | 10 complexity | 5a121d8e4273f13ea163e96f6f9a4360 MD5 | raw file
  1var io = require('socket.io')
  2  , http = require('http')
  3  , querystring = require('querystring')
  4  , assert = require('assert')
  5  , port = 7500
  6  , encode = require('socket.io/utils').encode
  7  , _decode = require('socket.io/utils').decode
  8  , Polling = require('socket.io/transports/jsonp-polling');
  9
 10function decode(data){
 11  var io = {
 12    JSONP: [{
 13      '_': _decode
 14    }]
 15  };
 16  // the decode function simulates a browser executing the javascript jsonp call
 17  return eval(data);
 18};
 19
 20function server(callback){
 21  return http.createServer(function(){});
 22};
 23
 24function listen(s, callback){
 25  s._port = port;
 26  s.listen(port, callback);
 27  port++;
 28  return s;
 29};
 30
 31function client(s){
 32  return http.createClient(s._port, 'localhost');
 33};
 34
 35function socket(server, options){
 36  if (!options) options = {};
 37  options.log = false;
 38  if (!options.transportOptions) options.transportOptions = { 
 39    'jsonp-polling': {
 40      closeTimeout: 100
 41    }
 42  };
 43  return io.listen(server, options);
 44};
 45
 46function get(client, url, callback){
 47  var request = client.request('GET', url + '/' + (+new Date) + '/0', {host: 'localhost'});
 48  request.end();
 49  request.on('response', function(response){
 50    var data = '';
 51    response.setEncoding('utf8');
 52    response.on('data', function(chunk){
 53      data += chunk;
 54    });
 55    response.on('end', function(chunk){
 56      callback(data);
 57    });
 58  });
 59};
 60
 61function post(client, url, data, callback){
 62  var query = querystring.stringify(data)
 63    , request = client.request('POST', url, {'Content-Length': Buffer.byteLength(query)});
 64  request.write(query);
 65  request.end();
 66};
 67
 68module.exports = {
 69  
 70  'test connection and handshake': function(){
 71    var _server = server()
 72      , _socket = socket(_server)
 73      , trips = 2;
 74
 75    _socket.on('connection', function(client){
 76      assert.ok(client instanceof Polling);
 77      client.on('message', function(msg){
 78        assert.ok(msg == 'from client');
 79        --trips || _server.close();
 80      });
 81    });
 82
 83    listen(_server, function(){
 84      get(client(_server), '/socket.io/jsonp-polling/', function(data){
 85        var sessid = decode(data);
 86        assert.ok(Object.keys(_socket.clients).length == 1);
 87        assert.ok(sessid == Object.keys(_socket.clients)[0]);
 88        assert.ok(_socket.clients[sessid] instanceof Polling);
 89        
 90        _socket.clients[sessid].send('from server');
 91        
 92        get(client(_server), '/socket.io/jsonp-polling/' + sessid, function(data){
 93          assert.ok(decode(data), 'from server');
 94          --trips || _server.close();
 95        });
 96        
 97        post(client(_server), '/socket.io/jsonp-polling/' + sessid + '/send//0', {data: encode('from client')});
 98      });
 99    });
100  },
101  
102  'test clients tracking': function(){
103    var _server = server()
104      , _socket = socket(_server);
105    
106    listen(_server, function(){
107      get(client(_server), '/socket.io/jsonp-polling/', function(){
108        assert.ok(Object.keys(_socket.clients).length == 1);
109        get(client(_server), '/socket.io/jsonp-polling/', function(){
110          assert.ok(Object.keys(_socket.clients).length == 2);
111          _server.close();
112        });
113      });
114    });
115  },
116  
117  'test buffered messages': function(){
118    var _server = server()
119      , _socket = socket(_server, { transportOptions: { 
120        'jsonp-polling': {
121          closeTimeout: 100,
122          duration: 200
123        }
124      } });
125      
126    listen(_server, function(){
127      get(client(_server), '/socket.io/jsonp-polling/', function(data){
128        var sessid = decode(data);
129        assert.ok(_socket.clients[sessid]._open === false);
130        assert.ok(_socket.clients[sessid].connected);
131        _socket.clients[sessid].send('from server');
132        get(client(_server), '/socket.io/jsonp-polling/' + sessid, function(data){
133          var durationCheck;
134          assert.ok(decode(data) == 'from server');
135          setTimeout(function(){
136            assert.ok(_socket.clients[sessid]._open);
137            assert.ok(_socket.clients[sessid].connected);
138            durationCheck = true;
139          }, 50);
140          get(client(_server), '/socket.io/jsonp-polling/' + sessid, function(){
141            assert.ok(durationCheck);
142            _server.close();
143          });
144        });
145      });
146    });
147  }
148  
149};