// 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(
"",
" ",
" ",
" " + MY_CHAIN_BINDING.patternString() + "",
" ",
" ",
"");
Http http = buildHttp(xml);
FilterBinding binding = first(http.getBindings());
assertEquals("my-request-chain", binding.chainId().getName());
assertEquals(MY_CHAIN_BINDING, binding.binding());
Chain 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(
"",
" ",
" ",
" " + MY_CHAIN_BINDING.patternString() + "",
" ",
" ",
"");
Http http = buildHttp(xml);
FilterBinding binding = first(http.getBindings());
assertEquals("my-response-chain", binding.chainId().getName());
assertEquals(MY_CHAIN_BINDING, binding.binding());
Chain 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(
"",
" ",
" ",
" ",
" " + MY_CHAIN_BINDING.patternString() + "",
" ",
" ",
" ",
" ",
" ",
"");
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://*/my-binding",
" ",
" ",
"");
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());
}
}