{"size": 1552, "ext": "js", "max_stars_repo_path": "test/expressValidateIp.js", "max_stars_repo_name": "One-com/express-validate-ip", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "test/expressValidateIp.js", "max_issues_repo_name": "One-com/express-validate-ip", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "test/expressValidateIp.js", "max_forks_repo_name": "One-com/express-validate-ip", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 28.7407407407, "max_line_length": 106, "alphanum_fraction": 0.462628866}
/*global describe, it*/

process.env.NODE_ENV = 'test'; // Silence Connect's built-in error handler that logs errors to the console

var expect = require('unexpected').use(require('unexpected-express'));
var express = require('express');
var expressValidateIp = require('../lib/expressValidateIp');

describe('expressValidateIp middleware', function () {
    it('should propagate errors from IpWhitelist', function () {
        expect(function () {
            expressValidateIp('foobar');
        }, 'to throw exception', 'validateIp: Cannot parse foobar');
    });

    it('should not block an allowed ip', function () {
        return expect(
            express()
                .use(expressValidateIp('*'))
                .use(function (req, res, next) {
                    res.end('Hello world.');
                }),
            'to yield exchange',
            {
                request: {
                    remoteAddress: '1.2.3.4',
                    url: '/'
                },
                response: 200
            }
        );
    });

    it('should block an unallowed ip', function () {
        return expect(
            express()
                .use(expressValidateIp([]))
                .use(function (req, res, next) {
                    res.end('Hello world.');
                }),
            'to yield exchange',
            {
                request: {
                    remoteAddress: '1.2.3.4',
                    url: '/'
                },
                response: 403
            }
        );
    });


});
