var
	HTTP = require('http');

function testDuplicateRoutesError(test) {
	var router = new HTTP_Router_Server();

	router.get('/pokemon/:id/abilities', function handler() { });

	try {
		router.get('/pokemon/:whatever/abilities', function handler() { });
		test.ok(false, 'This should have thrown an error');
		test.done();
	} catch (error) {
		test.ok(
			error.toString().contains(
				'Duplicate route defined for uri pattern'
			)
		);
		test.done();
	}
}

function testRouteSpecificity(test) {

	var
		router = new HTTP_Router_Server(),
		server = HTTP.createServer(router.serve);

	server.listen(Network_Mapper.createAPIMapping().http_port);

	var
		complete_count = 0,
		expected       = 4;

	function 
() {
		complete_count++;

		if (complete_count === expected) {
			server.close();

			test.done();
		}
	}

	router.get('/foo/:id', function handler(... response) {
		test.ok(false, 'We should not be here');
		response.send({ });
		test.done();
	});

	router.get('/foo/specific', function handler(request, response) {
		test.equals(request.url, '/foo/specific');
		response.send({ });
		incrementCompleteCount();
	});

	Querier.get('/api/foo/specific', { }, function finisher() { });

	router.get('/bar/:id', function handler(request, response) {
		test.equals(request.url, '/bar/123');
		response.send({ });
		incrementCompleteCount();
	});

	router.get('/bar/specific', function handler(... response) {
		test.ok(false, 'We should not be here');
		response.send({ });
		test.done();
	});

	Querier.get('/api/bar/123', { }, function finisher() { });

	router.get('/baz/specific', function handler(request, response) {
		test.equals(request.url, '/baz/specific');
		response.send({ });
		incrementCompleteCount();
	});

	router.get('/baz/:id', function handler(... response) {
		test.ok(false, 'We should not be here');
		response.send({ });
		test.done();
	});

	Querier.get('/api/baz/specific', { }, function finisher() { });

	router.get('/wat/specific', function handler(... response) {
		test.ok(false, 'We should not be here');
		response.send({ });

		server.close(function handler() {
			Network_Mapper.deleteAPIMapping();
			test.done();
		});
	});

	router.get('/wat/:id', function handler(request, response) {
		test.equals(request.url, '/wat/123');
		response.send({ });
		incrementCompleteCount();
	});

	Querier.get('/api/wat/123', { } , function finisher() { });
}

function serve(test) {
	// The router should trigger a response error
	// if an invalid HTTP method was specified:
	WithInvalidHTTPMethod: {

		let mock_headers = { };

		let mock_native_request = {
			headers: mock_headers,
			url:     '/',
			method:  'nonexistent_method'
		};

		let mock_request = (new HTTP_Request())
			.setNativeRequest(mock_native_request);

		let mock_response = (new HTTP_Response())
			.setUrl('/');

		mock_response.error = function handleError(error) {
			test.ok(error instanceof Error_InvalidHttpMethod);
		};

		let router = new HTTP_Router_Server();

		router.serve(mock_request, mock_response);
	}

	test.done();
}


module.exports = {
	testDuplicateRoutesError,
	testRouteSpecificity,
	serve
};

incrementCompleteCount