summaryrefslogtreecommitdiff
path: root/core/src/test/java/org/elasticsearch/rest/RestControllerTests.java
blob: d6e1a97ac8fd322313d9d33eb726dceb28b0afe4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.elasticsearch.rest;

import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.rest.FakeRestRequest;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import static org.hamcrest.CoreMatchers.equalTo;

public class RestControllerTests extends ESTestCase {

    public void testRegisterRelevantHeaders() throws InterruptedException {

        final RestController restController = new RestController(Settings.EMPTY);

        int iterations = randomIntBetween(1, 5);

        Set<String> headers = new HashSet<>();
        ExecutorService executorService = Executors.newFixedThreadPool(iterations);
        for (int i = 0; i < iterations; i++) {
            int headersCount = randomInt(10);
            final Set<String> newHeaders = new HashSet<>();
            for (int j = 0; j < headersCount; j++) {
                String usefulHeader = randomRealisticUnicodeOfLengthBetween(1, 30);
                newHeaders.add(usefulHeader);
            }
            headers.addAll(newHeaders);

            executorService.submit((Runnable) () -> restController.registerRelevantHeaders(newHeaders.toArray(new String[newHeaders.size()])));
        }

        executorService.shutdown();
        assertThat(executorService.awaitTermination(1, TimeUnit.SECONDS), equalTo(true));
        String[] relevantHeaders = restController.relevantHeaders().toArray(new String[restController.relevantHeaders().size()]);
        assertThat(relevantHeaders.length, equalTo(headers.size()));

        Arrays.sort(relevantHeaders);
        String[] headersArray = new String[headers.size()];
        headersArray = headers.toArray(headersArray);
        Arrays.sort(headersArray);
        assertThat(relevantHeaders, equalTo(headersArray));
    }

    public void testApplyRelevantHeaders() {
        final ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
        final RestController restController = new RestController(Settings.EMPTY) {
            @Override
            boolean checkRequestParameters(RestRequest request, RestChannel channel) {
                return true;
            }

            @Override
            void executeHandler(RestRequest request, RestChannel channel) throws Exception {
                assertEquals("true", threadContext.getHeader("header.1"));
                assertEquals("true", threadContext.getHeader("header.2"));
                assertNull(threadContext.getHeader("header.3"));

            }
        };
        threadContext.putHeader("header.3", "true");
        restController.registerRelevantHeaders("header.1", "header.2");
        Map<String, String> restHeaders = new HashMap<>();
        restHeaders.put("header.1", "true");
        restHeaders.put("header.2", "true");
        restHeaders.put("header.3", "false");
        restController.dispatchRequest(new FakeRestRequest(restHeaders), null, threadContext);
        assertNull(threadContext.getHeader("header.1"));
        assertNull(threadContext.getHeader("header.2"));
        assertEquals("true", threadContext.getHeader("header.3"));
    }
}