Skip to content

Instantly share code, notes, and snippets.

@ronanguilloux
Last active August 29, 2015 14:25
Show Gist options
  • Save ronanguilloux/3ca84c03dd5f17323296 to your computer and use it in GitHub Desktop.
Save ronanguilloux/3ca84c03dd5f17323296 to your computer and use it in GitHub Desktop.
Mocha based URLs HTTP code checker
/**
* Created by ronan on 27/07/2015.
*
* Principe:
*
* 1 - Create an urls.json file as described below
* 2 - install npm deps
* 3 - run mocha tests, the regular way
*
* How-to:
*
* $ npm install mocha chai it-each
* $ [EDIT urls.json]
* $ mkdir test; cd test;
* $ curl -O https://gist.githubusercontent.com/ronanguilloux/3ca84c03dd5f17323296/raw/test.js
* $ cd ../
* $ node_modules/mocha/bin/mocha
*
*
* urls.json example:
{
"list": [
{
"url": "http://www.domain.org/.git",
"expected": 404,
}
]
}
*
* Apache2 HTTP rules to set 4xx codes
RedirectMatch 404 /\.git
RedirectMatch 404 /*\.md
RedirectMatch 404 /*\.prod
RedirectMatch 404 /*\.preprod
RedirectMatch 404 /*\.dev
RedirectMatch 404 /*\.dist
RedirectMatch 404 /*\.conf
RedirectMatch 404 /*settings*
RedirectMatch 404 /Makefile
*/
require('it-each')({ testPerIteration: true });
var http = require('http');
var expect = require('chai').expect;
var expectedDataFile = 'urls.json';
var urls = require(__dirname + '/../' + expectedDataFile);
describe('Asynchronous loop testing over ' + urls.list.length + ' URLs', function () {
it.each(urls.list, 'expects %s from %s', ['expected', 'url'], function (url, nextUrl) {
var expected = false;
if (('undefined' === typeof url.url) || ('undefined' === typeof url.expected)) {
throw new Error("If element is an Object in urls.json, it must contain 2 properties: 'url' and 'expected' with an HTTP status code.");
}
expected = url.expected;
url = url.url;
//console.log(url + " expects " + expected);
http.get(url, function (res) {
res.on('data', function() {
//... Don't know why, but this help to handle 403 responses whitout timeout.
});
res.on('end', function() {
expect(res.statusCode).to.equal(expected);
nextUrl();
});
}).on('error', function(e) {
console.log("Got error for " + url + ": " + e.message);
nextUrl();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment