Search in sources :

Example 56 with HttpContext

use of org.apache.http.protocol.HttpContext in project robolectric by robolectric.

the class ShadowDefaultRequestDirectorTest method setUp_EnsureStaticStateIsReset.

@Before
public void setUp_EnsureStaticStateIsReset() {
    FakeHttpLayer fakeHttpLayer = FakeHttp.getFakeHttpLayer();
    assertFalse(fakeHttpLayer.hasPendingResponses());
    assertFalse(fakeHttpLayer.hasRequestInfos());
    assertFalse(fakeHttpLayer.hasResponseRules());
    connectionKeepAliveStrategy = new ConnectionKeepAliveStrategy() {

        @Override
        public long getKeepAliveDuration(HttpResponse httpResponse, HttpContext httpContext) {
            return 0;
        }
    };
    requestDirector = new DefaultRequestDirector(null, null, null, connectionKeepAliveStrategy, null, null, null, null, null, null, null, null);
}
Also used : ConnectionKeepAliveStrategy(org.apache.http.conn.ConnectionKeepAliveStrategy) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) DefaultRequestDirector(org.apache.http.impl.client.DefaultRequestDirector) Before(org.junit.Before)

Example 57 with HttpContext

use of org.apache.http.protocol.HttpContext in project undertow by undertow-io.

the class ChunkedResponseTrailersTestCase method sendHttpRequest.

@Test
public void sendHttpRequest() throws Exception {
    // this test will still run under h2-upgrade, but will fail
    Assume.assumeFalse(DefaultServer.isH2());
    HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
    TestHttpClient client = new TestHttpClient();
    final AtomicReference<ChunkedInputStream> stream = new AtomicReference<>();
    client.addResponseInterceptor(new HttpResponseInterceptor() {

        public void process(final HttpResponse response, final HttpContext context) throws IOException {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream instream = entity.getContent();
                if (instream instanceof ChunkedInputStream) {
                    stream.set(((ChunkedInputStream) instream));
                }
            }
        }
    });
    try {
        generateMessage(1);
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals(message, HttpClientUtils.readResponse(result));
        Header[] footers = stream.get().getFooters();
        Assert.assertEquals(2, footers.length);
        for (final Header header : footers) {
            if (header.getName().equals("foo")) {
                Assert.assertEquals("fooVal", header.getValue());
            } else if (header.getName().equals("bar")) {
                Assert.assertEquals("barVal", header.getValue());
            } else {
                Assert.fail("Unknown header" + header);
            }
        }
        generateMessage(1000);
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Assert.assertEquals(message, HttpClientUtils.readResponse(result));
        footers = stream.get().getFooters();
        Assert.assertEquals(2, footers.length);
        for (final Header header : footers) {
            if (header.getName().equals("foo")) {
                Assert.assertEquals("fooVal", header.getValue());
            } else if (header.getName().equals("bar")) {
                Assert.assertEquals("barVal", header.getValue());
            } else {
                Assert.fail("Unknown header" + header);
            }
        }
    } finally {
        client.getConnectionManager().shutdown();
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) ChunkedInputStream(org.apache.http.impl.io.ChunkedInputStream) ChunkedInputStream(org.apache.http.impl.io.ChunkedInputStream) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) TestHttpClient(io.undertow.testutils.TestHttpClient) Header(org.apache.http.Header) HttpResponseInterceptor(org.apache.http.HttpResponseInterceptor) Test(org.junit.Test)

Example 58 with HttpContext

use of org.apache.http.protocol.HttpContext 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 59 with HttpContext

use of org.apache.http.protocol.HttpContext in project undertow by undertow-io.

the class ServletFormAuthDefaultPageTestCase method testServletFormAuthWithoutSavedPostBody.

@Test
public void testServletFormAuthWithoutSavedPostBody() 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;
            }
            if (request.getRequestLine().getUri().equals(DEFAULT_PAGE)) {
                response.setStatusCode(StatusCodes.OK);
                // Skip redirecting, because the resource isn't available in this test
                return false;
            }
            // force the test to fail
            response.setStatusCode(StatusCodes.EXPECTATION_FAILED);
            return super.isRedirected(request, response, context);
        }
    });
    try {
        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/j_security_check");
        post.setEntity(new UrlEncodedFormEntity(data));
        HttpResponse result = client.execute(post);
        assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        String response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("", 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) 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 60 with HttpContext

use of org.apache.http.protocol.HttpContext in project undertow by undertow-io.

the class ServletFormAuthDefaultPageTestCase method testServletFormAuth.

@Test
public void testServletFormAuth() 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;
            }
            if (request.getRequestLine().getUri().equals(DEFAULT_PAGE)) {
                response.setStatusCode(StatusCodes.OK);
                // Skip redirecting, because the resource isn't available in this test
                return false;
            }
            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/j_security_check;jsessionid=dsjahfklsahdfjklsa");
        post.setEntity(new UrlEncodedFormEntity(data));
        result = client.execute(post);
        assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("", 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)

Aggregations

HttpContext (org.apache.http.protocol.HttpContext)169 HttpResponse (org.apache.http.HttpResponse)77 IOException (java.io.IOException)72 HttpRequest (org.apache.http.HttpRequest)55 BasicHttpContext (org.apache.http.protocol.BasicHttpContext)47 HttpException (org.apache.http.HttpException)30 Test (org.junit.Test)29 HttpGet (org.apache.http.client.methods.HttpGet)27 HttpHost (org.apache.http.HttpHost)26 HttpPost (org.apache.http.client.methods.HttpPost)26 ArrayList (java.util.ArrayList)22 Header (org.apache.http.Header)21 StringEntity (org.apache.http.entity.StringEntity)21 NameValuePair (org.apache.http.NameValuePair)18 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)18 ProtocolException (org.apache.http.ProtocolException)17 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)17 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)17 URI (java.net.URI)16 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)16