<commit_before>var test    = require('tape');
var request = require('request');
var qs      = require('querystring');
var host    = "http://127.0.0.1:";
var port    = Math.floor(Math.random() * 9000) + 1000;

// var server = require('../server');

var exec = require('child_process').exec;
exec('PORT='+port +' node ./example/server.js', function(error, stdout, stderr) {
  // console.log(stdout);             // uncomment for console.log
  // console.log('stderr: ', stderr); // uncomment for errors
  if (error !== null) {
    console.log('exec error: ', error);
  }

});

setTimeout(function() { // only run tests once child_process has started

  test("Connect to localhost "+host+":"+port, function (t) {
    request(host+port ,function (err, res, body) {
      // console.log(err);
      // t.equal(err, null, "No Errors Connecting");
      // console.log(res);
      t.equal(res.statusCode, 200, "Status 200");
      t.end();
    });
  });

  // attempt to access content before being authenticated
  test("Attempt auth "+host+port+"/auth (incorrect username/password should fail)", function (t) {

    var form = {
      username: 'lordbusiness',
      password: '<PASSWORD>',
    };

    var formData      = qs.stringify(form);
    var contentLength = formData.length;

    var options = {
      headers: {
        'Content-Length': contentLength,
        'Content-Type': 'application/x-www-form-urlencoded',
        'user-agent': 'Mozilla/5.0'
      },
      uri: host+port+"/auth",
      body: formData,
      method: 'POST'
    }

    request.post(options ,function (err, res, body) {
      t.equal(res.statusCode, 401, "Cannot authenticate (incorrect un/pw)");
      t.end();
    });
  });

  test("Attempt to access restricted content: "+host+port+"/private without supplying a valid token!", function (t) {

    var options = {
      headers: {
        'authorization': 'invalid',
        'user-agent': 'Mozilla/5.0'
      },
      uri: host+port+"/private",
      method: 'GET'
    }

    request(options ,function (err, res, body) {
      t.equal(res.statusCode, 401, "Private content access denied!");
      t.end();
    });
  });


  var token   = null; // starts out empty && Yes, GLOBAL (its a test!)

  test("Authenticate "+host+port+"/auth", function (t) {

    var form = {
      username: 'masterbuilder',
      password: '<PASSWORD>',
    };

    var formData      = qs.stringify(form);
    var contentLength = formData.length;

    var options = {
      headers: {
        'Content-Length': contentLength,
        'Content-Type': 'application/x-www-form-urlencoded',
        'user-agent': 'Mozilla/5.0'
      },
      uri: host+port+"/auth",
      body: formData,
      method: 'POST'
    }

    request.post(options ,function (err, res, body) {
      token = res.headers.authorization; // save the token for later
      t.equal(res.statusCode, 200, "Authenticated");
      t.end();
    });
  });

  test("Access restricted content: "+host+port+"/private", function (t) {

    var options = {
      headers: {
        'authorization': token,
        'user-agent': 'Mozilla/5.0'
      },
      uri: host+port+"/private",
      method: 'GET'
    }

    request(options ,function (err, res, body) {

      t.equal(res.statusCode, 200, "Private content accessed");
      t.end();
    });
  });

  // simulate logging out (reset token) >> how do we Invalidate it...?
  test("Log out "+host+port+"/logout", function (t) {
    var options = {
      headers: {
        'authorization': token,
        'user-agent': 'Mozilla/5.0'
      },
      uri: host+port+"/logout",
      method: 'GET'
    }
    request(options ,function (err, res, body) {
      t.equal(body, 'Logged Out!', "Exit server");
      t.equal(res.statusCode, 200, "End tests!");<commit_msg>remove newline<commit_after>var test    = require('tape');
var request = require('request');
var qs      = require('querystring');
var host    = "http://127.0.0.1:";
var port    = Math.floor(Math.random() * 9000) + 1000;

// var server = require('../server');

var exec = require('child_process').exec;
exec('PORT='+port +' node ./example/server.js', function(error, stdout, stderr) {
  // console.log(stdout);             // uncomment for console.log
  // console.log('stderr: ', stderr); // uncomment for errors
  if (error !== null) {
    console.log('exec error: ', error);
  }

});

setTimeout(function() { // only run tests once child_process has started

  test("Connect to localhost "+host+":"+port, function (t) {
    request(host+port ,function (err, res, body) {
      // console.log(err);
      // t.equal(err, null, "No Errors Connecting");
      // console.log(res);
      t.equal(res.statusCode, 200, "Status 200");
      t.end();
    });
  });

  // attempt to access content before being authenticated
  test("Attempt auth "+host+port+"/auth (incorrect username/password should fail)", function (t) {

    var form = {
      username: 'lordbusiness',
      password: '<PASSWORD>',
    };

    var formData      = qs.stringify(form);
    var contentLength = formData.length;

    var options = {
      headers: {
        'Content-Length': contentLength,
        'Content-Type': 'application/x-www-form-urlencoded',
        'user-agent': 'Mozilla/5.0'
      },
      uri: host+port+"/auth",
      body: formData,
      method: 'POST'
    }

    request.post(options ,function (err, res, body) {
      t.equal(res.statusCode, 401, "Cannot authenticate (incorrect un/pw)");
      t.end();
    });
  });

  test("Attempt to access restricted content: "+host+port+"/private without supplying a valid token!", function (t) {

    var options = {
      headers: {
        'authorization': 'invalid',
        'user-agent': 'Mozilla/5.0'
      },
      uri: host+port+"/private",
      method: 'GET'
    }

    request(options ,function (err, res, body) {
      t.equal(res.statusCode, 401, "Private content access denied!");
      t.end();
    });
  });


  var token   = null; // starts out empty && Yes, GLOBAL (its a test!)

  test("Authenticate "+host+port+"/auth", function (t) {

    var form = {
      username: 'masterbuilder',
      password: '<PASSWORD>',
    };

    var formData      = qs.stringify(form);
    var contentLength = formData.length;

    var options = {
      headers: {
        'Content-Length': contentLength,
        'Content-Type': 'application/x-www-form-urlencoded',
        'user-agent': 'Mozilla/5.0'
      },
      uri: host+port+"/auth",
      body: formData,
      method: 'POST'
    }

    request.post(options ,function (err, res, body) {
      token = res.headers.authorization; // save the token for later
      t.equal(res.statusCode, 200, "Authenticated");
      t.end();
    });
  });

  test("Access restricted content: "+host+port+"/private", function (t) {

    var options = {
      headers: {
        'authorization': token,
        'user-agent': 'Mozilla/5.0'
      },
      uri: host+port+"/private",
      method: 'GET'
    }

    request(options ,function (err, res, body) {
      t.equal(res.statusCode, 200, "Private content accessed");
      t.end();
    });
  });

  // simulate logging out (reset token) >> how do we Invalidate it...?
  test("Log out "+host+port+"/logout", function (t) {
    var options = {
      headers: {
        'authorization': token,
        'user-agent': 'Mozilla/5.0'
      },
      uri: host+port+"/logout",
      method: 'GET'
    }
    request(options ,function (err, res, body) {
      t.equal(body, 'Logged Out!', "Exit server");
      t.equal(res.statusCode, 200, "End tests!");
{'fraction_non_alphanumeric': 0.1697563559322034, 'fraction_numerical': 0.011917372881355932, 'mean_word_length': 2.541022034692921, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 2, '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': '3191420', 'n_tokens_mistral': 2496, 'n_tokens_neox': 2386, 'n_words': 586}