Search in sources :

Example 6 with HttpRequest

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

the class PushedResourcesTest method testPushedResources.

@Test
public void testPushedResources() throws Exception {
    Random random = new Random();
    byte[] bytes = new byte[512];
    random.nextBytes(bytes);
    byte[] pushBytes1 = new byte[1024];
    random.nextBytes(pushBytes1);
    byte[] pushBytes2 = new byte[2048];
    random.nextBytes(pushBytes2);
    String path1 = "/secondary1";
    String path2 = "/secondary2";
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            if (target.equals(path1)) {
                response.getOutputStream().write(pushBytes1);
            } else if (target.equals(path2)) {
                response.getOutputStream().write(pushBytes2);
            } else {
                baseRequest.getPushBuilder().path(path1).push();
                baseRequest.getPushBuilder().path(path2).push();
                response.getOutputStream().write(bytes);
            }
        }
    });
    CountDownLatch latch1 = new CountDownLatch(1);
    CountDownLatch latch2 = new CountDownLatch(1);
    HttpRequest request = (HttpRequest) client.newRequest("localhost", connector.getLocalPort());
    ContentResponse response = request.pushListener((mainRequest, pushedRequest) -> new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            Assert.assertTrue(result.isSucceeded());
            if (pushedRequest.getPath().equals(path1)) {
                Assert.assertArrayEquals(pushBytes1, getContent());
                latch1.countDown();
            } else if (pushedRequest.getPath().equals(path2)) {
                Assert.assertArrayEquals(pushBytes2, getContent());
                latch2.countDown();
            }
        }
    }).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    Assert.assertArrayEquals(bytes, response.getContent());
    Assert.assertTrue(latch1.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(latch2.await(5, TimeUnit.SECONDS));
}
Also used : HttpRequest(org.eclipse.jetty.client.HttpRequest) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpRequest(org.eclipse.jetty.client.HttpRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Random(java.util.Random) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Test(org.junit.Test)

Aggregations

HttpRequest (org.eclipse.jetty.client.HttpRequest)6 CountDownLatch (java.util.concurrent.CountDownLatch)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)3 Result (org.eclipse.jetty.client.api.Result)3 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)3 Request (org.eclipse.jetty.server.Request)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 Random (java.util.Random)2 ServletException (javax.servlet.ServletException)2 HttpExchange (org.eclipse.jetty.client.HttpExchange)2 HttpResponse (org.eclipse.jetty.client.HttpResponse)2 Response (org.eclipse.jetty.client.api.Response)2 MetaData (org.eclipse.jetty.http.MetaData)2 ResetFrame (org.eclipse.jetty.http2.frames.ResetFrame)2 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)2 HttpResponseException (org.eclipse.jetty.client.HttpResponseException)1 Request (org.eclipse.jetty.client.api.Request)1