use of org.elasticsearch.client.ResponseException in project elasticsearch by elastic.
the class ESClientYamlSuiteTestCase method initAndResetContext.
@Before
public void initAndResetContext() throws IOException {
if (restTestExecutionContext == null) {
assert adminExecutionContext == null;
assert blacklistPathMatchers == null;
String[] specPaths = resolvePathsProperty(REST_TESTS_SPEC, DEFAULT_SPEC_PATH);
ClientYamlSuiteRestSpec restSpec = null;
FileSystem fileSystem = getFileSystem();
// ... and you can't close() the default filesystem
try {
restSpec = ClientYamlSuiteRestSpec.parseFrom(fileSystem, DEFAULT_SPEC_PATH, specPaths);
} finally {
IOUtils.close(fileSystem);
}
validateSpec(restSpec);
List<HttpHost> hosts = getClusterHosts();
RestClient restClient = client();
Version infoVersion = readVersionsFromInfo(restClient, hosts.size());
Version esVersion;
try {
Tuple<Version, Version> versionVersionTuple = readVersionsFromCatNodes(restClient);
esVersion = versionVersionTuple.v1();
Version masterVersion = versionVersionTuple.v2();
logger.info("initializing yaml client, minimum es version: [{}] master version: [{}] hosts: {}", esVersion, masterVersion, hosts);
} catch (ResponseException ex) {
if (ex.getResponse().getStatusLine().getStatusCode() == 403) {
logger.warn("Fallback to simple info '/' request, _cat/nodes is not authorized");
esVersion = infoVersion;
logger.info("initializing yaml client, minimum es version: [{}] hosts: {}", esVersion, hosts);
} else {
throw ex;
}
}
ClientYamlTestClient clientYamlTestClient = new ClientYamlTestClient(restSpec, restClient, hosts, esVersion);
restTestExecutionContext = new ClientYamlTestExecutionContext(clientYamlTestClient, randomizeContentType());
adminExecutionContext = new ClientYamlTestExecutionContext(clientYamlTestClient, false);
String[] blacklist = resolvePathsProperty(REST_TESTS_BLACKLIST, null);
blacklistPathMatchers = new ArrayList<>();
for (String entry : blacklist) {
blacklistPathMatchers.add(new BlacklistedPathPatternMatcher(entry));
}
}
assert restTestExecutionContext != null;
assert adminExecutionContext != null;
assert blacklistPathMatchers != null;
// admin context must be available for @After always, regardless of whether the test was blacklisted
adminExecutionContext.clear();
//skip test if it matches one of the blacklist globs
for (BlacklistedPathPatternMatcher blacklistedPathMatcher : blacklistPathMatchers) {
String testPath = testCandidate.getSuitePath() + "/" + testCandidate.getTestSection().getName();
assumeFalse("[" + testCandidate.getTestPath() + "] skipped, reason: blacklisted", blacklistedPathMatcher.isSuffixMatch(testPath));
}
restTestExecutionContext.clear();
//skip test if the whole suite (yaml file) is disabled
assumeFalse(testCandidate.getSetupSection().getSkipSection().getSkipMessage(testCandidate.getSuitePath()), testCandidate.getSetupSection().getSkipSection().skip(restTestExecutionContext.esVersion()));
//skip test if the whole suite (yaml file) is disabled
assumeFalse(testCandidate.getTeardownSection().getSkipSection().getSkipMessage(testCandidate.getSuitePath()), testCandidate.getTeardownSection().getSkipSection().skip(restTestExecutionContext.esVersion()));
//skip test if test section is disabled
assumeFalse(testCandidate.getTestSection().getSkipSection().getSkipMessage(testCandidate.getTestPath()), testCandidate.getTestSection().getSkipSection().skip(restTestExecutionContext.esVersion()));
}
use of org.elasticsearch.client.ResponseException in project elasticsearch by elastic.
the class DetailedErrorsDisabledIT method testThatErrorTraceParamReturns400.
public void testThatErrorTraceParamReturns400() throws IOException {
ResponseException e = expectThrows(ResponseException.class, () -> getRestClient().performRequest("DELETE", "/", Collections.singletonMap("error_trace", "true")));
Response response = e.getResponse();
assertThat(response.getHeader("Content-Type"), is("application/json; charset=UTF-8"));
assertThat(EntityUtils.toString(e.getResponse().getEntity()), containsString("\"error\":\"error traces in responses are disabled.\""));
assertThat(response.getStatusLine().getStatusCode(), is(400));
}
use of org.elasticsearch.client.ResponseException in project elasticsearch by elastic.
the class ResponseHeaderPluginIT method testThatSettingHeadersWorks.
public void testThatSettingHeadersWorks() throws IOException {
ensureGreen();
try {
getRestClient().performRequest("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"));
}
Response authResponse = getRestClient().performRequest("GET", "/_protected", new BasicHeader("Secret", "password"));
assertThat(authResponse.getStatusLine().getStatusCode(), equalTo(200));
assertThat(authResponse.getHeader("Secret"), equalTo("granted"));
}
use of org.elasticsearch.client.ResponseException in project elasticsearch by elastic.
the class CorsRegexIT method testThatPreFlightRequestReturnsNullOnNonMatch.
public void testThatPreFlightRequestReturnsNullOnNonMatch() throws IOException {
try {
getRestClient().performRequest("OPTIONS", "/", new BasicHeader("User-Agent", "Mozilla Bar"), new BasicHeader("Origin", "http://evil-host:9200"), new BasicHeader("Access-Control-Request-Method", "GET"));
fail("request should have failed");
} catch (ResponseException e) {
Response response = e.getResponse();
// a rejected origin gets a FORBIDDEN - 403
assertThat(response.getStatusLine().getStatusCode(), is(403));
assertThat(response.getHeader("Access-Control-Allow-Origin"), nullValue());
assertThat(response.getHeader("Access-Control-Allow-Methods"), nullValue());
}
}
Aggregations