Search in sources :

Example 6 with DefaultRedirectStrategy

use of org.apache.http.impl.client.DefaultRedirectStrategy in project undertow by undertow-io.

the class ServletFormAuthURLRewriteTestCase method testServletFormAuthWithOriginalRequestParams.

@Test
public void testServletFormAuthWithOriginalRequestParams() throws IOException {
    TestHttpClient client = new TestHttpClient();
    client.setRedirectStrategy(new DefaultRedirectStrategy() {

        @Override
        public boolean isRedirected(final HttpRequest request, final HttpResponse response, final HttpContext context) throws ProtocolException {
            if (response.getStatusLine().getStatusCode() == StatusCodes.FOUND) {
                return true;
            }
            return super.isRedirected(request, response, context);
        }
    });
    try {
        final String uri = DefaultServer.getDefaultServerURL() + "/servletContext/secured/echoParam?param=developer";
        HttpPost post = new HttpPost(uri);
        post.setEntity(new StringEntity("String Entity"));
        HttpResponse result = client.execute(post);
        assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        String response = HttpClientUtils.readResponse(result);
        Assert.assertTrue(response.startsWith("j_security_check"));
        BasicNameValuePair[] pairs = new BasicNameValuePair[] { new BasicNameValuePair("j_username", "user1"), new BasicNameValuePair("j_password", "password1") };
        final List<NameValuePair> data = new ArrayList<>();
        data.addAll(Arrays.asList(pairs));
        post = new HttpPost(DefaultServer.getDefaultServerURL() + "/servletContext/" + response);
        post.setEntity(new UrlEncodedFormEntity(data));
        result = client.execute(post);
        assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        response = HttpClientUtils.readResponse(result);
        assertEquals("developer", response);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpRequest(org.apache.http.HttpRequest) ProtocolException(org.apache.http.ProtocolException) HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpContext(org.apache.http.protocol.HttpContext) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) TestHttpClient(io.undertow.testutils.TestHttpClient) StringEntity(org.apache.http.entity.StringEntity) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) DefaultRedirectStrategy(org.apache.http.impl.client.DefaultRedirectStrategy) Test(org.junit.Test)

Example 7 with DefaultRedirectStrategy

use of org.apache.http.impl.client.DefaultRedirectStrategy in project spark by perwendel.

the class SparkTestUtil method setFollowRedirectStrategy.

public void setFollowRedirectStrategy(Integer... codes) {
    final List<Integer> redirectCodes = Arrays.asList(codes);
    DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy() {

        public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) {
            boolean isRedirect = false;
            try {
                isRedirect = super.isRedirected(request, response, context);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (!isRedirect) {
                int responseCode = response.getStatusLine().getStatusCode();
                if (redirectCodes.contains(responseCode)) {
                    return true;
                }
            }
            return isRedirect;
        }
    };
    this.httpClient = httpClientBuilder().setRedirectStrategy(redirectStrategy).build();
}
Also used : HttpRequest(org.apache.http.HttpRequest) HttpContext(org.apache.http.protocol.HttpContext) DefaultRedirectStrategy(org.apache.http.impl.client.DefaultRedirectStrategy) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 8 with DefaultRedirectStrategy

use of org.apache.http.impl.client.DefaultRedirectStrategy in project undertow by undertow-io.

the class FormAuthTestCase method testFormAuth.

@Test
public void testFormAuth() throws IOException {
    TestHttpClient client = new TestHttpClient();
    client.setRedirectStrategy(new DefaultRedirectStrategy() {

        @Override
        public boolean isRedirected(final HttpRequest request, final HttpResponse response, final HttpContext context) throws ProtocolException {
            if (response.getStatusLine().getStatusCode() == StatusCodes.FOUND) {
                return true;
            }
            return super.isRedirected(request, response, context);
        }
    });
    try {
        final String uri = DefaultServer.getDefaultServerURL() + "/secured/test";
        HttpGet get = new HttpGet(uri);
        HttpResponse result = client.execute(get);
        assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        String response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("Login Page", response);
        BasicNameValuePair[] pairs = new BasicNameValuePair[] { new BasicNameValuePair("j_username", "userOne"), new BasicNameValuePair("j_password", "passwordOne") };
        final List<NameValuePair> data = new ArrayList<>();
        data.addAll(Arrays.asList(pairs));
        HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/j_security_check;jsessionid=dsjahfklsahdfjklsa");
        post.setEntity(new UrlEncodedFormEntity(data));
        result = client.execute(post);
        assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Header[] values = result.getHeaders("ProcessedBy");
        assertEquals(1, values.length);
        assertEquals("ResponseHandler", values[0].getValue());
        HttpClientUtils.readResponse(result);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpRequest(org.apache.http.HttpRequest) ProtocolException(org.apache.http.ProtocolException) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpPost(org.apache.http.client.methods.HttpPost) HttpGet(org.apache.http.client.methods.HttpGet) HttpContext(org.apache.http.protocol.HttpContext) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) TestHttpClient(io.undertow.testutils.TestHttpClient) Header(org.apache.http.Header) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) DefaultRedirectStrategy(org.apache.http.impl.client.DefaultRedirectStrategy) Test(org.junit.Test)

