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

/**
 * Test dependencies.
 */

var sio = require('socket.io')
  , should = require('./common')
  , HTTPClient = should.HTTPClient
  , parser = sio.parser
  , ports = 15500;

/**
 * HTTPClient for jsonp-polling transport.
 */

function JSONPPolling (port) {
  HTTPClient.call(this, port);
};

/**
 * Inhertis from HTTPClient.
 */

JSONPPolling.prototype.__proto__ = HTTPClient.prototype;

/**
 * Override GET requests.
 *
 * @api public
 */

JSONPPolling.prototype.get = function (path, opts, fn) {
  if ('function' == typeof opts) {
    fn = opts;
    opts = {};
  }

  opts = opts || {};

  opts.parse = function (data) {
    var head = 'io.j('
      , foot = ');';

    if (~path.indexOf('?i=1')) {
      head = 'io[1](';
    }

    data.substr(0, head.length).should.eql(head);
    data.substr(-foot.length).should.eql(foot);

    data = data.substr(head.length, data.length - head.length - foot.length);

    return JSON.parse(data);
  };

  return HTTPClient.prototype.get.call(this, path, opts, fn);
};

/**
 * Create client for this transport.
 *
 * @api public
 */

function client (port) {
  return new JSONPPolling(port);
};

/**
 * Test.
 */

module.exports = {

  'test handshake': function (done) {
    var cl = client(++ports)
      , io = create(cl);

    io.configure(function () {
      io.set('close timeout', 0);
      io.set('polling duration', 0);
    });<commit_msg>Fixed test jsonp handshake parser.<commit_after>
/*!
 * socket.io-node
 * Copyright(c) 2011 LearnBoost <<EMAIL>>
 * MIT Licensed
 */

/**
 * Test dependencies.
 */

var sio = require('socket.io')
  , should = require('./common')
  , HTTPClient = should.HTTPClient
  , parser = sio.parser
  , ports = 15500;

/**
 * HTTPClient for jsonp-polling transport.
 */

function JSONPPolling (port) {
  HTTPClient.call(this, port);
};

/**
 * Inhertis from HTTPClient.
 */

JSONPPolling.prototype.__proto__ = HTTPClient.prototype;

/**
 * Performs a json-p (cross domain) handshake
 *
 * @api public
 */

JSONPPolling.prototype.handshake = function (opts, fn) {
  if ('function' == typeof opts) {
    fn = opts;
    opts = {};
  }

  return this.get(
      '/socket.io/{protocol}?jsonp=59'
    , opts
    , function (res, data) {
        var head = 'io[59]('
          , foot = ');';

        data.substr(0, head.length).should.eql(head);
        data.substr(-foot.length).should.eql(foot);
        data = data.slice(head.length, data.length - foot.length);

        fn && fn.apply(null, JSON.parse(data).split(':'));
      }
  );
};

/**
 * Override GET requests.
 *
 * @api public
 */

JSONPPolling.prototype.get = function (path, opts, fn) {
  if ('function' == typeof opts) {
    fn = opts;
    opts = {};
  }

  opts = opts || {};

  opts.parse = function (data) {
    var head = 'io.j('
      , foot = ');';

    if (~path.indexOf('?i=1')) {
      head = 'io[1](';
    }

    data.substr(0, head.length).should.eql(head);
    data.substr(-foot.length).should.eql(foot);

    data = data.substr(head.length, data.length - head.length - foot.length);

    return JSON.parse(data);
  };

  return HTTPClient.prototype.get.call(this, path, opts, fn);
};

/**
 * Create client for this transport.
 *
 * @api public
 */

function client (port) {
  return new JSONPPolling(port);
};

/**
 * Test.
 */

module.exports = {

  'test jsonp handshake': function (done) {
    var cl = client(++ports)
      , io = create(cl);

    io.configure(function () {
      io.set('close timeout', 0);
      io.set('polling duration', 0);
    });
{'fraction_non_alphanumeric': 0.18043237250554323, 'fraction_numerical': 0.009146341463414634, 'mean_word_length': 2.6977459016393444, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '7043213', 'n_tokens_mistral': 1289, 'n_tokens_neox': 1240, 'n_words': 267}