Search in sources :

Example 41 with HttpServletResponse

use of javax.servlet.http.HttpServletResponse in project jetty.project by eclipse.

the class RequestTest method testSessionAfterRedirect.

@Test
public void testSessionAfterRedirect() throws Exception {
    Handler handler = new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            response.sendRedirect("/foo");
            try {
                request.getSession(true);
                fail("Session should not be created after response committed");
            } catch (IllegalStateException e) {
            //expected
            } catch (Exception e) {
                fail("Session creation after response commit should throw IllegalStateException");
            }
        }
    };
    _server.stop();
    _server.setHandler(handler);
    _server.start();
    String response = _connector.getResponse("GET / HTTP/1.1\n" + "Host: myhost\n" + "Connection: close\n" + "\n");
    assertThat(response, Matchers.containsString(" 302 Found"));
    assertThat(response, Matchers.containsString("Location: http://myhost/foo"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) HttpServletResponse(javax.servlet.http.HttpServletResponse) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) ServletException(javax.servlet.ServletException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Test(org.junit.Test)

Example 42 with HttpServletResponse

use of javax.servlet.http.HttpServletResponse in project jetty.project by eclipse.

the class RequestTest method testQueryAfterRead.

@Test
public void testQueryAfterRead() throws Exception {
    Handler handler = new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            Reader reader = request.getReader();
            String in = IO.toString(reader);
            String param = request.getParameter("param");
            byte[] b = ("read='" + in + "' param=" + param + "\n").getBytes(StandardCharsets.UTF_8);
            response.setContentLength(b.length);
            response.getOutputStream().write(b);
            response.flushBuffer();
        }
    };
    _server.stop();
    _server.setHandler(handler);
    _server.start();
    String request = "POST /?param=right HTTP/1.1\r\n" + "Host: whatever\r\n" + "Content-Type: application/x-www-form-urlencoded\r\n" + "Content-Length: " + 11 + "\r\n" + "Connection: close\r\n" + "\r\n" + "param=wrong\r\n";
    String responses = _connector.getResponse(request);
    assertTrue(responses.indexOf("read='param=wrong' param=right") > 0);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) HttpServletResponse(javax.servlet.http.HttpServletResponse) Reader(java.io.Reader) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Test(org.junit.Test)

Example 43 with HttpServletResponse

use of javax.servlet.http.HttpServletResponse in project jetty.project by eclipse.

the class HttpServerTestBase method testInterruptedRequest.

@Test
public void testInterruptedRequest() throws Exception {
    final AtomicBoolean fourBytesRead = new AtomicBoolean(false);
    final AtomicBoolean earlyEOFException = new AtomicBoolean(false);
    configureServer(new AbstractHandler.ErrorDispatchHandler() {

        @Override
        public void doNonErrorHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            int contentLength = request.getContentLength();
            ServletInputStream inputStream = request.getInputStream();
            for (int i = 0; i < contentLength; i++) {
                try {
                    inputStream.read();
                } catch (EofException e) {
                    earlyEOFException.set(true);
                    throw new QuietServletException(e);
                }
                if (i == 3)
                    fourBytesRead.set(true);
            }
        }
    });
    StringBuffer request = new StringBuffer("GET / HTTP/1.0\n");
    request.append("Host: localhost\n");
    request.append("Content-length: 6\n\n");
    request.append("foo");
    Socket client = newSocket(_serverURI.getHost(), _serverURI.getPort());
    OutputStream os = client.getOutputStream();
    os.write(request.toString().getBytes());
    os.flush();
    client.shutdownOutput();
    String response = readResponse(client);
    client.close();
    assertThat("response contains 500", response, Matchers.containsString(" 500 "));
    assertThat("The 4th byte (-1) has not been passed to the handler", fourBytesRead.get(), is(false));
    assertThat("EofException has been caught", earlyEOFException.get(), is(true));
}
Also used : EofException(org.eclipse.jetty.io.EofException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) 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) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ServletInputStream(javax.servlet.ServletInputStream) Socket(java.net.Socket) Test(org.junit.Test)

Example 44 with HttpServletResponse

