var assert = require('assert'),
	plugin = require('../../lib/hapi-statsd.js'),
	mockStatsdClient = {

		incStat: '',
		timingStat: '',
		timingDate: '',

		increment: function(statName) {
			this.incStat = statName;
		},

		timing: function(statName, date) {
			this.timingStat = statName;
			this.timingDate = date;
		}
	},
	Hapi = require('hapi');

beforeEach(function(done) {
	mockStatsdClient.incStat = '';
	mockStatsdClient.timingStat = '';
	mockStatsdClient.timingDate = '';
	server = new Hapi.Server();

	server.connection({
		host: 'localhost',
		port: 8085
	});

	var get = function (request, reply) {
		reply('Success!');
	};

	var err = function (request, reply) {
		reply(new Error());
	};

	server.route({ method: ['GET','OPTIONS'], path: '/', handler: get, config: {cors: true} });
	server.route({ method: 'GET', path: '/err', handler: err, config: {cors: true} });
	server.route({ method: 'GET', path: '/test/{param}', handler: get, config: {cors: true} });
	server.route({ method: 'GET', path: '/default', handler: get, config: {cors: true} });
	server.route({ method: 'GET', path: '/override', handler: get, config: {cors: true} });
	server.route({ method: 'GET', path: '/rename', handler: get, config: {cors: true} });
	server.route({ method: 'GET', path: '/match/id', handler: get, config: {id: 'match-my-id', cors: true} });

	server.register({
		register: plugin,
		options: {
			statsdClient: mockStatsdClient,
			defaultFilter: {
				enableCounter: false,
				enableTimer: true,
			},
			filters: [
				{ path: '/', enableCounter: true },
				{ path: '/err', enableCounter: true },
				{ path: '/test/{param}', enableCounter: true },
				{ path: '/override', enableCounter: true, enableTimer: false },
				{ path: '/rename', name: 'rename_stat', enableCounter: true, enableTimer: true },
				{ id: 'match-my-id', name: 'match_id_stat', enableCounter: true, enableTimer: true },
			],
		},
	}, done);
});

describe('hapi-statsd plugin tests', function() {

	it('should expose statsd client to the hapi server', function() {
		assert.equal(server.statsd, mockStatsdClient);
	});

	it('should report stats with no path in stat name', function(done) {
		server.inject('/', function(res) {
			assert(mockStatsdClient.incStat == 'GET.200');
			assert(mockStatsdClient.timingStat == 'GET.200');
			assert(mockStatsdClient.timingDate instanceof Date);
			done();
		});
	});

	it('should honor default filter', function(done) {
		server.inject('/default', function(res) {
			assert(mockStatsdClient.incStat == '');
			assert(mockStatsdClient.timingStat == 'default.GET.200');
			assert(mockStatsdClient.timingDate instanceof Date);
			done();
		});
	});

	it('should honor filter override', function(done) {
		server.inject('/override', function(res) {
			assert(mockStatsdClient.incStat == 'override.GET.200');
			assert(mockStatsdClient.timingStat == '');
			done();
		});
	});

	it('should rename stat', function(done) {
		server.inject('/rename', function(res) {
			assert(mockStatsdClient.incStat == 'rename_stat');
			assert(mockStatsdClient.timingStat == 'rename_stat');
			done();
		});
	});

	it('should match on route id', function(done) {
		server.inject('/match/id', function(res) {
			assert(mockStatsdClient.incStat == 'match_id_stat');
			assert(mockStatsdClient.timingStat == 'match_id_stat');
			done();
		});
	});

	it('should report stats with path in stat name', function(done) {
		server.inject('/test/123', function(res) {
			assert(mockStatsdClient.incStat == 'test_{param}.GET.200');
			assert(mockStatsdClient.timingStat == 'test_{param}.GET.200');
			assert(mockStatsdClient.timingDate instanceof Date);
			done();
		});
	});

	it('should report stats with generic not found path', function(done) {
		server.inject('/fnord', function(res) {
			assert(mockStatsdClient.incStat == '');
			assert(mockStatsdClient.timingStat == '{notFound*}.GET.404');
			assert(mockStatsdClient.timingDate instanceof Date);
			done();
		});
	});

	it('should report stats with generic CORS path', function(done) {
		server.inject({
			method: 'OPTIONS',
			headers: {
				Origin: 'http://test.domain.com'
			},
			url: '/'
		}, function(res) {
			assert(mockStatsdClient.incStat == '{cors*}.OPTIONS.200');
			assert(mockStatsdClient.timingStat == '{cors*}.OPTIONS.200');
			assert(mockStatsdClient.timingDate instanceof Date);
			done();
		});
	});

	it('should not change the status code of a response', function(done) {
		server.inject('/err', function(res) {
			assert(res.statusCode === 500);
			done();
		});
	});
});

@@ -2880,32 +2880,303 @@
 ();%0A%09%09%7D);%0A%09%7D);%0A%0A
+%09it('should use cached value', function(done) %7B%0A%09%09server.inject('/override', function() %7B%0A%09%09%09server.inject('/override', function() %7B%0A%09%09%09%09assert(mockStatsdClient.incStat == 'override.GET.200');%0A%09%09%09%09assert(mockStatsdClient.timingStat == '');%0A%09%09%09%09done();%0A%09%09%09%7D);%0A%09%09%7D);%0A%09%7D);%0A%0A
 %09it('should rena

Add another test to improve code coverage