Пример #1
0
        public void TestSingleResult()
        {
            // Test we can get an endpoint from a single resolver correctly when the service exists, and when it doesn't
            const string serviceName     = "test-service-please-ignore";
            const string serviceEndpoint = "http://localhost:5000";

            const ServiceResultType serviceType = ServiceResultType.Configuration;

            var mockServiceResolver = new MockServiceResolver(serviceType, 0);

            // Register our service
            mockServiceResolver.ServiceMap[serviceName] = serviceEndpoint;

            serviceCollection.AddSingleton <IServiceResolver>(mockServiceResolver);
            var resolver = serviceCollection.BuildServiceProvider().GetService <IServiceResolution>();

            Assert.NotNull(resolver);
            Assert.True(resolver.Resolvers.Count == 1);

            var positiveResult = resolver.ResolveService(serviceName).Result;
            var negativeResult = resolver.ResolveService(serviceName + serviceName).Result;

            Assert.NotNull(negativeResult);
            Assert.Equal(ServiceResultType.Unknown, negativeResult.Type);

            Assert.NotNull(positiveResult);
            Assert.Equal(serviceType, positiveResult.Type);
            Assert.Equal(serviceEndpoint, positiveResult.Endpoint);
        }
Пример #2
0
        public void TestVersion1Dot1Resolution()
        {
            // Versions can be v1.1, which we need to support
            const string serviceName     = ServiceNameConstants.PROJECT_SERVICE;
            const string serviceEndpoint = "http://localhost:5020";
            const string serviceRoute    = "/my-special-version";

            const string expectedUrl = "http://localhost:5020/api/v1.1/my-special-version";

            const ServiceResultType serviceType = ServiceResultType.Configuration;

            var mockServiceResolver = new MockServiceResolver(serviceType, 0);

            // Register our service
            mockServiceResolver.ServiceMap[serviceName] = serviceEndpoint;

            serviceCollection.AddSingleton <IServiceResolver>(mockServiceResolver);
            var resolver = serviceCollection.BuildServiceProvider().GetService <IServiceResolution>();

            Assert.NotNull(resolver);

            var url = resolver.ResolveLocalServiceEndpoint(ApiService.Project, ApiType.Public, ApiVersion.V1_1, serviceRoute).Result;

            Assert.Equal(expectedUrl, url);
        }
Пример #3
0
        public void TestExplicitUrlResolution()
        {
            // Test we can get an endpoint from a single resolver correctly when the service exists, and when it doesn't
            const string serviceName = "test-service-for-url";
            const string
                serviceConfigurationKey = "test-service-for-url_public_v2"; // Modified to include the api type and version
            const string
                serviceEndpoint =
                "http://localhost:5023/test-endpoint/rewritten/for/simplicity/v1.99/"; // Something like a TPaaS URL which doesn't match our naming convention
            const string     route      = "/explicit-route";
            const ApiVersion apiVersion = ApiVersion.V2;
            const ApiType    apiType    = ApiType.Public;

            const string expectedUrl = "http://localhost:5023/test-endpoint/rewritten/for/simplicity/v1.99/explicit-route";

            var mockServiceResolver = new MockServiceResolver(ServiceResultType.Configuration, 0);

            // Register our service, using the new config key which clear describes the exact endpoint (so no generation occurs in our logic)
            mockServiceResolver.ServiceMap[serviceConfigurationKey] = serviceEndpoint;

            serviceCollection.AddSingleton <IServiceResolver>(mockServiceResolver);
            var resolver = serviceCollection.BuildServiceProvider().GetService <IServiceResolution>();

            Assert.NotNull(resolver);


            var url = resolver.ResolveRemoteServiceEndpoint(serviceName, apiType, apiVersion, route).Result;

            Assert.Equal(expectedUrl, url);
        }
Пример #4
0
        public void TestUrlResolution()
        {
            // Test we can get an endpoint from a single resolver correctly when the service exists, and when it doesn't
            const string     serviceName     = "test-service-for-url";
            const string     serviceEndpoint = "http://localhost:5000/"; // note the final slash, this should be removed
            const string     route           = "/my-route";
            const ApiVersion apiVersion      = ApiVersion.V2;
            const ApiType    apiType         = ApiType.Private;

            var queryParams = new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>("test1", "%#$%^%"),
                new KeyValuePair <string, string>("parameter2", "just-a-normal-value")
            };

            const string expectedUrl = "http://localhost:5000/internal/v2/my-route?test1=%25%23%24%25%5E%25&parameter2=just-a-normal-value";

            var mockServiceResolver = new MockServiceResolver(ServiceResultType.Configuration, 0);

            // Register our service
            mockServiceResolver.ServiceMap[serviceName] = serviceEndpoint;

            serviceCollection.AddSingleton <IServiceResolver>(mockServiceResolver);
            var resolver = serviceCollection.BuildServiceProvider().GetService <IServiceResolution>();

            Assert.NotNull(resolver);


            var url = resolver.ResolveRemoteServiceEndpoint(serviceName, apiType, apiVersion, route, queryParams).Result;

            Assert.Equal(expectedUrl, url);
        }
