aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/proxy/ProxyRequestTest.java
blob: d851eb568905f0bc5d786b7191fcf809970ec329 (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
60
61
62
63
64
65
66
67
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.proxy;

import ai.vespa.http.HttpURL.Path;
import com.yahoo.jdisc.http.HttpRequest;
import org.junit.jupiter.api.Test;

import java.net.URI;
import java.util.List;
import java.util.Map;

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

/**
 * @author Haakon Dybdahl
 */
public class ProxyRequestTest {

    @Test
    void testBadUri() {
        assertEquals("Request path '/path' does not end with proxy path '/zone/v2/'",
                assertThrows(IllegalArgumentException.class,
                        () -> testRequest("http://domain.tld/path", "/zone/v2/")).getMessage());
    }

    @Test
    void testUris() {
        {
            // Root request
            ProxyRequest request = testRequest("http://controller.domain.tld/my/path", "");
            assertEquals(URI.create("http://controller.domain.tld/my/path/"), request.getControllerPrefixUri());
            assertEquals(URI.create("https://cfg.prod.us-north-1.domain.tld:1234"),
                    request.createConfigServerRequestUri(URI.create("https://cfg.prod.us-north-1.domain.tld:1234/")));
        }

        {
            // Root request with trailing /
            ProxyRequest request = testRequest("http://controller.domain.tld/my/path/", "/");
            assertEquals(URI.create("http://controller.domain.tld/my/path/"), request.getControllerPrefixUri());
            assertEquals(URI.create("https://cfg.prod.us-north-1.domain.tld:1234/"),
                    request.createConfigServerRequestUri(URI.create("https://cfg.prod.us-north-1.domain.tld:1234/")));
        }

        {
            // API path test
            ProxyRequest request = testRequest("http://controller.domain.tld:1234/my/path/nodes/v2", "/nodes/v2");
            assertEquals(URI.create("http://controller.domain.tld:1234/my/path/"), request.getControllerPrefixUri());
            assertEquals(URI.create("https://cfg.prod.us-north-1.domain.tld/nodes/v2"),
                    request.createConfigServerRequestUri(URI.create("https://cfg.prod.us-north-1.domain.tld")));
        }

        {
            // API path test with query
            ProxyRequest request = testRequest("http://controller.domain.tld:1234/my/path/nodes/v2/?some=thing", "/nodes/v2/");
            assertEquals(URI.create("http://controller.domain.tld:1234/my/path/"), request.getControllerPrefixUri());
            assertEquals(URI.create("https://cfg.prod.us-north-1.domain.tld/nodes/v2/?some=thing"),
                    request.createConfigServerRequestUri(URI.create("https://cfg.prod.us-north-1.domain.tld")));
        }
    }

    private static ProxyRequest testRequest(String url, String pathPrefix) {
        return new ProxyRequest(HttpRequest.Method.GET, URI.create(url), Map.of(), null,
                                List.of(URI.create("http://example.com")), Path.parse(pathPrefix));
    }

}