<commit_before>/*
 * Copyright 2014-2015 Red Hat, Inc. and/or its affiliates
 * and other contributors as indicated by the @author tags.
 *
 * Licensed 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.hawkular.metrics.rest

import static org.junit.Assert.assertEquals

import static org.hawkular.metrics.api.jaxrs.util.Headers.ACCESS_CONTROL_ALLOW_CREDENTIALS;
import static org.hawkular.metrics.api.jaxrs.util.Headers.ACCESS_CONTROL_ALLOW_HEADERS;
import static org.hawkular.metrics.api.jaxrs.util.Headers.ACCESS_CONTROL_ALLOW_METHODS;
import static org.hawkular.metrics.api.jaxrs.util.Headers.ACCESS_CONTROL_ALLOW_ORIGIN;
import static org.hawkular.metrics.api.jaxrs.util.Headers.ACCESS_CONTROL_MAX_AGE;
import static org.hawkular.metrics.api.jaxrs.util.Headers.ACCESS_CONTROL_REQUEST_METHOD;
import static org.hawkular.metrics.api.jaxrs.util.Headers.DEFAULT_CORS_ACCESS_CONTROL_ALLOW_HEADERS;
import static org.hawkular.metrics.api.jaxrs.util.Headers.DEFAULT_CORS_ACCESS_CONTROL_ALLOW_METHODS;
import static org.hawkular.metrics.api.jaxrs.util.Headers.ORIGIN;

import org.junit.Test

class CORSITest extends RESTTest {

    @Test
    void testOptionsWithOrigin() {
        def testOrigin = "TestOrigin123";

        def response = hawkularMetrics.options(path: "ping",
         headers: [
            ACCESS_CONTROL_REQUEST_METHOD: "POST",
            ORIGIN: testOrigin
         ]);

         //print headers
         response.headers.each { println "${it.name} : ${it.value}" }

         //Expected a 200 because this is be a pre-flight call that should never reach the resource router
         assertEquals(200, response.status)
         assertEquals(DEFAULT_CORS_ACCESS_CONTROL_ALLOW_METHODS, response.headers[ACCESS_CONTROL_ALLOW_METHODS].value);
         assertEquals(DEFAULT_CORS_ACCESS_CONTROL_ALLOW_HEADERS, response.headers[ACCESS_CONTROL_ALLOW_HEADERS].value);
         assertEquals(testOrigin, response.headers[ACCESS_CONTROL_ALLOW_ORIGIN].value);
         assertEquals("true", response.headers[ACCESS_CONTROL_ALLOW_CREDENTIALS].value);
         assertEquals((72 * 60 * 60)+"", response.headers[ACCESS_CONTROL_MAX_AGE].value);
    }

    @Test
    void testOptionsWithoutOrigin() {
        def response = hawkularMetrics.options(path: "ping",
         headers: [
            ACCESS_CONTROL_REQUEST_METHOD: "GET",
         ]);

         //print headers
         response.headers.each { println "${it.name} : ${it.value}" }

         //Expected a 200 because this is be a pre-flight call that should never reach the resource router
         assertEquals(200, response.status)
         assertEquals(DEFAULT_CORS_ACCESS_CONTROL_ALLOW_METHODS, response.headers[ACCESS_CONTROL_ALLOW_METHODS].value);
         assertEquals(DEFAULT_CORS_ACCESS_CONTROL_ALLOW_HEADERS, response.headers[ACCESS_CONTROL_ALLOW_HEADERS].value);
         assertEquals("*", response.headers[ACCESS_CONTROL_ALLOW_ORIGIN].value);
         assertEquals("true", response.headers[ACCESS_CONTROL_ALLOW_CREDENTIALS].value);
         assertEquals((72 * 60 * 60)+"", response.headers[ACCESS_CONTROL_MAX_AGE].value);
    }
}
<commit_msg>[HWKMETRICS-225] Enhance test output from the CORS integration tests.<commit_after>/*
 * Copyright 2014-2015 Red Hat, Inc. and/or its affiliates
 * and other contributors as indicated by the @author tags.
 *
 * Licensed 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.hawkular.metrics.rest

import static org.junit.Assert.assertEquals

import static org.hawkular.metrics.api.jaxrs.util.Headers.ACCESS_CONTROL_ALLOW_CREDENTIALS;
import static org.hawkular.metrics.api.jaxrs.util.Headers.ACCESS_CONTROL_ALLOW_HEADERS;
import static org.hawkular.metrics.api.jaxrs.util.Headers.ACCESS_CONTROL_ALLOW_METHODS;
import static org.hawkular.metrics.api.jaxrs.util.Headers.ACCESS_CONTROL_ALLOW_ORIGIN;
import static org.hawkular.metrics.api.jaxrs.util.Headers.ACCESS_CONTROL_MAX_AGE;
import static org.hawkular.metrics.api.jaxrs.util.Headers.ACCESS_CONTROL_REQUEST_METHOD;
import static org.hawkular.metrics.api.jaxrs.util.Headers.DEFAULT_CORS_ACCESS_CONTROL_ALLOW_HEADERS;
import static org.hawkular.metrics.api.jaxrs.util.Headers.DEFAULT_CORS_ACCESS_CONTROL_ALLOW_METHODS;
import static org.hawkular.metrics.api.jaxrs.util.Headers.ORIGIN;

import org.junit.Test

class CORSITest extends RESTTest {

    @Test
    void testOptionsWithOrigin() {
        def testOrigin = "TestOrigin123";

        def response = hawkularMetrics.options(path: "gauges/test/data",
         headers: [
            ACCESS_CONTROL_REQUEST_METHOD: "POST",
            ORIGIN: testOrigin
         ]);

         //print headers
         println("==== Request Headers = Start  ====");
         response.headers.each { println "${it.name} : ${it.value}" }
         println("==== Request Headers = End    ====");

         //Expected a 200 because this is be a pre-flight call that should never reach the resource router
         assertEquals(200, response.status)
         assertEquals(DEFAULT_CORS_ACCESS_CONTROL_ALLOW_METHODS, response.headers[ACCESS_CONTROL_ALLOW_METHODS].value);
         assertEquals(DEFAULT_CORS_ACCESS_CONTROL_ALLOW_HEADERS, response.headers[ACCESS_CONTROL_ALLOW_HEADERS].value);
         assertEquals(testOrigin, response.headers[ACCESS_CONTROL_ALLOW_ORIGIN].value);
         assertEquals("true", response.headers[ACCESS_CONTROL_ALLOW_CREDENTIALS].value);
         assertEquals((72 * 60 * 60)+"", response.headers[ACCESS_CONTROL_MAX_AGE].value);
    }

    @Test
    void testOptionsWithoutOrigin() {
        def response = hawkularMetrics.options(path: "gauges/test/data",
         headers: [
            ACCESS_CONTROL_REQUEST_METHOD: "GET",
         ]);

         //print headers
         println("==== Request Headers = Start  ====");
         response.headers.each { println "${it.name} : ${it.value}" }
         println("==== Request Headers = End    ====");

         //Expected a 200 because this is be a pre-flight call that should never reach the resource router
         assertEquals(200, response.status)
         assertEquals(DEFAULT_CORS_ACCESS_CONTROL_ALLOW_METHODS, response.headers[ACCESS_CONTROL_ALLOW_METHODS].value);
         assertEquals(DEFAULT_CORS_ACCESS_CONTROL_ALLOW_HEADERS, response.headers[ACCESS_CONTROL_ALLOW_HEADERS].value);
         assertEquals("*", response.headers[ACCESS_CONTROL_ALLOW_ORIGIN].value);
         assertEquals("true", response.headers[ACCESS_CONTROL_ALLOW_CREDENTIALS].value);
         assertEquals((72 * 60 * 60)+"", response.headers[ACCESS_CONTROL_MAX_AGE].value);
    }
}

{'fraction_non_alphanumeric': 0.09575596816976127, 'fraction_numerical': 0.010742705570291777, 'mean_word_length': 3.997349237905898, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4045555', 'n_tokens_mistral': 2220, 'n_tokens_neox': 2123, 'n_words': 490}