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());
}
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());
}
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));
}
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));
}
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());
}
Aggregations