<commit_before>const { default: bodyParser } = require('../build/')
const events = require('events');
const chai = require('chai');

const urlencodedParser = bodyParser({ urlEncoded: true });
const jsonParser = bodyParser({ json: true });

const identity = function (id) { return id };
let request;

const expect = chai.expect;

beforeEach(function (){
  request = {};
  const requestObj = new events.EventEmitter();
  request.req = () => requestObj
});

describe('urlencodedParser', function () {
  beforeEach(function () {
    request.headers = { 'content-type': 'application/x-www-form-urlencoded' }
  });

  it('should return an object', function () {
    const prom = urlencodedParser(identity)(request);
    request.req().emit('data', Buffer.from('{}'))
    request.req().emit('end');
    return prom.then(function (obj) { return expect(obj).to.be.a('object') });
  });

  it('parse a form with one key', function () {
    const prom = urlencodedParser(identity)(request);
    request.req().emit('data', Buffer.from('key='));
    request.req().emit('end');
    return prom.then(function (obj) { return expect(obj.body['key']).to.not.equal(undefined) });
  });

  it('should decode URI components', function () {
    const prom = urlencodedParser(identity)(request);
    request.req().emit('data', Buffer.from('key=!%40%23%24%25%5E%26*()_%2B'));
    request.req().emit('end');
    return prom.then(function (obj) { return expect(obj.body['key']).to.equal('!@#$%^&*()_+') });
  });

  it('should eliminate "+" from the body', function () {
    const prom = urlencodedParser(identity)(request);
    request.req().emit('data', Buffer.from('key=plus+delimited+input'));
    request.req().emit('end');
    return prom.then(function (obj) { return expect(obj.body['key']).to.equal('plus delimited input') });
  });

  it('should parse multikey forms', function () {
    const prom = urlencodedParser(identity)(request);
    request.req().emit('data', Buffer.from('key1=some+value&key2=another+value'));
    request.req().emit('end');
    return prom.then(function (obj) {
      return expect(obj.body).to.eql({
        'key1': 'some value',
        'key2': 'another value'
      })
    });
  });
});

describe('jsonParser', function () {
  beforeEach(function () {
    request.headers = { 'content-type': 'application/json' }
  });

  it('should return an object', function () {
    const prom = jsonParser(identity)(request);
    request.req().emit('data', Buffer.from('{}'))
    request.req().emit('end');
    return prom.then(function (obj) { return expect(obj).to.be.a('object') });
  });

  it('parse a form with one key', function () {
    const prom = jsonParser(identity)(request);
    request.req().emit('data', Buffer.from('{ "key": "value"}'));
    request.req().emit('end');
    return prom.then(function (obj) { return expect(obj.body['key']).to.equal('value') });
  });

  it('should parse multikey forms', function () {
    const prom = jsonParser(identity)(request);
    request.req().emit('data', Buffer.from('{ "key1": "some value", "key2": "another value" }'));
    request.req().emit('end');
    return prom.then(function (obj) {
      return expect(obj.body).to.eql({
        'key1': 'some value',
        'key2': 'another value'
      })
    });
  });
});<commit_msg>convert tests to coding style<commit_after>const { default: bodyParser } = require('../build/');
const events = require('events');
const chai = require('chai');

const urlencodedParser = bodyParser({ urlEncoded: true });
const jsonParser = bodyParser({ json: true });

const identity = function(id) { return id; };
let request;

const expect = chai.expect;

beforeEach(function() {
  request = {};
  const requestObj = new events.EventEmitter();
  request.req = () => requestObj;
});

describe('urlencodedParser', function() {
  beforeEach(function() {
    request.headers = { 'content-type': 'application/x-www-form-urlencoded' };
  });

  it('should return an object', function() {
    const prom = urlencodedParser(identity)(request);
    request.req().emit('data', Buffer.from('{}'));
    request.req().emit('end');
    return prom.then(function(obj) { return expect(obj).to.be.a('object'); });
  });

  it('parse a form with one key', function() {
    const prom = urlencodedParser(identity)(request);
    request.req().emit('data', Buffer.from('key='));
    request.req().emit('end');
    return prom.then(function(obj) {
      return expect(obj.body['key']).to.not.equal(undefined);
    });
  });

  it('should decode URI components', function() {
    const prom = urlencodedParser(identity)(request);
    request.req().emit('data', Buffer.from('key=!%40%23%24%25%5E%26*()_%2B'));
    request.req().emit('end');
    return prom.then(function(obj) {
      return expect(obj.body['key']).to.equal('!@#$%^&*()_+');
    });
  });

  it('should eliminate "+" from the body', function() {
    const prom = urlencodedParser(identity)(request);
    request.req().emit('data', Buffer.from('key=plus+delimited+input'));
    request.req().emit('end');
    return prom.then(function(obj) {
      return expect(obj.body['key']).to.equal('plus delimited input');
    });
  });

  it('should parse multikey forms', function() {
    const prom = urlencodedParser(identity)(request);
    request.req().emit('data',
      Buffer.from('key1=some+value&key2=another+value'));
    request.req().emit('end');
    return prom.then(function(obj) {
      return expect(obj.body).to.eql({
        'key1': 'some value',
        'key2': 'another value',
      });
    });
  });
});

describe('jsonParser', function() {
  beforeEach(function() {
    request.headers = { 'content-type': 'application/json' };
  });

  it('should return an object', function() {
    const prom = jsonParser(identity)(request);
    request.req().emit('data', Buffer.from('{}'));
    request.req().emit('end');
    return prom.then(function(obj) { return expect(obj).to.be.a('object'); });
  });

  it('parse a form with one key', function() {
    const prom = jsonParser(identity)(request);
    request.req().emit('data', Buffer.from('{ "key": "value"}'));
    request.req().emit('end');
    return prom.then(function(obj) {
      return expect(obj.body['key']).to.equal('value');
    });
  });

  it('should parse multikey forms', function() {
    const prom = jsonParser(identity)(request);
    request.req().emit('data',
      Buffer.from('{ "key1": "some value", "key2": "another value" }'));
    request.req().emit('end');
    return prom.then(function(obj) {
      return expect(obj.body).to.eql({
        'key1': 'some value',
        'key2': 'another value',
      });
    });
  });
});
{'fraction_non_alphanumeric': 0.20798192771084337, 'fraction_numerical': 0.006024096385542169, 'mean_word_length': 4.008295625942685, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3773348', 'n_tokens_mistral': 2091, 'n_tokens_neox': 2048, 'n_words': 407}