use of org.opensearch.client.Response in project OpenSearch by opensearch-project.
the class ResponseHeaderPluginIT method testThatSettingHeadersWorks.
public void testThatSettingHeadersWorks() throws IOException {
ensureGreen();
try {
getRestClient().performRequest(new Request("GET", "/_protected"));
fail("request should have failed");
} catch (ResponseException e) {
Response response = e.getResponse();
assertThat(response.getStatusLine().getStatusCode(), equalTo(401));
assertThat(response.getHeader("Secret"), equalTo("required"));
}
Request request = new Request("GET", "/_protected");
RequestOptions.Builder options = request.getOptions().toBuilder();
options.addHeader("Secret", "password");
request.setOptions(options);
Response authResponse = getRestClient().performRequest(request);
assertThat(authResponse.getStatusLine().getStatusCode(), equalTo(200));
assertThat(authResponse.getHeader("Secret"), equalTo("granted"));
}
use of org.opensearch.client.Response in project OpenSearch by opensearch-project.
the class SearchRestCancellationIT method verifyCancellationDuringQueryPhase.
void verifyCancellationDuringQueryPhase(String searchAction, Request searchRequest) throws Exception {
Map<String, String> nodeIdToName = readNodesInfo();
List<ScriptedBlockPlugin> plugins = initBlockFactory();
indexTestData();
CountDownLatch latch = new CountDownLatch(1);
AtomicReference<Exception> error = new AtomicReference<>();
Cancellable cancellable = getRestClient().performRequestAsync(searchRequest, new ResponseListener() {
@Override
public void onSuccess(Response response) {
latch.countDown();
}
@Override
public void onFailure(Exception exception) {
error.set(exception);
latch.countDown();
}
});
awaitForBlock(plugins);
cancellable.cancel();
ensureSearchTaskIsCancelled(searchAction, nodeIdToName::get);
disableBlocks(plugins);
latch.await();
assertThat(error.get(), instanceOf(CancellationException.class));
}
use of org.opensearch.client.Response in project OpenSearch by opensearch-project.
the class SystemIndexRestIT method testSystemIndexAccessBlockedByDefault.
public void testSystemIndexAccessBlockedByDefault() throws Exception {
// create index
{
Request putDocRequest = new Request("POST", "/_sys_index_test/add_doc/42");
Response resp = getRestClient().performRequest(putDocRequest);
assertThat(resp.getStatusLine().getStatusCode(), equalTo(201));
}
// make sure the system index now exists
assertBusy(() -> {
Request searchRequest = new Request("GET", "/" + SystemIndexTestPlugin.SYSTEM_INDEX_NAME + "/_count");
searchRequest.setOptions(expectWarnings("this request accesses system indices: [" + SystemIndexTestPlugin.SYSTEM_INDEX_NAME + "], but in a future major version, direct access to system indices will be prevented by default"));
// Disallow no indices to cause an exception if the flag above doesn't work
searchRequest.addParameter("allow_no_indices", "false");
searchRequest.setJsonEntity("{\"query\": {\"match\": {\"some_field\": \"some_value\"}}}");
final Response searchResponse = getRestClient().performRequest(searchRequest);
assertThat(searchResponse.getStatusLine().getStatusCode(), is(200));
Map<String, Object> responseMap = entityAsMap(searchResponse);
assertThat(responseMap, hasKey("count"));
assertThat(responseMap.get("count"), equalTo(1));
});
// And with a partial wildcard
assertDeprecationWarningOnAccess(".test-*", SystemIndexTestPlugin.SYSTEM_INDEX_NAME);
// And with a total wildcard
assertDeprecationWarningOnAccess(randomFrom("*", "_all"), SystemIndexTestPlugin.SYSTEM_INDEX_NAME);
// Try to index a doc directly
{
String expectedWarning = "this request accesses system indices: [" + SystemIndexTestPlugin.SYSTEM_INDEX_NAME + "], but in a " + "future major version, direct access to system indices will be prevented by default";
Request putDocDirectlyRequest = new Request("PUT", "/" + SystemIndexTestPlugin.SYSTEM_INDEX_NAME + "/_doc/43");
putDocDirectlyRequest.setJsonEntity("{\"some_field\": \"some_other_value\"}");
putDocDirectlyRequest.setOptions(expectWarnings(expectedWarning));
Response response = getRestClient().performRequest(putDocDirectlyRequest);
assertThat(response.getStatusLine().getStatusCode(), equalTo(201));
}
}
use of org.opensearch.client.Response in project OpenSearch by opensearch-project.
the class SystemIndexRestIT method assertDeprecationWarningOnAccess.
private void assertDeprecationWarningOnAccess(String queryPattern, String warningIndexName) throws IOException {
String expectedWarning = "this request accesses system indices: [" + warningIndexName + "], but in a " + "future major version, direct access to system indices will be prevented by default";
Request searchRequest = new Request("GET", "/" + queryPattern + randomFrom("/_count", "/_search"));
searchRequest.setJsonEntity("{\"query\": {\"match\": {\"some_field\": \"some_value\"}}}");
// Disallow no indices to cause an exception if this resolves to zero indices, so that we're sure it resolved the index
searchRequest.addParameter("allow_no_indices", "false");
if (!assertedWarnings.contains(expectedWarning)) {
searchRequest.setOptions(expectWarnings(expectedWarning));
assertedWarnings.add(expectedWarning);
}
Response response = getRestClient().performRequest(searchRequest);
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
}
use of org.opensearch.client.Response in project OpenSearch by opensearch-project.
the class VerifyVersionConstantsIT method testLuceneVersionConstant.
public void testLuceneVersionConstant() throws IOException, ParseException {
final Response response = client().performRequest(new Request("GET", "/"));
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
final ObjectPath objectPath = ObjectPath.createFromResponse(response);
final String opensearchVersionString = objectPath.evaluate("version.number").toString();
final Version opensearchVersion = Version.fromString(opensearchVersionString.replace("-SNAPSHOT", ""));
final String luceneVersionString = objectPath.evaluate("version.lucene_version").toString();
final org.apache.lucene.util.Version luceneVersion = org.apache.lucene.util.Version.parse(luceneVersionString);
assertThat(opensearchVersion.luceneVersion, equalTo(luceneVersion));
}
Aggregations