<commit_before>// Copyright 2016 Yahoo Inc.
// Licensed under the terms of the MIT license. Please see LICENSE file in the project root for terms.

var subject = require('..');
var request = require('supertest');
var assert = require('assert');
var fs = require('fs');

var yakbak;

beforeEach(function () {
  yakbak = subject(this.host, { dirname: this.tmpdir });
});

describe('record', function () {

  it('proxies the request to the server', function (done) {
    var server = this.server;

    request(yakbak)
    .get('/record/1')
    .set('host', 'localhost:3001')
    .expect('X-Yakbak-Tape', '1a574e91da6cf00ac18bc97abaed139e')
    .expect('Content-Type', 'text/html')
    .expect(201, 'OK')
    .end(function (err) {
      assert.ifError(err);
      assert.equal(server.requests.length, 1);
      done();
    });
  });

  it('writes the tape to disk', function (done) {
    var tmpdir = this.tmpdir;

    request(yakbak)
    .get('/record/2')
    .set('host', 'localhost:3001')
    .expect('X-Yakbak-Tape', '3234ee470c8605a1837e08f218494326')
    .expect('Content-Type', 'text/html')
    .expect(201, 'OK')
    .end(function (err) {
      assert.ifError(err);
      assert(fs.existsSync(tmpdir + '/3234ee470c8605a1837e08f218494326.js'));
      done();
    });
  });

});

describe('playback', function () {

  beforeEach(function (done) {
    var file = '305c77b0a3ad7632e51c717408d8be0f.js';
    var tape = [
      'var path = require("path");',
      'module.exports = function (req, res) {',
      '  res.statusCode = 201;',
      '  res.setHeader("content-type", "text/html")',
      '  res.setHeader("x-yakbak-tape", path.basename(__filename, ".js"));',
      '  res.end("YAY");',
      '}',
      ''
    ].join('\n');

    fs.writeFile(this.tmpdir + '/' + file, tape, done);
  });

  it('does not make a request to the server', function (done) {
    var server = this.server;

    request(yakbak)
    .get('/playback/1')
    .set('host', 'localhost:3001')
    .expect('X-Yakbak-Tape', '305c77b0a3ad7632e51c717408d8be0f')
    .expect('Content-Type', 'text/html')
    .expect(201, 'YAY')
    .end(function (err) {
      assert.ifError(err);
      assert.equal(server.requests.length, 0);
      done();
    });

  });
});
<commit_msg>Move yakbak beforeEach hook inside describe blocks<commit_after>// Copyright 2016 Yahoo Inc.
// Licensed under the terms of the MIT license. Please see LICENSE file in the project root for terms.

var subject = require('..');
var request = require('supertest');
var assert = require('assert');
var fs = require('fs');

describe('record', function () {
  var yakbak;

  beforeEach(function () {
    yakbak = subject(this.host, { dirname: this.tmpdir });
  });

  it('proxies the request to the server', function (done) {
    var server = this.server;

    request(yakbak)
    .get('/record/1')
    .set('host', 'localhost:3001')
    .expect('X-Yakbak-Tape', '1a574e91da6cf00ac18bc97abaed139e')
    .expect('Content-Type', 'text/html')
    .expect(201, 'OK')
    .end(function (err) {
      assert.ifError(err);
      assert.equal(server.requests.length, 1);
      done();
    });
  });

  it('writes the tape to disk', function (done) {
    var tmpdir = this.tmpdir;

    request(yakbak)
    .get('/record/2')
    .set('host', 'localhost:3001')
    .expect('X-Yakbak-Tape', '3234ee470c8605a1837e08f218494326')
    .expect('Content-Type', 'text/html')
    .expect(201, 'OK')
    .end(function (err) {
      assert.ifError(err);
      assert(fs.existsSync(tmpdir + '/3234ee470c8605a1837e08f218494326.js'));
      done();
    });
  });

});

describe('playback', function () {
  var yakbak;

  beforeEach(function () {
    yakbak = subject(this.host, { dirname: this.tmpdir });
  });

  beforeEach(function (done) {
    var file = '305c77b0a3ad7632e51c717408d8be0f.js';
    var tape = [
      'var path = require("path");',
      'module.exports = function (req, res) {',
      '  res.statusCode = 201;',
      '  res.setHeader("content-type", "text/html")',
      '  res.setHeader("x-yakbak-tape", path.basename(__filename, ".js"));',
      '  res.end("YAY");',
      '}',
      ''
    ].join('\n');

    fs.writeFile(this.tmpdir + '/' + file, tape, done);
  });

  it('does not make a request to the server', function (done) {
    var server = this.server;

    request(yakbak)
    .get('/playback/1')
    .set('host', 'localhost:3001')
    .expect('X-Yakbak-Tape', '305c77b0a3ad7632e51c717408d8be0f')
    .expect('Content-Type', 'text/html')
    .expect(201, 'YAY')
    .end(function (err) {
      assert.ifError(err);
      assert.equal(server.requests.length, 0);
      done();
    });

  });
});

{'fraction_non_alphanumeric': 0.18471474703982776, 'fraction_numerical': 0.061571582346609255, 'mean_word_length': 3.089788732394366, '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': '177420', 'n_tokens_mistral': 1869, 'n_tokens_neox': 1688, 'n_words': 323}