use of org.elasticsearch.test.rest.FakeRestRequest in project elasticsearch by elastic.
the class BytesRestResponseTests method testNullThrowable.
public void testNullThrowable() throws Exception {
RestRequest request = new FakeRestRequest();
RestChannel channel = new SimpleExceptionRestChannel(request);
BytesRestResponse response = new BytesRestResponse(channel, null);
String text = response.content().utf8ToString();
assertThat(text, containsString("\"error\":\"unknown\""));
assertThat(text, not(containsString("error_trace")));
}
use of org.elasticsearch.test.rest.FakeRestRequest in project elasticsearch by elastic.
the class BytesRestResponseTests method testResponseWhenInternalServerError.
public void testResponseWhenInternalServerError() throws IOException {
final RestRequest request = new FakeRestRequest();
final RestChannel channel = new DetailedExceptionRestChannel(request);
final BytesRestResponse response = new BytesRestResponse(channel, new ElasticsearchException("simulated"));
assertNotNull(response.content());
final String content = response.content().utf8ToString();
assertThat(content, containsString("\"type\":\"exception\""));
assertThat(content, containsString("\"reason\":\"simulated\""));
assertThat(content, containsString("\"status\":" + 500));
}
use of org.elasticsearch.test.rest.FakeRestRequest in project elasticsearch by elastic.
the class RestControllerTests method testDispatchWithContentStream.
public void testDispatchWithContentStream() {
final String mimeType = randomFrom("application/json", "application/smile");
String content = randomAsciiOfLengthBetween(1, BREAKER_LIMIT.bytesAsInt());
FakeRestRequest fakeRestRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withContent(new BytesArray(content), null).withPath("/foo").withHeaders(Collections.singletonMap("Content-Type", Collections.singletonList(mimeType))).build();
AssertingChannel channel = new AssertingChannel(fakeRestRequest, true, RestStatus.OK);
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.test.rest.FakeRestRequest in project elasticsearch by elastic.
the class RestControllerTests method testDispatchWithContentStreamNoContentType.
public void testDispatchWithContentStreamNoContentType() {
FakeRestRequest fakeRestRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withContent(new BytesArray("{}"), null).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.test.rest.FakeRestRequest in project elasticsearch by elastic.
the class RestControllerTests method testDispatchUnsupportedContentType.
public void testDispatchUnsupportedContentType() {
FakeRestRequest fakeRestRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withContent(new BytesArray("{}"), null).withPath("/").withHeaders(Collections.singletonMap("Content-Type", Collections.singletonList("application/x-www-form-urlencoded"))).build();
AssertingChannel channel = new AssertingChannel(fakeRestRequest, true, RestStatus.NOT_ACCEPTABLE);
assertFalse(channel.getSendResponseCalled());
restController.dispatchRequest(fakeRestRequest, channel, new ThreadContext(Settings.EMPTY));
assertTrue(channel.getSendResponseCalled());
}
Aggregations