Search in sources :

Example 11 with NodeClient

use of org.elasticsearch.client.node.NodeClient 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 12 with NodeClient

use of org.elasticsearch.client.node.NodeClient in project elasticsearch by elastic.

the class RestControllerTests method testApplyRelevantHeaders.

public void testApplyRelevantHeaders() throws Exception {
    final ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
    Set<String> headers = new HashSet<>(Arrays.asList("header.1", "header.2"));
    final RestController restController = new RestController(Settings.EMPTY, headers, null, null, circuitBreakerService);
    Map<String, List<String>> restHeaders = new HashMap<>();
    restHeaders.put("header.1", Collections.singletonList("true"));
    restHeaders.put("header.2", Collections.singletonList("true"));
    restHeaders.put("header.3", Collections.singletonList("false"));
    restController.dispatchRequest(new FakeRestRequest.Builder(xContentRegistry()).withHeaders(restHeaders).build(), null, null, threadContext, (RestRequest request, RestChannel channel, NodeClient client) -> {
        assertEquals("true", threadContext.getHeader("header.1"));
        assertEquals("true", threadContext.getHeader("header.2"));
        assertNull(threadContext.getHeader("header.3"));
    });
    // the rest controller relies on the caller to stash the context, so we should expect these values here as we didn't stash the
    // context in this test
    assertEquals("true", threadContext.getHeader("header.1"));
    assertEquals("true", threadContext.getHeader("header.2"));
    assertNull(threadContext.getHeader("header.3"));
}
Also used : NodeClient(org.elasticsearch.client.node.NodeClient) HashMap(java.util.HashMap) ThreadContext(org.elasticsearch.common.util.concurrent.ThreadContext) Matchers.containsString(org.hamcrest.Matchers.containsString) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) List(java.util.List) HashSet(java.util.HashSet)

Example 13 with NodeClient

use of org.elasticsearch.client.node.NodeClient in project elasticsearch by elastic.

the class BaseRestHandlerTests method testMultipleUnconsumedParameters.

public void testMultipleUnconsumedParameters() throws Exception {
    final AtomicBoolean executed = new AtomicBoolean();
    BaseRestHandler handler = new BaseRestHandler(Settings.EMPTY) {

        @Override
        protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
            request.param("consumed");
            return channel -> executed.set(true);
        }
    };
    final HashMap<String, String> params = new HashMap<>();
    params.put("consumed", randomAsciiOfLength(8));
    params.put("unconsumed-first", randomAsciiOfLength(8));
    params.put("unconsumed-second", randomAsciiOfLength(8));
    RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withParams(params).build();
    RestChannel channel = new FakeRestChannel(request, randomBoolean(), 1);
    final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> handler.handleRequest(request, channel, mock(NodeClient.class)));
    assertThat(e, hasToString(containsString("request [/] contains unrecognized parameters: [unconsumed-first], [unconsumed-second]")));
    assertFalse(executed.get());
}
Also used : FakeRestChannel(org.elasticsearch.test.rest.FakeRestChannel) AbstractCatAction(org.elasticsearch.rest.action.cat.AbstractCatAction) Table(org.elasticsearch.common.Table) Set(java.util.Set) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) IOException(java.io.IOException) HashMap(java.util.HashMap) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Settings(org.elasticsearch.common.settings.Settings) NodeClient(org.elasticsearch.client.node.NodeClient) ESTestCase(org.elasticsearch.test.ESTestCase) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NodeClient(org.elasticsearch.client.node.NodeClient) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) HashMap(java.util.HashMap) FakeRestChannel(org.elasticsearch.test.rest.FakeRestChannel) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) StringContains.containsString(org.hamcrest.core.StringContains.containsString) FakeRestChannel(org.elasticsearch.test.rest.FakeRestChannel)

Example 14 with NodeClient

use of org.elasticsearch.client.node.NodeClient in project elasticsearch by elastic.

the class BaseRestHandlerTests method testCatResponseParameters.

public void testCatResponseParameters() throws Exception {
    final AtomicBoolean executed = new AtomicBoolean();
    AbstractCatAction handler = new AbstractCatAction(Settings.EMPTY) {

        @Override
        protected RestChannelConsumer doCatRequest(RestRequest request, NodeClient client) {
            return channel -> executed.set(true);
        }

        @Override
        protected void documentation(StringBuilder sb) {
        }

        @Override
        protected Table getTableWithHeader(RestRequest request) {
            return null;
        }
    };
    final HashMap<String, String> params = new HashMap<>();
    params.put("format", randomAsciiOfLength(8));
    params.put("h", randomAsciiOfLength(8));
    params.put("v", randomAsciiOfLength(8));
    params.put("ts", randomAsciiOfLength(8));
    params.put("pri", randomAsciiOfLength(8));
    params.put("bytes", randomAsciiOfLength(8));
    params.put("size", randomAsciiOfLength(8));
    params.put("time", randomAsciiOfLength(8));
    RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withParams(params).build();
    RestChannel channel = new FakeRestChannel(request, randomBoolean(), 1);
    handler.handleRequest(request, channel, mock(NodeClient.class));
    assertTrue(executed.get());
}
Also used : FakeRestChannel(org.elasticsearch.test.rest.FakeRestChannel) AbstractCatAction(org.elasticsearch.rest.action.cat.AbstractCatAction) Table(org.elasticsearch.common.Table) Set(java.util.Set) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) IOException(java.io.IOException) HashMap(java.util.HashMap) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Settings(org.elasticsearch.common.settings.Settings) NodeClient(org.elasticsearch.client.node.NodeClient) ESTestCase(org.elasticsearch.test.ESTestCase) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AbstractCatAction(org.elasticsearch.rest.action.cat.AbstractCatAction) NodeClient(org.elasticsearch.client.node.NodeClient) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) HashMap(java.util.HashMap) FakeRestChannel(org.elasticsearch.test.rest.FakeRestChannel) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) StringContains.containsString(org.hamcrest.core.StringContains.containsString) FakeRestChannel(org.elasticsearch.test.rest.FakeRestChannel)

Example 15 with NodeClient

use of org.elasticsearch.client.node.NodeClient 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)

Aggregations

NodeClient (org.elasticsearch.client.node.NodeClient)125 Settings (org.elasticsearch.common.settings.Settings)115 RestRequest (org.elasticsearch.rest.RestRequest)108 RestController (org.elasticsearch.rest.RestController)107 IOException (java.io.IOException)103 BaseRestHandler (org.elasticsearch.rest.BaseRestHandler)88 Strings (org.elasticsearch.common.Strings)61 GET (org.elasticsearch.rest.RestRequest.Method.GET)57 RestResponse (org.elasticsearch.rest.RestResponse)50 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)36 BytesRestResponse (org.elasticsearch.rest.BytesRestResponse)35 POST (org.elasticsearch.rest.RestRequest.Method.POST)34 IndicesOptions (org.elasticsearch.action.support.IndicesOptions)30 RestBuilderListener (org.elasticsearch.rest.action.RestBuilderListener)27 Set (java.util.Set)24 Table (org.elasticsearch.common.Table)24 OK (org.elasticsearch.rest.RestStatus.OK)23 AcknowledgedRestListener (org.elasticsearch.rest.action.AcknowledgedRestListener)23 RestResponseListener (org.elasticsearch.rest.action.RestResponseListener)20 Collections (java.util.Collections)18