<commit_before>var express = require('express');
var http = require('http');
var sio = require('socket.io');
var request = require('supertest');
var agent = require('../lib');
require('should');

/**
 * Helper function
 */
// Creates a mock of the analytics server.
// Returns: Server address
var createAnalyticsServer = function(socketHandler) {
  var server = http.Server();
  var io = sio(server);

  io.on('connection', socketHandler.bind(server));

  var addr = server.address();
  if (!addr) {
    addr = server.listen().address();
  }

  return addr;
};

describe('Agent', function() {

  it('should record event with an Express server', function(done) {
    var analyticsAddr = createAnalyticsServer(function(socket) {
      socket.on('record', function(event) {
        event.should.be.ok;
        event.should.have.property('version');

        done();
      });
    });

    // Create Express server for api call
    var app = express();

    // Attach agent
    app.use(agent('fake-key', {host: analyticsAddr.address, port: analyticsAddr.port}));

    // Setup a route
    app.get('/', function(req, res) {
      res.send('Bonjour');
    });

    // Start the server
    app.listen(function() {

      // Call the route
      request(app)
        .get('/')
        .expect('Bonjour')
        .end(function() {});
    });

  });

  it('should record event with an HTTP server', function(done) {
    var analyticsAddr = createAnalyticsServer(function(socket) {
      socket.on('record', function(event) {
        event.should.be.ok;
        event.should.have.property('version');

        done();
      });
    });

    // Create server and attach agent
    var analytics = agent('fake-key', {host: analyticsAddr.address, port: analyticsAddr.port});
    var server = http.createServer(function(req, res) {
      analytics(req, res);

      res.writeHead(200, { 'Content-Type': 'text/plain'});
      res.write('This is a test');
      res.end();
    });
    server.listen(function() {
      // Call the route
      request(server)
        .get('/')
        .expect(200)
        .expect('This is a test')
        .end(function() {});
    });
  });
});<commit_msg>added test case for custom logger<commit_after>var express = require('express');
var http = require('http');
var sio = require('socket.io');
var request = require('supertest');
var agent = require('../lib');
require('should');

/**
 * Helper function
 */
// Creates a mock of the analytics server.
// Returns: Server address
var createAnalyticsServer = function(socketHandler) {
  var server = http.Server();
  var io = sio(server);

  io.on('connection', socketHandler.bind(server));

  var addr = server.address();
  if (!addr) {
    addr = server.listen().address();
  }

  return addr;
};

describe('Agent', function() {

  it('should record event with an Express server', function(done) {
    var analyticsAddr = createAnalyticsServer(function(socket) {
      socket.on('record', function(event) {
        event.should.be.ok;
        event.should.have.property('version');

        done();
      });
    });

    // Create Express server for api call
    var app = express();

    // Attach agent
    app.use(agent('fake-key', {host: analyticsAddr.address, port: analyticsAddr.port}));

    // Setup a route
    app.get('/', function(req, res) {
      res.send('Bonjour');
    });

    // Start the server
    app.listen(function() {

      // Call the route
      request(app)
        .get('/')
        .expect('Bonjour')
        .end(function() {});
    });

  });

  it('should record event with an HTTP server', function(done) {
    var analyticsAddr = createAnalyticsServer(function(socket) {
      socket.on('record', function(event) {
        event.should.be.ok;
        event.should.have.property('version');

        done();
      });
    });

    // Create server and attach agent
    var analytics = agent('fake-key', {host: analyticsAddr.address, port: analyticsAddr.port});
    var server = http.createServer(function(req, res) {
      analytics(req, res);

      res.writeHead(200, { 'Content-Type': 'text/plain'});
      res.write('This is a test');
      res.end();
    });
    server.listen(function() {
      // Call the route
      request(server)
        .get('/')
        .expect(200)
        .expect('This is a test')
        .end(function() {});
    });
  });

  it('should use custom logger', function(done) {
    var analyticsAddr = createAnalyticsServer(function(socket) {
    });

    // Create server and attach agent
    var analytics = agent('fake-key', {host: analyticsAddr.address, port: analyticsAddr.port, logger: function(message) {
      message.should.equal('Connected to API Analytics socket.io server with service token fake-key.');

      done();
    }});
    var server = http.createServer(function(req, res) {
      analytics(req, res);

      res.writeHead(200, { 'Content-Type': 'text/plain'});
      res.write('This is a test');
      res.end();
    });
  });
});
{'fraction_non_alphanumeric': 0.16452648475120385, 'fraction_numerical': 0.003009630818619583, 'mean_word_length': 2.7481203007518795, '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': '7105651', 'n_tokens_mistral': 1488, 'n_tokens_neox': 1444, 'n_words': 360}