use of org.elasticsearch.common.util.concurrent.ThreadContext in project elasticsearch by elastic.
the class RestControllerTests method testDispatchBadRequestUnknownCause.
public void testDispatchBadRequestUnknownCause() {
final FakeRestRequest fakeRestRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).build();
final AssertingChannel channel = new AssertingChannel(fakeRestRequest, true, RestStatus.BAD_REQUEST);
restController.dispatchBadRequest(fakeRestRequest, channel, new ThreadContext(Settings.EMPTY), null);
assertTrue(channel.getSendResponseCalled());
assertThat(channel.getRestResponse().content().utf8ToString(), containsString("unknown cause"));
}
use of org.elasticsearch.common.util.concurrent.ThreadContext in project elasticsearch by elastic.
the class RestControllerTests method testDispatchBadRequest.
public void testDispatchBadRequest() {
final FakeRestRequest fakeRestRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).build();
final AssertingChannel channel = new AssertingChannel(fakeRestRequest, true, RestStatus.BAD_REQUEST);
restController.dispatchBadRequest(fakeRestRequest, channel, new ThreadContext(Settings.EMPTY), randomBoolean() ? new IllegalStateException("bad request") : new Throwable("bad request"));
assertTrue(channel.getSendResponseCalled());
assertThat(channel.getRestResponse().content().utf8ToString(), containsString("bad request"));
}
use of org.elasticsearch.common.util.concurrent.ThreadContext in project elasticsearch by elastic.
the class RestControllerTests method testDispatchRequiresContentTypeForRequestsWithContent.
public void testDispatchRequiresContentTypeForRequestsWithContent() {
String content = randomAsciiOfLengthBetween(1, BREAKER_LIMIT.bytesAsInt());
TestRestRequest request = new TestRestRequest("/", content, null);
AssertingChannel channel = new AssertingChannel(request, true, RestStatus.NOT_ACCEPTABLE);
restController = new RestController(Settings.builder().put(HttpTransportSettings.SETTING_HTTP_CONTENT_TYPE_REQUIRED.getKey(), true).build(), Collections.emptySet(), null, null, circuitBreakerService);
restController.registerHandler(RestRequest.Method.GET, "/", (r, c, client) -> c.sendResponse(new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY)));
assertFalse(channel.getSendResponseCalled());
restController.dispatchRequest(request, channel, new ThreadContext(Settings.EMPTY));
assertTrue(channel.getSendResponseCalled());
}
use of org.elasticsearch.common.util.concurrent.ThreadContext in project elasticsearch by elastic.
the class RestControllerTests method testNonStreamingXContentCausesErrorResponse.
public void testNonStreamingXContentCausesErrorResponse() throws IOException {
FakeRestRequest fakeRestRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withContent(YamlXContent.contentBuilder().startObject().endObject().bytes(), XContentType.YAML).withPath("/foo").build();
AssertingChannel channel = new AssertingChannel(fakeRestRequest, true, RestStatus.NOT_ACCEPTABLE);
restController.registerHandler(RestRequest.Method.GET, "/foo", new RestHandler() {
@Override
public void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception {
channel.sendResponse(new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY));
}
@Override
public boolean supportsContentStream() {
return true;
}
});
assertFalse(channel.getSendResponseCalled());
restController.dispatchRequest(fakeRestRequest, channel, new ThreadContext(Settings.EMPTY));
assertTrue(channel.getSendResponseCalled());
}
use of org.elasticsearch.common.util.concurrent.ThreadContext in project elasticsearch by elastic.
the class RestControllerTests method testDispatchRequestAddsAndFreesBytesOnlyOnceOnError.
public void testDispatchRequestAddsAndFreesBytesOnlyOnceOnError() {
int contentLength = BREAKER_LIMIT.bytesAsInt();
String content = randomAsciiOfLength(contentLength);
// we will produce an error in the rest handler and one more when sending the error response
TestRestRequest request = new TestRestRequest("/error", content, XContentType.JSON);
ExceptionThrowingChannel channel = new ExceptionThrowingChannel(request, true);
restController.dispatchRequest(request, channel, new ThreadContext(Settings.EMPTY));
assertEquals(0, inFlightRequestsBreaker.getTrippedCount());
assertEquals(0, inFlightRequestsBreaker.getUsed());
}
Aggregations