<commit_before>var expect = require('expect.js');
var io = require('../');
var hasCORS = require('has-cors');
var textBlobBuilder = require('text-blob-builder');

describe('connection', function() {
  this.timeout(20000);

  it('should connect to localhost', function(done) {
    var socket = io({ forceNew: true });
    socket.emit('hi');
    socket.on('hi', function(data){
      socket.disconnect();
      done();
    });
  });

  it('should work with acks', function(done){
    var socket = io({ forceNew: true });
    socket.emit('ack');
    socket.on('ack', function(fn){
      fn(5, { test: true });
    });
    socket.on('got it', function(){
      socket.disconnect();
      done();
    });
  });

  it('should receive date with ack', function(done){
    var socket = io({ forceNew: true });
    socket.emit('getAckDate', { test: true }, function(data){
       expect(data).to.be.a('string');
       socket.disconnect();
       done();
    });
  });

  it('should work with false', function(done){
    var socket = io({ forceNew: true });
    socket.emit('false');
    socket.on('false', function(f){
      expect(f).to.be(false);
      socket.disconnect();
      done();
    });
  });

  it('should receive utf8 multibyte characters', function(done) {
    var correct = [
      'てすと',
      'Я Б Г Д Ж Й',
      'Ä ä Ü ü ß',
      'utf8 — string',
      'utf8 — string'
    ];

    var socket = io({ forceNew: true });
    var i = 0;
    socket.on('takeUtf8', function(data) {
      expect(data).to.be(correct[i]);
      i++;
      if (i == correct.length) {
        socket.disconnect();
        done();
      }
    });
    socket.emit('getUtf8');
  });

  it('should connect to a namespace after connection established', function(done) {
    var manager = io.Manager();
    var socket = manager.socket('/');
    socket.on('connect', function(){
      var foo = manager.socket('/foo');
      foo.on('connect', function(){
        foo.close();
        socket.close();
        done();
      });
    });
  });

  it('should reconnect by default', function(done){
    var socket = io({ forceNew: true });
    socket.io.on('reconnect', function() {
      socket.disconnect();
      done();
    });

    setTimeout(function() {
      socket.io.engine.close();
    }, 500);
  });

  it('reconnect event should fire in socket', function(done){
    var socket = io({ forceNew: true });

    socket.on('reconnect', function() {
      socket.disconnect();
      done();
    });

    setTimeout(function() {
      socket.io.engine.close();
    }, 500);
  });

  it('should try to reconnect twice and fail when requested two attempts with immediate timeout and reconnect enabled', function(done) {
    var manager = io.Manager({ reconnection: true, timeout: 0, reconnectionAttempts: 2, reconnectionDelay: 10 });
    var socket;

    var reconnects = 0;
    var reconnectCb = function() {
      reconnects++;
    };

    manager.on('reconnect_attempt', reconnectCb);
    manager.on('reconnect_failed', function failed() {
      expect(reconnects).to.be(2);
      socket.close();
      done();
    });

    socket = manager.socket('/timeout');
  });

  it('should fire reconnect_* events on socket', function(done) {
    var manager = io.Manager({ reconnection: true, timeout: 0, reconnectionAttempts: 2, reconnectionDelay: 10 });
    var socket = manager.socket('/timeout_socket');

    var reconnects = 0;
    var reconnectCb = function(attempts) {
      reconnects++;
      expect(attempts).to.be(reconnects);
    };

    socket.on('reconnect_attempt', reconnectCb);
    socket.on('reconnect_failed', function failed() {
      expect(reconnects).to.be(2);
      socket.close();
      done();
    });
  });

  it('should fire error on socket', function(done) {
    var manager = io.Manager({ reconnection: true });
    var socket = manager.socket('/timeout_socket');

    socket.on('error', function(data) {
      expect(data.code).to.be('test');
      done();
    });

    socket.on('connect', function() {
      manager.engine.onPacket({ type: 'error', data: 'test' });
    });
  });

  it('should fire reconnecting (on socket) with attempts number when reconnecting twice', function(done) {
    var manager = io.Manager({ reconnection: true, timeout: 0, reconnectionAttempts: 2, reconnectionDelay: 10 });
    var socket = manager.socket('/timeout_socket');

    var reconnects = 0;<commit_msg>Fix possible security error on IE by closing all sockets<commit_after>var expect = require('expect.js');
var io = require('../');
var hasCORS = require('has-cors');
var textBlobBuilder = require('text-blob-builder');

describe('connection', function() {
  this.timeout(20000);

  it('should connect to localhost', function(done) {
    var socket = io({ forceNew: true });
    socket.emit('hi');
    socket.on('hi', function(data){
      socket.disconnect();
      done();
    });
  });

  it('should work with acks', function(done){
    var socket = io({ forceNew: true });
    socket.emit('ack');
    socket.on('ack', function(fn){
      fn(5, { test: true });
    });
    socket.on('got it', function(){
      socket.disconnect();
      done();
    });
  });

  it('should receive date with ack', function(done){
    var socket = io({ forceNew: true });
    socket.emit('getAckDate', { test: true }, function(data){
       expect(data).to.be.a('string');
       socket.disconnect();
       done();
    });
  });

  it('should work with false', function(done){
    var socket = io({ forceNew: true });
    socket.emit('false');
    socket.on('false', function(f){
      expect(f).to.be(false);
      socket.disconnect();
      done();
    });
  });

  it('should receive utf8 multibyte characters', function(done) {
    var correct = [
      'てすと',
      'Я Б Г Д Ж Й',
      'Ä ä Ü ü ß',
      'utf8 — string',
      'utf8 — string'
    ];

    var socket = io({ forceNew: true });
    var i = 0;
    socket.on('takeUtf8', function(data) {
      expect(data).to.be(correct[i]);
      i++;
      if (i == correct.length) {
        socket.disconnect();
        done();
      }
    });
    socket.emit('getUtf8');
  });

  it('should connect to a namespace after connection established', function(done) {
    var manager = io.Manager();
    var socket = manager.socket('/');
    socket.on('connect', function(){
      var foo = manager.socket('/foo');
      foo.on('connect', function(){
        foo.close();
        socket.close();
        done();
      });
    });
  });

  it('should reconnect by default', function(done){
    var socket = io({ forceNew: true });
    socket.io.on('reconnect', function() {
      socket.disconnect();
      done();
    });

    setTimeout(function() {
      socket.io.engine.close();
    }, 500);
  });

  it('reconnect event should fire in socket', function(done){
    var socket = io({ forceNew: true });

    socket.on('reconnect', function() {
      socket.disconnect();
      done();
    });

    setTimeout(function() {
      socket.io.engine.close();
    }, 500);
  });

  it('should try to reconnect twice and fail when requested two attempts with immediate timeout and reconnect enabled', function(done) {
    var manager = io.Manager({ reconnection: true, timeout: 0, reconnectionAttempts: 2, reconnectionDelay: 10 });
    var socket;

    var reconnects = 0;
    var reconnectCb = function() {
      reconnects++;
    };

    manager.on('reconnect_attempt', reconnectCb);
    manager.on('reconnect_failed', function failed() {
      expect(reconnects).to.be(2);
      socket.close();
      done();
    });

    socket = manager.socket('/timeout');
  });

  it('should fire reconnect_* events on socket', function(done) {
    var manager = io.Manager({ reconnection: true, timeout: 0, reconnectionAttempts: 2, reconnectionDelay: 10 });
    var socket = manager.socket('/timeout_socket');

    var reconnects = 0;
    var reconnectCb = function(attempts) {
      reconnects++;
      expect(attempts).to.be(reconnects);
    };

    socket.on('reconnect_attempt', reconnectCb);
    socket.on('reconnect_failed', function failed() {
      expect(reconnects).to.be(2);
      socket.close();
      done();
    });
  });

  it('should fire error on socket', function(done) {
    var manager = io.Manager({ reconnection: true });
    var socket = manager.socket('/timeout_socket');

    socket.on('error', function(data) {
      expect(data.code).to.be('test');
      socket.close();
      done();
    });

    socket.on('connect', function() {
      manager.engine.onPacket({ type: 'error', data: 'test' });
    });
  });

  it('should fire reconnecting (on socket) with attempts number when reconnecting twice', function(done) {
    var manager = io.Manager({ reconnection: true, timeout: 0, reconnectionAttempts: 2, reconnectionDelay: 10 });
    var socket = manager.socket('/timeout_socket');

    var reconnects = 0;
{'fraction_non_alphanumeric': 0.17082815501073326, 'fraction_numerical': 0.007908710880126539, 'mean_word_length': 2.899559471365639, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '307169', 'n_tokens_mistral': 2769, 'n_tokens_neox': 2660, 'n_words': 667}