Search in sources :

Example 26 with FakeRestRequest

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

Example 27 with FakeRestRequest

use of org.elasticsearch.test.rest.FakeRestRequest 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());
}
Also used : 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 28 with FakeRestRequest

use of org.elasticsearch.test.rest.FakeRestRequest in project elasticsearch by elastic.

the class RestControllerTests method testDispatchDoesNotRequireContentTypeForRequestsWithoutContent.

public void testDispatchDoesNotRequireContentTypeForRequestsWithoutContent() {
    FakeRestRequest fakeRestRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).build();
    AssertingChannel channel = new AssertingChannel(fakeRestRequest, true, RestStatus.OK);
    assertFalse(channel.getSendResponseCalled());
    restController.dispatchRequest(fakeRestRequest, channel, new ThreadContext(Settings.EMPTY));
    assertTrue(channel.getSendResponseCalled());
}
Also used : ThreadContext(org.elasticsearch.common.util.concurrent.ThreadContext) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest)

Example 29 with FakeRestRequest

use of org.elasticsearch.test.rest.FakeRestRequest in project elasticsearch by elastic.

the class MultiSearchRequestTests method testSimpleAddWithCarriageReturn.

public void testSimpleAddWithCarriageReturn() throws Exception {
    final String requestContent = "{\"index\":\"test\", \"ignore_unavailable\" : true, \"expand_wildcards\" : \"open,closed\"}}\r\n" + "{\"query\" : {\"match_all\" :{}}}\r\n";
    FakeRestRequest restRequest = new FakeRestRequest.Builder(xContentRegistry()).withContent(new BytesArray(requestContent), XContentType.JSON).build();
    MultiSearchRequest request = RestMultiSearchAction.parseRequest(restRequest, true);
    assertThat(request.requests().size(), equalTo(1));
    assertThat(request.requests().get(0).indices()[0], equalTo("test"));
    assertThat(request.requests().get(0).indicesOptions(), equalTo(IndicesOptions.fromOptions(true, true, true, true, IndicesOptions.strictExpandOpenAndForbidClosed())));
    assertThat(request.requests().get(0).types().length, equalTo(0));
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest)

Example 30 with FakeRestRequest

use of org.elasticsearch.test.rest.FakeRestRequest in project elasticsearch by elastic.

the class DedicatedClusterSnapshotRestoreIT method testThatSensitiveRepositorySettingsAreNotExposed.

public void testThatSensitiveRepositorySettingsAreNotExposed() throws Exception {
    Settings nodeSettings = Settings.builder().put().build();
    logger.info("--> start two nodes");
    internalCluster().startNodes(2, nodeSettings);
    // Register mock repositories
    client().admin().cluster().preparePutRepository("test-repo").setType("mock").setSettings(Settings.builder().put("location", randomRepoPath()).put(MockRepository.Plugin.USERNAME_SETTING.getKey(), "notsecretusername").put(MockRepository.Plugin.PASSWORD_SETTING.getKey(), "verysecretpassword")).get();
    NodeClient nodeClient = internalCluster().getInstance(NodeClient.class);
    RestGetRepositoriesAction getRepoAction = new RestGetRepositoriesAction(nodeSettings, mock(RestController.class), internalCluster().getInstance(SettingsFilter.class));
    RestRequest getRepoRequest = new FakeRestRequest();
    getRepoRequest.params().put("repository", "test-repo");
    final CountDownLatch getRepoLatch = new CountDownLatch(1);
    final AtomicReference<AssertionError> getRepoError = new AtomicReference<>();
    getRepoAction.handleRequest(getRepoRequest, new AbstractRestChannel(getRepoRequest, true) {

        @Override
        public void sendResponse(RestResponse response) {
            try {
                assertThat(response.content().utf8ToString(), containsString("notsecretusername"));
                assertThat(response.content().utf8ToString(), not(containsString("verysecretpassword")));
            } catch (AssertionError ex) {
                getRepoError.set(ex);
            }
            getRepoLatch.countDown();
        }
    }, nodeClient);
    assertTrue(getRepoLatch.await(1, TimeUnit.SECONDS));
    if (getRepoError.get() != null) {
        throw getRepoError.get();
    }
    RestClusterStateAction clusterStateAction = new RestClusterStateAction(nodeSettings, mock(RestController.class), internalCluster().getInstance(SettingsFilter.class));
    RestRequest clusterStateRequest = new FakeRestRequest();
    final CountDownLatch clusterStateLatch = new CountDownLatch(1);
    final AtomicReference<AssertionError> clusterStateError = new AtomicReference<>();
    clusterStateAction.handleRequest(clusterStateRequest, new AbstractRestChannel(clusterStateRequest, true) {

        @Override
        public void sendResponse(RestResponse response) {
            try {
                assertThat(response.content().utf8ToString(), containsString("notsecretusername"));
                assertThat(response.content().utf8ToString(), not(containsString("verysecretpassword")));
            } catch (AssertionError ex) {
                clusterStateError.set(ex);
            }
            clusterStateLatch.countDown();
        }
    }, nodeClient);
    assertTrue(clusterStateLatch.await(1, TimeUnit.SECONDS));
    if (clusterStateError.get() != null) {
        throw clusterStateError.get();
    }
}
Also used : NodeClient(org.elasticsearch.client.node.NodeClient) RestClusterStateAction(org.elasticsearch.rest.action.admin.cluster.RestClusterStateAction) RestResponse(org.elasticsearch.rest.RestResponse) RestGetRepositoriesAction(org.elasticsearch.rest.action.admin.cluster.RestGetRepositoriesAction) RestController(org.elasticsearch.rest.RestController) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) SettingsFilter(org.elasticsearch.common.settings.SettingsFilter) RestRequest(org.elasticsearch.rest.RestRequest) FakeRestRequest(org.elasticsearch.test.rest.FakeRestRequest) AbstractRestChannel(org.elasticsearch.rest.AbstractRestChannel) Settings(org.elasticsearch.common.settings.Settings)

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