Search in sources :

Example 6 with ResponseException

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()));
}
Also used : ResponseException(org.elasticsearch.client.ResponseException) RestClient(org.elasticsearch.client.RestClient) ClientYamlSuiteRestSpec(org.elasticsearch.test.rest.yaml.restspec.ClientYamlSuiteRestSpec) Version(org.elasticsearch.Version) HttpHost(org.apache.http.HttpHost) FileSystem(java.nio.file.FileSystem) Before(org.junit.Before)

Example 7 with ResponseException

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));
}
Also used : Response(org.elasticsearch.client.Response) ResponseException(org.elasticsearch.client.ResponseException)

Example 8 with ResponseException

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"));
}
Also used : Response(org.elasticsearch.client.Response) ResponseException(org.elasticsearch.client.ResponseException) BasicHeader(org.apache.http.message.BasicHeader)

Example 9 with ResponseException

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());
    }
}
Also used : Response(org.elasticsearch.client.Response) ResponseException(org.elasticsearch.client.ResponseException) BasicHeader(org.apache.http.message.BasicHeader)

Aggregations

ResponseException (org.elasticsearch.client.ResponseException)9 Response (org.elasticsearch.client.Response)7 BasicHeader (org.apache.http.message.BasicHeader)4 Map (java.util.Map)2 HttpHost (org.apache.http.HttpHost)2 RestClient (org.elasticsearch.client.RestClient)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 FileSystem (java.nio.file.FileSystem)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 ContentTooLongException (org.apache.http.ContentTooLongException)1 Header (org.apache.http.Header)1 HttpEntity (org.apache.http.HttpEntity)1 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)1 Supplier (org.apache.logging.log4j.util.Supplier)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 ElasticsearchStatusException (org.elasticsearch.ElasticsearchStatusException)1