PageRenderTime 102ms CodeModel.GetById 60ms app.highlight 12ms RepoModel.GetById 28ms app.codeStats 0ms

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

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