예제 #1
0
 private static IServiceCollection RegisterHttpClient(
     this IServiceCollection serviceCollection,
     HttPlaceholderClientConfiguration config)
 {
     serviceCollection.AddHttpClient <IHttPlaceholderClient, HttPlaceholderClient>(client => client.ApplyConfiguration(config));
     return(serviceCollection);
 }
 internal static void ApplyConfiguration(this HttpClient httpClient, HttPlaceholderClientConfiguration config)
 {
     httpClient.BaseAddress = new Uri(config.RootUrl);
     if (!string.IsNullOrWhiteSpace(config.Username) && !string.IsNullOrWhiteSpace(config.Password))
     {
         var auth = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{config.Username}:{config.Password}"));
         httpClient.DefaultRequestHeaders.Add("Authorization", $"Basic {auth}");
     }
 }
예제 #3
0
        /// <summary>
        /// Adds the <see cref="HttPlaceholderClient"/> to the service collection.
        /// </summary>
        /// <param name="serviceCollection">The service collection the client should be added to.</param>
        /// <param name="configAction">An action to configure the client.</param>
        /// <returns>The <see cref="IServiceCollection"/>.</returns>
        public static IServiceCollection AddHttPlaceholderClient(
            this IServiceCollection serviceCollection,
            Action <HttPlaceholderClientConfiguration> configAction = null)
        {
            var config = new HttPlaceholderClientConfiguration();

            configAction?.Invoke(config);
            config.Validate();
            return(serviceCollection.RegisterHttpClient(config));
        }
예제 #4
0
    public void RootUrl_EndsInSlash_ShouldNotAddExtraSlash()
    {
        // Arrange / Act
        var config = new HttPlaceholderClientConfiguration {
            RootUrl = "http://localhost:5000/"
        };

        // Assert
        Assert.AreEqual("http://localhost:5000/", config.RootUrl);
    }
예제 #5
0
    public void RootUrl_NoValueSet_ShouldLeaveRootUrlEmpty()
    {
        // Arrange / Act
        var config = new HttPlaceholderClientConfiguration {
            RootUrl = string.Empty
        };

        // Assert
        Assert.IsNull(config.RootUrl);
    }
예제 #6
0
        /// <summary>
        /// Method for creating the <see cref="IHttPlaceholderClient"/>.
        /// </summary>
        /// <param name="config">The configuration of the client.</param>
        /// <returns>The <see cref="IHttPlaceholderClient"/>.</returns>
        public static IHttPlaceholderClient CreateHttPlaceholderClient(HttPlaceholderClientConfiguration config)
        {
            config.Validate();
            if (_httpClient == null)
            {
                _httpClient = new HttpClient();
                _httpClient.ApplyConfiguration(config);
            }

            return(new HttPlaceholderClient(_httpClient));
        }
예제 #7
0
    public void Validate_RootUrlEmpty_ShouldThrowArgumentException()
    {
        // Arrange
        var config = new HttPlaceholderClientConfiguration {
            RootUrl = string.Empty
        };

        // Act
        var exception = Assert.ThrowsException <ArgumentException>(() => config.Validate());

        // Assert
        Assert.AreEqual("No value set for RootUrl in HttPlaceholder configuration.", exception.Message);
    }
예제 #8
0
    public void CreateHttPlaceholderClient_ShouldSetBaseUrl()
    {
        // Arrange
        var config = new HttPlaceholderClientConfiguration {
            RootUrl = "http://localhost:5000"
        };

        // Act
        var client = (HttPlaceholderClient)HttPlaceholderClientFactory.CreateHttPlaceholderClient(config);

        // Assert
        Assert.AreEqual("http://localhost:5000/", client.HttpClient.BaseAddress.OriginalString);
    }
예제 #9
0
    public void CreateHttPlaceholderClient_ShouldReuseHttpClient()
    {
        // Arrange
        var config = new HttPlaceholderClientConfiguration {
            RootUrl = "http://localhost:5000"
        };

        // Act
        var client1 = (HttPlaceholderClient)HttPlaceholderClientFactory.CreateHttPlaceholderClient(config);
        var client2 = (HttPlaceholderClient)HttPlaceholderClientFactory.CreateHttPlaceholderClient(config);

        // Assert
        Assert.AreEqual(client1.HttpClient, client2.HttpClient);
    }
    public void ApplyConfiguration_NoAuthSet_ShouldOnlySetBaseUrl()
    {
        // Arrange
        var config = new HttPlaceholderClientConfiguration {
            RootUrl = "http://localhost:5000"
        };
        var httpClient = new HttpClient();

        // Act
        httpClient.ApplyConfiguration(config);

        // Assert
        Assert.AreEqual("http://localhost:5000/", httpClient.BaseAddress.OriginalString);
        Assert.IsFalse(httpClient.DefaultRequestHeaders.Any(h => h.Key == "Authorization"));
    }
    public void ApplyConfiguration_AuthSet_ShouldSetBaseUrlAndAuth()
    {
        // Arrange
        var config = new HttPlaceholderClientConfiguration
        {
            RootUrl = "http://localhost:5000", Username = "******", Password = "******"
        };
        var httpClient = new HttpClient();

        // Act
        httpClient.ApplyConfiguration(config);

        // Assert
        Assert.AreEqual("http://localhost:5000/", httpClient.BaseAddress.OriginalString);

        var authHeader    = httpClient.DefaultRequestHeaders.Single(h => h.Key == "Authorization");
        var base64Decoded =
            Encoding.UTF8.GetString(
                Convert.FromBase64String(authHeader.Value.Single().Replace("Basic ", string.Empty)));
        var parts = base64Decoded.Split(':');

        Assert.AreEqual("username", parts[0]);
        Assert.AreEqual("password", parts[1]);
    }