'use strict';

// MODULES //

var tape = require( 'tape' );
var http = require( 'http' );
var noop = require( '@stdlib/utils/noop' );
var createServer = require( './fixtures/server.js' );
var httpServer = require( './../lib' );


// TESTS //

tape( 'main export is a function', function test( t ) {
	t.ok( true, __filename );
	t.equal( typeof httpServer, 'function', 'main export is a function' );
	t.end();
});

tape( 'the function will throw if not provided enough arguments', function test( t ) {
	t.throws( foo, Error, 'throws error' );
	t.end();
	function foo() {
		httpServer();
	}
});

tape( 'the function will throw an error if provided an invalid option', function test( t ) {
	t.throws( foo, Error, 'throws error' );
	t.end();
	function foo() {
		httpServer({
			'port': Math.PI
		});
	}
});

tape( 'the function will throw an error if provided a request listener which is not a function', function test( t ) {
	var values;
	var i;

	values = [
		'5',
		5,
		NaN,
		true,
		null,
		undefined,
		[],
		{}
	];

	for ( i = 0; i < values.length; i++ ) {
		t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided '+values[i] );
	}
	t.end();
	function badValue( value ) {
		return function badValue() {
			httpServer( {}, value );
		};
	}
});

tape( 'the function returns a function', function test( t ) {
	t.equal( typeof httpServer( {} ), 'function', 'returns a function' );
	t.end();
});

tape( 'the returned function will throw an error if provided a callback argument which is not a function', function test( t ) {
	var values;
	var create;
	var i;

	values = [
		'5',
		5,
		NaN,
		true,
		null,
		undefined,
		[],
		{}
	];

	create = httpServer( {} );

	for ( i = 0; i < values.length; i++ ) {
		t.throws( badValue( values[ i ] ) , TypeError, 'throws an error when provided '+values[i] );
	}
	t.end();
	function badValue( value ) {
		return function badValue() {
			create( value );
		};
	}
});

tape( 'the created server listens on a specified port', function test( t ) {
	var create;
	var opts;

	opts = {};
	opts.port = 7331;

	create = httpServer( opts );
	create( onServer );

	function onServer( error, server ) {
		if ( error ) {
			t.ok( false, error.message );
		} else {
			t.equal( server.address().port, opts.port, 'listens on port '+opts.port );
		}
		server.close();
		t.end();
	}
});

tape( 'the returned function will throw if the server encounters an error', function test( t ) {
	var create;
	var server;
	var opts;

	opts = {};
	opts.port = 1337;

	create = httpServer( opts );
	create( next );

	function next( error, s ) {
		server = s;
		t.throws( foo, Error, 'throws error' );
		server.close();
		t.end();
	}

	function foo() {
		var err = new Error( 'Server error.' );
		server.emit( 'error', err );
	}
});

tape( 'the returned function will throw if unable to listen on a specified port (default behavior)', function test( t ) {
	var create;
	var server;
	var opts;

	opts = {};
	opts.port = 10000;

	create = httpServer( opts );
	create( next );

	function next( error, s ) {
		server = s;
		t.throws( foo, Error, 'throws an error' );
		server.close();
		t.end();
	}

	function foo() {
		var err = new Error( 'Server address already in use.' );
		err.code = 'EADDRINUSE';

		server.emit( 'error', err );
	}
});

tape( 'the returned function will port hunt', function test( t ) {
	var eServer;
	var server;
	var create;
	var opts;

	opts = {};
	opts.port = 8080;
	opts.maxport = 9999;

	create = httpServer( opts );
	eServer = createServer( opts.port, next );

	function next() {
		create( onServer );
	}

	function onServer( error, s ) {
		var port;
		server = s;
		if ( error ) {
			t.notOk( true, error.message );
		} else {
			port = server.address().port;
			t.equal( port > opts.port, true, 'returns a server with a port ('+port+') higher than '+opts.port );
		}
		setTimeout( onTimeout, 0 );
	}

	function onTimeout() {
		server.close();
		eServer.close();
		t.end();
	}
});

tape( 'the server will listen on a specified hostname', function test( t ) {
	var create;
	var opts;

	opts = {};
	opts.hostname = 'localhost';

	create = httpServer( opts );
	create( onServer );

	function onServer( error, server ) {
		if ( error ) {
			t.ok( false, error.message );
		} else {
			t.equal( server.address().address, '127.0.0.1', 'listens on address 127.0.0.1 (localhost)' );
		}
		server.close();
		t.end();
	}
});

tape( 'the server will listen on a specified address', function test( t ) {
	var create;
	var opts;

	opts = {};
	opts.address = '127.0.0.1';

	create = httpServer( opts );
	create( onServer );

	function onServer( error, server ) {
		if ( error ) {
			t.ok( false, error.message );
		} else {
			t.equal( server.address().address, opts.address, 'listens at address '+opts.address );
		}
		server.close();
		t.end();
	}
});

tape( 'the server will use a provided request listener', function test( t ) {
	var create;
	var server;
	var opts;

	opts = {};
	opts.port = 7000;

	create = httpServer( opts, onRequest );
	create( onServer );

	function onServer( error, s ) {
		if ( error ) {
			t.ok( false, error.message );
		}
		server = s;
		http.get( 'http://127.0.0.1:7000', noop );
	}
	function onRequest( request, response ) {
		t.ok( true, 'uses request listener' );
		response.end( 'OK' );
		server.close();
		t.end();
	}
});

@@ -3530,16 +3530,160 @@
 ext() %7B%0A
+%09%09var addr = eServer.address();%0A%09%09t.equal( addr.port, opts.port, 'fixture server bound to port '+opts.port+' and with address '+addr.address );%0A
 %09%09create

Add test case for fixture server