Search in sources :

Example 6 with StringContentProvider

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

the class AsyncIOServletTest method testOnErrorThrows.

@Test
public void testOnErrorThrows() throws Exception {
    AtomicInteger errors = new AtomicInteger();
    start(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            assertScope();
            if (request.getDispatcherType() == DispatcherType.ERROR) {
                response.flushBuffer();
                return;
            }
            request.startAsync(request, response);
            request.getInputStream().setReadListener(new ReadListener() {

                @Override
                public void onDataAvailable() throws IOException {
                    assertScope();
                    throw new NullPointerException("explicitly_thrown_by_test_1");
                }

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

                @Override
                public void onError(Throwable t) {
                    assertScope();
                    errors.incrementAndGet();
                    throw new NullPointerException("explicitly_thrown_by_test_2") {

                        {
                            this.initCause(t);
                        }
                    };
                }
            });
        }
    });
    try (StacklessLogging stackless = new StacklessLogging(HttpChannel.class)) {
        ContentResponse response = client.newRequest(newURI()).path(servletPath).content(new StringContentProvider("0123456789")).timeout(5, TimeUnit.SECONDS).send();
        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR_500, response.getStatus());
        Assert.assertEquals(1, errors.get());
    }
}
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) 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) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) Test(org.junit.Test)

Example 7 with StringContentProvider

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

the class AsyncIOServletTest method testIsReadyAtEOF.

@Test
public void testIsReadyAtEOF() throws Exception {
    String text = "TEST\n";
    byte[] data = text.getBytes(StandardCharsets.UTF_8);
    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() {

                transient int _i = 0;

                transient boolean _minusOne = false;

                transient boolean _finished = false;

                @Override
                public void onDataAvailable() throws IOException {
                    assertScope();
                    while (input.isReady() && !input.isFinished()) {
                        int b = input.read();
                        if (b == -1)
                            _minusOne = true;
                        else if (data[_i++] != b)
                            throw new IllegalStateException();
                    }
                    if (input.isFinished())
                        _finished = true;
                }

                @Override
                public void onAllDataRead() throws IOException {
                    assertScope();
                    output.write(String.format("i=%d eof=%b finished=%b", _i, _minusOne, _finished).getBytes(StandardCharsets.UTF_8));
                    async.complete();
                }

                @Override
                public void onError(Throwable t) {
                    assertScope();
                    t.printStackTrace();
                    async.complete();
                }
            });
        }
    });
    ContentResponse response = client.newRequest(newURI()).method(HttpMethod.POST).path(servletPath).header(HttpHeader.CONNECTION, "close").content(new StringContentProvider(text)).timeout(5, TimeUnit.SECONDS).send();
    String responseContent = response.getContentAsString();
    assertThat(responseContent, containsString("i=" + data.length + " eof=true finished=true"));
}
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) ServletInputStream(javax.servlet.ServletInputStream) Test(org.junit.Test)

Example 8 with StringContentProvider

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

the class ForwardProxyTLSServerTest method testTwoExchanges.

