<commit_before>/* jshint mocha: true */
'use strict';

var assert = require('assert');
var http = require('http');
var _ = require('lodash');

var bowerBrowser = require('../lib/');

var baseOptions = {
  path: 'test/fixtures',
  open: false,
  silent: true
};

describe('Server', function () {

  this.timeout(10000);

  it('returns HTTP response', function (done) {
    var app = bowerBrowser(baseOptions);
    app.on('start', function () {
      http.get('http://localhost:3010/', function (res) {
        assert(res.statusCode === 200);
        app.close();
        done();
      });
    });
  });

  it('isn\'t accessible after `close` event', function (done) {
    var app = bowerBrowser(baseOptions);
    app.on('start', function () {
      http.get('http://localhost:3010/', function (res) {
        assert(res.statusCode === 200);
        app.close();
      });
    });
    app.on('close', function () {
      http.get('http://localhost:3010/')
      .on('error', function (error) {
        assert(error.code = 'ECONNRESET');
        done();
      });
    });
  });

  it('listens specified port', function (done) {
    var app = bowerBrowser(_.merge({}, baseOptions, {
      port: 3011
    }));
    app.on('start', function () {
      http.get('http://localhost:3011/', function (res) {
        assert(res.statusCode === 200);
        app.close();
        done();
      });
    });
  });

});<commit_msg>Add test for fetching API<commit_after>/* jshint mocha: true */
'use strict';

var assert = require('assert');
var http = require('http');
var _ = require('lodash');

var bowerBrowser = require('../lib/');

var baseOptions = {
  path: 'test/fixtures',
  open: false,
  silent: true
};

describe('Server', function () {

  this.timeout(10000);

  it('returns HTTP response', function (done) {
    var app = bowerBrowser(baseOptions);
    app.on('start', function () {
      http.get('http://localhost:3010/', function (res) {
        assert(res.statusCode === 200);
        app.close();
        done();
      });
    });
  });

  it('isn\'t accessible after `close` event', function (done) {
    var app = bowerBrowser(baseOptions);
    app.on('start', function () {
      http.get('http://localhost:3010/', function (res) {
        assert(res.statusCode === 200);
        app.close();
      });
    });
    app.on('close', function () {
      http.get('http://localhost:3010/')
      .on('error', function (error) {
        assert(error.code = 'ECONNRESET');
        done();
      });
    });
  });

  it('listens specified port', function (done) {
    var app = bowerBrowser(_.merge({}, baseOptions, {
      port: 3011
    }));
    app.on('start', function () {
      http.get('http://localhost:3011/', function (res) {
        assert(res.statusCode === 200);
        app.close();
        done();
      });
    });
  });

  it('fetches API and returns json', function (done) {
    this.timeout(20000);
    var app = bowerBrowser(_.merge({}, baseOptions, {
      cache: 0
    }));
    app.on('start', function () {
      http.get('http://localhost:3010/api/bower-component-list.json', function (res) {
        var body = '';
        assert(res.statusCode === 200);
        res.on('data', function (chunk) {
          body += chunk;
        });
        res.on('end', function () {
          var data;
          var isJson;
          try {
            data = JSON.parse(body);
            isJson = typeof data === 'object' && data !== null;
          }
          catch (e) {
            isJson = false;
          }
          assert(isJson);
          app.close();
          done();
        });
      });
    });
  });

});
{'fraction_non_alphanumeric': 0.19207484865162355, 'fraction_numerical': 0.02228948816730875, 'mean_word_length': 2.265947888589398, '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': '4359967', 'n_tokens_mistral': 1197, 'n_tokens_neox': 1132, 'n_words': 238}