aboutsummaryrefslogtreecommitdiffstats
path: root/http-utils/src/test/java/ai/vespa/util/http/hc5/HttpToHttpsRoutePlannerTest.java
blob: fe0cb86dfcfa9de35a7caba5076baa9d904b60bd (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.util.http.hc5;

import org.apache.hc.client5.http.HttpRoute;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.hc.core5.http.HttpHost;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
 * @author jonmv
 */
public class HttpToHttpsRoutePlannerTest {

    final HttpToHttpsRoutePlanner planner = new HttpToHttpsRoutePlanner();

    @Test
    void verifySchemeMustBeHttp() {
        try {
            planner.determineRoute(new HttpHost("https", "host", 1), new HttpClientContext());
        }
        catch (IllegalArgumentException e) {
            assertEquals("Scheme must be 'http' when using HttpToHttpsRoutePlanner", e.getMessage());
        }
    }

    @Test
    void verifyPortMustBeSet() {
        try {
            planner.determineRoute(new HttpHost("http", "host", -1), new HttpClientContext());
        }
        catch (IllegalArgumentException e) {
            assertEquals("Port must be set when using HttpToHttpsRoutePlanner", e.getMessage());
        }
    }


    @Test
    @SuppressWarnings("deprecation")
    void verifyProxyIsDisallowed() {
        HttpClientContext context = new HttpClientContext();
        context.setRequestConfig(RequestConfig.custom().setProxy(new HttpHost("proxy")).build());
        try {
            planner.determineRoute(new HttpHost("http", "host", 1), context);
        }
        catch (IllegalArgumentException e) {
            assertEquals("Proxies are not supported with HttpToHttpsRoutePlanner", e.getMessage());
        }
    }

    @Test
    void verifySchemeIsRewritten() {
        assertEquals(new HttpRoute(new HttpHost("https", "host", 1)),
                planner.determineRoute(new HttpHost("http", "host", 1), new HttpClientContext()));
    }

}