<commit_before>/**
 * Copyright (c) 2013-2017 <NAME>. All rights reserved.
 * Sources at https://github.com/Memba
 */

/* jshint node: true, mocha: true, expr: true */

'use strict';

var chai = require('chai');
var http = require('http');
var sinon = require('sinon');
var sinonChai = require('sinon-chai');
var url = require('url');
var expect = chai.expect;
chai.use(sinonChai);

var config;
try {
    config = require('../../../webapp/config');
} catch (exception) {
    config = require('../../../api/config');
}

// Config before redirect loads rules
config.set('redirect', {
    http: {
        match: 'memba.com$',
        forward: 'https://www.kidoju.com'
    }
});

var redirect;
try {
    redirect = require('../../../webapp/middleware/redirect');
} catch (exception) {
    redirect = require('../../../api/middleware/redirect');
}

function Response() {}
Response.prototype.redirect = sinon.spy();

describe('middleware/redirect', function () {

    beforeEach(function () {
        Response.prototype.redirect.reset();
    });

    it('redirection with rule', function () {
        var req = {
            headers: {
                // Note: we have configured a rule for http
                'x-forwarded-proto' : 'http',
                host: 'www.memba.com'
            },
            originalUrl: '/blog/posts?id=100'
        };
        var res = new Response();
        var next = sinon.spy();
        redirect.handler(req, res, next);
        expect(res.redirect).to.have.been.calledWith(301, url.resolve(config.get('redirect:http:forward'), req.originalUrl));
        expect(next).not.to.have.been.called;
    });


    it('no redirection without rule', function () {
        var req = {
            headers: {
                // Note: we have not configured a rule for https
                'x-forwarded-proto' : 'https',
                host: 'www.memba.com'
            },
            originalUrl: 'favicon.ico'
        };
        var res = new Response();
        var next = sinon.spy();
        redirect.handler(req, res, next);
        expect(res.redirect).not.to.have.been.called;
        expect(next).to.have.been.called;
    });
<commit_msg>Implemented redirection of kidoju http traffic to https https://github.com/Memba/Memba-Blog/issues/122<commit_after>/**
 * Copyright (c) 2013-2017 Memba Sarl. All rights reserved.
 * Sources at https://github.com/Memba
 */

/* jshint node: true, mocha: true, expr: true */

'use strict';

var chai = require('chai');
var http = require('http');
var sinon = require('sinon');
var sinonChai = require('sinon-chai');
var url = require('url');
var expect = chai.expect;
chai.use(sinonChai);

var config;
try {
    config = require('../../../webapp/config');
} catch (exception) {
    config = require('../../../api/config');
}

// Config before redirect loads rules
// Note this will be ignore if a rule is set in json files
// So our tests need to be compatible
config.set('redirect', {
    http: {
        match: 'kidoju.com$',
        forward: 'https://www.kidoju.com'
    }
});

var redirect;
try {
    redirect = require('../../../webapp/middleware/redirect');
} catch (exception) {
    redirect = require('../../../api/middleware/redirect');
}

function Response() {}
Response.prototype.redirect = sinon.spy();

describe('middleware/redirect', function () {

    beforeEach(function () {
        Response.prototype.redirect.reset();
    });

    it('redirection with rule', function () {
        var req = {
            headers: {
                // Note: we have configured a rule for http
                'x-forwarded-proto' : 'http',
                host: 'blog.kidoju.com'
            },
            originalUrl: '/blog/posts?id=100'
        };
        var res = new Response();
        var next = sinon.spy();
        redirect.handler(req, res, next);
        expect(res.redirect).to.have.been.calledWith(301, url.resolve(config.get('redirect:http:forward'), req.originalUrl));
        expect(next).not.to.have.been.called;
    });


    it('no redirection without rule', function () {
        var req = {
            headers: {
                // Note: we have not configured a rule for https
                'x-forwarded-proto' : 'https',
                host: 'blog.kidoju.com'
            },
            originalUrl: 'favicon.ico'
        };
        var res = new Response();
        var next = sinon.spy();
        redirect.handler(req, res, next);
        expect(res.redirect).not.to.have.been.called;
        expect(next).to.have.been.called;
    });

{'fraction_non_alphanumeric': 0.16471885336273429, 'fraction_numerical': 0.006835722160970232, 'mean_word_length': 2.207920792079208, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 5, 'lorem ipsum': 0, 'www.': 4, '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': '2541220', 'n_tokens_mistral': 1409, 'n_tokens_neox': 1349, 'n_words': 321}