Package org.mockserver.mappers

Source Code of org.mockserver.mappers.ApacheHttpClientToMockServerResponseMapperTest

package org.mockserver.mappers;

import org.apache.http.ProtocolVersion;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicStatusLine;
import org.junit.Test;
import org.mockserver.model.Cookie;
import org.mockserver.model.Header;
import org.mockserver.model.HttpResponse;

import java.io.IOException;
import java.util.Arrays;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* @author jamesdbloom
*/
public class ApacheHttpClientToMockServerResponseMapperTest {

    @Test
    public void shouldMapHttpClientResponseToHttpResponse() throws IOException {
        // given
        CloseableHttpResponse httpClientResponse = mock(CloseableHttpResponse.class);
        when(httpClientResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 500, "Server Error"));
        when(httpClientResponse.getAllHeaders()).thenReturn(new org.apache.http.Header[]{
                new BasicHeader("header_name", "header_value"),
                new BasicHeader("Set-Cookie", "cookie_name=cookie_value")
        });
        when(httpClientResponse.getEntity()).thenReturn(new StringEntity("some_other_body"));

        // when
        HttpResponse httpResponse = new ApacheHttpClientToMockServerResponseMapper().mapApacheHttpClientResponseToMockServerResponse(httpClientResponse, false);

        // then
        assertEquals(httpResponse.getStatusCode(), new Integer(500));
        assertThat(httpResponse.getHeaders(), containsInAnyOrder(
                new Header("header_name", "header_value"),
                new Header("Set-Cookie", "cookie_name=cookie_value")
        ));
        assertEquals(httpResponse.getCookies(), Arrays.asList(
                new Cookie("cookie_name", "cookie_value")
        ));
        assertEquals(httpResponse.getBodyAsString(), "some_other_body");
    }

    @Test
    public void shouldFilterHeader() throws IOException {
        // given
        CloseableHttpResponse httpClientResponse = mock(CloseableHttpResponse.class);
        when(httpClientResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 500, "Server Error"));
        when(httpClientResponse.getAllHeaders()).thenReturn(new org.apache.http.Header[]{
                new BasicHeader("header_name", "header_value"),
                new BasicHeader("Content-Encoding", "gzip"),
                new BasicHeader("Content-Length", "1024"),
                new BasicHeader("Transfer-Encoding", "chunked")
        });
        when(httpClientResponse.getEntity()).thenReturn(new StringEntity(""));

        // when
        HttpResponse httpResponse = new ApacheHttpClientToMockServerResponseMapper().mapApacheHttpClientResponseToMockServerResponse(httpClientResponse, false);

        // then
        assertEquals(httpResponse.getHeaders(), Arrays.asList(
                new Header("header_name", "header_value")
        ));
    }

    @Test
    public void shouldIgnoreIncorrectlyFormattedCookies() throws IOException {
        // given
        CloseableHttpResponse httpClientResponse = mock(CloseableHttpResponse.class);
        when(httpClientResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 500, "Server Error"));
        when(httpClientResponse.getAllHeaders()).thenReturn(new org.apache.http.Header[]{
                new BasicHeader("Set-Cookie", "valid_name=valid_value"),
                new BasicHeader("Set-Cookie", "=invalid"),
                new BasicHeader("Set-Cookie", "valid_name="),
                new BasicHeader("Set-Cookie", "invalid"),
                new BasicHeader("Set-Cookie", "")
        });
        when(httpClientResponse.getEntity()).thenReturn(new StringEntity(""));

        // when
        HttpResponse httpResponse = new ApacheHttpClientToMockServerResponseMapper().mapApacheHttpClientResponseToMockServerResponse(httpClientResponse, false);

        // then
        assertEquals(httpResponse.getCookies(), Arrays.asList(
                new Cookie("valid_name", "valid_value", "")
        ));
    }
}
TOP

Related Classes of org.mockserver.mappers.ApacheHttpClientToMockServerResponseMapperTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.