<commit_before>
/*!
 * socket.io-node
 * Copyright(c) 2011 LearnBoost <<EMAIL>>
 * MIT Licensed
 */

/**
 * Test dependencies.
 */

var sio = require('../')
  , should = require('./common')
  , parser = sio.parser
  , ports = 15800;

/**
 * Tests.
 */

module.exports = {
  'websocket identifies as websocket': function (done) {
    var cl = client(++ports)
      , io = create(cl)
      , ws;

    io.set('transports', ['websocket']);
    io.sockets.on('connection', function (socket) {
      socket.manager.transports[socket.id].name.should.equal('websocket');
      ws.finishClose();
      cl.end();
      io.server.close();
      done();
    });
    cl.handshake(function (sid) {
      ws = websocket(cl, sid);
    });
  },

  'default websocket draft parser is used for unknown sec-websocket-version': function (done) {
    var cl = client(++ports)
      , io = create(cl)
      , ws;

    io.set('transports', ['websocket']);
    io.sockets.on('connection', function (socket) {
      socket.manager.transports[socket.id].protocolVersion.should.equal('hixie-76');
      ws.finishClose();
      cl.end();
      io.server.close();
      done();
    });

    cl.handshake(function (sid) {
      ws = websocket(cl, sid);
    });
  },

  'hybi-07-12 websocket draft parser is used for sec-websocket-version: 8': function (done) {
    var cl = client(++ports)
      , io = create(cl);

    io.set('transports', ['websocket']);
    io.sockets.on('connection', function (socket) {
      socket.manager.transports[socket.id].protocolVersion.should.equal('07-12');
      cl.end();
      io.server.close();
      done();
    });

    var headers = {
      'sec-websocket-version': 8,
      'upgrade': 'websocket',
      'Sec-WebSocket-Key': '<KEY>=='
    }

    cl.get('/socket.io/{protocol}', {}, function (res, data) {
      var sid = data.split(':')[0];
      var url = '/socket.io/' + sio.protocol + '/websocket/' + sid;
      cl.get(url, {headers: headers}, function (res, data) {});
    });
  },

  'hybi-16 websocket draft parser is used for sec-websocket-version: 13': function (done) {
    var cl = client(++ports)
      , io = create(cl)

    io.set('transports', ['websocket']);

    io.sockets.on('connection', function (socket) {
      socket.manager.transports[socket.id].protocolVersion.should.equal('16');
      cl.end();
      io.server.close();
      done();
    });

    var headers = {
      'sec-websocket-version': 13,
      'upgrade': 'websocket',
      'Sec-WebSocket-Key': '<KEY>
    }

    cl.get('/socket.io/{protocol}', {}, function (res, data) {
      var sid = data.split(':')[0];
      var url = '/socket.io/' + sio.protocol + '/websocket/' + sid;
      cl.get(url, {headers: headers}, function (res, data) {});
    });
  },

  'hybi-07-12 origin filter blocks access for mismatched sec-websocket-origin': function (done) {
    var cl = client(++ports)
      , io = create(cl)

    io.set('transports', ['websocket']);
    io.set('origins', 'foo.bar.com:*');
    var notConnected = true;
    io.sockets.on('connection', function() {
        notConnected = false;
    });

    var headers = {
      'sec-websocket-version': 8,
      'upgrade': 'websocket',
      'Sec-WebSocket-Origin': 'http://baz.bar.com',
      'Sec-WebSocket-Key': '<KEY>
    }

    // handshake uses correct origin -- we want to block the actual websocket call
    cl.get('/socket.io/{protocol}', {headers: {origin: 'http://foo.bar.com'}}, function (res, data) {
      var sid = data.split(':')[0];
      var url = '/socket.io/' + sio.protocol + '/websocket/' + sid;
      var req = cl.get(url, {headers: headers}, function (res, data) {});
      var closed = false;
      req.socket.on('close', function() {
        if (closed) return;
        closed = true;
        notConnected.should.be.true;
        cl.end();
        try {
          io.server.close();
        }
        catch (e) {}
        done();
      });
    });
  },

  'hybi-16 origin filter blocks access for mismatched sec-websocket-origin': function (done) {
    var cl = client(++ports)
      , io = create(cl);

    io.set('transports', ['websocket']);
    io.set('origins', 'foo.bar.com:*');
    var notConnected = true;
    io.sockets.on('connection', function() {
        notConnected = false;
    });

    var headers = {
      'sec-websocket-version': 13,
      'upgrade': 'websocket',
      'origin': 'http://baz.bar.com',
      'Sec-WebSocket-Key': '<KEY>
    }

    // handshake uses correct origin -- we want to block the actual websocket call
    cl.get('/socket.io/{protocol}', {headers: {origin: 'http://foo.bar.com'}}, function (res, data) {
      var sid = data.split(':')[0];
      var url = '/socket.io/' + sio.protocol + '/websocket/' + sid;
      var req = cl.get(url, {headers: headers}, function (res, data) {});
      var closed = false;
      req.socket.on('close', function() {
        if (closed) return;
        closed = true;
        notConnected.should.be.true;
        cl.end();
        try {
          io.server.close();
        }
        catch (e) {}
        done();<commit_msg>Make these 2 tests work both on 0.4 and 0.6<commit_after>
/*!
 * socket.io-node
 * Copyright(c) 2011 LearnBoost <<EMAIL>>
 * MIT Licensed
 */

/**
 * Test dependencies.
 */

var sio = require('../')
  , should = require('./common')
  , parser = sio.parser
  , ports = 15800;

/**
 * Tests.
 */

module.exports = {
  'websocket identifies as websocket': function (done) {
    var cl = client(++ports)
      , io = create(cl)
      , ws;

    io.set('transports', ['websocket']);
    io.sockets.on('connection', function (socket) {
      socket.manager.transports[socket.id].name.should.equal('websocket');
      ws.finishClose();
      cl.end();
      io.server.close();
      done();
    });
    cl.handshake(function (sid) {
      ws = websocket(cl, sid);
    });
  },

  'default websocket draft parser is used for unknown sec-websocket-version': function (done) {
    var cl = client(++ports)
      , io = create(cl)
      , ws;

    io.set('transports', ['websocket']);
    io.sockets.on('connection', function (socket) {
      socket.manager.transports[socket.id].protocolVersion.should.equal('hixie-76');
      ws.finishClose();
      cl.end();
      io.server.close();
      done();
    });

    cl.handshake(function (sid) {
      ws = websocket(cl, sid);
    });
  },

  'hybi-07-12 websocket draft parser is used for sec-websocket-version: 8': function (done) {
    var cl = client(++ports)
      , io = create(cl);

    io.set('transports', ['websocket']);
    io.sockets.on('connection', function (socket) {
      socket.manager.transports[socket.id].protocolVersion.should.equal('07-12');
      cl.end();
      io.server.close();
      done();
    });

    var headers = {
      'sec-websocket-version': 8,
      'upgrade': 'websocket',
      'Sec-WebSocket-Key': '<KEY>
    }

    cl.get('/socket.io/{protocol}', {}, function (res, data) {
      var sid = data.split(':')[0];
      var url = '/socket.io/' + sio.protocol + '/websocket/' + sid;
      cl.get(url, {headers: headers}, function (res, data) {});
    });
  },

  'hybi-16 websocket draft parser is used for sec-websocket-version: 13': function (done) {
    var cl = client(++ports)
      , io = create(cl)

    io.set('transports', ['websocket']);

    io.sockets.on('connection', function (socket) {
      socket.manager.transports[socket.id].protocolVersion.should.equal('16');
      cl.end();
      io.server.close();
      done();
    });

    var headers = {
      'sec-websocket-version': 13,
      'upgrade': 'websocket',
      'Sec-WebSocket-Key': '<KEY>
    }

    cl.get('/socket.io/{protocol}', {}, function (res, data) {
      var sid = data.split(':')[0];
      var url = '/socket.io/' + sio.protocol + '/websocket/' + sid;
      cl.get(url, {headers: headers}, function (res, data) {});
    });
  },

  'hybi-07-12 origin filter blocks access for mismatched sec-websocket-origin': function (done) {
    var cl = client(++ports)
      , io = create(cl)

    io.set('transports', ['websocket']);
    io.set('origins', 'foo.bar.com:*');
    var notConnected = true;
    io.sockets.on('connection', function() {
        notConnected = false;
    });

    var headers = {
      'sec-websocket-version': 8,
      'upgrade': 'websocket',
      'Sec-WebSocket-Origin': 'http://baz.bar.com',
      'Sec-WebSocket-Key': '<KEY>
    }

    // handshake uses correct origin -- we want to block the actual websocket call
    cl.get('/socket.io/{protocol}', {headers: {origin: 'http://foo.bar.com'}}, function (res, data) {
      var sid = data.split(':')[0];
      var url = '/socket.io/' + sio.protocol + '/websocket/' + sid;
      var req = cl.get(url, {headers: headers}, function (res, data) {});
      var closed = false;
      (req.socket || req).on('close', function() {
        if (closed) return;
        closed = true;
        notConnected.should.be.true;
        cl.end();
        try {
          io.server.close();
        }
        catch (e) {}
        done();
      });
    });
  },

  'hybi-16 origin filter blocks access for mismatched sec-websocket-origin': function (done) {
    var cl = client(++ports)
      , io = create(cl);

    io.set('transports', ['websocket']);
    io.set('origins', 'foo.bar.com:*');
    var notConnected = true;
    io.sockets.on('connection', function() {
        notConnected = false;
    });

    var headers = {
      'sec-websocket-version': 13,
      'upgrade': 'websocket',
      'origin': 'http://baz.bar.com',
      'Sec-WebSocket-Key': '<KEY>
    }

    // handshake uses correct origin -- we want to block the actual websocket call
    cl.get('/socket.io/{protocol}', {headers: {origin: 'http://foo.bar.com'}}, function (res, data) {
      var sid = data.split(':')[0];
      var url = '/socket.io/' + sio.protocol + '/websocket/' + sid;
      var req = cl.get(url, {headers: headers}, function (res, data) {});
      var closed = false;
      (req.socket || req).on('close', function() {
        if (closed) return;
        closed = true;
        notConnected.should.be.true;
        cl.end();
        try {
          io.server.close();
        }
        catch (e) {}
        done();
{'fraction_non_alphanumeric': 0.1877883577108079, 'fraction_numerical': 0.008736625110434867, 'mean_word_length': 2.427994616419919, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '5196344', 'n_tokens_mistral': 3465, 'n_tokens_neox': 3298, 'n_words': 734}