package org.jogger.test;

import java.net.URI;
import java.net.URISyntaxException;

import org.jogger.Jogger;
import org.jogger.Route;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;

/**
 * This is an utility class that you can extend for testing Jogger applications. It provides methods
 * to emulate requests some abstract methods that you need to implement.
 * 
 * @author German Escobar
 */
public abstract class JoggerTest {
	
	@BeforeSuite
	public void init() {	
		System.setProperty("JOGGER_ENV", "test");
	}
	
	@AfterSuite
	public void destroy() {
		System.clearProperty("JOGGER_ENV");
	}
	
	/**
	 * Helper method. Creates and returns a {@link MockRequest} with 'GET' as the HTTP method. Call the 
	 * {@link MockRequest#run()} method to execute the request. 
	 * 
	 * @param path the path of the GET request.
	 * 
	 * @return a {@link MockRequest} object.
	 */
	public MockRequest get(String path) throws Exception {
		return service("GET", path);
	}
	
	/**
	 * Helper method. Creates and returns a {@link MockRequest} with 'POST' as the HTTP method. Call the 
	 * {@link MockRequest#run()} method to execute the request. 
	 * 
	 * @param path the path of the POST request.
	 * 
	 * @return a {@link MockRequest} object.
	 */
	public MockRequest post(String path) throws Exception {
		return service("POST", path);
	}
	
	/**
	 * Helper method. Builds a {@link MockRequest} based on the received arguments.
	 * 
	 * @param httpMethod the HTTP method.
	 * @param path the requested path.
	 * 
	 * @return a {@link MockRequest} object.
	 * @throws Exception
	 */
	private MockRequest service(String httpMethod, String path) throws Exception {
		if (!path.startsWith("/")) {
			path = "/" + path;
		}
		
		String url = "http://localhost" + path;
		Route route = getJogger().getRoute(httpMethod, fixPath(path));
		return new MockRequest(getJogger(), route, httpMethod, url);
	}

	/**
	 * Retrieves the path by removing the query string, hash tags, etc from the <code>path</code>. 
	 * 
	 * @param path the path that we will fix
	 * 
	 * @return a String
	 * @throws URISyntaxException
	 */
	private String fixPath(String path) throws URISyntaxException {
		URI uri = new URI(path);
		return uri.getPath();
	}

	/**
	 * Retrieves the {@link Jogger} object used to configure the application.
	 * 
	 * @return the {@link Jogger} object.
	 * @throws Exception
	 */
	protected abstract Jogger getJogger() throws Exception;
	
}

@@ -1352,32 +1352,802 @@
 T%22, path);%0A%09%7D%0A%09%0A
+%09/**%0A%09 * Helper method. Creates and returns a %7B@link MockRequest%7D with 'DELETE' as the HTTP method. Call the%0A%09 * %7B@link MockRequest#run()%7D method to execute the request.%0A%09 *%0A%09 * @param path the path of the DELETE request%0A%09 *%0A%09 * @return a %7B@link MockRequest%7D object.%0A%09 * @throws Exception%0A%09 */%0A%09public MockRequest delete(String path) throws Exception %7B%0A%09%09return service(%22DELETE%22, path);%0A%09%7D%0A%0A%09/**%0A%09 * Helper method. Creates and returns a %7B@link MockRequest%7D with 'PUT' as the HTTP method. Call the%0A%09 * %7B@link MockRequest#run()%7D method to execute the request.%0A%09 *%0A%09 * @param path the path of the PUT request%0A%09 *%0A%09 * @return a %7B@link MockRequest%7D object.%0A%09 * @throws Exception%0A%09 */%0A%09public MockRequest put(String path) throws Exception %7B%0A%09%09return service(%22PUT%22, path);%0A%09%7D%0A%0A
 %09/**%0A%09 * Helper 

Add put and delete methods to JoggerTest