PageRenderTime 37ms CodeModel.GetById 23ms app.highlight 10ms RepoModel.GetById 2ms app.codeStats 0ms

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

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 216 lines | 190 code | 23 blank | 3 comment | 26 complexity | 87d775e8226841139e1210c1484a855e MD5 | raw file
  1var io = require('socket.io')
  2  , net = require('net')
  3  , http = require('http')
  4  , assert = require('assert')
  5  , querystring = require('querystring')
  6  , port = 7300
  7  , encode = require('socket.io/utils').encode
  8  , decode = require('socket.io/utils').decode
  9  , EventEmitter = require('events').EventEmitter
 10  , Multipart = require('socket.io/transports/xhr-multipart');
 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    'xhr-multipart': {
 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, callback){
 40  var client = net.createConnection(server._port);
 41  client.on('connect', function(){
 42    client.write('GET ' + url + " HTTP/1.1\r\n\r\n");
 43    
 44    var resp = new EventEmitter()
 45      , buffer = '';
 46    
 47    callback(resp);
 48    client.on('data', function(_data){
 49      var data = _data.toString()
 50        , i = 0
 51        , l = data.length;
 52        
 53      while (i < l){
 54        // lookahead for the `--socketio\n` boundary
 55        if (data.charAt(i) == '-' && data.substr(i, 11) == "--socketio\n"){
 56          // header filtering
 57          var filtered = buffer.replace(/[A-Z][^\n]+\r?\n?\n/gm, '')
 58                               .replace(/^\r\n$/, '')
 59                               .replace(/\n$/, '');
 60          if (filtered !== '') resp.emit('data', filtered);
 61          buffer = '';
 62          i = i + 11;
 63        } else {
 64          buffer += data.charAt(i);
 65          i++;
 66        }
 67      }
 68    });
 69  });
 70  return client;
 71};
 72
 73function post(client, url, data, callback){
 74  var query = querystring.stringify(data)
 75    , request = client.request('POST', url, {'Content-Length': Buffer.byteLength(query)});
 76  request.write(query);
 77  request.end();
 78};
 79
 80module.exports = {
 81  
 82  'test connection and handshake': function(){
 83    var _server = server()
 84      , _socket = socket(_server)
 85      , trips = 2;
 86
 87    listen(_server, function(){
 88      var _client = get(_server, '/socket.io/xhr-multipart', function(response){
 89        var i = 0;
 90        response.on('data', function(data){
 91          var msg = decode(data);
 92          switch (i++){
 93            case 0:
 94              assert.ok(Object.keys(_socket.clients).length == 1);
 95              assert.ok(msg == Object.keys(_socket.clients)[0]);
 96              assert.ok(_socket.clients[msg] instanceof Multipart);
 97              _socket.clients[msg].send('from server');
 98              post(client(_server), '/socket.io/xhr-multipart/' + msg + '/send', {data: encode('from client')});
 99              break;
100            case 1:
101              assert.ok(msg == 'from server');
102              --trips || close();
103          }
104        });
105      });
106      
107      _socket.on('connection', function(client){
108        assert.ok(client instanceof Multipart);
109        client.on('message', function(msg){
110          assert.ok(msg == 'from client');
111          --trips || close();
112        });
113      });
114      
115      function close(){
116        _client.end();
117        _server.close();
118      };
119    });
120  },
121  
122  'test clients tracking': function(){
123    var _server = server()
124      , _socket = socket(_server);
125    
126    listen(_server, function(){
127      var _client = get(_server, '/socket.io/xhr-multipart', function(response){
128        var once = false
129        response.on('data', function(){
130          if (!once){
131            assert.ok(Object.keys(_socket.clients).length == 1);
132            once = true;
133            var _client2 = get(_server, '/socket.io/xhr-multipart', function(response){
134              response.on('data', function(){
135                assert.ok(Object.keys(_socket.clients).length == 2);
136                _client.end();
137                _client2.end();
138                _server.close();
139              });
140            });
141          }
142        });
143      });
144    });
145  },
146  
147  'test buffered messages': function(){
148    var _server = server()
149      , _socket = socket(_server, { transportOptions: { 
150        'xhr-multipart': {
151          closeTimeout: 100
152        }
153      } });
154      
155    listen(_server, function(){
156      var _client = get(_server, '/socket.io/xhr-multipart', function(response){
157        var once = false;
158        response.on('data', function(data){
159          if (!once){
160            var sessid = decode(data);
161            assert.ok(_socket.clients[sessid]._open === true);
162            assert.ok(_socket.clients[sessid].connected);
163            
164            _socket.clients[sessid].connection.addListener('end', function(){
165              assert.ok(_socket.clients[sessid]._open === false);
166              assert.ok(_socket.clients[sessid].connected);
167              
168              _socket.clients[sessid].send('from server');
169              
170              _client = get(_server, '/socket.io/xhr-multipart/' + sessid, function(response){
171                response.on('data', function(data){
172                  assert.ok(decode(data) == 'from server');
173                  _client.end();
174                  _server.close();
175                });
176              });
177            });
178            _client.end();
179            once = true;
180          }
181        });
182      });
183    });
184  },
185  
186  'test hearbeat timeout': function(){
187    var _server = server()
188      , _socket = socket(_server, {
189          transportOptions: {
190            'xhr-multipart': {
191              timeout: 100,
192              heartbeatInterval: 1
193            }
194          }
195        });
196    listen(_server, function(){
197      var client = get(_server, '/socket.io/xhr-multipart', function(response){
198        var messages = 0;
199        response.on('data', function(data){
200          ++messages;
201          var msg = decode(data);
202          if (msg[0].substr(0, 3) == '~h~'){
203            assert.ok(messages == 2);
204            assert.ok(Object.keys(_socket.clients).length == 1);
205            setTimeout(function(){
206              assert.ok(Object.keys(_socket.clients).length == 0);
207              client.end();
208              _server.close();
209            }, 150);
210          }
211        });
212      });
213    });
214  }
215  
216};