// when - hit secured page
HttpResponse landingPageResponse = httpClient.execute(new HttpGet("http://127.0.0.1:8080/"));
// then - login page is displayed
LoginPage securePageResponse = new LoginPage(getBodyAndClose(landingPageResponse));
securePageResponse.shouldHaveCorrectFields();
csrf = securePageResponse.csrfValue();
// when - login is performed
HttpPost login = new HttpPost("https://127.0.0.1:8443/login");
login.setEntity(new UrlEncodedFormEntity(Arrays.asList(
new BasicNameValuePair("username", "fake@email.com"),
new BasicNameValuePair("password", "NewPassword123"),
new BasicNameValuePair("_csrf", csrf)
)));
HttpResponse loginResponse = httpClient.execute(login);
// then - secured page is displayed
assertThat(loginResponse.getStatusLine().getStatusCode(), is(HttpStatus.SC_MOVED_TEMPORARILY));
assertThat(loginResponse.getFirstHeader("Location").getValue(), is("https://127.0.0.1:8443/"));
getBodyAndClose(loginResponse);
// when - logout
httpClient = createApacheClient();
HttpResponse logoutResponse = httpClient.execute(new HttpGet("https://127.0.0.1:8443/logout"));
// then - should get redirected to login page
LoginPage logoutRequestResponse = new LoginPage(getBodyAndClose(logoutResponse));
logoutRequestResponse.shouldHaveCorrectFields();
}