Search in sources :

Example 51 with HttpClient

use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.

the class Usage method testRequestWithExplicitConnectionControl.

@Test
public void testRequestWithExplicitConnectionControl() throws Exception {
    HttpClient client = new HttpClient();
    client.start();
    // Create an explicit connection, and use try-with-resources to manage it
    FuturePromise<Connection> futureConnection = new FuturePromise<>();
    client.getDestination("http", "localhost", 8080).newConnection(futureConnection);
    try (Connection connection = futureConnection.get(5, TimeUnit.SECONDS)) {
        Request request = client.newRequest("localhost", 8080);
        // Asynchronous send but using FutureResponseListener
        FutureResponseListener listener = new FutureResponseListener(request);
        connection.send(request, listener);
        // Wait for the response on the listener
        Response response = listener.get(5, TimeUnit.SECONDS);
        Assert.assertNotNull(response);
        Assert.assertEquals(200, response.getStatus());
    }
}
Also used : HttpClient(org.eclipse.jetty.client.HttpClient) FuturePromise(org.eclipse.jetty.util.FuturePromise) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 52 with HttpClient

use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.

the class Usage method testFileUpload.

@Test
public void testFileUpload() throws Exception {
    HttpClient client = new HttpClient();
    client.start();
    // One liner to upload files
    Response response = client.newRequest("localhost", 8080).file(Paths.get("file_to_upload.txt")).send();
    Assert.assertEquals(200, response.getStatus());
}
Also used : HttpClient(org.eclipse.jetty.client.HttpClient) Test(org.junit.Test)

Example 53 with HttpClient

use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.

the class Usage method testGETBlocking.

@Test
public void testGETBlocking() throws Exception {
    HttpClient client = new HttpClient();
    client.start();
    // Address must be provided, it's the only thing non defaultable
    Request request = client.newRequest("localhost", 8080).scheme("https").method(HttpMethod.GET).path("/uri").version(HttpVersion.HTTP_1_1).param("a", "b").header("X-Header", "Y-value").agent("Jetty HTTP Client").idleTimeout(5000, TimeUnit.MILLISECONDS).timeout(20, TimeUnit.SECONDS);
    ContentResponse response = request.send();
    Assert.assertEquals(200, response.getStatus());
}
Also used : HttpClient(org.eclipse.jetty.client.HttpClient) Test(org.junit.Test)

Example 54 with HttpClient

use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.

the class Usage method testRequestInputStream.

@Test
public void testRequestInputStream() throws Exception {
    HttpClient client = new HttpClient();
    client.start();
    InputStream input = new ByteArrayInputStream("content".getBytes(StandardCharsets.UTF_8));
    ContentResponse response = client.newRequest("localhost", 8080).content(new InputStreamContentProvider(input)).send();
    Assert.assertEquals(200, response.getStatus());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpClient(org.eclipse.jetty.client.HttpClient) InputStreamContentProvider(org.eclipse.jetty.client.util.InputStreamContentProvider) Test(org.junit.Test)

Example 55 with HttpClient

use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.

the class Usage method testGETBlocking_ShortAPI.

@Test
public void testGETBlocking_ShortAPI() throws Exception {
    HttpClient client = new HttpClient();
    client.start();
    // Block to get the response
    ContentResponse response = client.GET("http://localhost:8080/foo");
    // Verify response status code
    Assert.assertEquals(200, response.getStatus());
    // Access headers
    response.getHeaders().get("Content-Length");
}
Also used : HttpClient(org.eclipse.jetty.client.HttpClient) Test(org.junit.Test)

Aggregations

HttpClient (org.eclipse.jetty.client.HttpClient)197 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)102 Test (org.junit.Test)94 Request (org.eclipse.jetty.client.api.Request)53 HttpServletRequest (javax.servlet.http.HttpServletRequest)42 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)40 Test (org.testng.annotations.Test)34 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)24 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)19 CloudStore (com.yahoo.athenz.zts.store.CloudStore)17 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)16 HttpCertSigner (com.yahoo.athenz.zts.cert.impl.HttpCertSigner)14 HttpCertSignerFactory (com.yahoo.athenz.zts.cert.impl.HttpCertSignerFactory)14 URI (java.net.URI)11 HTTP2Client (org.eclipse.jetty.http2.client.HTTP2Client)11 ExecutionException (java.util.concurrent.ExecutionException)8 HttpProxy (org.eclipse.jetty.client.HttpProxy)8 StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)8 IOException (java.io.IOException)7 CountDownLatch (java.util.concurrent.CountDownLatch)7