public void testWeb() throws Exception
{
String baseURLNoAuth = "http://" + getServerHostForURL() +
":" + Integer.getInteger("web.port", 8080) + "/";
HttpClient httpConn = new HttpClient();
GetMethod indexGet = new GetMethod(baseURLNoAuth + "sdtolerate/");
int responseCode = httpConn.executeMethod(indexGet);
String body = indexGet.getResponseBodyAsString();
assertTrue("Get OK(" + responseCode + ")", responseCode == HttpURLConnection.HTTP_OK);
assertTrue("Redirected to login page", body.indexOf("j_security_check") > 0);
HttpState state = httpConn.getState();
Cookie[] cookies = state.getCookies();
String sessionID = null;
for (int c = 0; c < cookies.length; c++)
{
Cookie k = cookies[c];
if (k.getName().equalsIgnoreCase("JSESSIONID"))
sessionID = k.getValue();
}
getLog().debug("Saw JSESSIONID=" + sessionID);
// Submit the login form
PostMethod formPost = new PostMethod(baseURLNoAuth + "sdtolerate/j_security_check");
formPost.addRequestHeader("Referer", baseURLNoAuth + "sdtolerate/login.jsp");
formPost.addParameter("j_username", this.username);
formPost.addParameter("j_password", new String(password));
responseCode = httpConn.executeMethod(formPost);
String loginResult = formPost.getResponseBodyAsString();
if( loginResult.indexOf("Encountered a login error") > 0 )
fail("Login Failed");
String response = formPost.getStatusText();
log.debug("responseCode="+responseCode+", response="+response);
assertTrue("Saw HTTP_MOVED_TEMP", responseCode == HttpURLConnection.HTTP_MOVED_TEMP);
// Follow the redirect to the index.jsp
Header location = formPost.getResponseHeader("Location");
String indexURI = location.getValue();
GetMethod war1Index = new GetMethod(indexURI);
responseCode = httpConn.executeMethod(war1Index);
response = war1Index.getStatusText();
log.debug("responseCode="+responseCode+", response="+response);
assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK);
}