aboutsummaryrefslogtreecommitdiffstats
path: root/http-utils/src/test/java/ai/vespa/util/http/hc4/VespaHttpClientBuilderTest.java
blob: 27819ff011788751e5c16e68fee11929e6cc4e96 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.util.http.hc4;

import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.routing.HttpRoutePlanner;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;

/**
 * @author bjorncs
 */
public class VespaHttpClientBuilderTest {

    @Test
    void route_planner_modifies_scheme_of_requests() throws HttpException {
        verifyProcessedUriMatchesExpectedOutput("http://dummyhostname:8080", "https://dummyhostname:8080");
    }

    @Test
    void route_planer_handles_implicit_http_port() throws HttpException {
        verifyProcessedUriMatchesExpectedOutput("http://dummyhostname", "https://dummyhostname:80");
    }

    @Test
    void route_planer_handles_https_port() throws HttpException {
        verifyProcessedUriMatchesExpectedOutput("http://dummyhostname:443", "https://dummyhostname:443");
    }

    private static void verifyProcessedUriMatchesExpectedOutput(String inputHostString, String expectedHostString) throws HttpException {
        HttpRoutePlanner routePlanner = new VespaHttpClientBuilder.HttpToHttpsRoutePlanner();
        HttpRoute newRoute = routePlanner.determineRoute(HttpHost.create(inputHostString), mock(HttpRequest.class), new HttpClientContext());
        HttpHost target = newRoute.getTargetHost();
        assertEquals(expectedHostString, target.toURI());
    }

}