Search in sources :

Example 16 with ThreadContext

use of org.elasticsearch.common.util.concurrent.ThreadContext in project elasticsearch by elastic.

the class DeprecationLoggerTests method testCanRemoveThreadContext.

public void testCanRemoveThreadContext() throws IOException {
    final String expected = "testCanRemoveThreadContext";
    final String unexpected = "testCannotRemoveThreadContext";
    try (ThreadContext threadContext = new ThreadContext(Settings.EMPTY)) {
        DeprecationLogger.setThreadContext(threadContext);
        logger.deprecated(expected);
        {
            final Map<String, List<String>> responseHeaders = threadContext.getResponseHeaders();
            final List<String> responses = responseHeaders.get("Warning");
            assertThat(responses, hasSize(1));
            assertThat(responses.get(0), warningValueMatcher);
            assertThat(responses.get(0), containsString(expected));
        }
        DeprecationLogger.removeThreadContext(threadContext);
        logger.deprecated(unexpected);
        {
            final Map<String, List<String>> responseHeaders = threadContext.getResponseHeaders();
            final List<String> responses = responseHeaders.get("Warning");
            assertThat(responses, hasSize(1));
            assertThat(responses.get(0), warningValueMatcher);
            assertThat(responses.get(0), containsString(expected));
            assertThat(responses.get(0), not(containsString(unexpected)));
        }
    }
}
Also used : ThreadContext(org.elasticsearch.common.util.concurrent.ThreadContext) List(java.util.List) Matchers.containsString(org.hamcrest.Matchers.containsString) Map(java.util.Map)

Example 17 with ThreadContext

use of org.elasticsearch.common.util.concurrent.ThreadContext in project elasticsearch by elastic.

the class QueryParseContextTests method beforeTest.

@Before
public void beforeTest() throws IOException {
    this.threadContext = new ThreadContext(Settings.EMPTY);
    DeprecationLogger.setThreadContext(threadContext);
}
Also used : ThreadContext(org.elasticsearch.common.util.concurrent.ThreadContext) Before(org.junit.Before)

Example 18 with ThreadContext

use of org.elasticsearch.common.util.concurrent.ThreadContext in project elasticsearch by elastic.

the class RestControllerTests method testDispatchFailsWithPlainText.

public void testDispatchFailsWithPlainText() {
    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("text/plain"))).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));
        }
    });
    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 19 with ThreadContext

use of org.elasticsearch.common.util.concurrent.ThreadContext in project elasticsearch by elastic.

the class RestControllerTests method testUnknownContentWithContentStream.

public void testUnknownContentWithContentStream() {
    FakeRestRequest fakeRestRequest = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withContent(new BytesArray("aaaabbbbb"), null).withPath("/foo").withHeaders(Collections.singletonMap("Content-Type", Collections.singletonList("foo/bar"))).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)

Example 20 with ThreadContext

use of org.elasticsearch.common.util.concurrent.ThreadContext in project elasticsearch by elastic.

the class RestControllerTests method testDispatchWorksWithNewlineDelimitedJson.

public void testDispatchWorksWithNewlineDelimitedJson() {
    final String mimeType = "application/x-ndjson";
    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)

Aggregations

ThreadContext (org.elasticsearch.common.util.concurrent.ThreadContext)33 Matchers.containsString (org.hamcrest.Matchers.containsString)15 IOException (java.io.IOException)12 FakeRestRequest (org.elasticsearch.test.rest.FakeRestRequest)12 NodeClient (org.elasticsearch.client.node.NodeClient)8 BytesArray (org.elasticsearch.common.bytes.BytesArray)8 List (java.util.List)7 ActionListener (org.elasticsearch.action.ActionListener)4 Before (org.junit.Before)4 HashSet (java.util.HashSet)3 Map (java.util.Map)3 Settings (org.elasticsearch.common.settings.Settings)3 TransportAddress (org.elasticsearch.common.transport.TransportAddress)3 HttpServerTransport (org.elasticsearch.http.HttpServerTransport)3 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)2 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)2 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)2 HashMap (java.util.HashMap)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Strings.collectionToDelimitedString (org.elasticsearch.common.Strings.collectionToDelimitedString)2