Search in sources :

Example 66 with ContentResponse

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

the class HttpClientTest method test_PUT_WithParameters.

@Test
public void test_PUT_WithParameters() throws Exception {
    final String paramName = "a";
    final String paramValue = "€";
    final String encodedParamValue = URLEncoder.encode(paramValue, "UTF-8");
    start(new AbstractHandler() {

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            String value = request.getParameter(paramName);
            if (paramValue.equals(value)) {
                response.setCharacterEncoding("UTF-8");
                response.setContentType("text/plain");
                response.getOutputStream().print(value);
            }
        }
    });
    URI uri = URI.create(scheme + "://localhost:" + connector.getLocalPort() + "/path?" + paramName + "=" + encodedParamValue);
    ContentResponse response = client.newRequest(uri).method(HttpMethod.PUT).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals(paramValue, new String(response.getContent(), StandardCharsets.UTF_8));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) URI(java.net.URI) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Test(org.junit.Test)

Example 67 with ContentResponse

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

the class HttpClientTest method testUserAgentCanBeRemoved.

@Test
public void testUserAgentCanBeRemoved() throws Exception {
    start(new AbstractHandler() {

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            ArrayList<String> userAgents = Collections.list(request.getHeaders("User-Agent"));
            if ("/ua".equals(target))
                Assert.assertEquals(1, userAgents.size());
            else
                Assert.assertEquals(0, userAgents.size());
        }
    });
    // User agent not specified, use default.
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).path("/ua").timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
    // User agent explicitly removed.
    response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).agent(null).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
    // User agent explicitly removed.
    response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).header(HttpHeader.USER_AGENT, null).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ArrayList(java.util.ArrayList) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Test(org.junit.Test)

Example 68 with ContentResponse

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

the class HttpClientTest method test_Request_IdleTimeout.

@Slow
@Test
public void test_Request_IdleTimeout() throws Exception {
    final long idleTimeout = 1000;
    start(new AbstractHandler() {

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            try {
                baseRequest.setHandled(true);
                TimeUnit.MILLISECONDS.sleep(2 * idleTimeout);
            } catch (InterruptedException x) {
                throw new ServletException(x);
            }
        }
    });
    final String host = "localhost";
    final int port = connector.getLocalPort();
    try {
        client.newRequest(host, port).scheme(scheme).idleTimeout(idleTimeout, TimeUnit.MILLISECONDS).timeout(3 * idleTimeout, TimeUnit.MILLISECONDS).send();
        Assert.fail();
    } catch (ExecutionException expected) {
        Assert.assertTrue(expected.getCause() instanceof TimeoutException);
    }
    // Make another request without specifying the idle timeout, should not fail
    ContentResponse response = client.newRequest(host, port).scheme(scheme).timeout(3 * idleTimeout, TimeUnit.MILLISECONDS).send();
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) EndPoint(org.eclipse.jetty.io.EndPoint) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 69 with ContentResponse

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

the class HttpClientTest method test_POST_WithParameters_WithContent.

@Test
public void test_POST_WithParameters_WithContent() throws Exception {
    final byte[] content = { 0, 1, 2, 3 };
    final String paramName = "a";
    final String paramValue = "€";
    start(new AbstractHandler() {

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            consume(request.getInputStream(), true);
            String value = request.getParameter(paramName);
            if (paramValue.equals(value)) {
                response.setCharacterEncoding("UTF-8");
                response.setContentType("text/plain");
                response.getOutputStream().write(content);
            }
        }
    });
    ContentResponse response = client.POST(scheme + "://localhost:" + connector.getLocalPort() + "/?b=1").param(paramName, paramValue).content(new BytesContentProvider(content)).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertArrayEquals(content, response.getContent());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Test(org.junit.Test)

Example 70 with ContentResponse

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

the class HttpClientTest method testCustomHostHeader.

@Test
public void testCustomHostHeader() throws Exception {
    final String host = "localhost";
    start(new AbstractHandler() {

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            Assert.assertEquals(host, request.getServerName());
        }
    });
    ContentResponse response = client.newRequest("http://127.0.0.1:" + connector.getLocalPort() + "/path").scheme(scheme).header(HttpHeader.HOST, host).send();
    Assert.assertEquals(200, response.getStatus());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) 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