<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The SF 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.apache.felix.hc.generalchecks;

import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doReturn;

import java.util.HashMap;
import java.util.Iterator;

import org.apache.felix.hc.api.FormattingResultLog;
import org.apache.felix.hc.api.Result;
import org.apache.felix.hc.api.ResultLog.Entry;
import org.apache.felix.hc.generalchecks.HttpRequestsCheck.RequestSpec;
import org.apache.felix.hc.generalchecks.HttpRequestsCheck.ResponseCheck.ResponseCheckResult;
import org.junit.Test;
import org.mockito.Mockito;

public class HttpRequestsCheckTest {

    FormattingResultLog log = new FormattingResultLog();

    HttpRequestsCheck.Response simple200HtmlResponse = new HttpRequestsCheck.Response(200, "OK", null, "<html><head><title>test</title></head><body>body text</body></html>", 200);

    @Test
    public void testRequestSpecParsing() throws Exception {
        
        HttpRequestsCheck.RequestSpec requestSpec = new HttpRequestsCheck.RequestSpec("/path/to/page.html");
        assertEquals("/path/to/page.html", requestSpec.url);
        assertEquals("GET", requestSpec.method);
        assertEquals(new HashMap<String,String>(), requestSpec.headers);
        assertNull(requestSpec.data);
        assertNull(requestSpec.user);
        assertNull(requestSpec.connectTimeoutInMs);
        assertNull(requestSpec.readTimeoutInMs);
        assertNull(requestSpec.proxy);
        
        requestSpec = new HttpRequestsCheck.RequestSpec("-X POST -H \"X-Test: Test\" -d \"{ 1,2,3 }\" -u admin:admin --connect-timeout 4 -m 5 --proxy http://proxy:2000 /path/to/page.html => 201");
        assertEquals("/path/to/page.html", requestSpec.url);
        assertEquals("POST", requestSpec.method);
        HashMap<String, String> expectedHeaders = new HashMap<String,String>();
        expectedHeaders.put("X-Test", "Test");
        expectedHeaders.put("Authorization", "Basic YWRtaW46YWRtaW4=");
        assertEquals(expectedHeaders, requestSpec.headers);
        assertEquals("{ 1,2,3 }", requestSpec.data);
        assertEquals("admin", requestSpec.user);
        assertEquals((Integer) 4000, requestSpec.connectTimeoutInMs);
        assertEquals((Integer) 5000, requestSpec.readTimeoutInMs);
        assertEquals("proxy:2000", requestSpec.proxy.address().toString());

    }
    
    @Test
    public void testSimpleRequestSpec() throws Exception {<commit_msg>FELIX-6472 - Fix test for java 11<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The SF 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.apache.felix.hc.generalchecks;

import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doReturn;

import java.util.HashMap;
import java.util.Iterator;

import org.apache.felix.hc.api.FormattingResultLog;
import org.apache.felix.hc.api.Result;
import org.apache.felix.hc.api.ResultLog.Entry;
import org.apache.felix.hc.generalchecks.HttpRequestsCheck.RequestSpec;
import org.apache.felix.hc.generalchecks.HttpRequestsCheck.ResponseCheck.ResponseCheckResult;
import org.junit.Test;
import org.mockito.Mockito;

public class HttpRequestsCheckTest {

    FormattingResultLog log = new FormattingResultLog();

    HttpRequestsCheck.Response simple200HtmlResponse = new HttpRequestsCheck.Response(200, "OK", null, "<html><head><title>test</title></head><body>body text</body></html>", 200);

    @Test
    public void testRequestSpecParsingPath() throws Exception {
        
        HttpRequestsCheck.RequestSpec requestSpec = new HttpRequestsCheck.RequestSpec("/path/to/page.html");
        assertEquals("/path/to/page.html", requestSpec.url);
        assertEquals("GET", requestSpec.method);
        assertEquals(new HashMap<String,String>(), requestSpec.headers);
        assertNull(requestSpec.data);
        assertNull(requestSpec.user);
        assertNull(requestSpec.connectTimeoutInMs);
        assertNull(requestSpec.readTimeoutInMs);
        assertNull(requestSpec.proxy);
    }
    
    @Test
    public void testRequestSpecParsingPost() throws Exception {
        RequestSpec requestSpec = new HttpRequestsCheck.RequestSpec("-X POST -H \"X-Test: Test\" -d \"{ 1,2,3 }\" -u admin:admin --connect-timeout 4 -m 5 --proxy http://proxy:2000 /path/to/page.html => 201");
        assertEquals("/path/to/page.html", requestSpec.url);
        assertEquals("POST", requestSpec.method);
        HashMap<String, String> expectedHeaders = new HashMap<String,String>();
        expectedHeaders.put("X-Test", "Test");
        expectedHeaders.put("Authorization", "Basic YWRtaW46YWRtaW4=");
        assertEquals(expectedHeaders, requestSpec.headers);
        assertEquals("{ 1,2,3 }", requestSpec.data);
        assertEquals("admin", requestSpec.user);
        assertEquals((Integer) 4000, requestSpec.connectTimeoutInMs);
        assertEquals((Integer) 5000, requestSpec.readTimeoutInMs);
        String proxyAddress = requestSpec.proxy.address().toString();
        assertThat(proxyAddress, containsString("proxy"));
        assertThat(proxyAddress, containsString(":2000"));
    }
    
    @Test
    public void testSimpleRequestSpec() throws Exception {
{'fraction_non_alphanumeric': 0.10860908966085404, 'fraction_numerical': 0.012632157078127145, 'mean_word_length': 4.221505376344086, 'pattern_counts': {'":': 1, '<': 25, '<?xml version=': 0, '>': 27, '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': '6966488', 'n_tokens_mistral': 2008, 'n_tokens_neox': 1954, 'n_words': 521}