Search in sources :

Example 66 with Request

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

the class HttpClientTest method testRequestAfterFailedRequest.

@Test
public void testRequestAfterFailedRequest() throws Exception {
    int length = FlowControlStrategy.DEFAULT_WINDOW_SIZE;
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            try {
                baseRequest.setHandled(true);
                response.getOutputStream().write(new byte[length]);
            } catch (IOException e) {
            }
        }
    });
    // Make a request with a large enough response buffer.
    org.eclipse.jetty.client.api.Request request = client.newRequest(newURI());
    FutureResponseListener listener = new FutureResponseListener(request, length);
    request.send(listener);
    ContentResponse response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(response.getStatus(), 200);
    // Make a request with a small response buffer, should fail.
    try {
        request = client.newRequest(newURI());
        listener = new FutureResponseListener(request, length / 10);
        request.send(listener);
        listener.get(5, TimeUnit.SECONDS);
        Assert.fail();
    } catch (ExecutionException x) {
        Assert.assertThat(x.getMessage(), Matchers.containsString("Buffering capacity exceeded"));
    }
    // Verify that we can make another request.
    request = client.newRequest(newURI());
    listener = new FutureResponseListener(request, length);
    request.send(listener);
    response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(response.getStatus(), 200);
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ExecutionException(java.util.concurrent.ExecutionException) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 67 with Request

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

the class HttpClientTest method testOPTIONS.

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

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            Assert.assertTrue(HttpMethod.OPTIONS.is(request.getMethod()));
            Assert.assertEquals("*", target);
            Assert.assertEquals("*", request.getPathInfo());
        }
    });
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).method(HttpMethod.OPTIONS).path("*").timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
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) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Test(org.junit.Test)

Example 68 with Request

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

the class HttpClientTest method testOPTIONSWithRelativeRedirect.

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

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            if ("*".equals(target)) {
                // Be nasty and send a relative redirect.
                // Code 303 will change the method to GET.
                response.setStatus(HttpStatus.SEE_OTHER_303);
                response.setHeader("Location", "/");
            }
        }
    });
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).method(HttpMethod.OPTIONS).path("*").timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
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) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Test(org.junit.Test)

Example 69 with Request

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

the class PublicAnnouncementManagerIntegrationTest method testAnnouncementsWork.

