public async Task CredentialsFilePath_QuotaProject()
        {
            var file = Path.GetTempFileName();

            File.WriteAllText(file, DummyServiceAccountCredentialFileContents);

            try
            {
                var builder = new SampleClientBuilder
                {
                    CredentialsPath = file,
                    QuotaProject    = SampleQuotaProject
                };

                Action <BaseClientService.Initializer> validator = initializer =>
                {
                    var credential = Assert.IsAssignableFrom <GoogleCredential>(initializer.HttpClientInitializer);
                    Assert.Equal(SampleQuotaProject, credential.QuotaProject);
                };

                await ValidateResultAsync(builder, validator);
            }
            finally
            {
                File.Delete(file);
            }
        }
        private async Task ValidateResultAsync(SampleClientBuilder builder, Action <CallInvoker> validator)
        {
            var callInvoker = builder.Build();

            validator(callInvoker);
            builder.ResetChannelCreation();
            callInvoker = await builder.BuildAsync(default);
        public async Task ApiKey_NoOtherCredentials_QuotaProject()
        {
            var builder = new SampleClientBuilder
            {
                ApiKey       = SampleApiKey,
                QuotaProject = SampleQuotaProject
            };

            Action <BaseClientService.Initializer> validator = initializer =>
            {
                Assert.Equal(SampleApiKey, initializer.ApiKey);
                Assert.NotNull(initializer.HttpClientInitializer);

                var quotaProjectInitializer = initializer.HttpClientInitializer;
                using var handler = new FakeMessageHandler();
                using var client  = new ConfigurableHttpClient(new ConfigurableMessageHandler(handler));

                quotaProjectInitializer.Initialize(client);
                client.GetAsync("https://will.be.ignored").Wait();

                var header = Assert.Single(handler.LatestRequestHeaders, header => header.Key == "x-goog-user-project");
                var value  = Assert.Single(header.Value);
                Assert.Equal(SampleQuotaProject, value);
            };

            await ValidateResultAsync(builder, validator);
        }
        public async Task CredentialsFilePath()
        {
            var file = Path.GetTempFileName();

            File.WriteAllText(file, DummyServiceAccountCredentialFileContents);

            try
            {
                var builder = new SampleClientBuilder {
                    CredentialsPath = file
                };

                // We won't use the channel pool when custom credentials are specified.
                // We can't easily check anything about the actual credentials though.
                Action <CallInvoker> validator = invoker =>
                {
                    Assert.NotNull(builder.ChannelCreated);
                    Assert.Same(builder.ChannelCreated, GetChannel(invoker));
                    Assert.Equal(SampleClientBuilder.DefaultEndpoint, builder.EndpointUsedToCreateChannel);
                };

                await ValidateResultAsync(builder, validator);
            }
            finally
            {
                File.Delete(file);
            }
        }
Esempio n. 5
0
        public async Task TokenAccessMethod()
        {
            var builder = new SampleClientBuilder {
                TokenAccessMethod = CustomTokenAccess
            };

            await ValidateResultAsync(builder, AssertNonChannelPool(builder));
        }
Esempio n. 6
0
        public async Task JsonCredentials()
        {
            var builder = new SampleClientBuilder {
                JsonCredentials = DummyServiceAccountCredentialFileContents
            };

            await ValidateResultAsync(builder, AssertNonChannelPool(builder));
        }
Esempio n. 7
0
        public async Task QuotaProject()
        {
            var builder = new SampleClientBuilder {
                QuotaProject = SampleQuotaProject
            };

            await ValidateResultAsync(builder, AssertNonChannelPool(builder));
        }
Esempio n. 8
0
        public async Task CustomScopes()
        {
            var builder = new SampleClientBuilder {
                Scopes = new[] { "a", "b" }
            };

            await ValidateResultAsync(builder, AssertNonChannelPool(builder));
        }
Esempio n. 9
0
        public async Task Credential()
        {
            var builder = new SampleClientBuilder {
                Credential = GoogleCredential.FromJson(DummyServiceAccountCredentialFileContents)
            };

            await ValidateResultAsync(builder, AssertNonChannelPool(builder));
        }
        public async Task JsonCredentials()
        {
            var builder = new SampleClientBuilder {
                JsonCredentials = DummyServiceAccountCredentialFileContents
            };

            await ValidateResultAsync(builder, initializer => Assert.IsAssignableFrom <GoogleCredential>(initializer.HttpClientInitializer));
        }
        public async Task ApplicationNameSpecified()
        {
            string applicationName = "CustomApplicationName";
            var    builder         = new SampleClientBuilder {
                ApplicationName = applicationName
            };

            await ValidateResultAsync(builder, initializer => Assert.Equal(applicationName, initializer.ApplicationName));
        }
        public async Task BaseUri()
        {
            string uri     = "https://foo.googleapis.com";
            var    builder = new SampleClientBuilder {
                BaseUri = uri
            };

            await ValidateResultAsync(builder, initializer => Assert.Equal(uri, initializer.BaseUri));
        }
        public async Task HttpClientFactory()
        {
            var factory = new HttpClientFactory();
            var builder = new SampleClientBuilder {
                HttpClientFactory = factory
            };

            await ValidateResultAsync(builder, initializer => Assert.Same(factory, initializer.HttpClientFactory));
        }
        public async Task CustomCredential()
        {
            var credential = GoogleCredential.FromJson(DummyServiceAccountCredentialFileContents);
            var builder    = new SampleClientBuilder {
                Credential = credential
            };

            await ValidateResultAsync(builder, initializer => Assert.Same(credential, initializer.HttpClientInitializer));
        }
Esempio n. 15
0
        public async Task JwtClientEnabledTest(bool clientUsesJwt, bool poolUsesJwt)
        {
            var builder = new SampleClientBuilder(clientUsesJwt, poolUsesJwt)
            {
                JsonCredentials = DummyServiceAccountCredentialFileContents
            };
            ChannelBase channelFromPool = builder.ChannelPool.GetChannel(GrpcCoreAdapter.Instance, TestServiceMetadata.TestService.DefaultEndpoint, SampleClientBuilder.DefaultOptions);

            // Jwt of client does not match pool, so we won't use channel pool
            await ValidateResultAsync(builder, AssertNonChannelPool(builder));
        }
        public async Task ApiKey_NoOtherCredentials()
        {
            var builder = new SampleClientBuilder {
                ApiKey = SampleApiKey
            };

            Action <BaseClientService.Initializer> validator = initializer =>
            {
                Assert.Null(initializer.HttpClientInitializer);
                Assert.Equal(SampleApiKey, initializer.ApiKey);
            };

            await ValidateResultAsync(builder, validator);
        }
Esempio n. 17
0
        public async Task CustomCallInvoker()
        {
            var builder = new SampleClientBuilder {
                CallInvoker = CustomInvoker
            };

            Action <CallInvoker> validator = invoker =>
            {
                Assert.Same(invoker, CustomInvoker);
                Assert.Null(builder.ChannelCreated);
            };

            await ValidateResultAsync(builder, validator);
        }
        public async Task DefaultsToChannelPool()
        {
            var builder = new SampleClientBuilder();

            Channel channelFromPool = builder.ChannelPool.GetChannel(SampleClientBuilder.DefaultEndpoint);

            Action <CallInvoker> validator = invoker =>
            {
                var channelFromBuilder = GetChannel(invoker);
                Assert.Same(channelFromPool, channelFromBuilder);
                Assert.Null(builder.ChannelCreated);
            };

            await ValidateResultAsync(builder, validator);
        }
Esempio n. 19
0
        public async Task DefaultsToChannelPool()
        {
            var builder = new SampleClientBuilder();

            ChannelBase channelFromPool = builder.ChannelPool.GetChannel(GrpcCoreAdapter.Instance, TestServiceMetadata.TestService.DefaultEndpoint, SampleClientBuilder.DefaultOptions);

            Action <CallInvoker> validator = invoker =>
            {
                var channelFromBuilder = GetChannel(invoker);
                Assert.Same(channelFromPool, channelFromBuilder);
                Assert.Null(builder.ChannelCreated);
            };

            await ValidateResultAsync(builder, validator);
        }
        public async Task JsonCredentials_QuotaProject()
        {
            var builder = new SampleClientBuilder
            {
                JsonCredentials = DummyServiceAccountCredentialFileContents,
                QuotaProject    = SampleQuotaProject
            };

            Action <BaseClientService.Initializer> validator = initializer =>
            {
                var credential = Assert.IsAssignableFrom <GoogleCredential>(initializer.HttpClientInitializer);
                Assert.Equal(SampleQuotaProject, credential.QuotaProject);
            };

            await ValidateResultAsync(builder, validator);
        }
        public async Task ApiKey_WithOtherCredentials()
        {
            var builder = new SampleClientBuilder
            {
                ApiKey          = SampleApiKey,
                JsonCredentials = DummyServiceAccountCredentialFileContents
            };

            Action <BaseClientService.Initializer> validator = initializer =>
            {
                Assert.NotNull(initializer.HttpClientInitializer);
                Assert.Equal(SampleApiKey, initializer.ApiKey);
            };

            await ValidateResultAsync(builder, validator);
        }
Esempio n. 22
0
        public async Task JwtClientAndPoolEnabledTest(bool enabledJwts)
        {
            var         builder         = new SampleClientBuilder(enabledJwts, enabledJwts);
            ChannelBase channelFromPool = builder.ChannelPool.GetChannel(GrpcCoreAdapter.Instance, TestServiceMetadata.TestService.DefaultEndpoint, SampleClientBuilder.DefaultOptions);

            // Jwt is either enabled or disabled for both client and pool
            // We use channel pool
            Action <CallInvoker> validator = invoker =>
            {
                var channelFromBuilder = GetChannel(invoker);
                Assert.Null(builder.ChannelCreated);
                Assert.Same(builder.ChannelCredentials, builder.CredentialsUsedToCreateChannel);
            };

            await ValidateResultAsync(builder, validator);
        }
        public async Task CustomChannelCredentials()
        {
            var builder = new SampleClientBuilder {
                ChannelCredentials = ChannelCredentials.Insecure
            };

            Action <CallInvoker> validator = invoker =>
            {
                Assert.NotNull(builder.ChannelCreated);
                Assert.Same(builder.ChannelCreated, GetChannel(invoker));
                Assert.Equal(SampleClientBuilder.DefaultEndpoint, builder.EndpointUsedToCreateChannel);
                Assert.Same(builder.ChannelCredentials, builder.CredentialsUsedToCreateChannel);
            };

            await ValidateResultAsync(builder, validator);
        }
        public async Task CustomCallInvoker()
        {
            var builder = new SampleClientBuilder {
                CallInvoker = CustomInvoker
            };

            // We won't use the channel pool when there are custom scopes.
            // We can't easily check anything about the actual credentials though.
            Action <CallInvoker> validator = invoker =>
            {
                Assert.Same(invoker, CustomInvoker);
                Assert.Null(builder.ChannelCreated);
            };

            await ValidateResultAsync(builder, validator);
        }
        public async Task QuotaProject()
        {
            var builder = new SampleClientBuilder {
                QuotaProject = SampleQuotaProject
            };

            // We won't use the channel pool when there is a quota project.
            // We can't easily check anything about the actual credentials though.
            Action <CallInvoker> validator = invoker =>
            {
                Assert.NotNull(builder.ChannelCreated);
                Assert.Same(builder.ChannelCreated, GetChannel(invoker));
                Assert.Equal(SampleClientBuilder.DefaultEndpoint, builder.EndpointUsedToCreateChannel);
            };

            await ValidateResultAsync(builder, validator);
        }
        public async Task TokenAccessMethod()
        {
            var builder = new SampleClientBuilder {
                TokenAccessMethod = CustomTokenAccess
            };

            // We won't use the channel pool when there are custom scopes.
            // We can't easily check anything about the actual credentials though.
            Action <CallInvoker> validator = invoker =>
            {
                Assert.NotNull(builder.ChannelCreated);
                Assert.Same(builder.ChannelCreated, GetChannel(invoker));
                Assert.Equal(SampleClientBuilder.DefaultEndpoint, builder.EndpointUsedToCreateChannel);
            };

            await ValidateResultAsync(builder, validator);
        }
        public async Task CustomChannelCredentialsAndEndpoint()
        {
            var endpoint = new ServiceEndpoint("custom.nowhere.com", 443);
            var builder  = new SampleClientBuilder {
                ChannelCredentials = ChannelCredentials.Insecure, Endpoint = endpoint
            };

            Action <CallInvoker> validator = invoker =>
            {
                Assert.NotNull(builder.ChannelCreated);
                Assert.Same(builder.ChannelCreated, GetChannel(invoker));
                Assert.Equal(endpoint, builder.EndpointUsedToCreateChannel);
                Assert.Same(builder.ChannelCredentials, builder.CredentialsUsedToCreateChannel);
            };

            await ValidateResultAsync(builder, validator);
        }
        public async Task JsonCredentials()
        {
            var builder = new SampleClientBuilder {
                JsonCredentials = DummyServiceAccountCredentialFileContents
            };

            // We won't use the channel pool when custom credentials are specified.
            // We can't easily check anything about the actual credentials though.
            Action <CallInvoker> validator = invoker =>
            {
                Assert.NotNull(builder.ChannelCreated);
                Assert.Same(builder.ChannelCreated, GetChannel(invoker));
                Assert.Equal(SampleClientBuilder.DefaultEndpoint, builder.EndpointUsedToCreateChannel);
            };

            await ValidateResultAsync(builder, validator);
        }
Esempio n. 29
0
        public async Task CredentialsFilePath()
        {
            var file = Path.GetTempFileName();

            File.WriteAllText(file, DummyServiceAccountCredentialFileContents);

            try
            {
                var builder = new SampleClientBuilder {
                    CredentialsPath = file
                };
                await ValidateResultAsync(builder, AssertNonChannelPool(builder));
            }
            finally
            {
                File.Delete(file);
            }
        }
        public async Task CredentialsFilePath()
        {
            var file = Path.GetTempFileName();

            File.WriteAllText(file, DummyServiceAccountCredentialFileContents);

            try
            {
                var builder = new SampleClientBuilder {
                    CredentialsPath = file
                };
                await ValidateResultAsync(builder, initializer => Assert.IsAssignableFrom <GoogleCredential>(initializer.HttpClientInitializer));
            }
            finally
            {
                File.Delete(file);
            }
        }