Пример #5
0
        public void TestPriority()
        {
            // Ensure that the higher priority (lower number) service resolver is used before the lower priority
            // But if the higher priority resolver doesn't know about the service, the lower ones will be used if they do
            const string            serviceOneName = "test-service-with-priority";
            const string            higherPriorityServiceOneEndpoint = "http://localhost:1234";
            const string            lowerPriorityServiceOneEndpoint  = "http://127.0.0.1:9999";
            const int               highPriority = 1;
            const int               lowPriority  = 2;
            const ServiceResultType higherPriorityServiceType = ServiceResultType.InternalKubernetes;
            const ServiceResultType lowerPriorityServiceType  = ServiceResultType.Configuration;

            const string serviceTwoName     = "test-service-low-priority-only";
            const string serviceTwoEndpoint = "http://192.168.1.1:64000";

            var highPriorityResolver = new MockServiceResolver(higherPriorityServiceType, highPriority);
            var lowPriorityResolver  = new MockServiceResolver(lowerPriorityServiceType, lowPriority);

            // service one will exist in both, but service two just in the lower priority
            highPriorityResolver.ServiceMap[serviceOneName] = higherPriorityServiceOneEndpoint;
            lowPriorityResolver.ServiceMap[serviceOneName]  = lowerPriorityServiceOneEndpoint;

            lowPriorityResolver.ServiceMap[serviceTwoName] = serviceTwoEndpoint;

            serviceCollection.AddSingleton <IServiceResolver>(highPriorityResolver);
            serviceCollection.AddSingleton <IServiceResolver>(lowPriorityResolver);
            var resolver = serviceCollection.BuildServiceProvider().GetService <IServiceResolution>();

            Assert.NotNull(resolver);
            Assert.True(resolver.Resolvers.Count == 2);

            // Check the first service comes from the higher priority
            var serviceOne = resolver.ResolveService(serviceOneName).Result;

            Assert.NotNull(serviceOne);
            Assert.Equal(higherPriorityServiceType, serviceOne.Type);
            Assert.Equal(higherPriorityServiceOneEndpoint, serviceOne.Endpoint);

            var serviceTwo = resolver.ResolveService(serviceTwoName).Result;

            Assert.NotNull(serviceTwo);
            Assert.Equal(lowerPriorityServiceType, serviceTwo.Type);
            Assert.Equal(serviceTwoEndpoint, serviceTwo.Endpoint);
        }
Пример #6
0
        public void TestUrlResolutionWithQueryParameterArray()
        {
            // Test we can get an endpoint from a single resolver correctly when the service exists, and when it doesn't
            const string     serviceName     = "test-service-for-url";
            const string     serviceEndpoint = "http://*****:*****@"http://localhost:5000/internal/v2/my-route?test1=the+test+name&fileUid={uid1}&fileUid={uid2}&fileUid={uid3}";

            var mockServiceResolver = new MockServiceResolver(ServiceResultType.Configuration, 0);

            // Register our service
            mockServiceResolver.ServiceMap[serviceName] = serviceEndpoint;

            serviceCollection.AddSingleton <IServiceResolver>(mockServiceResolver);
            var resolver = serviceCollection.BuildServiceProvider().GetService <IServiceResolution>();

            Assert.NotNull(resolver);

            var url = resolver.ResolveRemoteServiceEndpoint(serviceName, apiType, apiVersion, route, queryParams).Result;

            Assert.Equal(expectedUrl, url);
        }
Пример #7
0
        public void TestExplicitUrlResolutionPriority()
        {
            // Explicit URLs should be given higher priority than URLS being assembled from scratch

            // Test we can get an endpoint from a single resolver correctly when the service exists, and when it doesn't
            const string serviceName         = "test-service";
            const string explictConfigKey    = "test-service_public_v2"; // Modified to include the api type and version
            const string serviceRealEndpoint = "http://my-real-host/with/my/real/route/";
            const string serviceBaseUrl      = "http://localhost:9000/";
            const string route = "/i/dont/know/where/i/am";

            const ApiType    apiType           = ApiType.Public;
            const ApiVersion explictApiVersion = ApiVersion.V2;
            const ApiVersion otherApiVersion   = ApiVersion.V3; // different to whats defined in the configuration key, this should be build from the base url

            const string expectedV2Url    = "http://my-real-host/with/my/real/route/i/dont/know/where/i/am";
            const string expectedOtherUrl = "http://localhost:9000/api/v3/i/dont/know/where/i/am";


            var mockServiceResolver = new MockServiceResolver(ServiceResultType.Configuration, 0);

            mockServiceResolver.ServiceMap[explictConfigKey] = serviceRealEndpoint; // should be ued if looking for v2
            mockServiceResolver.ServiceMap[serviceName]      = serviceBaseUrl;

            serviceCollection.AddSingleton <IServiceResolver>(mockServiceResolver);
            var resolver = serviceCollection.BuildServiceProvider().GetService <IServiceResolution>();

            Assert.NotNull(resolver);


            var v2Url    = resolver.ResolveRemoteServiceEndpoint(serviceName, apiType, explictApiVersion, route).Result;
            var otherUrl = resolver.ResolveRemoteServiceEndpoint(serviceName, apiType, otherApiVersion, route).Result;

            Assert.Equal(expectedV2Url, v2Url);
            Assert.Equal(expectedOtherUrl, otherUrl);
        }