@Test
public void testAnnouncementsWork() throws Exception {
    final AtomicReference<byte[]> requestBody = new AtomicReference<>();
    try (HttpdForTests httpd = new HttpdForTests()) {
        httpd.addHandler(new AbstractHandler() {

            @Override
            public void handle(String s, Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
                httpServletResponse.setStatus(200);
                request.setHandled(true);
                if (request.getUri().getPath().equals("/status.php")) {
                    return;
                }
                requestBody.set(ByteStreams.toByteArray(httpServletRequest.getInputStream()));
                FrontendRequest thriftRequest = new FrontendRequest();
                ThriftUtil.deserialize(ThriftProtocol.BINARY, requestBody.get(), thriftRequest);
                assertTrue("Request should contain the repository.", thriftRequest.getAnnouncementRequest().getRepository().equals(REPOSITORY));
                try (DataOutputStream out = new DataOutputStream(httpServletResponse.getOutputStream())) {
                    Announcement announcement = new Announcement();
                    announcement.setErrorMessage(ERROR_MSG);
                    announcement.setSolutionMessage(SOLUTION_MSG);
                    AnnouncementResponse announcementResponse = new AnnouncementResponse();
                    announcementResponse.setAnnouncements(ImmutableList.of(announcement));
                    FrontendResponse frontendResponse = new FrontendResponse();
                    frontendResponse.setType(FrontendRequestType.ANNOUNCEMENT);
                    frontendResponse.setAnnouncementResponse(announcementResponse);
                    out.write(ThriftUtil.serialize(ThriftProtocol.BINARY, frontendResponse));
                }
            }
        });
        httpd.start();
        Clock clock = new DefaultClock();
        BuckEventBus eventBus = BuckEventBusFactory.newInstance(clock);
        ExecutionEnvironment executionEnvironment = new DefaultExecutionEnvironment(ImmutableMap.copyOf(System.getenv()), System.getProperties());
        BuckConfig buckConfig = new FakeBuckConfig.Builder().setSections(ImmutableMap.of("log", ImmutableMap.of("slb_server_pool", "http://localhost:" + httpd.getRootUri().getPort()))).build();
        TestConsole console = new TestConsole();
        SuperConsoleEventBusListener listener = new SuperConsoleEventBusListener(new SuperConsoleConfig(FakeBuckConfig.builder().build()), console, clock, /* verbosity */
        TestResultSummaryVerbosity.of(false, false), executionEnvironment, Optional.empty(), Locale.US, logPath, TimeZone.getTimeZone("UTC"));
        eventBus.register(listener);
        PublicAnnouncementManager manager = new PublicAnnouncementManager(clock, eventBus, listener, REPOSITORY, new RemoteLogBuckConfig(buckConfig), MoreExecutors.newDirectExecutorService());
        manager.getAndPostAnnouncements();
        Optional<String> announcements = listener.getPublicAnnouncements();
        assertEquals("The header and the message", announcements.get(), "**-------------------------------**\n" + "**- Sticky Public Announcements -**\n" + "**-------------------------------**\n" + "** This is the error message. This is the solution message.");
    }
}
Also used : BuckEventBus(com.facebook.buck.event.BuckEventBus) RemoteLogBuckConfig(com.facebook.buck.util.network.RemoteLogBuckConfig) DefaultExecutionEnvironment(com.facebook.buck.util.environment.DefaultExecutionEnvironment) ExecutionEnvironment(com.facebook.buck.util.environment.ExecutionEnvironment) Announcement(com.facebook.buck.distributed.thrift.Announcement) AnnouncementResponse(com.facebook.buck.distributed.thrift.AnnouncementResponse) DataOutputStream(java.io.DataOutputStream) DefaultClock(com.facebook.buck.timing.DefaultClock) Clock(com.facebook.buck.timing.Clock) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) RemoteLogBuckConfig(com.facebook.buck.util.network.RemoteLogBuckConfig) BuckConfig(com.facebook.buck.cli.BuckConfig) FakeBuckConfig(com.facebook.buck.cli.FakeBuckConfig) DefaultClock(com.facebook.buck.timing.DefaultClock) FrontendRequest(com.facebook.buck.distributed.thrift.FrontendRequest) HttpdForTests(com.facebook.buck.testutil.integration.HttpdForTests) DefaultExecutionEnvironment(com.facebook.buck.util.environment.DefaultExecutionEnvironment) Request(org.eclipse.jetty.server.Request) FrontendRequest(com.facebook.buck.distributed.thrift.FrontendRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) FrontendResponse(com.facebook.buck.distributed.thrift.FrontendResponse) TestConsole(com.facebook.buck.testutil.TestConsole) Test(org.junit.Test)

Example 70 with Request

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

the class IndexHandlerDelegateTest method testIndexHandlerReturnsCorrectTemplateAndData.

@Test
public void testIndexHandlerReturnsCorrectTemplateAndData() throws IOException {
    Request baseRequest = EasyMock.createMock(Request.class);
    EasyMock.replay(baseRequest);
    IndexHandlerDelegate indexHandlerDelegate = new IndexHandlerDelegate();
    assertEquals("buck.index", indexHandlerDelegate.getTemplateForRequest(baseRequest));
    SoyMapData templateData = indexHandlerDelegate.getDataForRequest(baseRequest);
    assertTrue(templateData.getKeys().isEmpty());
    EasyMock.verify(baseRequest);
}
Also used : SoyMapData(com.google.template.soy.data.SoyMapData) Request(org.eclipse.jetty.server.Request) Test(org.junit.Test)

Aggregations

Request (org.eclipse.jetty.server.Request)297 HttpServletRequest (javax.servlet.http.HttpServletRequest)250 HttpServletResponse (javax.servlet.http.HttpServletResponse)225 Test (org.junit.Test)177 IOException (java.io.IOException)169 ServletException (javax.servlet.ServletException)150 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)147 CountDownLatch (java.util.concurrent.CountDownLatch)66 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)64 InterruptedIOException (java.io.InterruptedIOException)45 Server (org.eclipse.jetty.server.Server)41 InputStream (java.io.InputStream)37 AtomicReference (java.util.concurrent.atomic.AtomicReference)35 ByteArrayInputStream (java.io.ByteArrayInputStream)27 Response (org.eclipse.jetty.client.api.Response)27 Result (org.eclipse.jetty.client.api.Result)27 ServletInputStream (javax.servlet.ServletInputStream)24 ByteArrayOutputStream (java.io.ByteArrayOutputStream)23 ServletOutputStream (javax.servlet.ServletOutputStream)23 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)23