<commit_before>var expect = require("chai").expect;
var ajaxWrapper = require("../js/helpers/ajaxWrapper");
var config = require("../js/config/config");

var expectAsync = require("./helpers/expectAsync");
var HttpServer = require("./helpers/HttpServer").HttpServer;

var server = new HttpServer(config.localTestserverURI);
config.apiURL = "http://" + server.address + ":" + server.port + "/";

describe("ajaxWrapper", function () {

  beforeEach(function () {
    this.server = server
      .setup({
        "name": "Marathon"
      }, 200)
      .start();
  });

  afterEach(function (done) {
    this.server.stop(done);
  });

  describe("on GET request", function () {

    it("returns a JSON object on success", function (done) {
      ajaxWrapper({
        method: "GET",
        url: config.apiURL
      })
      .success(function (response) {
        expectAsync(function () {
          expect(response.body.name).to.equal("Marathon");
        }, done);
      })
      .error(function () {
        done(new Error("I should not be called"));
      });
    });

    it("defaults to GET when no method is supplied", function (done) {
      ajaxWrapper({
        url: config.apiURL
      })
      .success(function (response) {
        expectAsync(function () {
          expect(response.body.name).to.equal("Marathon");
        }, done);
      })
      .error(function () {
        done(new Error("I should not be called"));
      });
    });

    it("handles failure gracefully", function (done) {
      this.server.setup({message: "Guru Meditation"}, 404);

      ajaxWrapper({
        method: "GET",
        url: config.apiURL + "/foo/bar"
      })
      .success(function () {
        done(new Error("I should not be called"));
      })
      .error(function (error) {
        expectAsync(function () {
          expect(error.body.message).to.equal("Guru Meditation");
        }, done);
      });
    });

  });

  describe("on concurrent request", function () {

    it("should timeout on second request", function (done) {
      var responses = 0;
      var timeoutId;
      var initialTimeout = this.timeout();
      this.timeout(50);

      var increaseResponses = function () {
        responses++;
        if (responses === 2) {
          clearTimeout(timeoutId);
          done(new Error("Second request should never be fulfilled"));
        }
      };

      ajaxWrapper({
        method: "GET",
        url: config.apiURL + "/concurrent"
      })
      .success(increaseResponses);

      ajaxWrapper({
        method: "GET",
        url: config.apiURL + "/concurrent"<commit_msg>Add individual server setup per test-case<commit_after>var expect = require("chai").expect;
var ajaxWrapper = require("../js/helpers/ajaxWrapper");
var config = require("../js/config/config");

var expectAsync = require("./helpers/expectAsync");
var HttpServer = require("./helpers/HttpServer").HttpServer;

var server = new HttpServer(config.localTestserverURI);
config.apiURL = "http://" + server.address + ":" + server.port + "/";

describe("ajaxWrapper", function () {

  describe("on GET request", function () {

    beforeEach(function () {
      this.server = server
        .setup({
          "name": "Marathon"
        }, 200)
        .start();
    });

    afterEach(function (done) {
      this.server.stop(done);
    });

    it("returns a JSON object on success", function (done) {
      ajaxWrapper({
        method: "GET",
        url: config.apiURL
      })
      .success(function (response) {
        expectAsync(function () {
          expect(response.body.name).to.equal("Marathon");
        }, done);
      })
      .error(function () {
        done(new Error("I should not be called"));
      });
    });

    it("defaults to GET when no method is supplied", function (done) {
      ajaxWrapper({
        url: config.apiURL
      })
      .success(function (response) {
        expectAsync(function () {
          expect(response.body.name).to.equal("Marathon");
        }, done);
      })
      .error(function () {
        done(new Error("I should not be called"));
      });
    });

    it("handles failure gracefully", function (done) {
      this.server.setup({message: "Guru Meditation"}, 404);

      ajaxWrapper({
        method: "GET",
        url: config.apiURL + "/foo/bar"
      })
      .success(function () {
        done(new Error("I should not be called"));
      })
      .error(function (error) {
        expectAsync(function () {
          expect(error.body.message).to.equal("Guru Meditation");
        }, done);
      });
    });

  });

  describe("on concurrent request", function () {

    beforeEach(function () {
      this.server = server
        .setup({
          "name": "Marathon"
        }, 200)
        .start();
    });

    afterEach(function (done) {
      this.server.stop(done);
    });

    it("should timeout on second request", function (done) {
      var responses = 0;
      var timeoutId;
      var initialTimeout = this.timeout();
      this.timeout(50);

      var increaseResponses = function () {
        responses++;
        if (responses === 2) {
          clearTimeout(timeoutId);
          done(new Error("Second request should never be fulfilled"));
        }
      };

      ajaxWrapper({
        method: "GET",
        url: config.apiURL + "/concurrent"
      })
      .success(increaseResponses);

      ajaxWrapper({
        method: "GET",
        url: config.apiURL + "/concurrent"
{'fraction_non_alphanumeric': 0.1554945054945055, 'fraction_numerical': 0.004212454212454212, 'mean_word_length': 2.242874109263658, 'pattern_counts': {'":': 5, '<': 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': '4413126', 'n_tokens_mistral': 1590, 'n_tokens_neox': 1528, 'n_words': 347}