*
* @throws Exception if an error occurs when running the test.
*/
public void testRegularFormAuth() throws Exception
{
GetMethod getMethod = new GetMethod(this.testAppBaseURL + this.securedServletPath);
// execute a plain request to the SecureServlet
try
{
int responseCode = this.httpClient.executeMethod(getMethod);
String body = getMethod.getResponseBodyAsString();
// check the response code and assert the redirection to the login page
assertTrue("Unexpected response code received: " + responseCode, responseCode == HttpURLConnection.HTTP_OK);
assertTrue("Failed to redirect the request to the login page", body.indexOf("j_security_check") > 0);
}
finally
{
getMethod.releaseConnection();
}
HttpState state = this.httpClient.getState();
// fill in the login form and submit it
PostMethod postMethod = new PostMethod(this.testAppBaseURL + "j_security_check");
postMethod.addRequestHeader("Referer", this.testAppBaseURL + "restricted/login.html");
postMethod.addParameter("j_username", "jduke");
postMethod.addParameter("j_password", "theduke");
Header location = null;
try
{
int responseCode = this.httpClient.executeMethod(postMethod.getHostConfiguration(), postMethod, state);
log.debug("responseCode=" + responseCode + ", response=" + postMethod.getStatusText());
// check the response code received and the presence of a location header in the response
assertTrue("Unexpected response code received: " + responseCode,
responseCode == HttpURLConnection.HTTP_MOVED_TEMP);
location = postMethod.getResponseHeader("Location");
assertNotNull("Location header not found in response", location);
}
finally
{
postMethod.releaseConnection();
}
// follow the redirect as defined by the location header
String indexURI = location.getValue();
getMethod = new GetMethod(indexURI);
try
{
int responseCode = this.httpClient.executeMethod(getMethod.getHostConfiguration(), getMethod, state);
log.debug("responseCode=" + responseCode + ", response=" + getMethod.getStatusText());
// check the reponse code received
assertTrue("Unexpected response code received: " + responseCode, responseCode == HttpURLConnection.HTTP_OK);
String body = getMethod.getResponseBodyAsString();
// assert the redirection of to the SecureServlet
assertTrue("Redirect to SecureServlet has failed", body.indexOf("SecureServlet") > 0);
}
finally
{
getMethod.releaseConnection();
}
}