<commit_before>/*
 * Copyright 2017 <NAME>
 *
 * 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 com.jsunsoft.http;

import com.github.tomakehurst.wiremock.junit.WireMockRule;
import org.apache.http.HttpHeaders;
import org.junit.Rule;
import org.junit.Test;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.apache.http.HttpHeaders.CONTENT_TYPE;
import static org.apache.http.entity.ContentType.APPLICATION_XML;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class HttpRequestSimpleTest {
    @Rule
    public final WireMockRule wireMockRule = new WireMockRule(8080);
    private final String userAgent = "JsunSoftAgent/1.0";

    private final HttpRequest<?> httpRequestUserAgent = HttpRequestBuilder.createGet("http://localhost:8080/userAgent")
            .addDefaultHeader(HttpHeaders.USER_AGENT, userAgent).build();

    private final HttpRequest<XmlWrapper> xmlHttpRequest = HttpRequestBuilder.createPost("http://localhost:8080/xml", XmlWrapper.class)
            .addContentType(APPLICATION_XML)
            .build();

    private final HttpRequest<String> httpRequestWithoutParse = HttpRequestBuilder.createPost("http://localhost:8080/text", String.class)
            .responseDeserializer(ResponseDeserializer.ignorableDeserializer()).build();

    @Test
    public void userAgentTest() {
        wireMockRule.stubFor(get(urlEqualTo("/userAgent"))
                .withHeader("User-Agent", equalTo(userAgent))
                .willReturn(aResponse().withStatus(200)));

        assertTrue(httpRequestUserAgent.execute().isSuccess());
    }

    @Test
    public void xmlParsingTest() {
        String xmlBody = "<xml><id>1</id></xml>";
        wireMockRule.stubFor(post(urlEqualTo("/xml"))
                .withHeader(CONTENT_TYPE, equalTo(APPLICATION_XML.getMimeType()))
                .withRequestBody(equalTo(xmlBody))
                .willReturn(
                        aResponse()
                                .withBody(xmlBody)
                                .withHeader(CONTENT_TYPE, APPLICATION_XML.getMimeType())
                                .withStatus(200)
                )
        );

        ResponseHandler<XmlWrapper> responseHandler = xmlHttpRequest.executeWithBody(xmlBody);

        assertTrue(responseHandler.isSuccess());
        assertTrue(responseHandler.hasContent());

        XmlWrapper parsedXml = xmlHttpRequest.executeWithBody(xmlBody).get();<commit_msg>Corrected tests<commit_after>/*
 * Copyright 2017 <NAME>
 *
 * 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 com.jsunsoft.http;

import com.github.tomakehurst.wiremock.junit.WireMockRule;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpHeaders;
import org.junit.Rule;
import org.junit.Test;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.apache.http.HttpHeaders.CONTENT_TYPE;
import static org.apache.http.entity.ContentType.APPLICATION_XML;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class HttpRequestSimpleTest {
    private static final Log LOGGER = LogFactory.getLog(HttpRequestSimpleTest.class);
    @Rule
    public final WireMockRule wireMockRule = new WireMockRule(8080);
    private final String userAgent = "JsunSoftAgent/1.0";

    private final HttpRequest<?> httpRequestUserAgent = HttpRequestBuilder.createGet("http://localhost:8080/userAgent")
            .addDefaultHeader(HttpHeaders.USER_AGENT, userAgent).build();

    private final HttpRequest<XmlWrapper> xmlHttpRequest = HttpRequestBuilder.createPost("http://localhost:8080/xml", XmlWrapper.class)
            .addContentType(APPLICATION_XML)
            .build();

    private final HttpRequest<String> httpRequestWithoutParse = HttpRequestBuilder.createPost("http://localhost:8080/text", String.class)
            .responseDeserializer(ResponseDeserializer.ignorableDeserializer()).build();

    @Test
    public void userAgentTest() {
        wireMockRule.stubFor(get(urlEqualTo("/userAgent"))
                .withHeader("User-Agent", equalTo(userAgent))
                .willReturn(aResponse().withStatus(200)));

        assertTrue(httpRequestUserAgent.execute().isSuccess());
    }

    @Test
    public void xmlParsingTest() {
        String xmlBody = "<xml><id>1</id></xml>";
        wireMockRule.stubFor(post(urlEqualTo("/xml"))
                .withHeader(CONTENT_TYPE, equalTo(APPLICATION_XML.toString()))
                .withRequestBody(equalTo(xmlBody))
                .willReturn(
                        aResponse()
                                .withBody(xmlBody)
                                .withHeader(CONTENT_TYPE, APPLICATION_XML.toString())
                                .withStatus(200)
                )
        );

        ResponseHandler<XmlWrapper> responseHandler = xmlHttpRequest.executeWithBody(xmlBody);
        responseHandler.filter(ResponseHandler::hasNotContent).ifPassed(r -> LOGGER.info(r.getErrorText()));
        
        assertTrue(responseHandler.isSuccess());
        assertTrue(responseHandler.hasContent());

        XmlWrapper parsedXml = xmlHttpRequest.executeWithBody(xmlBody).get();
{'fraction_non_alphanumeric': 0.10197263760738148, 'fraction_numerical': 0.010499522748965956, 'mean_word_length': 3.2508451656524677, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 26}, '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': '5323082', 'n_tokens_mistral': 1713, 'n_tokens_neox': 1632, 'n_words': 368}