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