<commit_before>var assert = require('assert')
  , https = require('https')
  , http = require('http')
  , should = require('should')
  , WebSocket = require('../')
  , WebSocketServer = require('../').Server
  , fs = require('fs')
  , server = require('./testserver')
  , crypto = require('crypto');

var port = 20000;

function getArrayBuffer(buf) {
  var l = buf.length;
  var arrayBuf = new ArrayBuffer(l);
  var uint8View = new Uint8Array(arrayBuf);
  for (var i = 0; i < l; i++) {
    uint8View[i] = buf[i];
  }
  return uint8View.buffer;
}


function areArraysEqual(x, y) {
  if (x.length != y.length) return false;
  for (var i = 0, l = x.length; i < l; ++i) {
    if (x[i] !== y[i]) return false;
  }
  return true;
}

describe('WebSocket', function() {
  describe('#ctor', function() {
    it('throws exception for invalid url', function(done) {
      try {
        var ws = new WebSocket('echo.websocket.org');
      }
      catch (e) {
        done();
      }
    });
    it('throws TypeError when called without new', function(done) {
      try {
        var ws = WebSocket('ws://localhost:' + port);
      }
      catch (e) {
        e.should.be.instanceof(TypeError);
        done();
      }
    });
  });

  describe('options', function() {
    it('should accept an `agent` option', function(done) {
      var wss = new WebSocketServer({port: ++port}, function() {
        var agent = {
          addRequest: function() {
            wss.close();
            done();
          }
        };
        var ws = new WebSocket('ws://localhost:' + port, { agent: agent });
      });
    });
    // GH-227
    it('should accept the `options` object as the 3rd argument', function(done) {
      var wss = new WebSocketServer({port: ++port}, function() {
        var agent = {
          addRequest: function() {
            wss.close();
            done();
          }
        };
        var ws = new WebSocket('ws://localhost:' + port, [], { agent: agent });
      });
    });
  });

  describe('properties', function() {
    it('#bytesReceived exposes number of bytes received', function(done) {
      var wss = new WebSocketServer({port: ++port}, function() {<commit_msg>Add test for localAddress option<commit_after>var assert = require('assert')
  , https = require('https')
  , http = require('http')
  , should = require('should')
  , WebSocket = require('../')
  , WebSocketServer = require('../').Server
  , fs = require('fs')
  , os = require('os')
  , server = require('./testserver')
  , crypto = require('crypto');

var port = 20000;

function getArrayBuffer(buf) {
  var l = buf.length;
  var arrayBuf = new ArrayBuffer(l);
  var uint8View = new Uint8Array(arrayBuf);
  for (var i = 0; i < l; i++) {
    uint8View[i] = buf[i];
  }
  return uint8View.buffer;
}


function areArraysEqual(x, y) {
  if (x.length != y.length) return false;
  for (var i = 0, l = x.length; i < l; ++i) {
    if (x[i] !== y[i]) return false;
  }
  return true;
}

describe('WebSocket', function() {
  describe('#ctor', function() {
    it('throws exception for invalid url', function(done) {
      try {
        var ws = new WebSocket('echo.websocket.org');
      }
      catch (e) {
        done();
      }
    });
    it('throws TypeError when called without new', function(done) {
      try {
        var ws = WebSocket('ws://localhost:' + port);
      }
      catch (e) {
        e.should.be.instanceof(TypeError);
        done();
      }
    });
  });

  describe('options', function() {
    it('should accept an `agent` option', function(done) {
      var wss = new WebSocketServer({port: ++port}, function() {
        var agent = {
          addRequest: function() {
            wss.close();
            done();
          }
        };
        var ws = new WebSocket('ws://localhost:' + port, { agent: agent });
      });
    });
    // GH-227
    it('should accept the `options` object as the 3rd argument', function(done) {
      var wss = new WebSocketServer({port: ++port}, function() {
        var agent = {
          addRequest: function() {
            wss.close();
            done();
          }
        };
        var ws = new WebSocket('ws://localhost:' + port, [], { agent: agent });
      });
    });

    it('should accept the localAddress option', function(done) {
      // explore existing interfaces
      var devs = os.networkInterfaces()
        , localAddresses = []
        , j, ifc, dev, devname;
      for ( devname in devs ) {
        dev = devs[devname];
        for ( j=0;j<dev.length;j++ ) {
          ifc = dev[j];
          if ( !ifc.internal && ifc.family === 'IPv4' ) {
            localAddresses.push(ifc.address);
          }
        }
      }
      var wss = new WebSocketServer({port: ++port}, function() {
        var ws = new WebSocket('ws://localhost:' + port, { localAddress: localAddresses[0] });
        ws.on('open', function () {
          done();
        });
      });
    });

    it('should accept the localAddress option whether it was wrong interface', function(done) {
      var wss = new WebSocketServer({port: ++port}, function() {
        var ws = new WebSocket('ws://localhost:' + port, { localAddress: '123.456.789.428' });
        ws.on('error', function (error) {
          error.code.should.eql('EADDRNOTAVAIL');
          done();
        });
      });
    });
  });

  describe('properties', function() {
    it('#bytesReceived exposes number of bytes received', function(done) {
      var wss = new WebSocketServer({port: ++port}, function() {
{'fraction_non_alphanumeric': 0.1693313953488372, 'fraction_numerical': 0.008175872093023256, 'mean_word_length': 2.2748364069006546, 'pattern_counts': {'":': 0, '<': 8, '<?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': '5073588', 'n_tokens_mistral': 1740, 'n_tokens_neox': 1687, 'n_words': 435}