Skip to content

Commit ec46cf1

Browse files
committed
Fix tests + back to using dispatchBadRequest to emit correct headers and trace information
1 parent ad506a0 commit ec46cf1

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

server/src/main/java/org/elasticsearch/http/AbstractHttpServerTransport.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import org.elasticsearch.core.RefCounted;
3939
import org.elasticsearch.rest.RestChannel;
4040
import org.elasticsearch.rest.RestRequest;
41-
import org.elasticsearch.rest.RestResponse;
4241
import org.elasticsearch.tasks.Task;
4342
import org.elasticsearch.telemetry.tracing.Tracer;
4443
import org.elasticsearch.threadpool.ThreadPool;
@@ -488,7 +487,7 @@ void dispatchRequest(final RestRequest restRequest, final RestChannel channel, f
488487
populatePerRequestThreadContext(restRequest, threadContext);
489488
} catch (Exception e) {
490489
try {
491-
channel.sendResponse(new RestResponse(channel, e));
490+
dispatcher.dispatchBadRequest(channel, threadContext, e);
492491
} catch (Exception inner) {
493492
inner.addSuppressed(e);
494493
logger.error(() -> "failed to send failure response for uri [" + restRequest.uri() + "]", inner);

server/src/test/java/org/elasticsearch/http/AbstractHttpServerTransportTests.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,6 @@ public void dispatchBadRequest(final RestChannel channel, final ThreadContext th
355355
}
356356

357357
};
358-
// the set of headers to copy
359-
Set<RestHeaderDefinition> headers = Set.of(new RestHeaderDefinition(Task.TRACE_PARENT_HTTP_HEADER, false));
360358
// sample request headers to test with
361359
Map<String, List<String>> restHeaders = new HashMap<>();
362360
restHeaders.put(Task.TRACE_PARENT_HTTP_HEADER, Collections.singletonList(traceParentValue));
@@ -397,7 +395,7 @@ public HttpStats stats() {
397395

398396
@Override
399397
protected void populatePerRequestThreadContext(RestRequest restRequest, ThreadContext threadContext) {
400-
getFakeActionModule(headers).copyRequestHeadersToThreadContext(restRequest.getHttpRequest(), threadContext);
398+
getFakeActionModule(Set.of()).copyRequestHeadersToThreadContext(restRequest.getHttpRequest(), threadContext);
401399
}
402400
}
403401
) {

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/netty4/SecurityNetty4HttpServerTransportTests.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,21 @@ public void testHttpHeaderAuthnBypassHeaderValidator() throws Exception {
370370
new NetworkService(List.of()),
371371
testThreadPool,
372372
xContentRegistry(),
373-
new NullDispatcher(),
373+
new HttpServerTransport.Dispatcher() {
374+
@Override
375+
public void dispatchRequest(RestRequest request, RestChannel channel, ThreadContext threadContext) {
376+
fail("Request should not be dispatched");
377+
}
378+
379+
@Override
380+
public void dispatchBadRequest(RestChannel channel, ThreadContext threadContext, Throwable cause) {
381+
try {
382+
channel.sendResponse(new RestResponse(channel, (Exception) cause));
383+
} catch (IOException e) {
384+
fail(e, "Unexpected exception dispatching bad request");
385+
}
386+
}
387+
},
374388
randomClusterSettings(),
375389
new SharedGroupFactory(settings),
376390
Tracer.NOOP,

0 commit comments

Comments
 (0)