Search in sources :

Example 1 with StringContentProvider

use of org.eclipse.jetty.client.util.StringContentProvider in project jetty.project by eclipse.

the class DigestPostTest method testServerWithHttpClientStreamContent.

@Test
public void testServerWithHttpClientStreamContent() throws Exception {
    String srvUrl = "http://127.0.0.1:" + ((NetworkConnector) _server.getConnectors()[0]).getLocalPort() + "/test/";
    HttpClient client = new HttpClient();
    try {
        AuthenticationStore authStore = client.getAuthenticationStore();
        authStore.addAuthentication(new DigestAuthentication(new URI(srvUrl), "test", "testuser", "password"));
        client.start();
        String sent = IO.toString(new FileInputStream("src/test/resources/message.txt"));
        Request request = client.newRequest(srvUrl);
        request.method(HttpMethod.POST);
        request.content(new StringContentProvider(sent));
        _received = null;
        request = request.timeout(5, TimeUnit.SECONDS);
        ContentResponse response = request.send();
        Assert.assertEquals(200, response.getStatus());
        Assert.assertEquals(sent, _received);
    } finally {
        client.stop();
    }
}
Also used : StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpClient(org.eclipse.jetty.client.HttpClient) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) NetworkConnector(org.eclipse.jetty.server.NetworkConnector) DigestAuthentication(org.eclipse.jetty.client.util.DigestAuthentication) URI(java.net.URI) FileInputStream(java.io.FileInputStream) AuthenticationStore(org.eclipse.jetty.client.api.AuthenticationStore) Test(org.junit.Test)

Example 2 with StringContentProvider

use of org.eclipse.jetty.client.util.StringContentProvider in project jetty.project by eclipse.

the class AsyncIOServletTest method testCompleteBeforeOnAllDataRead.

@Test
public void testCompleteBeforeOnAllDataRead() throws Exception {
    String success = "SUCCESS";
    AtomicBoolean allDataRead = new AtomicBoolean(false);
    start(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            assertScope();
            response.flushBuffer();
            AsyncContext async = request.startAsync();
            ServletInputStream input = request.getInputStream();
            ServletOutputStream output = response.getOutputStream();
            input.setReadListener(new ReadListener() {

                @Override
                public void onDataAvailable() throws IOException {
                    assertScope();
                    while (input.isReady()) {
                        int b = input.read();
                        if (b < 0) {
                            output.write(success.getBytes(StandardCharsets.UTF_8));
                            async.complete();
                            return;
                        }
                    }
                }

                @Override
                public void onAllDataRead() throws IOException {
                    assertScope();
                    output.write("FAILURE".getBytes(StandardCharsets.UTF_8));
                    allDataRead.set(true);
                    throw new IllegalStateException();
                }

                @Override
                public void onError(Throwable t) {
                    assertScope();
                    t.printStackTrace();
                }
            });
        }
    });
    ContentResponse response = client.newRequest(newURI()).method(HttpMethod.POST).path(servletPath).header(HttpHeader.CONNECTION, "close").content(new StringContentProvider("XYZ")).timeout(5, TimeUnit.SECONDS).send();
    assertThat(response.getStatus(), Matchers.equalTo(HttpStatus.OK_200));
    assertThat(response.getContentAsString(), Matchers.equalTo(success));
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) Matchers.containsString(org.hamcrest.Matchers.containsString) UncheckedIOException(java.io.UncheckedIOException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) ReadListener(javax.servlet.ReadListener) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ServletInputStream(javax.servlet.ServletInputStream) Test(org.junit.Test)

Example 3 with StringContentProvider

use of org.eclipse.jetty.client.util.StringContentProvider in project jetty.project by eclipse.

the class ClientConnectionCloseTest method test_ClientConnectionClose_ServerConnectionClose_ClientClosesAfterExchange.

@Test
public void test_ClientConnectionClose_ServerConnectionClose_ClientClosesAfterExchange() throws Exception {
    byte[] data = new byte[128 * 1024];
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            ServletInputStream input = request.getInputStream();
            while (true) {
                int read = input.read();
                if (read < 0)
                    break;
            }
            response.setContentLength(data.length);
            response.getOutputStream().write(data);
            try {
                // Delay the server from sending the TCP FIN.
                Thread.sleep(1000);
            } catch (InterruptedException x) {
                throw new InterruptedIOException();
            }
        }
    });
    String host = "localhost";
    int port = connector.getLocalPort();
    HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, host, port);
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
    ContentResponse response = client.newRequest(host, port).scheme(scheme).header(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.asString()).content(new StringContentProvider("0")).onRequestSuccess(request -> {
        HttpConnectionOverHTTP connection = (HttpConnectionOverHTTP) connectionPool.getActiveConnections().iterator().next();
        Assert.assertFalse(connection.getEndPoint().isOutputShutdown());
    }).send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    Assert.assertArrayEquals(data, response.getContent());
    Assert.assertEquals(0, connectionPool.getConnectionCount());
}
Also used : Request(org.eclipse.jetty.server.Request) HttpHeaderValue(org.eclipse.jetty.http.HttpHeaderValue) ServletException(javax.servlet.ServletException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) ServletInputStream(javax.servlet.ServletInputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Test(org.junit.Test) StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider) InterruptedIOException(java.io.InterruptedIOException) ByteBuffer(java.nio.ByteBuffer) TimeUnit(java.util.concurrent.TimeUnit) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) CountDownLatch(java.util.concurrent.CountDownLatch) HttpHeader(org.eclipse.jetty.http.HttpHeader) HttpServletRequest(javax.servlet.http.HttpServletRequest) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) HttpStatus(org.eclipse.jetty.http.HttpStatus) Assert(org.junit.Assert) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) InterruptedIOException(java.io.InterruptedIOException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServletInputStream(javax.servlet.ServletInputStream) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) Test(org.junit.Test)

