|
| 1 | +// |
| 2 | +// ======================================================================== |
| 3 | +// Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others. |
| 4 | +// |
| 5 | +// This program and the accompanying materials are made available under the |
| 6 | +// terms of the Eclipse Public License v. 2.0 which is available at |
| 7 | +// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| 8 | +// which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| 9 | +// |
| 10 | +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| 11 | +// ======================================================================== |
| 12 | +// |
| 13 | + |
| 14 | +package org.eclipse.jetty.servlet; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | +import java.util.concurrent.LinkedBlockingQueue; |
| 18 | +import javax.servlet.ServletException; |
| 19 | +import javax.servlet.http.HttpServletRequest; |
| 20 | +import javax.servlet.http.HttpServletResponse; |
| 21 | + |
| 22 | +import org.eclipse.jetty.client.HttpClient; |
| 23 | +import org.eclipse.jetty.client.api.ContentResponse; |
| 24 | +import org.eclipse.jetty.server.Handler; |
| 25 | +import org.eclipse.jetty.server.Request; |
| 26 | +import org.eclipse.jetty.server.Server; |
| 27 | +import org.eclipse.jetty.server.ServerConnector; |
| 28 | +import org.eclipse.jetty.server.handler.AbstractHandler; |
| 29 | +import org.eclipse.jetty.server.handler.DefaultHandler; |
| 30 | +import org.eclipse.jetty.server.handler.HandlerCollection; |
| 31 | +import org.eclipse.jetty.server.handler.ResourceHandler; |
| 32 | +import org.eclipse.jetty.server.handler.gzip.GzipHandler; |
| 33 | +import org.eclipse.jetty.toolchain.test.jupiter.WorkDir; |
| 34 | +import org.eclipse.jetty.util.component.LifeCycle; |
| 35 | +import org.eclipse.jetty.util.resource.PathResource; |
| 36 | +import org.junit.jupiter.api.AfterEach; |
| 37 | +import org.junit.jupiter.api.Test; |
| 38 | + |
| 39 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 40 | +import static org.hamcrest.Matchers.containsString; |
| 41 | +import static org.hamcrest.Matchers.is; |
| 42 | + |
| 43 | +/** |
| 44 | + * Tests of behavior of GzipHandler when Request.isHandled() or Response.isCommitted() is true |
| 45 | + */ |
| 46 | +public class GzipHandlerIsHandledTest |
| 47 | +{ |
| 48 | + public WorkDir workDir; |
| 49 | + |
| 50 | + private Server server; |
| 51 | + private HttpClient client; |
| 52 | + public LinkedBlockingQueue<String> events = new LinkedBlockingQueue<>(); |
| 53 | + |
| 54 | + public void startServer(Handler rootHandler) throws Exception |
| 55 | + { |
| 56 | + server = new Server(); |
| 57 | + ServerConnector connector = new ServerConnector(server); |
| 58 | + connector.setPort(0); |
| 59 | + server.addConnector(connector); |
| 60 | + |
| 61 | + server.setHandler(rootHandler); |
| 62 | + server.start(); |
| 63 | + |
| 64 | + client = new HttpClient(); |
| 65 | + client.start(); |
| 66 | + } |
| 67 | + |
| 68 | + @AfterEach |
| 69 | + public void tearDown() |
| 70 | + { |
| 71 | + LifeCycle.stop(client); |
| 72 | + LifeCycle.stop(server); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testRequest() throws Exception |
| 77 | + { |
| 78 | + HandlerCollection handlers = new HandlerCollection(); |
| 79 | + |
| 80 | + ResourceHandler resourceHandler = new ResourceHandler(); |
| 81 | + resourceHandler.setBaseResource(new PathResource(workDir.getPath())); |
| 82 | + resourceHandler.setDirectoriesListed(true); |
| 83 | + resourceHandler.setHandler(new EventHandler(events, "ResourceHandler")); |
| 84 | + |
| 85 | + GzipHandler gzipHandler = new GzipHandler(); |
| 86 | + gzipHandler.setMinGzipSize(32); |
| 87 | + gzipHandler.setHandler(new EventHandler(events, "GzipHandler-wrapped-handler")); |
| 88 | + |
| 89 | + handlers.setHandlers(new Handler[]{resourceHandler, gzipHandler, new DefaultHandler()}); |
| 90 | + |
| 91 | + startServer(handlers); |
| 92 | + |
| 93 | + ContentResponse response = client.GET(server.getURI().resolve("/")); |
| 94 | + assertThat("response.status", response.getStatus(), is(200)); |
| 95 | + // we should have received a directory listing from the ResourceHandler |
| 96 | + assertThat("response.content", response.getContentAsString(), containsString("Directory: /")); |
| 97 | + // resource handler should have handled the request |
| 98 | + // the gzip handler and default handlers should have been executed, seeing as this is a HandlerCollection |
| 99 | + // but the gzip handler should not have acted on the request, as the response is committed |
| 100 | + assertThat("One event should have been recorded", events.size(), is(1)); |
| 101 | + // the event handler should see the request.isHandled = true |
| 102 | + // and response.isCommitted = true as the gzip handler didn't really do anything due to these |
| 103 | + // states and let the wrapped handler (the EventHandler in this case) make the call on what it should do. |
| 104 | + assertThat("Event indicating that GzipHandler-wrapped-handler ran", events.remove(), is("GzipHandler-wrapped-handler [request.isHandled=true, response.isCommitted=true]")); |
| 105 | + } |
| 106 | + |
| 107 | + private static class EventHandler extends AbstractHandler |
| 108 | + { |
| 109 | + private final LinkedBlockingQueue<String> events; |
| 110 | + private final String action; |
| 111 | + |
| 112 | + public EventHandler(LinkedBlockingQueue<String> events, String action) |
| 113 | + { |
| 114 | + this.events = events; |
| 115 | + this.action = action; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException |
| 120 | + { |
| 121 | + events.offer(String.format("%s [request.isHandled=%b, response.isCommitted=%b]", action, baseRequest.isHandled(), response.isCommitted())); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments