Search in sources :

Example 76 with ContentResponse

use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.

the class ContentResponseTest method testResponseWithMediaType.

@Test
public void testResponseWithMediaType() throws Exception {
    final String content = "The quick brown fox jumped over the lazy dog";
    final String mediaType = "text/plain";
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            response.setHeader(HttpHeader.CONTENT_TYPE.asString(), mediaType);
            response.getOutputStream().write(content.getBytes("UTF-8"));
        }
    });
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals(content, response.getContentAsString());
    Assert.assertEquals(mediaType, response.getMediaType());
    Assert.assertNull(response.getEncoding());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Test(org.junit.Test)

Example 77 with ContentResponse

use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.

the class ContentResponseTest method testResponseWithContentType.

@Test
public void testResponseWithContentType() throws Exception {
    final String content = "The quick brown fox jumped over the lazy dog";
    final String mediaType = "text/plain";
    final String encoding = "UTF-8";
    final String contentType = mediaType + "; charset=" + encoding;
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            response.setHeader(HttpHeader.CONTENT_TYPE.asString(), contentType);
            response.getOutputStream().write(content.getBytes("UTF-8"));
        }
    });
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals(content, response.getContentAsString());
    Assert.assertEquals(mediaType, response.getMediaType());
    Assert.assertEquals(encoding, response.getEncoding());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Test(org.junit.Test)

Example 78 with ContentResponse

use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.

the class HttpClientTLSTest method testHandshakeSucceededWithSessionResumption.

@Test
public void testHandshakeSucceededWithSessionResumption() throws Exception {
    SslContextFactory serverTLSFactory = createSslContextFactory();
    startServer(serverTLSFactory, new EmptyServerHandler());
    AtomicReference<byte[]> serverSession = new AtomicReference<>();
    connector.addBean(new SslHandshakeListener() {

        @Override
        public void handshakeSucceeded(Event event) {
            serverSession.set(event.getSSLEngine().getSession().getId());
        }
    });
    SslContextFactory clientTLSFactory = createSslContextFactory();
    startClient(clientTLSFactory);
    AtomicReference<byte[]> clientSession = new AtomicReference<>();
    client.addBean(new SslHandshakeListener() {

        @Override
        public void handshakeSucceeded(Event event) {
            clientSession.set(event.getSSLEngine().getSession().getId());
        }
    });
    // First request primes the TLS session.
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(HttpScheme.HTTPS.asString()).header(HttpHeader.CONNECTION, "close").timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    Assert.assertNotNull(serverSession.get());
    Assert.assertNotNull(clientSession.get());
    connector.removeBean(connector.getBean(SslHandshakeListener.class));
    client.removeBean(client.getBean(SslHandshakeListener.class));
    CountDownLatch serverLatch = new CountDownLatch(1);
    connector.addBean(new SslHandshakeListener() {

        @Override
        public void handshakeSucceeded(Event event) {
            if (Arrays.equals(serverSession.get(), event.getSSLEngine().getSession().getId()))
                serverLatch.countDown();
        }
    });
    CountDownLatch clientLatch = new CountDownLatch(1);
    client.addBean(new SslHandshakeListener() {

        @Override
        public void handshakeSucceeded(Event event) {
            if (Arrays.equals(clientSession.get(), event.getSSLEngine().getSession().getId()))
                clientLatch.countDown();
        }
    });
    // Second request should have the same session ID.
    response = client.newRequest("localhost", connector.getLocalPort()).scheme(HttpScheme.HTTPS.asString()).header(HttpHeader.CONNECTION, "close").timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    Assert.assertTrue(serverLatch.await(1, TimeUnit.SECONDS));
    Assert.assertTrue(clientLatch.await(1, TimeUnit.SECONDS));
}
Also used : SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) SslHandshakeListener(org.eclipse.jetty.io.ssl.SslHandshakeListener) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 79 with ContentResponse

use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.

the class HttpClientTLSTest method testHandshakeSucceeded.

@Test
public void testHandshakeSucceeded() throws Exception {
    SslContextFactory serverTLSFactory = createSslContextFactory();
    startServer(serverTLSFactory, new EmptyServerHandler());
    CountDownLatch serverLatch = new CountDownLatch(1);
    connector.addBean(new SslHandshakeListener() {

        @Override
        public void handshakeSucceeded(Event event) {
            serverLatch.countDown();
        }
    });
    SslContextFactory clientTLSFactory = createSslContextFactory();
    startClient(clientTLSFactory);
    CountDownLatch clientLatch = new CountDownLatch(1);
    client.addBean(new SslHandshakeListener() {

        @Override
        public void handshakeSucceeded(Event event) {
            clientLatch.countDown();
        }
    });
    ContentResponse response = client.GET("https://localhost:" + connector.getLocalPort());
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    Assert.assertTrue(serverLatch.await(1, TimeUnit.SECONDS));
    Assert.assertTrue(clientLatch.await(1, TimeUnit.SECONDS));
}
Also used : SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) SslHandshakeListener(org.eclipse.jetty.io.ssl.SslHandshakeListener) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 80 with ContentResponse

use of org.eclipse.jetty.client.api.ContentResponse in project jetty.project by eclipse.

the class HttpClientTest method testSendToIPv6Address.

@Test
public void testSendToIPv6Address() throws Exception {
    start(new EmptyServerHandler());
    ContentResponse response = client.newRequest("[::1]", connector.getLocalPort()).scheme(scheme).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Test(org.junit.Test)

Aggregations

ContentResponse (org.eclipse.jetty.client.api.ContentResponse)408 Test (org.junit.Test)322 HttpServletRequest (javax.servlet.http.HttpServletRequest)204 IOException (java.io.IOException)164 HttpServletResponse (javax.servlet.http.HttpServletResponse)159 ServletException (javax.servlet.ServletException)150 Request (org.eclipse.jetty.client.api.Request)117 HttpClient (org.eclipse.jetty.client.HttpClient)105 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)92 HttpServlet (javax.servlet.http.HttpServlet)48 Properties (java.util.Properties)45 ModelMetaData (org.teiid.adminapi.impl.ModelMetaData)45 InterruptedIOException (java.io.InterruptedIOException)42 Request (org.eclipse.jetty.server.Request)39 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)38 CountDownLatch (java.util.concurrent.CountDownLatch)37 StringContentProvider (org.eclipse.jetty.client.util.StringContentProvider)29 BytesContentProvider (org.eclipse.jetty.client.util.BytesContentProvider)28 Test (org.testng.annotations.Test)28 HardCodedExecutionFactory (org.teiid.runtime.HardCodedExecutionFactory)27