PageRenderTime 420ms CodeModel.GetById 14ms RepoModel.GetById 0ms 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
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. var 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. function server(callback){
  12. return http.createServer(function(){});
  13. };
  14. function listen(s, callback){
  15. s._port = port;
  16. s.listen(port, callback);
  17. port++;
  18. return s;
  19. };
  20. function client(s){
  21. return http.createClient(s._port, 'localhost');
  22. };
  23. function socket(server, options){
  24. if (!options) options = {};
  25. options.log = false;
  26. if (!options.transportOptions) options.transportOptions = {
  27. 'xhr-multipart': {
  28. // disable heartbeats for tests, re-enabled in heartbeat test below
  29. timeout: null
  30. }
  31. };
  32. return io.listen(server, options);
  33. };
  34. function get(server, url, callback){
  35. var client = net.createConnection(server._port);
  36. client.on('connect', function(){
  37. client.write('GET ' + url + " HTTP/1.1\r\n\r\n");
  38. var resp = new EventEmitter()
  39. , buffer = '';
  40. callback(resp);
  41. client.on('data', function(_data){
  42. var data = _data.toString()
  43. , i = 0
  44. , l = data.length;
  45. while (i < l){
  46. // lookahead for the `--socketio\n` boundary
  47. if (data.charAt(i) == '-' && data.substr(i, 11) == "--socketio\n"){
  48. // header filtering
  49. var filtered = buffer.replace(/[A-Z][^\n]+\r?\n?\n/gm, '')
  50. .replace(/^\r\n$/, '')
  51. .replace(/\n$/, '');
  52. if (filtered !== '') resp.emit('data', filtered);
  53. buffer = '';
  54. i = i + 11;
  55. } else {
  56. buffer += data.charAt(i);
  57. i++;
  58. }
  59. }
  60. });
  61. });
  62. return client;
  63. };
  64. function post(client, url, data, callback){
  65. var query = querystring.stringify(data)
  66. , request = client.request('POST', url, {'Content-Length': Buffer.byteLength(query)});
  67. request.write(query);
  68. request.end();
  69. };
  70. module.exports = {
  71. 'test connection and handshake': function(){
  72. var _server = server()
  73. , _socket = socket(_server)
  74. , trips = 2;
  75. listen(_server, function(){
  76. var _client = get(_server, '/socket.io/xhr-multipart', function(response){
  77. var i = 0;
  78. response.on('data', function(data){
  79. var msg = decode(data);
  80. switch (i++){
  81. case 0:
  82. assert.ok(Object.keys(_socket.clients).length == 1);
  83. assert.ok(msg == Object.keys(_socket.clients)[0]);
  84. assert.ok(_socket.clients[msg] instanceof Multipart);
  85. _socket.clients[msg].send('from server');
  86. post(client(_server), '/socket.io/xhr-multipart/' + msg + '/send', {data: encode('from client')});
  87. break;
  88. case 1:
  89. assert.ok(msg == 'from server');
  90. --trips || close();
  91. }
  92. });
  93. });
  94. _socket.on('connection', function(client){
  95. assert.ok(client instanceof Multipart);
  96. client.on('message', function(msg){
  97. assert.ok(msg == 'from client');
  98. --trips || close();
  99. });
  100. });
  101. function close(){
  102. _client.end();
  103. _server.close();
  104. };
  105. });
  106. },
  107. 'test clients tracking': function(){
  108. var _server = server()
  109. , _socket = socket(_server);
  110. listen(_server, function(){
  111. var _client = get(_server, '/socket.io/xhr-multipart', function(response){
  112. var once = false
  113. response.on('data', function(){
  114. if (!once){
  115. assert.ok(Object.keys(_socket.clients).length == 1);
  116. once = true;
  117. var _client2 = get(_server, '/socket.io/xhr-multipart', function(response){
  118. response.on('data', function(){
  119. assert.ok(Object.keys(_socket.clients).length == 2);
  120. _client.end();
  121. _client2.end();
  122. _server.close();
  123. });
  124. });
  125. }
  126. });
  127. });
  128. });
  129. },
  130. 'test buffered messages': function(){
  131. var _server = server()
  132. , _socket = socket(_server, { transportOptions: {
  133. 'xhr-multipart': {
  134. closeTimeout: 100
  135. }
  136. } });
  137. listen(_server, function(){
  138. var _client = get(_server, '/socket.io/xhr-multipart', function(response){
  139. var once = false;
  140. response.on('data', function(data){
  141. if (!once){
  142. var sessid = decode(data);
  143. assert.ok(_socket.clients[sessid]._open === true);
  144. assert.ok(_socket.clients[sessid].connected);
  145. _socket.clients[sessid].connection.addListener('end', function(){
  146. assert.ok(_socket.clients[sessid]._open === false);
  147. assert.ok(_socket.clients[sessid].connected);
  148. _socket.clients[sessid].send('from server');
  149. _client = get(_server, '/socket.io/xhr-multipart/' + sessid, function(response){
  150. response.on('data', function(data){
  151. assert.ok(decode(data) == 'from server');
  152. _client.end();
  153. _server.close();
  154. });
  155. });
  156. });
  157. _client.end();
  158. once = true;
  159. }
  160. });
  161. });
  162. });
  163. },
  164. 'test hearbeat timeout': function(){
  165. var _server = server()
  166. , _socket = socket(_server, {
  167. transportOptions: {
  168. 'xhr-multipart': {
  169. timeout: 100,
  170. heartbeatInterval: 1
  171. }
  172. }
  173. });
  174. listen(_server, function(){
  175. var client = get(_server, '/socket.io/xhr-multipart', function(response){
  176. var messages = 0;
  177. response.on('data', function(data){
  178. ++messages;
  179. var msg = decode(data);
  180. if (msg[0].substr(0, 3) == '~h~'){
  181. assert.ok(messages == 2);
  182. assert.ok(Object.keys(_socket.clients).length == 1);
  183. setTimeout(function(){
  184. assert.ok(Object.keys(_socket.clients).length == 0);
  185. client.end();
  186. _server.close();
  187. }, 150);
  188. }
  189. });
  190. });
  191. });
  192. }
  193. };