use of javax.servlet.http.HttpServletResponse in project jetty.project by eclipse.

the class HttpTrailersTest method testHugeTrailer.

@Test
public void testHugeTrailer() throws Exception {
    start(new AbstractHandler.ErrorDispatchHandler() {

        @Override
        protected void doNonErrorHandle(String target, Request jettyRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            jettyRequest.setHandled(true);
            try {
                // EOF will not be reached because of the huge trailer.
                ServletInputStream input = jettyRequest.getInputStream();
                while (true) {
                    int read = input.read();
                    if (read < 0)
                        break;
                }
                Assert.fail();
            } catch (IOException x) {
            // Expected.
            }
        }
    });
    char[] huge = new char[1024 * 1024];
    Arrays.fill(huge, 'X');
    try (Socket client = new Socket("localhost", connector.getLocalPort())) {
        client.setSoTimeout(5000);
        try {
            String request = "" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + "Transfer-Encoding: chunked\r\n" + "\r\n" + "0\r\n" + "Trailer: " + new String(huge) + "\r\n" + "\r\n";
            OutputStream output = client.getOutputStream();
            output.write(request.getBytes(StandardCharsets.UTF_8));
            output.flush();
            HttpTester.Response response = HttpTester.parseResponse(HttpTester.from(client.getInputStream()));
            Assert.assertNotNull(response);
            Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
        } catch (Exception e) {
        // May be thrown if write fails and error handling is aborted
        }
    }
}
Also used : OutputStream(java.io.OutputStream) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) HttpTester(org.eclipse.jetty.http.HttpTester) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServletInputStream(javax.servlet.ServletInputStream) Socket(java.net.Socket) Test(org.junit.Test)

Example 45 with HttpServletResponse

use of javax.servlet.http.HttpServletResponse in project jetty.project by eclipse.

the class HttpVersionCustomizerTest method testCustomizeHttpVersion.

@Test
public void testCustomizeHttpVersion() throws Exception {
    Server server = new Server();
    HttpConfiguration httpConfig = new HttpConfiguration();
    httpConfig.addCustomizer((connector, config, request) -> request.setHttpVersion(HttpVersion.HTTP_1_1));
    ServerConnector connector = new ServerConnector(server, new HttpConnectionFactory(httpConfig));
    server.addConnector(connector);
    server.setHandler(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            response.setStatus(500);
            Assert.assertEquals(HttpVersion.HTTP_1_1.asString(), request.getProtocol());
            response.setStatus(200);
            response.getWriter().println("OK");
        }
    });
    server.start();
    try {
        try (SocketChannel socket = SocketChannel.open(new InetSocketAddress("localhost", connector.getLocalPort()))) {
            HttpTester.Request request = HttpTester.newRequest();
            request.setVersion(HttpVersion.HTTP_1_0);
            socket.write(request.generate());
            HttpTester.Response response = HttpTester.parseResponse(HttpTester.from(socket));
            Assert.assertNotNull(response);
            Assert.assertThat(response.getStatus(), Matchers.equalTo(HttpStatus.OK_200));
        }
    } finally {
        server.stop();
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpTester(org.eclipse.jetty.http.HttpTester) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Test(org.junit.Test)

Aggregations

HttpServletResponse (javax.servlet.http.HttpServletResponse)3285 HttpServletRequest (javax.servlet.http.HttpServletRequest)2624 Test (org.junit.Test)1229 IOException (java.io.IOException)873 ServletException (javax.servlet.ServletException)671 PrintWriter (java.io.PrintWriter)286 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)250 FilterChain (javax.servlet.FilterChain)243 Request (org.eclipse.jetty.server.Request)223 HashMap (java.util.HashMap)204 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)195 HttpServlet (javax.servlet.http.HttpServlet)188 CountDownLatch (java.util.concurrent.CountDownLatch)183 ServletOutputStream (javax.servlet.ServletOutputStream)183 StringWriter (java.io.StringWriter)180 Test (org.testng.annotations.Test)176 HttpSession (javax.servlet.http.HttpSession)175 ServletContext (javax.servlet.ServletContext)173 InputStream (java.io.InputStream)141 Map (java.util.Map)137