Search in sources :

Example 36 with Request

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

the class ScopedHandlerTest method testTriple.

@Test
public void testTriple() throws Exception {
    Request request = new Request(null, null);
    Response response = new Response(null, null);
    TestHandler handler0 = new TestHandler("0");
    OtherHandler handlerA = new OtherHandler("A");
    TestHandler handler1 = new TestHandler("1");
    OtherHandler handlerB = new OtherHandler("B");
    TestHandler handler2 = new TestHandler("2");
    OtherHandler handlerC = new OtherHandler("C");
    handler0.setServer(new Server());
    handlerA.setServer(handler0.getServer());
    handler1.setServer(handler0.getServer());
    handlerB.setServer(handler0.getServer());
    handler2.setServer(handler0.getServer());
    handlerC.setServer(handler0.getServer());
    handler0.setHandler(handlerA);
    handlerA.setHandler(handler1);
    handler1.setHandler(handlerB);
    handlerB.setHandler(handler2);
    handler2.setHandler(handlerC);
    handler0.start();
    handler0.handle("target", request, request, response);
    handler0.stop();
    String history = _history.toString();
    assertEquals(">S0>S1>S2>W0>HA>W1>HB>W2>HC<HC<W2<HB<W1<HA<W0<S2<S1<S0", history);
}
Also used : Response(org.eclipse.jetty.server.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Server(org.eclipse.jetty.server.Server) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) Test(org.junit.Test)

Example 37 with Request

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

the class HandlerTest method testInsertWrapperBadChain.

@Test
public void testInsertWrapperBadChain() {
    HandlerWrapper a = new HandlerWrapper();
    HandlerWrapper b = new HandlerWrapper();
    HandlerWrapper c = new HandlerWrapper();
    HandlerWrapper d = new HandlerWrapper();
    a.insertHandler(d);
    b.insertHandler(c);
    c.setHandler(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        }
    });
    try {
        a.insertHandler(b);
        fail();
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), containsString("bad tail"));
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) HandlerWrapper(org.eclipse.jetty.server.handler.HandlerWrapper) Test(org.junit.Test)

Example 38 with Request

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

the class JDK9ALPNTest method testClientNotSupportingALPNServerSpeaksDefaultProtocol.

@Test
public void testClientNotSupportingALPNServerSpeaksDefaultProtocol() throws Exception {
    startServer(new AbstractHandler.ErrorDispatchHandler() {

        @Override
        protected void doNonErrorHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
        }
    });
    SslContextFactory sslContextFactory = new SslContextFactory(true);
    sslContextFactory.start();
    SSLContext sslContext = sslContextFactory.getSslContext();
    try (SSLSocket client = (SSLSocket) sslContext.getSocketFactory().createSocket("localhost", connector.getLocalPort())) {
        client.setUseClientMode(true);
        client.setSoTimeout(5000);
        client.startHandshake();
        OutputStream output = client.getOutputStream();
        output.write(("" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "\r\n" + "").getBytes(StandardCharsets.UTF_8));
        output.flush();
        InputStream input = client.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8));
        String line = reader.readLine();
        Assert.assertTrue(line.contains(" 200 "));
        while (true) {
            if (reader.readLine() == null)
                break;
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) SSLSocket(javax.net.ssl.SSLSocket) OutputStream(java.io.OutputStream) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) SSLContext(javax.net.ssl.SSLContext) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) BufferedReader(java.io.BufferedReader) Test(org.junit.Test)

Example 39 with Request

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

the class TrailersTest method testTrailersSentByClient.

@Test
public void testTrailersSentByClient() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    start(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            MetaData.Request request = (MetaData.Request) frame.getMetaData();
            Assert.assertFalse(frame.isEndStream());
            Assert.assertTrue(request.getFields().containsKey("X-Request"));
            return new Stream.Listener.Adapter() {

                @Override
                public void onHeaders(Stream stream, HeadersFrame frame) {
                    MetaData trailer = frame.getMetaData();
                    Assert.assertTrue(frame.isEndStream());
                    Assert.assertTrue(trailer.getFields().containsKey("X-Trailer"));
                    latch.countDown();
                }
            };
        }
    });
    Session session = newClient(new Session.Listener.Adapter());
    HttpFields requestFields = new HttpFields();
    requestFields.put("X-Request", "true");
    MetaData.Request request = newRequest("GET", requestFields);
    HeadersFrame requestFrame = new HeadersFrame(request, null, false);
    FuturePromise<Stream> streamPromise = new FuturePromise<>();
    session.newStream(requestFrame, streamPromise, new Stream.Listener.Adapter());
    Stream stream = streamPromise.get(5, TimeUnit.SECONDS);
    // Send the trailers.
    HttpFields trailerFields = new HttpFields();
    trailerFields.put("X-Trailer", "true");
    MetaData trailers = new MetaData(HttpVersion.HTTP_2, trailerFields);
    HeadersFrame trailerFrame = new HeadersFrame(stream.getId(), trailers, null, true);
    stream.headers(trailerFrame, Callback.NOOP);
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) FuturePromise(org.eclipse.jetty.util.FuturePromise) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) ServletInputStream(javax.servlet.ServletInputStream) Stream(org.eclipse.jetty.http2.api.Stream) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 40 with Request

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

the class ServletContextHandlerTest method testFallThrough.

@Test
public void testFallThrough() throws Exception {
    HandlerList list = new HandlerList();
    _server.setHandler(list);
    ServletContextHandler root = new ServletContextHandler(list, "/", ServletContextHandler.SESSIONS);
    ServletHandler servlet = root.getServletHandler();
    servlet.setEnsureDefaultServlet(false);
    servlet.addServletWithMapping(HelloServlet.class, "/hello/*");
    list.addHandler(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            response.sendError(404, "Fell Through");
        }
    });
    _server.start();
    String response = _connector.getResponses("GET /hello HTTP/1.0\r\n\r\n");
    Assert.assertThat(response, Matchers.containsString("200 OK"));
    response = _connector.getResponses("GET /other HTTP/1.0\r\n\r\n");
    Assert.assertThat(response, Matchers.containsString("404 Fell Through"));
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Test(org.junit.Test)

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