Search in sources :

Example 16 with Request

use of org.eclipse.jetty.server.Request in project jetty.project by eclipse.

the class ContentResponseTest method testResponseWithoutContentType.

@Test
public void testResponseWithoutContentType() throws Exception {
    final byte[] content = new byte[1024];
    new Random().nextBytes(content);
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            response.getOutputStream().write(content);
        }
    });
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
    Assert.assertArrayEquals(content, response.getContent());
    Assert.assertNull(response.getMediaType());
    Assert.assertNull(response.getEncoding());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Random(java.util.Random) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Test(org.junit.Test)

Example 17 with Request

use of org.eclipse.jetty.server.Request 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());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Test(org.junit.Test)

Example 18 with Request

use of org.eclipse.jetty.server.Request 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());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Test(org.junit.Test)

Example 19 with Request

use of org.eclipse.jetty.server.Request in project jetty.project by eclipse.

the class HttpClientGZIPTest method testGZIPContentSentTwiceInOneWrite.

@Test
public void testGZIPContentSentTwiceInOneWrite() throws Exception {
    final byte[] data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            response.setHeader("Content-Encoding", "gzip");
            ByteArrayOutputStream gzipData = new ByteArrayOutputStream();
            GZIPOutputStream gzipOutput = new GZIPOutputStream(gzipData);
            gzipOutput.write(data);
            gzipOutput.finish();
            byte[] gzipBytes = gzipData.toByteArray();
            byte[] content = Arrays.copyOf(gzipBytes, 2 * gzipBytes.length);
            System.arraycopy(gzipBytes, 0, content, gzipBytes.length, gzipBytes.length);
            ServletOutputStream output = response.getOutputStream();
            output.write(content);
        }
    });
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).send();
    Assert.assertEquals(200, response.getStatus());
    byte[] expected = Arrays.copyOf(data, 2 * data.length);
    System.arraycopy(data, 0, expected, data.length, data.length);
    Assert.assertArrayEquals(expected, response.getContent());
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) GZIPOutputStream(java.util.zip.GZIPOutputStream) Test(org.junit.Test)

Example 20 with Request

use of org.eclipse.jetty.server.Request in project jetty.project by eclipse.

the class HttpClientGZIPTest method testGZIPContentFragmented.

private void testGZIPContentFragmented(final int fragment) throws Exception {
    final byte[] data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            response.setHeader("Content-Encoding", "gzip");
            ByteArrayOutputStream gzipData = new ByteArrayOutputStream();
            GZIPOutputStream gzipOutput = new GZIPOutputStream(gzipData);
            gzipOutput.write(data);
            gzipOutput.finish();
            byte[] gzipBytes = gzipData.toByteArray();
            byte[] chunk1 = Arrays.copyOfRange(gzipBytes, 0, gzipBytes.length - fragment);
            byte[] chunk2 = Arrays.copyOfRange(gzipBytes, gzipBytes.length - fragment, gzipBytes.length);
            ServletOutputStream output = response.getOutputStream();
            output.write(chunk1);
            output.flush();
            sleep(500);
            output.write(chunk2);
            output.flush();
        }
    });
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).send();
    Assert.assertEquals(200, response.getStatus());
    Assert.assertArrayEquals(data, response.getContent());
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) GZIPOutputStream(java.util.zip.GZIPOutputStream)

Aggregations

Request (org.eclipse.jetty.server.Request)248 HttpServletRequest (javax.servlet.http.HttpServletRequest)223 HttpServletResponse (javax.servlet.http.HttpServletResponse)200 Test (org.junit.Test)182 IOException (java.io.IOException)151 ServletException (javax.servlet.ServletException)137 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)129 CountDownLatch (java.util.concurrent.CountDownLatch)65 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)64 InterruptedIOException (java.io.InterruptedIOException)45 InputStream (java.io.InputStream)36 AtomicReference (java.util.concurrent.atomic.AtomicReference)34 Server (org.eclipse.jetty.server.Server)29 Response (org.eclipse.jetty.client.api.Response)27 Result (org.eclipse.jetty.client.api.Result)27 ByteArrayInputStream (java.io.ByteArrayInputStream)26 ServletInputStream (javax.servlet.ServletInputStream)23 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)23 ServletOutputStream (javax.servlet.ServletOutputStream)22 ByteArrayOutputStream (java.io.ByteArrayOutputStream)21