PageRenderTime 27ms CodeModel.GetById 6ms app.highlight 15ms RepoModel.GetById 2ms app.codeStats 0ms

/js/lib/Socket.IO-node/tests/listener.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 157 lines | 139 code | 17 blank | 1 comment | 15 complexity | d0ca72a6894ed2d6ad2ebbd6bf1ac4c3 MD5 | raw file
  1var http = require('http')
  2  , net = require('net')
  3  , io = require('socket.io')
  4  , assert = require('assert')
  5  , decode = require('socket.io/utils').decode
  6  , port = 7100
  7  , Listener = io.Listener
  8  , WebSocket = require('./../support/node-websocket-client/lib/websocket').WebSocket;
  9
 10function server(callback){
 11  return http.createServer(callback || function(){});
 12};
 13
 14function socket(server, options){
 15  if (!options) options = {};
 16  if (options.log === undefined) options.log = false;
 17  return io.listen(server, options);
 18};
 19
 20function listen(s, callback){
 21  s._port = port;
 22  s.listen(port, callback);
 23  port++;
 24  return s;
 25};
 26
 27function client(server, sessid){
 28  sessid = sessid ? '/' + sessid : '';
 29  return new WebSocket('ws://localhost:' + server._port + '/socket.io/websocket' + sessid, 'borf');
 30};
 31
 32module.exports = {
 33  
 34  'test serving static javascript client': function(){
 35    var _server = server()
 36      , _socket = socket(_server);
 37    assert.response(_server
 38     , { url: '/socket.io/socket.io.js' }
 39     , { body: /setPath/, headers: { 'Content-Type': 'text/javascript' }});
 40    assert.response(_server
 41     , { url: '/socket.io/lib/vendor/web-socket-js/WebSocketMain.swf' }
 42     , { headers: { 'Content-Type': 'application/x-shockwave-flash' }}
 43     , function(resp){
 44       assert.type(resp.headers.etag, 'string');
 45       assert.response(_server
 46        , { url: '/socket.io/lib/vendor/web-socket-js/WebSocketMain.swf', headers: { 'If-None-Match': resp.headers.etag } }
 47        , { status: 304 });
 48     });
 49     assert.response(_server
 50      , { url: '/socket.io/WebSocketMain.swf' }
 51      , { headers: { 'Content-Type': 'application/x-shockwave-flash' }});
 52  },
 53  
 54  'test serving non-socket.io requests': function(){
 55    var _server = server()
 56      , _socket = socket(_server);
 57    _server.on('request', function(req, res){
 58      if (req.url == '/test'){
 59        res.writeHead(200);
 60        res.end('Hello world');
 61      }
 62    });
 63    assert.response(_server
 64     , { url: '/test' }
 65     , { body: 'Hello world' });
 66  },
 67  
 68  'test destroying an upgrade connection that is not WebSocket': function(){
 69    var _server = server()
 70
 71      , _socket = socket(_server);
 72    listen(_server, function(){
 73      var client = http.createClient(_server._port)
 74        , request = client.request('/', {
 75            'Connection': 'Upgrade',
 76            'Upgrade': 'IRC'
 77          })
 78        , upgraded = false;
 79      client.addListener('upgrade', function(){
 80        upgraded = true;
 81      });
 82      client.addListener('end', function(){
 83        assert.ok(! upgraded);
 84        _server.close();
 85      });
 86      client.on('error', function (e) {
 87        // ignore errors
 88      });
 89      request.end();
 90    });
 91  },
 92  
 93  'test broadcasting to clients': function(){
 94    var _server = server()
 95      , _socket = socket(_server);
 96    listen(_server, function(){
 97      var _client1 = client(_server)
 98        , _client2 = client(_server)
 99        , trips = 2
100        , expected = 2;
101      
102      _socket.on('connection', function(conn){
103        --expected || _socket.broadcast('broadcasted msg');
104      });
105        
106      function close(){
107        _client1.close();
108        _client2.close();
109        _server.close();
110      };
111        
112      _client1.onmessage = function(ev){
113        if (!_client1._first){
114          _client1._first = true;
115        } else {
116          assert.ok(decode(ev.data)[0] == 'broadcasted msg');
117          --trips || close();
118        }
119      };
120      _client2.onmessage = function(ev){
121        if (!_client2._first){
122          _client2._first = true;
123        } else {
124          assert.ok(decode(ev.data)[0] == 'broadcasted msg');
125          --trips || close();
126        }
127      };
128    })
129  },
130  
131  'test connecting with an invalid sessionid': function(){
132    var _server = server()
133      , _socket = socket(_server);
134    listen(_server, function(){
135      var _client = client(_server, 'fake-session-id')
136        , gotMessage = false;
137      _client.onmessage = function(){
138        gotMessage = true;
139      };
140      setTimeout(function(){
141        assert.ok(!gotMessage);
142        _server.close();
143      }, 200);
144    });
145  },
146  
147  'test connecting to an invalid transport': function(){
148    var _server = server(function(req, res){
149          res.writeHead(200);
150          res.end(req.url == '/socket.io/inexistent' ? 'All cool' : '');
151        })
152      , _socket = socket(_server);
153    
154    assert.response(_server, { url: '/socket.io/inexistent' }, { body: 'All cool' });
155  }
156  
157};