Example usage for org.apache.http.impl.client DefaultRedirectStrategy isRedirected

List of usage examples for org.apache.http.impl.client DefaultRedirectStrategy isRedirected

Introduction

In this page you can find the example usage for org.apache.http.impl.client DefaultRedirectStrategy isRedirected.

Prototype

public boolean isRedirected(final HttpRequest request, final HttpResponse response, final HttpContext context)
            throws ProtocolException 

Source Link

Usage

From source file:org.dataconservancy.ui.it.RegisterNewUserIT.java

/**
 * Verify the unregistered user cannot login. Register the unregistered
 * user. Login as admin. List pending registrations. Approve the
 * registration. Logout. Login as the newly registered user.
 * /*from   w ww. j a v a 2  s. c  om*/
 * @throws Exception
 */
@Test
public void testApproveNewUserRegistration() throws Exception {
    final DefaultHttpClient hc = new DefaultHttpClient();

    // Override the redirect strategy to redirect on POST.  So we can just test for 200 statuses in the
    // unit test.
    final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
    hc.setRedirectStrategy(new RedirectStrategy() {

        @Override
        public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context)
                throws ProtocolException {
            if (!redirectStrategy.isRedirected(request, response, context)) {
                return response.getStatusLine().getStatusCode() == 302;
            }
            return true;
        }

        @Override
        public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context)
                throws ProtocolException {
            return redirectStrategy.getRedirect(request, response, context);
        }
    });

    // Attempt to login as an unregistered user
    final HttpPost request = reqFactory.createLoginRequest(toRegister).asHttpPost();
    HttpResponse resp = hc.execute(request);
    String content = IOUtils.toString(resp.getEntity().getContent());
    assertTrue("Expected response '" + resp + "' to contain failure message '" + loginErrorMsg + "'.  "
            + "Page content was \n[" + content + "]\n", content.contains(loginErrorMsg));

    // Register a new user
    HttpAssert.assertStatus(hc, reqFactory.createRegisterRequest(toRegister).asHttpPost(), 200);

    // Login as admin
    HttpAssert.assertStatus(hc, reqFactory.createLoginRequest(adminUser).asHttpPost(), 200);

    // View pending registrations
    HttpAssert.assertStatus(hc, reqFactory.listPendingRegistrations().asHttpGet(), 200);

    // Approve registration
    HttpAssert.assertStatus(hc, reqFactory.createApproveRegistrationRequest(toRegister).asHttpPost(), 200);

    // Logout admin
    HttpAssert.assertStatus(hc, reqFactory.createLogoutRequest().asHttpGet(), 200);

    // Login as newly registered user
    resp = hc.execute(reqFactory.createLoginRequest(toRegister).asHttpPost());
    content = IOUtils.toString(resp.getEntity().getContent());
    assertFalse("Did NOT expect response '" + resp + "' to contain failure message '" + loginErrorMsg + "'.  "
            + "Page content was \n[" + content + "]\n", content.contains(loginErrorMsg));
    assertEquals(200, resp.getStatusLine().getStatusCode());
}