Example 9 with DefaultRedirectStrategy

use of org.apache.http.impl.client.DefaultRedirectStrategy in project undertow by undertow-io.

the class ServletCustomAuthTestCase method testServletCustomFormAuth.

@Test
public void testServletCustomFormAuth() throws IOException {
    TestHttpClient client = new TestHttpClient();
    client.setRedirectStrategy(new DefaultRedirectStrategy() {

        @Override
        public boolean isRedirected(final HttpRequest request, final HttpResponse response, final HttpContext context) throws ProtocolException {
            if (response.getStatusLine().getStatusCode() == StatusCodes.FOUND) {
                return true;
            }
            return super.isRedirected(request, response, context);
        }
    });
    try {
        final String uri = DefaultServer.getDefaultServerURL() + "/servletContext/secured/test";
        HttpGet get = new HttpGet(uri);
        HttpResponse result = client.execute(get);
        assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        String response = HttpClientUtils.readResponse(result);
        Assert.assertTrue(response.startsWith("j_security_check"));
        BasicNameValuePair[] pairs = new BasicNameValuePair[] { new BasicNameValuePair("j_username", "user1"), new BasicNameValuePair("j_password", "password1") };
        final List<NameValuePair> data = new ArrayList<>();
        data.addAll(Arrays.asList(pairs));
        HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/servletContext/" + CustomAuthenticationMechanism.POST_LOCATION);
        post.setEntity(new UrlEncodedFormEntity(data));
        result = client.execute(post);
        assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("user1", response);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpRequest(org.apache.http.HttpRequest) ProtocolException(org.apache.http.ProtocolException) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpPost(org.apache.http.client.methods.HttpPost) HttpGet(org.apache.http.client.methods.HttpGet) HttpContext(org.apache.http.protocol.HttpContext) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) TestHttpClient(io.undertow.testutils.TestHttpClient) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) DefaultRedirectStrategy(org.apache.http.impl.client.DefaultRedirectStrategy) Test(org.junit.Test)

Example 10 with DefaultRedirectStrategy

use of org.apache.http.impl.client.DefaultRedirectStrategy in project undertow by undertow-io.

the class ServletFormAuthURLRewriteTestCase method testServletFormAuthWithSavedPostBody.

@Test
public void testServletFormAuthWithSavedPostBody() throws IOException {
    TestHttpClient client = new TestHttpClient();
    client.setRedirectStrategy(new DefaultRedirectStrategy() {

        @Override
        public boolean isRedirected(final HttpRequest request, final HttpResponse response, final HttpContext context) throws ProtocolException {
            if (response.getStatusLine().getStatusCode() == StatusCodes.FOUND) {
                return true;
            }
            return super.isRedirected(request, response, context);
        }
    });
    try {
        final String uri = DefaultServer.getDefaultServerURL() + "/servletContext/secured/echo";
        HttpPost post = new HttpPost(uri);
        post.setEntity(new StringEntity("String Entity"));
        HttpResponse result = client.execute(post);
        assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        String response = HttpClientUtils.readResponse(result);
        Assert.assertTrue(response.startsWith("j_security_check"));
        BasicNameValuePair[] pairs = new BasicNameValuePair[] { new BasicNameValuePair("j_username", "user1"), new BasicNameValuePair("j_password", "password1") };
        final List<NameValuePair> data = new ArrayList<>();
        data.addAll(Arrays.asList(pairs));
        post = new HttpPost(DefaultServer.getDefaultServerURL() + "/servletContext/" + response);
        post.setEntity(new UrlEncodedFormEntity(data));
        result = client.execute(post);
        assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("String Entity", response);
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpRequest(org.apache.http.HttpRequest) ProtocolException(org.apache.http.ProtocolException) HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpContext(org.apache.http.protocol.HttpContext) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) TestHttpClient(io.undertow.testutils.TestHttpClient) StringEntity(org.apache.http.entity.StringEntity) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) DefaultRedirectStrategy(org.apache.http.impl.client.DefaultRedirectStrategy) Test(org.junit.Test)

Aggregations

HttpRequest (org.apache.http.HttpRequest)10 HttpResponse (org.apache.http.HttpResponse)10 DefaultRedirectStrategy (org.apache.http.impl.client.DefaultRedirectStrategy)10 HttpContext (org.apache.http.protocol.HttpContext)10 TestHttpClient (io.undertow.testutils.TestHttpClient)9 ProtocolException (org.apache.http.ProtocolException)9 ArrayList (java.util.ArrayList)8 NameValuePair (org.apache.http.NameValuePair)8 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)8 HttpPost (org.apache.http.client.methods.HttpPost)8 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)8 Test (org.junit.Test)8 HttpGet (org.apache.http.client.methods.HttpGet)4 StringEntity (org.apache.http.entity.StringEntity)4 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Header (org.apache.http.Header)1