<commit_before>'use strict';

const Fs = require('fs');
const Os = require('os');
const Path = require('path');

const Code = require('code');
const Form = require('multi-part');
const Hapi = require('hapi');
const Lab = require('lab');

const Henning = require('../lib/');

const lab = exports.lab = Lab.script();

lab.experiment('henning', () => {

    let server;

    lab.before((done) => {

        server = new Hapi.Server();
        server.connection();

        const setup = {
            register: Henning,
            options: {
                whitelist: ['image/png']
            }
        };

        const route = {
            config: {
                handler: (request, reply) => reply(),
                payload: {
                    output: 'stream',
                    parse: true
                }
            },
            method: '*',
            path: '/'
        };

        server.route(route);
        server.register(setup, done);
    });

    lab.test('should return error if some file in the payload is not allowed', (done) => {

        const png = Path.join(Os.tmpdir(), 'foo.png');
        const gif = Path.join(Os.tmpdir(), 'foo.gif');

        Fs.createWriteStream(png).end(Buffer.from('89504e47', 'hex'));
        Fs.createWriteStream(gif).end(Buffer.from('47494638', 'hex'));

        const form = new Form();
        form.append('file1', Fs.createReadStream(gif));
        form.append('file2', Fs.createReadStream(png));
        form.append('file3', Fs.createReadStream(gif));
        form.append('foo', 'bar');

        server.inject({ headers: form.getHeaders(), method: 'POST', payload: form.stream(), url: '/' }, (response) => {

            Code.expect(response.statusCode).to.equal(400);
            Code.expect(response.headers['content-validation']).to.equal('failure');
            Code.expect(response.result).to.include(['message', 'validation']);
            Code.expect(response.result.message).to.equal('child \"file1\" fails because [\"file1\" type is not allowed]');
            Code.expect(response.result.validation).to.include(['source', 'keys']);
            Code.expect(response.result.validation.source).to.equal('payload');
            Code.expect(response.result.validation.keys).to.include('file1');
            done();
        });
    });

    lab.test('should return control to the server if all files the payload are allowed', (done) => {

        const png = Path.join(Os.tmpdir(), 'foo.png');
        Fs.createWriteStream(png).end(Buffer.from('89504e47', 'hex'));

        const form = new Form();<commit_msg>Ensure fake test files are created beforehand<commit_after>'use strict';

const Fs = require('fs');
const Os = require('os');
const Path = require('path');

const Code = require('code');
const Form = require('multi-part');
const Hapi = require('hapi');
const Lab = require('lab');

const Henning = require('../lib/');

const lab = exports.lab = Lab.script();

lab.experiment('henning', () => {

    let server;
    let png;
    let gif;

    lab.before((done) => {

        server = new Hapi.Server();
        server.connection();

        const setup = {
            register: Henning,
            options: {
                whitelist: ['image/png']
            }
        };

        const route = {
            config: {
                handler: (request, reply) => reply(),
                payload: {
                    output: 'stream',
                    parse: true
                }
            },
            method: '*',
            path: '/'
        };

        server.route(route);
        server.register(setup, done);
    });

    lab.beforeEach((done) => {
        // Create fake png file
        png = Path.join(Os.tmpdir(), 'foo.png');
        Fs.createWriteStream(png).on('error', done).end(Buffer.from('89504e47', 'hex'), done);
    });

    lab.beforeEach((done) => {
        // Create fake gif file
        gif = Path.join(Os.tmpdir(), 'foo.gif');
        Fs.createWriteStream(gif).on('error', done).end(Buffer.from('47494638', 'hex'), done);
    });

    lab.test('should return error if some file in the payload is not allowed', (done) => {

        const form = new Form();
        form.append('file1', Fs.createReadStream(gif));
        form.append('file2', Fs.createReadStream(png));
        form.append('file3', Fs.createReadStream(gif));
        form.append('foo', 'bar');

        server.inject({ headers: form.getHeaders(), method: 'POST', payload: form.stream(), url: '/' }, (response) => {

            Code.expect(response.statusCode).to.equal(400);
            Code.expect(response.headers['content-validation']).to.equal('failure');
            Code.expect(response.result).to.include(['message', 'validation']);
            Code.expect(response.result.message).to.equal('child \"file1\" fails because [\"file1\" type is not allowed]');
            Code.expect(response.result.validation).to.include(['source', 'keys']);
            Code.expect(response.result.validation.source).to.equal('payload');
            Code.expect(response.result.validation.keys).to.include('file1');
            done();
        });
    });

    lab.test('should return control to the server if all files the payload are allowed', (done) => {

        const form = new Form();
{'fraction_non_alphanumeric': 0.17131474103585656, 'fraction_numerical': 0.010434452665528362, 'mean_word_length': 2.25633106856084, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '6222043', 'n_tokens_mistral': 1545, 'n_tokens_neox': 1466, 'n_words': 315}