Search in sources :

Example 1 with PathMatcher

use of io.grpc.xds.VirtualHost.Route.RouteMatch.PathMatcher in project grpc-java by grpc.

the class ClientXdsClient method parseRouteMatch.

@VisibleForTesting
@Nullable
static StructOrError<RouteMatch> parseRouteMatch(io.envoyproxy.envoy.config.route.v3.RouteMatch proto) {
    if (proto.getQueryParametersCount() != 0) {
        return null;
    }
    StructOrError<PathMatcher> pathMatch = parsePathMatcher(proto);
    if (pathMatch.getErrorDetail() != null) {
        return StructOrError.fromError(pathMatch.getErrorDetail());
    }
    FractionMatcher fractionMatch = null;
    if (proto.hasRuntimeFraction()) {
        StructOrError<FractionMatcher> parsedFraction = parseFractionMatcher(proto.getRuntimeFraction().getDefaultValue());
        if (parsedFraction.getErrorDetail() != null) {
            return StructOrError.fromError(parsedFraction.getErrorDetail());
        }
        fractionMatch = parsedFraction.getStruct();
    }
    List<HeaderMatcher> headerMatchers = new ArrayList<>();
    for (io.envoyproxy.envoy.config.route.v3.HeaderMatcher hmProto : proto.getHeadersList()) {
        StructOrError<HeaderMatcher> headerMatcher = parseHeaderMatcher(hmProto);
        if (headerMatcher.getErrorDetail() != null) {
            return StructOrError.fromError(headerMatcher.getErrorDetail());
        }
        headerMatchers.add(headerMatcher.getStruct());
    }
    return StructOrError.fromStruct(RouteMatch.create(pathMatch.getStruct(), headerMatchers, fractionMatch));
}
Also used : FractionMatcher(io.grpc.xds.internal.Matchers.FractionMatcher) HeaderMatcher(io.grpc.xds.internal.Matchers.HeaderMatcher) PathMatcher(io.grpc.xds.VirtualHost.Route.RouteMatch.PathMatcher) ArrayList(java.util.ArrayList) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Nullable(javax.annotation.Nullable)

Example 2 with PathMatcher

use of io.grpc.xds.VirtualHost.Route.RouteMatch.PathMatcher 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();
}
Also used : PathMatcher(io.grpc.xds.VirtualHost.Route.RouteMatch.PathMatcher) Metadata(io.grpc.Metadata) RouteMatch(io.grpc.xds.VirtualHost.Route.RouteMatch) Test(org.junit.Test)

Aggregations

PathMatcher (io.grpc.xds.VirtualHost.Route.RouteMatch.PathMatcher)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Metadata (io.grpc.Metadata)1 RouteMatch (io.grpc.xds.VirtualHost.Route.RouteMatch)1 FractionMatcher (io.grpc.xds.internal.Matchers.FractionMatcher)1 HeaderMatcher (io.grpc.xds.internal.Matchers.HeaderMatcher)1 ArrayList (java.util.ArrayList)1 Nullable (javax.annotation.Nullable)1 Test (org.junit.Test)1