use of io.grpc.xds.VirtualHost.Route.RouteMatch in project grpc-java by grpc.
the class XdsServerWrapperTest method interceptor_success.
@Test
@SuppressWarnings("unchecked")
public void interceptor_success() throws Exception {
ArgumentCaptor<ConfigApplyingInterceptor> interceptorCaptor = ArgumentCaptor.forClass(ConfigApplyingInterceptor.class);
final SettableFuture<Server> start = SettableFuture.create();
Executors.newSingleThreadExecutor().execute(new Runnable() {
@Override
public void run() {
try {
start.set(xdsServerWrapper.start());
} catch (Exception ex) {
start.setException(ex);
}
}
});
xdsClient.ldsResource.get(5, TimeUnit.SECONDS);
verify(mockBuilder).intercept(interceptorCaptor.capture());
ConfigApplyingInterceptor interceptor = interceptorCaptor.getValue();
RouteMatch routeMatch = RouteMatch.create(PathMatcher.fromPath("/FooService/barMethod", true), Collections.<HeaderMatcher>emptyList(), null);
Route route = Route.forAction(routeMatch, null, ImmutableMap.<String, FilterConfig>of());
VirtualHost virtualHost = VirtualHost.create("v1", Collections.singletonList("foo.google.com"), Arrays.asList(route), ImmutableMap.<String, FilterConfig>of());
final List<Integer> interceptorTrace = new ArrayList<>();
ServerInterceptor interceptor0 = new ServerInterceptor() {
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
interceptorTrace.add(0);
return next.startCall(call, headers);
}
};
ServerRoutingConfig realConfig = ServerRoutingConfig.create(ImmutableList.of(virtualHost), ImmutableMap.of(route, interceptor0));
ServerCall<Void, Void> serverCall = mock(ServerCall.class);
when(serverCall.getMethodDescriptor()).thenReturn(createMethod("FooService/barMethod"));
when(serverCall.getAttributes()).thenReturn(Attributes.newBuilder().set(ATTR_SERVER_ROUTING_CONFIG, new AtomicReference<>(realConfig)).build());
when(serverCall.getAuthority()).thenReturn("foo.google.com");
ServerCallHandler<Void, Void> next = mock(ServerCallHandler.class);
interceptor.interceptCall(serverCall, new Metadata(), next);
verify(next).startCall(eq(serverCall), any(Metadata.class));
assertThat(interceptorTrace).isEqualTo(Arrays.asList(0));
}
use of io.grpc.xds.VirtualHost.Route.RouteMatch in project grpc-java by grpc.
the class XdsServerWrapperTest method buildInterceptor_rds.
@Test
@SuppressWarnings("unchecked")
public void buildInterceptor_rds() throws Exception {
final SettableFuture<Server> start = SettableFuture.create();
Executors.newSingleThreadExecutor().execute(new Runnable() {
@Override
public void run() {
try {
start.set(xdsServerWrapper.start());
} catch (Exception ex) {
start.setException(ex);
}
}
});
xdsClient.ldsResource.get(5, TimeUnit.SECONDS);
Filter filter = mock(Filter.class, withSettings().extraInterfaces(ServerInterceptorBuilder.class));
when(filter.typeUrls()).thenReturn(new String[] { "filter-type-url" });
filterRegistry.register(filter);
FilterConfig f0 = mock(FilterConfig.class);
FilterConfig f0Override = mock(FilterConfig.class);
when(f0.typeUrl()).thenReturn("filter-type-url");
final List<Integer> interceptorTrace = new ArrayList<>();
ServerInterceptor interceptor0 = new ServerInterceptor() {
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
interceptorTrace.add(0);
return next.startCall(call, headers);
}
};
ServerInterceptor interceptor1 = new ServerInterceptor() {
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
interceptorTrace.add(1);
return next.startCall(call, headers);
}
};
when(((ServerInterceptorBuilder) filter).buildServerInterceptor(f0, null)).thenReturn(interceptor0);
when(((ServerInterceptorBuilder) filter).buildServerInterceptor(f0, f0Override)).thenReturn(interceptor1);
RouteMatch routeMatch = RouteMatch.create(PathMatcher.fromPath("/FooService/barMethod", true), Collections.<HeaderMatcher>emptyList(), null);
HttpConnectionManager rdsHcm = HttpConnectionManager.forRdsName(0L, "r0", Arrays.asList(new NamedFilterConfig("filter-config-name-0", f0), new NamedFilterConfig("filter-config-name-1", f0)));
EnvoyServerProtoData.FilterChain filterChain = createFilterChain("filter-chain-0", rdsHcm);
xdsClient.deliverLdsUpdate(Collections.singletonList(filterChain), null);
Route route = Route.forAction(routeMatch, null, ImmutableMap.<String, FilterConfig>of());
VirtualHost virtualHost = VirtualHost.create("v1", Collections.singletonList("foo.google.com"), Arrays.asList(route), ImmutableMap.of("filter-config-name-0", f0Override));
xdsClient.rdsCount.await(5, TimeUnit.SECONDS);
xdsClient.deliverRdsUpdate("r0", Collections.singletonList(virtualHost));
start.get(5000, TimeUnit.MILLISECONDS);
verify(mockServer).start();
assertThat(selectorManager.getSelectorToUpdateSelector().getRoutingConfigs().size()).isEqualTo(1);
ServerInterceptor realInterceptor = selectorManager.getSelectorToUpdateSelector().getRoutingConfigs().get(filterChain).get().interceptors().get(route);
assertThat(realInterceptor).isNotNull();
ServerCall<Void, Void> serverCall = mock(ServerCall.class);
ServerCallHandler<Void, Void> mockNext = mock(ServerCallHandler.class);
final ServerCall.Listener<Void> listener = new ServerCall.Listener<Void>() {
};
when(mockNext.startCall(any(ServerCall.class), any(Metadata.class))).thenReturn(listener);
realInterceptor.interceptCall(serverCall, new Metadata(), mockNext);
assertThat(interceptorTrace).isEqualTo(Arrays.asList(1, 0));
verify(mockNext).startCall(eq(serverCall), any(Metadata.class));
virtualHost = VirtualHost.create("v1", Collections.singletonList("foo.google.com"), Arrays.asList(route), ImmutableMap.<String, FilterConfig>of());
xdsClient.deliverRdsUpdate("r0", Collections.singletonList(virtualHost));
realInterceptor = selectorManager.getSelectorToUpdateSelector().getRoutingConfigs().get(filterChain).get().interceptors().get(route);
assertThat(realInterceptor).isNotNull();
interceptorTrace.clear();
realInterceptor.interceptCall(serverCall, new Metadata(), mockNext);
assertThat(interceptorTrace).isEqualTo(Arrays.asList(0, 0));
verify(mockNext, times(2)).startCall(eq(serverCall), any(Metadata.class));
xdsClient.rdsWatchers.get("r0").onResourceDoesNotExist("r0");
assertThat(selectorManager.getSelectorToUpdateSelector().getRoutingConfigs().get(filterChain).get()).isEqualTo(noopConfig);
}
use of io.grpc.xds.VirtualHost.Route.RouteMatch in project grpc-java by grpc.
the class XdsServerWrapperTest method createRoutingConfig.
private static ServerRoutingConfig createRoutingConfig(String path, String domain, String filterType, Route.RouteAction action) {
RouteMatch routeMatch = RouteMatch.create(PathMatcher.fromPath(path, true), Collections.<HeaderMatcher>emptyList(), null);
VirtualHost virtualHost = VirtualHost.create("v1", Collections.singletonList(domain), Arrays.asList(Route.forAction(routeMatch, action, ImmutableMap.<String, FilterConfig>of())), Collections.<String, FilterConfig>emptyMap());
FilterConfig f0 = mock(FilterConfig.class);
when(f0.typeUrl()).thenReturn(filterType);
return ServerRoutingConfig.create(ImmutableList.<VirtualHost>of(virtualHost), ImmutableMap.<Route, ServerInterceptor>of());
}
use of io.grpc.xds.VirtualHost.Route.RouteMatch in project grpc-java by grpc.
the class XdsNameResolverTest method routeMatching_withHeaders.
@Test
public void routeMatching_withHeaders() {
Metadata headers = new Metadata();
headers.put(Metadata.Key.of("authority", Metadata.ASCII_STRING_MARSHALLER), "foo.googleapis.com");
headers.put(Metadata.Key.of("grpc-encoding", Metadata.ASCII_STRING_MARSHALLER), "gzip");
headers.put(Metadata.Key.of("user-agent", Metadata.ASCII_STRING_MARSHALLER), "gRPC-Java");
headers.put(Metadata.Key.of("content-length", Metadata.ASCII_STRING_MARSHALLER), "1000");
headers.put(Metadata.Key.of("custom-key", Metadata.ASCII_STRING_MARSHALLER), "custom-value1");
headers.put(Metadata.Key.of("custom-key", Metadata.ASCII_STRING_MARSHALLER), "custom-value2");
ThreadSafeRandom random = mock(ThreadSafeRandom.class);
PathMatcher pathMatcher = PathMatcher.fromPath("/FooService/barMethod", true);
RouteMatch routeMatch1 = RouteMatch.create(pathMatcher, Arrays.asList(HeaderMatcher.forExactValue("grpc-encoding", "gzip", false), HeaderMatcher.forSafeRegEx("authority", Pattern.compile(".*googleapis.*"), false), HeaderMatcher.forRange("content-length", HeaderMatcher.Range.create(100, 10000), false), HeaderMatcher.forPresent("user-agent", true, false), HeaderMatcher.forPrefix("custom-key", "custom-", false), HeaderMatcher.forSuffix("custom-key", "value2", false)), null);
assertThat(XdsNameResolver.matchRoute(routeMatch1, "/FooService/barMethod", headers, random)).isTrue();
RouteMatch routeMatch2 = RouteMatch.create(pathMatcher, Collections.singletonList(HeaderMatcher.forSafeRegEx("authority", Pattern.compile(".*googleapis.*"), true)), null);
assertThat(XdsNameResolver.matchRoute(routeMatch2, "/FooService/barMethod", headers, random)).isFalse();
RouteMatch routeMatch3 = RouteMatch.create(pathMatcher, Collections.singletonList(HeaderMatcher.forExactValue("user-agent", "gRPC-Go", false)), null);
assertThat(XdsNameResolver.matchRoute(routeMatch3, "/FooService/barMethod", headers, random)).isFalse();
RouteMatch routeMatch4 = RouteMatch.create(pathMatcher, Collections.singletonList(HeaderMatcher.forPresent("user-agent", false, false)), null);
assertThat(XdsNameResolver.matchRoute(routeMatch4, "/FooService/barMethod", headers, random)).isFalse();
RouteMatch routeMatch5 = RouteMatch.create(pathMatcher, // inverted
Collections.singletonList(HeaderMatcher.forPresent("user-agent", false, true)), null);
assertThat(XdsNameResolver.matchRoute(routeMatch5, "/FooService/barMethod", headers, random)).isTrue();
RouteMatch routeMatch6 = RouteMatch.create(pathMatcher, Collections.singletonList(HeaderMatcher.forPresent("user-agent", true, true)), null);
assertThat(XdsNameResolver.matchRoute(routeMatch6, "/FooService/barMethod", headers, random)).isFalse();
RouteMatch routeMatch7 = RouteMatch.create(pathMatcher, Collections.singletonList(HeaderMatcher.forExactValue("custom-key", "custom-value1,custom-value2", false)), null);
assertThat(XdsNameResolver.matchRoute(routeMatch7, "/FooService/barMethod", headers, random)).isTrue();
RouteMatch routeMatch8 = RouteMatch.create(pathMatcher, Collections.singletonList(HeaderMatcher.forExactValue("content-type", "application/grpc", false)), null);
assertThat(XdsNameResolver.matchRoute(routeMatch8, "/FooService/barMethod", new Metadata(), random)).isTrue();
RouteMatch routeMatch9 = RouteMatch.create(pathMatcher, Collections.singletonList(HeaderMatcher.forExactValue("custom-key!", "custom-value1,custom-value2", false)), null);
assertThat(XdsNameResolver.matchRoute(routeMatch9, "/FooService/barMethod", headers, random)).isFalse();
}
use of io.grpc.xds.VirtualHost.Route.RouteMatch in project grpc-java by grpc.
the class XdsNameResolverTest method routeMatching_pathOnly.
@Test
public void routeMatching_pathOnly() {
Metadata headers = new Metadata();
ThreadSafeRandom random = mock(ThreadSafeRandom.class);
RouteMatch routeMatch1 = RouteMatch.create(PathMatcher.fromPath("/FooService/barMethod", true), Collections.<HeaderMatcher>emptyList(), null);
assertThat(XdsNameResolver.matchRoute(routeMatch1, "/FooService/barMethod", headers, random)).isTrue();
assertThat(XdsNameResolver.matchRoute(routeMatch1, "/FooService/bazMethod", headers, random)).isFalse();
RouteMatch routeMatch2 = RouteMatch.create(PathMatcher.fromPrefix("/FooService/", true), Collections.<HeaderMatcher>emptyList(), null);
assertThat(XdsNameResolver.matchRoute(routeMatch2, "/FooService/barMethod", headers, random)).isTrue();
assertThat(XdsNameResolver.matchRoute(routeMatch2, "/FooService/bazMethod", headers, random)).isTrue();
assertThat(XdsNameResolver.matchRoute(routeMatch2, "/BarService/bazMethod", headers, random)).isFalse();
RouteMatch routeMatch3 = RouteMatch.create(PathMatcher.fromRegEx(Pattern.compile(".*Foo.*")), Collections.<HeaderMatcher>emptyList(), null);
assertThat(XdsNameResolver.matchRoute(routeMatch3, "/FooService/barMethod", headers, random)).isTrue();
}
Aggregations