/*
 * 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.http.netty;

import org.elasticsearch.cache.recycler.MockPageCacheRecycler;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.network.NetworkService;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.MockBigArrays;
import org.elasticsearch.http.netty.cors.CorsConfig;
import org.elasticsearch.indices.breaker.NoneCircuitBreakerService;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.threadpool.ThreadPool;
import org.jboss.netty.handler.codec.http.HttpMethod;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import static org.elasticsearch.http.netty.NettyHttpServerTransport.SETTING_CORS_ALLOW_CREDENTIALS;
import static org.elasticsearch.http.netty.NettyHttpServerTransport.SETTING_CORS_ALLOW_HEADERS;
import static org.elasticsearch.http.netty.NettyHttpServerTransport.SETTING_CORS_ALLOW_METHODS;
import static org.elasticsearch.http.netty.NettyHttpServerTransport.SETTING_CORS_ALLOW_ORIGIN;
import static org.elasticsearch.http.netty.NettyHttpServerTransport.SETTING_CORS_ENABLED;
import static org.hamcrest.Matchers.equalTo;

/**
 * Tests for the {@link NettyHttpServerTransport} class.
 */
public class NettyHttpServerTransportTests extends ESTestCase {
    private NetworkService networkService;
    private ThreadPool threadPool;
    private MockPageCacheRecycler mockPageCacheRecycler;
    private MockBigArrays bigArrays;

    @Before
    public void setup() throws Exception {
        networkService = new NetworkService(Settings.EMPTY);
        threadPool = new ThreadPool("test");
        mockPageCacheRecycler = new MockPageCacheRecycler(Settings.EMPTY, threadPool);
        bigArrays = new MockBigArrays(mockPageCacheRecycler, new NoneCircuitBreakerService());
    }

    @After
    public void shutdown() throws Exception {
        if (threadPool != null) {
            threadPool.shutdownNow();
        }
        threadPool = null;
        networkService = null;
        mockPageCacheRecycler = null;
        bigArrays = null;
    }

    @Test
    public void testCorsConfig() throws Exception {
        final Set<String> methods = new HashSet<>(Arrays.asList("get", "options", "post"));
        final Set<String> headers = new HashSet<>(Arrays.asList("Content-Type", "Content-Length"));
        final Settings settings = Settings.builder()
                                      .put(SETTING_CORS_ENABLED, true)
                                      .put(SETTING_CORS_ALLOW_ORIGIN, "*")
                                      .put(SETTING_CORS_ALLOW_METHODS, Strings.collectionToCommaDelimitedString(methods))
                                      .put(SETTING_CORS_ALLOW_HEADERS, Strings.collectionToCommaDelimitedString(headers))
                                      .put(SETTING_CORS_ALLOW_CREDENTIALS, true)
                                      .build();
        final NettyHttpServerTransport transport = new NettyHttpServerTransport(settings, networkService, bigArrays);
        final CorsConfig corsConfig = transport.getCorsConfig();
        assertThat(corsConfig.isAnyOriginSupported(), equalTo(true));
        assertThat(corsConfig.allowedRequestHeaders(), equalTo(headers));
        final Set<String> allowedRequestMethods = new HashSet<>();
        for (HttpMethod method : corsConfig.allowedRequestMethods()) {
            allowedRequestMethods.add(method.getName());
        }
        assertThat(allowedRequestMethods, equalTo(methods));
        transport.close();
    }
}

@@ -4391,11 +4391,1256 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testCorsConfigDefaults() %7B%0A        final Set%3CString%3E headers = new HashSet%3C%3E(Arrays.asList(%22X-Requested-With%22, %22Content-Type%22, %22Content-Length%22));%0A        final Set%3CString%3E methods = new HashSet%3C%3E(Arrays.asList(%22OPTIONS%22, %22HEAD%22, %22GET%22, %22POST%22, %22PUT%22, %22DELETE%22));%0A        final Settings settings = Settings.builder()%0A                                      .put(SETTING_CORS_ENABLED, true)%0A                                      .put(SETTING_CORS_ALLOW_ORIGIN, %22*%22)%0A                                      .put(SETTING_CORS_ALLOW_CREDENTIALS, true)%0A                                      .build();%0A        final NettyHttpServerTransport transport = new NettyHttpServerTransport(settings, networkService, bigArrays);%0A        final CorsConfig corsConfig = transport.getCorsConfig();%0A        assertThat(corsConfig.isAnyOriginSupported(), equalTo(true));%0A        assertThat(corsConfig.allowedRequestHeaders(), equalTo(headers));%0A        final Set%3CString%3E allowedRequestMethods = new HashSet%3C%3E();%0A        for (HttpMethod method : corsConfig.allowedRequestMethods()) %7B%0A            allowedRequestMethods.add(method.getName());%0A        %7D%0A        assertThat(allowedRequestMethods, equalTo(methods));%0A        transport.close();%0A    %7D
 %0A%7D%0A

Add test to verify http.cors.allow-headers and http.cors.allow-methods honor the defaults.