@Test
public void testTwoExchanges() throws Exception {
    startTLSServer(new ServerHandler());
    startProxy();
    HttpClient httpClient = new HttpClient(newSslContextFactory());
    httpClient.getProxyConfiguration().getProxies().add(newHttpProxy());
    httpClient.start();
    try {
        String body = "BODY";
        ContentResponse response1 = httpClient.newRequest("localhost", serverConnector.getLocalPort()).scheme(HttpScheme.HTTPS.asString()).method(HttpMethod.GET).path("/echo?body=" + URLEncoder.encode(body, "UTF-8")).timeout(5, TimeUnit.SECONDS).send();
        Assert.assertEquals(HttpStatus.OK_200, response1.getStatus());
        String content = response1.getContentAsString();
        Assert.assertEquals(body, content);
        content = "body=" + body;
        ContentResponse response2 = httpClient.newRequest("localhost", serverConnector.getLocalPort()).scheme(HttpScheme.HTTPS.asString()).method(HttpMethod.POST).path("/echo").header(HttpHeader.CONTENT_TYPE, MimeTypes.Type.FORM_ENCODED.asString()).header(HttpHeader.CONTENT_LENGTH, String.valueOf(content.length())).content(new StringContentProvider(content)).timeout(5, TimeUnit.SECONDS).send();
        Assert.assertEquals(HttpStatus.OK_200, response2.getStatus());
        content = response2.getContentAsString();
        Assert.assertEquals(body, content);
    } finally {
        httpClient.stop();
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider) HttpClient(org.eclipse.jetty.client.HttpClient) Test(org.junit.Test)

Example 9 with StringContentProvider

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

the class ForwardProxyTLSServerTest method testTwoConcurrentExchanges.

@Test
public void testTwoConcurrentExchanges() throws Exception {
    startTLSServer(new ServerHandler());
    startProxy();
    final HttpClient httpClient = new HttpClient(newSslContextFactory());
    httpClient.getProxyConfiguration().getProxies().add(newHttpProxy());
    httpClient.start();
    try {
        final AtomicReference<Connection> connection = new AtomicReference<>();
        final CountDownLatch connectionLatch = new CountDownLatch(1);
        String content1 = "BODY";
        ContentResponse response1 = httpClient.newRequest("localhost", serverConnector.getLocalPort()).scheme(HttpScheme.HTTPS.asString()).method(HttpMethod.GET).path("/echo?body=" + URLEncoder.encode(content1, "UTF-8")).onRequestCommit(request -> {
            Destination destination = httpClient.getDestination(HttpScheme.HTTPS.asString(), "localhost", serverConnector.getLocalPort());
            destination.newConnection(new Promise.Adapter<Connection>() {

                @Override
                public void succeeded(Connection result) {
                    connection.set(result);
                    connectionLatch.countDown();
                }
            });
        }).timeout(5, TimeUnit.SECONDS).send();
        Assert.assertEquals(HttpStatus.OK_200, response1.getStatus());
        String content = response1.getContentAsString();
        Assert.assertEquals(content1, content);
        Assert.assertTrue(connectionLatch.await(5, TimeUnit.SECONDS));
        String body2 = "body=" + content1;
        org.eclipse.jetty.client.api.Request request2 = httpClient.newRequest("localhost", serverConnector.getLocalPort()).scheme(HttpScheme.HTTPS.asString()).method(HttpMethod.POST).path("/echo").header(HttpHeader.CONTENT_TYPE, MimeTypes.Type.FORM_ENCODED.asString()).header(HttpHeader.CONTENT_LENGTH, String.valueOf(body2.length())).content(new StringContentProvider(body2));
        // Make sure the second connection can send the exchange via the tunnel
        FutureResponseListener listener2 = new FutureResponseListener(request2);
        connection.get().send(request2, listener2);
        ContentResponse response2 = listener2.get(5, TimeUnit.SECONDS);
        Assert.assertEquals(HttpStatus.OK_200, response2.getStatus());
        String content2 = response2.getContentAsString();
        Assert.assertEquals(content1, content2);
    } finally {
        httpClient.stop();
    }
}
Also used : Destination(org.eclipse.jetty.client.api.Destination) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider) Connection(org.eclipse.jetty.client.api.Connection) HttpConnection(org.eclipse.jetty.server.HttpConnection) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Promise(org.eclipse.jetty.util.Promise) HttpClient(org.eclipse.jetty.client.HttpClient) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 10 with StringContentProvider

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

the class JettyContentExchange9 method setRequestContent.

public void setRequestContent(String data, String charset) throws UnsupportedEncodingException {
    StringContentProvider cp = charset != null ? new StringContentProvider(data, charset) : new StringContentProvider(data);
    this.request.content(cp, this.requestContentType);
}
Also used : StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider)

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