aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/vespa/model/container/http/FilterBindingsTest.java
blob: 70a859af0105ada658feb1d5ac4f4d960e3ac567 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.container.http;

import com.yahoo.config.model.builder.xml.test.DomBuilderTest;
import com.yahoo.config.model.deploy.DeployState;
import com.yahoo.jdisc.http.ServerConfig;
import com.yahoo.vespa.model.container.ContainerModel;
import com.yahoo.vespa.model.container.component.BindingPattern;
import com.yahoo.vespa.model.container.component.UserBindingPattern;
import com.yahoo.vespa.model.container.component.chain.Chain;
import com.yahoo.vespa.model.container.http.xml.HttpBuilder;
import com.yahoo.vespa.model.container.xml.ContainerModelBuilder;
import com.yahoo.vespa.model.container.xml.ContainerModelBuilder.Networking;
import org.junit.jupiter.api.Test;
import org.w3c.dom.Element;

import java.util.Set;

import static com.yahoo.collections.CollectionUtil.first;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
 * @author gjoranv
 */
public class FilterBindingsTest extends DomBuilderTest {

    private static final BindingPattern MY_CHAIN_BINDING = UserBindingPattern.fromHttpPath("/my-chain-binding");

    private Http buildHttp(Element xml) {
        Http http = new HttpBuilder(Set.of()).build(root.getDeployState(), root, xml);
        root.freezeModelTopology();
        http.validate();
        return http;
    }


    private void buildContainerCluster(Element containerElem) {
        ContainerModel model = new ContainerModelBuilder(true, Networking.enable).build(DeployState.createTestState(), null, null, root, containerElem);
        root.freezeModelTopology();
    }

    @Test
    void request_chain_binding_is_added_to_http() throws Exception {
        Element xml = parse(
                "<http>",
                "  <filtering>",
                "    <request-chain id='my-request-chain'>",
                "      <binding>" + MY_CHAIN_BINDING.patternString() + "</binding>",
                "    </request-chain>",
                "  </filtering>",
                "</http>");
        Http http = buildHttp(xml);

        FilterBinding binding = first(http.getBindings());
        assertEquals("my-request-chain", binding.chainId().getName());
        assertEquals(MY_CHAIN_BINDING, binding.binding());

        Chain<Filter> myChain = http.getFilterChains().allChains().getComponent("my-request-chain");
        assertNotNull(myChain, "Missing chain");
    }

    @Test
    void response_chain_binding_is_added_to_http() throws Exception {
        Element xml = parse(
                "<http>",
                "  <filtering>",
                "    <response-chain id='my-response-chain'>",
                "      <binding>" + MY_CHAIN_BINDING.patternString() + "</binding>",
                "    </response-chain>",
                "  </filtering>",
                "</http>");
        Http http = buildHttp(xml);

        FilterBinding binding = first(http.getBindings());
        assertEquals("my-response-chain", binding.chainId().getName());
        assertEquals(MY_CHAIN_BINDING, binding.binding());

        Chain<Filter> myChain = http.getFilterChains().allChains().getComponent("my-response-chain");
        assertNotNull(myChain, "Missing chain");
    }

    @Test
    void bindings_are_added_to_config_for_all_http_servers_with_jetty() {
        final Element xml = parse(
                "<container version='1.0'>",
                "  <http>",
                "    <filtering>",
                "      <request-chain id='my-request-chain'>",
                "        <binding>" + MY_CHAIN_BINDING.patternString() + "</binding>",
                "      </request-chain>",
                "    </filtering>",
                "    <server id='server1' port='8000' />",
                "    <server id='server2' port='9000' />",
                "  </http>",
                "</container>");
        buildContainerCluster(xml);

        {
            final ServerConfig config = root.getConfig(ServerConfig.class, "container/http/jdisc-jetty/server1");
            assertEquals(1, config.filter().size());
            assertEquals("my-request-chain", config.filter(0).id());
            assertEquals(MY_CHAIN_BINDING.patternString(), config.filter(0).binding());
        }
        {
            final ServerConfig config = root.getConfig(ServerConfig.class, "container/http/jdisc-jetty/server2");
            assertEquals(1, config.filter().size());
            assertEquals("my-request-chain", config.filter(0).id());
            assertEquals(MY_CHAIN_BINDING.patternString(), config.filter(0).binding());
        }
    }

    @Test
    void filter_binding_ports_are_overriden() {
        Element xml = parse(
                "<http>",
                "  <filtering>",
                "    <request-chain id='my-request-chain'>",
                "      <binding>http://*/my-binding</binding>",
                "    </request-chain>",
                "  </filtering>",
                "</http>");
        Http http = new HttpBuilder(Set.of(4443)).build(root.getDeployState(), root, xml);
        root.freezeModelTopology();
        http.validate();
        FilterBinding binding = first(http.getBindings());
        assertEquals("my-request-chain", binding.chainId().getName());
        assertEquals("http://*:4443/my-binding", binding.binding().patternString());
    }
}