@@ -40,16 +40,17 @@ protected Filter customFilter() {
40
40
return null ;
41
41
}
42
42
43
- void sendResponse (HttpExchange exchange , int status , String response ) throws IOException {
43
+ static void sendResponse (HttpExchange exchange , int status , String response ) throws IOException {
44
44
sendResponse (exchange , status , Collections .emptyMap (), response );
45
45
}
46
46
47
- void sendResponse (HttpExchange exchange , int status , Map <String , String > headers )
47
+ static void sendResponse (HttpExchange exchange , int status , Map <String , String > headers )
48
48
throws IOException {
49
49
sendResponse (exchange , status , headers , "" );
50
50
}
51
51
52
- void sendResponse (HttpExchange exchange , int status , Map <String , String > headers , String response )
52
+ static void sendResponse (
53
+ HttpExchange exchange , int status , Map <String , String > headers , String response )
53
54
throws IOException {
54
55
55
56
byte [] bytes = response .getBytes (Charset .defaultCharset ());
@@ -178,27 +179,8 @@ protected HttpServer setupServer() throws IOException {
178
179
}
179
180
180
181
// Make sure user decorators see spans.
181
- Filter spanFilter =
182
- new Filter () {
182
+ Filter spanFilter = new SpanFilter ();
183
183
184
- @ Override
185
- public void doFilter (HttpExchange exchange , Chain chain ) throws IOException {
186
-
187
- if (!Span .current ().getSpanContext ().isValid ()) {
188
- // Return an invalid code to fail any assertion
189
-
190
- exchange .sendResponseHeaders (601 , -1 );
191
- }
192
- exchange .getResponseHeaders ().set ("decoratingfunction" , "ok" );
193
- exchange .getResponseHeaders ().set ("decoratinghttpservicefunction" , "ok" );
194
- chain .doFilter (exchange );
195
- }
196
-
197
- @ Override
198
- public String description () {
199
- return "test" ;
200
- }
201
- };
202
184
contexts .forEach (ctx -> ctx .getFilters ().add (spanFilter ));
203
185
server .start ();
204
186
@@ -226,4 +208,30 @@ protected void configure(HttpServerTestOptions options) {
226
208
options .setTestPathParam (false );
227
209
options .setTestException (false );
228
210
}
211
+
212
+ static class SpanFilter extends Filter {
213
+
214
+ @ Override
215
+ public void doFilter (HttpExchange exchange , Chain chain ) throws IOException {
216
+
217
+ if (!Span .current ().getSpanContext ().isValid ()) {
218
+ // Return an invalid code to fail any assertion
219
+
220
+ exchange .sendResponseHeaders (601 , -1 );
221
+ }
222
+ exchange .getResponseHeaders ().set ("decoratingfunction" , "ok" );
223
+ exchange .getResponseHeaders ().set ("decoratinghttpservicefunction" , "ok" );
224
+ chain .doFilter (exchange );
225
+
226
+ // server will hang if nothing is sent
227
+ if (exchange .getResponseCode () == -1 ) {
228
+ sendResponse (exchange , 500 , "nothing" );
229
+ }
230
+ }
231
+
232
+ @ Override
233
+ public String description () {
234
+ return "test" ;
235
+ }
236
+ }
229
237
}
0 commit comments