Search in sources :

Example 26 with TestHttpClient

use of io.undertow.testutils.TestHttpClient in project undertow by undertow-io.

the class SimpleSSLTestCase method simpleSSLTestCase.

@Test
public void simpleSSLTestCase() throws IOException, GeneralSecurityException {
    DefaultServer.setRootHandler(new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) throws Exception {
            exchange.getResponseHeaders().put(HttpString.tryFromString("scheme"), exchange.getRequestScheme());
            exchange.endExchange();
        }
    });
    DefaultServer.startSSLServer();
    TestHttpClient client = new TestHttpClient();
    client.setSSLContext(DefaultServer.getClientSSLContext());
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerSSLAddress());
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        Header[] header = result.getHeaders("scheme");
        Assert.assertEquals("https", header[0].getValue());
    } finally {
        client.getConnectionManager().shutdown();
        DefaultServer.stopSSLServer();
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) Header(org.apache.http.Header) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) GeneralSecurityException(java.security.GeneralSecurityException) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 27 with TestHttpClient

use of io.undertow.testutils.TestHttpClient in project undertow by undertow-io.

the class BasicAuthenticationTestCase method _testBadUserName.

static void _testBadUserName() throws Exception {
    TestHttpClient client = new TestHttpClient();
    HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL());
    HttpResponse result = client.execute(get);
    assertEquals(StatusCodes.UNAUTHORIZED, result.getStatusLine().getStatusCode());
    Header[] values = result.getHeaders(WWW_AUTHENTICATE.toString());
    String header = getAuthHeader(BASIC, values);
    assertEquals(BASIC + " realm=\"Test Realm\"", header);
    HttpClientUtils.readResponse(result);
    get = new HttpGet(DefaultServer.getDefaultServerURL());
    get.addHeader(AUTHORIZATION.toString(), BASIC + " " + FlexBase64.encodeString("badUser:passwordOne".getBytes(), false));
    result = client.execute(get);
    assertEquals(StatusCodes.UNAUTHORIZED, result.getStatusLine().getStatusCode());
    HttpClientUtils.readResponse(result);
}
Also used : Header(org.apache.http.Header) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(io.undertow.testutils.TestHttpClient)

Example 28 with TestHttpClient

use of io.undertow.testutils.TestHttpClient in project undertow by undertow-io.

the class ClientCertRenegotiationTestCase method testClientCertSuccessWithLargePostBody.

@Test
public void testClientCertSuccessWithLargePostBody() throws Exception {
    PooledByteBuffer buf = DefaultServer.getBufferPool().allocate();
    int requestSize = buf.getBuffer().limit() - 1;
    buf.close();
    final StringBuilder messageBuilder = new StringBuilder(requestSize);
    for (int i = 0; i < requestSize; ++i) {
        messageBuilder.append("*");
    }
    TestHttpClient client = new TestHttpClient();
    client.setSSLContext(clientSSLContext);
    HttpPost post = new HttpPost(DefaultServer.getDefaultServerSSLAddress());
    post.setEntity(new StringEntity(messageBuilder.toString()));
    HttpResponse result = client.execute(post);
    assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
    Header[] values = result.getHeaders("ProcessedBy");
    assertEquals("ProcessedBy Headers", 1, values.length);
    assertEquals("ResponseHandler", values[0].getValue());
    values = result.getHeaders("AuthenticatedUser");
    assertEquals("AuthenticatedUser Headers", 1, values.length);
    assertEquals("CN=Test Client,OU=OU,O=Org,L=City,ST=State,C=GB", values[0].getValue());
    HttpClientUtils.readResponse(result);
    assertSingleNotificationType(EventType.AUTHENTICATED);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) Header(org.apache.http.Header) PooledByteBuffer(io.undertow.connector.PooledByteBuffer) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 29 with TestHttpClient

use of io.undertow.testutils.TestHttpClient in project undertow by undertow-io.

the class ClientCertRenegotiationTestCase method testClientCertSuccess.

@Test
public void testClientCertSuccess() throws Exception {
    TestHttpClient client = new TestHttpClient();
    client.setSSLContext(clientSSLContext);
    HttpGet get = new HttpGet(DefaultServer.getDefaultServerSSLAddress());
    HttpResponse result = client.execute(get);
    assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
    Header[] values = result.getHeaders("ProcessedBy");
    assertEquals("ProcessedBy Headers", 1, values.length);
    assertEquals("ResponseHandler", values[0].getValue());
    values = result.getHeaders("AuthenticatedUser");
    assertEquals("AuthenticatedUser Headers", 1, values.length);
    assertEquals("CN=Test Client,OU=OU,O=Org,L=City,ST=State,C=GB", values[0].getValue());
    HttpClientUtils.readResponse(result);
    assertSingleNotificationType(EventType.AUTHENTICATED);
}
Also used : Header(org.apache.http.Header) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 30 with TestHttpClient

use of io.undertow.testutils.TestHttpClient in project undertow by undertow-io.

the class ClientCertTestCase method testClientCertSuccess.

@Test
public void testClientCertSuccess() throws Exception {
    TestHttpClient client = new TestHttpClient();
    client.setSSLContext(clientSSLContext);
    HttpGet get = new HttpGet(DefaultServer.getDefaultServerSSLAddress());
    HttpResponse result = client.execute(get);
    assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
    Header[] values = result.getHeaders("ProcessedBy");
    assertEquals("ProcessedBy Headers", 1, values.length);
    assertEquals("ResponseHandler", values[0].getValue());
    values = result.getHeaders("AuthenticatedUser");
    assertEquals("AuthenticatedUser Headers", 1, values.length);
    assertEquals("CN=Test Client,OU=OU,O=Org,L=City,ST=State,C=GB", values[0].getValue());
    HttpClientUtils.readResponse(result);
    assertSingleNotificationType(EventType.AUTHENTICATED);
}
Also used : Header(org.apache.http.Header) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Aggregations

TestHttpClient (io.undertow.testutils.TestHttpClient)302 HttpResponse (org.apache.http.HttpResponse)290 Test (org.junit.Test)269 HttpGet (org.apache.http.client.methods.HttpGet)239 Header (org.apache.http.Header)66 HttpPost (org.apache.http.client.methods.HttpPost)54 IOException (java.io.IOException)38 StringEntity (org.apache.http.entity.StringEntity)30 Path (java.nio.file.Path)29 PathHandler (io.undertow.server.handlers.PathHandler)28 HttpHandler (io.undertow.server.HttpHandler)20 CanonicalPathHandler (io.undertow.server.handlers.CanonicalPathHandler)20 PathResourceManager (io.undertow.server.handlers.resource.PathResourceManager)20 ResourceHandler (io.undertow.server.handlers.resource.ResourceHandler)20 ArrayList (java.util.ArrayList)20 HttpServerExchange (io.undertow.server.HttpServerExchange)19 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)16 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)14 NameValuePair (org.apache.http.NameValuePair)13 DigestWWWAuthenticateToken (io.undertow.security.impl.DigestWWWAuthenticateToken)11