forked from hypertrace/javaagent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestHttpServer.java
More file actions
273 lines (236 loc) · 9.08 KB
/
TestHttpServer.java
File metadata and controls
273 lines (236 loc) · 9.08 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* Copyright The Hypertrace Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hypertrace.agent.testing;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.GZIPOutputStream;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.handler.HandlerList;
public class TestHttpServer implements AutoCloseable {
public static final String RESPONSE_HEADER_NAME = "test-response-header";
public static final String RESPONSE_HEADER_VALUE = "test-value";
private final Server server = new Server(0);
private final HandlerList handlerList = new HandlerList();
public void start() throws Exception {
HandlerList handlerList = new HandlerList();
handlerList.addHandler(new GetNoContentHandler());
handlerList.addHandler(new GetJsonHandler());
handlerList.addHandler(new PostHandler());
handlerList.addHandler(new PostRedirect());
handlerList.addHandler(new EchoHandler());
handlerList.addHandler(new GzipHandler());
handlerList.addHandler(new HeaderVerificationHandler());
server.setHandler(handlerList);
server.start();
}
public void addHandler(Handler handler) {
this.handlerList.addHandler(handler);
}
@Override
public void close() throws Exception {
server.stop();
}
public int port() {
return server.getConnectors()[0].getLocalPort();
}
static class ResponseTestHeadersHandler extends AbstractHandler {
@Override
public void handle(
String target,
Request baseRequest,
HttpServletRequest request,
HttpServletResponse response)
throws IOException {
response.setHeader(RESPONSE_HEADER_NAME, RESPONSE_HEADER_VALUE);
}
}
static class GetNoContentHandler extends ResponseTestHeadersHandler {
@Override
public void handle(
String target,
Request baseRequest,
HttpServletRequest request,
HttpServletResponse response)
throws IOException {
super.handle(target, baseRequest, request, response);
if (target.equals("/get_no_content") && "get".equalsIgnoreCase(request.getMethod())) {
response.setStatus(204);
baseRequest.setHandled(true);
}
}
}
public static class GetJsonHandler extends ResponseTestHeadersHandler {
public static final String RESPONSE_BODY = "{\"name\": \"james\"}";
@Override
public void handle(
String target,
Request baseRequest,
HttpServletRequest request,
HttpServletResponse response)
throws IOException {
super.handle(target, baseRequest, request, response);
if (target.equals("/get_json") && "get".equalsIgnoreCase(request.getMethod())) {
response.setStatus(200);
response.setContentType("aPplication/jSon");
response.getWriter().print(RESPONSE_BODY);
baseRequest.setHandled(true);
}
}
}
static class PostHandler extends ResponseTestHeadersHandler {
@Override
public void handle(
String target,
Request baseRequest,
HttpServletRequest request,
HttpServletResponse response)
throws IOException {
super.handle(target, baseRequest, request, response);
if (target.equals("/post") && "post".equalsIgnoreCase(request.getMethod())) {
// read the input stream to for sending on the client side
ServletInputStream inputStream = request.getInputStream();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
while ((nRead = inputStream.read()) != -1) {
buffer.write((byte) nRead);
}
System.out.printf("Test server received: %s\n", buffer.toString());
response.setStatus(204);
baseRequest.setHandled(true);
}
}
}
static class PostRedirect extends ResponseTestHeadersHandler {
@Override
public void handle(
String target,
Request baseRequest,
HttpServletRequest request,
HttpServletResponse response)
throws IOException {
super.handle(target, baseRequest, request, response);
if (target.equals("/post_redirect_to_get_no_content")
&& "post".equalsIgnoreCase(request.getMethod())) {
response.sendRedirect(
String.format("http://localhost:%d/get_no_content", baseRequest.getServerPort()));
baseRequest.setHandled(true);
}
}
}
static class EchoHandler extends ResponseTestHeadersHandler {
@Override
public void handle(
String target,
Request baseRequest,
HttpServletRequest request,
HttpServletResponse response)
throws IOException {
super.handle(target, baseRequest, request, response);
if (target.equals("/echo") && "post".equalsIgnoreCase(request.getMethod())) {
ServletInputStream inputStream = request.getInputStream();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
while ((nRead = inputStream.read()) != -1) {
buffer.write((byte) nRead);
}
response.setStatus(200);
response.setContentType(request.getContentType());
response.getWriter().print(buffer.toString());
baseRequest.setHandled(true);
}
}
}
public static class GzipHandler extends ResponseTestHeadersHandler {
public static final String RESPONSE_BODY = "{\"message\": \"hello\"}";
@Override
public void handle(
String target,
Request baseRequest,
HttpServletRequest request,
HttpServletResponse response)
throws IOException {
super.handle(target, baseRequest, request, response);
if (target.equals("/gzip") && "get".equalsIgnoreCase(request.getMethod())) {
response.setStatus(HttpServletResponse.SC_OK);
response.setHeader("Content-Encoding", "gzip");
response.setContentType("application/json");
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try (GZIPOutputStream gzipOut = new GZIPOutputStream(byteArrayOutputStream)) {
gzipOut.write(RESPONSE_BODY.getBytes(StandardCharsets.UTF_8));
gzipOut.finish();
}
byte[] gzipData = byteArrayOutputStream.toByteArray();
response.setContentLength(gzipData.length);
response.getOutputStream().write(gzipData);
response.getOutputStream().flush();
baseRequest.setHandled(true);
}
}
}
public static class HeaderVerificationHandler extends ResponseTestHeadersHandler {
public static final String VERIFY_HEADER_PATH = "/verify_headers";
public static final String HEADER_ECHO_PREFIX = "echo-header-";
public static final String SERVICE_NAME_HEADER_KEY = "ta-client-servicename";
public static final String RECEIVED_HEADER_JSON_FORMAT =
"{\"received_header\":\"%s\",\"header_value\":\"%s\"}";
@Override
public void handle(
String target,
Request baseRequest,
HttpServletRequest request,
HttpServletResponse response)
throws IOException {
if (target.equals(VERIFY_HEADER_PATH)) {
response.setStatus(200);
response.setContentType("application/json");
// Get all headers from the request
Map<String, String> headers = new HashMap<>();
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
String headerValue = request.getHeader(headerName);
headers.put(headerName, headerValue);
// Echo back the headers in the response with a prefix
response.setHeader(HEADER_ECHO_PREFIX + headerName, headerValue);
}
// Check specifically for our service name header
String serviceNameHeaderValue = request.getHeader(SERVICE_NAME_HEADER_KEY);
if (serviceNameHeaderValue != null) {
response
.getWriter()
.print(
String.format(
RECEIVED_HEADER_JSON_FORMAT,
SERVICE_NAME_HEADER_KEY,
serviceNameHeaderValue));
} else {
response.getWriter().print("{\"received_header\":\"none\"}");
}
baseRequest.setHandled(true);
}
}
}
}