Search in sources :

Example 6 with FakeRestRequest

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")));
}
Also used : FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) Matchers.containsString(org.hamcrest.Matchers.containsString) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest)

Example 7 with FakeRestRequest

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));
}
Also used : FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) ElasticsearchException(org.elasticsearch.ElasticsearchException) Matchers.containsString(org.hamcrest.Matchers.containsString) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest)

Example 8 with FakeRestRequest

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());
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) NodeClient(org.elasticsearch.client.node.NodeClient) ThreadContext(org.elasticsearch.common.util.concurrent.ThreadContext) Matchers.containsString(org.hamcrest.Matchers.containsString) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) IOException(java.io.IOException) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest)

Example 9 with FakeRestRequest

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());
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) NodeClient(org.elasticsearch.client.node.NodeClient) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) ThreadContext(org.elasticsearch.common.util.concurrent.ThreadContext) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) IOException(java.io.IOException)

Example 10 with FakeRestRequest

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());
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) ThreadContext(org.elasticsearch.common.util.concurrent.ThreadContext) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest)

Aggregations

FakeRestRequest (org.elasticsearch.test.rest.FakeRestRequest)30 IOException (java.io.IOException)11 Matchers.containsString (org.hamcrest.Matchers.containsString)11 ThreadContext (org.elasticsearch.common.util.concurrent.ThreadContext)10 NodeClient (org.elasticsearch.client.node.NodeClient)7 BytesArray (org.elasticsearch.common.bytes.BytesArray)7 ElasticsearchException (org.elasticsearch.ElasticsearchException)6 SearchPhaseExecutionException (org.elasticsearch.action.search.SearchPhaseExecutionException)6 ParsingException (org.elasticsearch.common.ParsingException)6 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)6 RemoteTransportException (org.elasticsearch.transport.RemoteTransportException)6 FileNotFoundException (java.io.FileNotFoundException)5 ElasticsearchStatusException (org.elasticsearch.ElasticsearchStatusException)5 ResourceAlreadyExistsException (org.elasticsearch.ResourceAlreadyExistsException)5 ResourceNotFoundException (org.elasticsearch.ResourceNotFoundException)5 BytesRestResponse (org.elasticsearch.rest.BytesRestResponse)4 RestRequest (org.elasticsearch.rest.RestRequest)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Settings (org.elasticsearch.common.settings.Settings)3 Table (org.elasticsearch.common.Table)2