Example 4 with StringContentProvider

use of org.eclipse.jetty.client.util.StringContentProvider in project jetty.project by eclipse.

the class HttpClientTest method testCopyRequest.

@Test
public void testCopyRequest() throws Exception {
    startClient();
    assertCopyRequest(client.newRequest("http://example.com/some/url").method(HttpMethod.HEAD).version(HttpVersion.HTTP_2).content(new StringContentProvider("some string")).timeout(321, TimeUnit.SECONDS).idleTimeout(2221, TimeUnit.SECONDS).followRedirects(true).header(HttpHeader.CONTENT_TYPE, "application/json").header("X-Some-Custom-Header", "some-value"));
    assertCopyRequest(client.newRequest("https://example.com").method(HttpMethod.POST).version(HttpVersion.HTTP_1_0).content(new StringContentProvider("some other string")).timeout(123231, TimeUnit.SECONDS).idleTimeout(232342, TimeUnit.SECONDS).followRedirects(false).header(HttpHeader.ACCEPT, "application/json").header("X-Some-Other-Custom-Header", "some-other-value"));
    assertCopyRequest(client.newRequest("https://example.com").header(HttpHeader.ACCEPT, "application/json").header(HttpHeader.ACCEPT, "application/xml").header("x-same-name", "value1").header("x-same-name", "value2"));
    assertCopyRequest(client.newRequest("https://example.com").header(HttpHeader.ACCEPT, "application/json").header(HttpHeader.CONTENT_TYPE, "application/json"));
    assertCopyRequest(client.newRequest("https://example.com").header("Accept", "application/json").header("Content-Type", "application/json"));
    assertCopyRequest(client.newRequest("https://example.com").header("X-Custom-Header-1", "value1").header("X-Custom-Header-2", "value2"));
    assertCopyRequest(client.newRequest("https://example.com").header("X-Custom-Header-1", "value").header("X-Custom-Header-2", "value"));
}
Also used : StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider) Test(org.junit.Test)

Example 5 with StringContentProvider

use of org.eclipse.jetty.client.util.StringContentProvider in project jetty.project by eclipse.

the class AsyncIOServletTest method testAsyncReadThrows.

private void testAsyncReadThrows(Throwable throwable) throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    start(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            assertScope();
            AsyncContext asyncContext = request.startAsync(request, response);
            request.getInputStream().setReadListener(new ReadListener() {

                @Override
                public void onDataAvailable() throws IOException {
                    assertScope();
                    if (throwable instanceof RuntimeException)
                        throw (RuntimeException) throwable;
                    if (throwable instanceof Error)
                        throw (Error) throwable;
                    throw new IOException(throwable);
                }

                @Override
                public void onAllDataRead() throws IOException {
                    assertScope();
                }

                @Override
                public void onError(Throwable t) {
                    assertScope();
                    Assert.assertThat("onError type", t, instanceOf(throwable.getClass()));
                    Assert.assertThat("onError message", t.getMessage(), is(throwable.getMessage()));
                    latch.countDown();
                    response.setStatus(500);
                    asyncContext.complete();
                }
            });
        }
    });
    ContentResponse response = client.newRequest(newURI()).method(HttpMethod.POST).path(servletPath).content(new StringContentProvider("0123456789")).timeout(5, TimeUnit.SECONDS).send();
    assertTrue(latch.await(5, TimeUnit.SECONDS));
    assertEquals(HttpStatus.INTERNAL_SERVER_ERROR_500, response.getStatus());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) UncheckedIOException(java.io.UncheckedIOException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ReadListener(javax.servlet.ReadListener) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException)

Aggregations

StringContentProvider (org.eclipse.jetty.client.util.StringContentProvider)11 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)9 Test (org.junit.Test)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 IOException (java.io.IOException)5 InterruptedIOException (java.io.InterruptedIOException)5 ServletException (javax.servlet.ServletException)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 UncheckedIOException (java.io.UncheckedIOException)4 ReadListener (javax.servlet.ReadListener)4 HttpServlet (javax.servlet.http.HttpServlet)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 AsyncContext (javax.servlet.AsyncContext)3 ServletInputStream (javax.servlet.ServletInputStream)3 HttpClient (org.eclipse.jetty.client.HttpClient)3 ServletOutputStream (javax.servlet.ServletOutputStream)2 Request (org.eclipse.jetty.client.api.Request)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 FileInputStream (java.io.FileInputStream)1 URI (java.net.URI)1