/**
 * Request module for test cases
 *
 * @author juanvallejo
 * @date 3/11/15
 */

// import a connection prototype
var Connection = require(__dirname + '/../prototypes/connection.js');

// declare our module object
var UasRequest = {};

// set module name
UasRequest.MODULE_NAME = 'UasRequest';

var defaultRequestOptions = {

	port 	: 8000,
	method 	: 'POST',
	path 	: '/api/test',

};

var defaultRequestHeaders = {
	'Content-Type' 	: 'application/x-www-form-urlencoded'
}

// declare default fields

/**
 * takes a Connection object and adds default connection headers
 */
UasRequest.setDefaultOptions = function(connection) {

	connection.setConnectionPort(defaultRequestOptions.port);
	connection.setConnectionPath(defaultRequestOptions.path);
	connection.setConnectionMethod(defaultRequestOptions.method);

}

/**
 * takes a Connection object and passes the default request headers object.
 */
UasRequest.setDefaultHeaders = function(connection) {
	connection.addHeaders(defaultRequestHeaders);
}

/**
 * Adds a require-authentication-header to the connection object
 */
UasRequest.requireAuthentication = function(connection) {

	connection.addHeaders({
		'UASAPI-Require-Authentication' : 'true'
	});

}

/**
 * Initializes a Connection object, along with methods
 */
UasRequest.create = function(protocol, requestMethod, uasAPIHeader) {

	// create a new connection object
	var UasConnectionObject = new Connection();

	// set the connection's default options
	UasRequest.setDefaultOptions(UasConnectionObject);
	UasRequest.setDefaultHeaders(UasConnectionObject);

	// set the protocol of our connection and add the api header
	UasConnectionObject.setConnectionProtocol(protocol);

	// set api values for the connection
	UasConnectionObject.addHeaders({
		'UASAPI-Method' : uasAPIHeader
	});

	UasConnectionObject.addHeaders({
		'UASAPI-Request-Method' : requestMethod
	});

	return UasConnectionObject;		

}

UasRequest.write = function(connection, data) {
	connection.write(data);
}

/**
 * Send and handle a response from the request created by the connection object
 */
UasRequest.send = function(connection, callback) {

	connection.end(function(response, cookies) {

		// determine the type of the request made
		if(this.getConnectionOptions('method') == 'GET') {
			return callback.call(UasRequest, response);
		}

		// assume the user used a POST method
		var response_body = '';
		
		response.on('data', function(chunk) {
 			response_body += chunk;
 		});

 		response.on('end', function() {
 			callback.call(UasRequest, response_body);
 		});

	});

}

/**
 * Takes a uasAPIHeader and creates a new GET request
 */
UasRequest.get = function(uasAPIHeader) {
	return UasRequest.create('http', 'GET', uasAPIHeader);
}

/**
 * Takes a uasAPIHeader and creates a new POST request
 */
UasRequest.post = function(uasAPIHeader) {
	return UasRequest.create('http', 'POST', uasAPIHeader);
}

module.exports = UasRequest;
@@ -29,16 +29,220 @@
 st cases
+. Prepares an http or http request, and appends headers to it%0A * such that they indicate to the test server which parameters should be used when dealing with a connection%0A * to the interoperability server
 %0A *%0A * @
@@ -1973,22 +1973,24 @@
 'UASAPI-
-Method
+Endpoint
 ' : uasA
@@ -2052,16 +2052,8 @@
 API-
-Request-
 Meth

update headers that are sent to the UAS module