public RemoteConfigurationProvider(RemoteConfigurationSource source) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (!string.IsNullOrEmpty(source.ConfigurationKeyPrefix)) { if (source.ConfigurationKeyPrefix.Trim().StartsWith(":")) { throw new ArgumentException(string.Format(Resource.Error_InvalidStartCharacter, nameof(source.ConfigurationKeyPrefix), ':')); } if (source.ConfigurationKeyPrefix.Trim().EndsWith(":")) { throw new ArgumentException(string.Format(Resource.Error_InvalidEndCharacter, nameof(source.ConfigurationKeyPrefix), ':')); } } Source = source; Backchannel = new HttpClient(source.BackchannelHttpHandler ?? new HttpClientHandler()); Backchannel.DefaultRequestHeaders.UserAgent.ParseAdd("Remote Confiugration Provider"); Backchannel.Timeout = source.BackchannelTimeout; Backchannel.MaxResponseContentBufferSize = 1024 * 1024 * 10; // 10 MB Parser = source.Parser ?? new JsonConfigurationParser(); }
/// <summary> /// Adds a remote configuration source to <paramref name="builder"/>. /// </summary> /// <param name="builder">The <see cref="IConfigurationBuilder"/> to add to.</param> /// <param name="configurationUri">The remote uri to </param> /// <param name="optional">Whether the remote configuration source is optional.</param> /// <param name="events">Events that get add </param> /// <returns>The <see cref="IConfigurationBuilder"/>.</returns> public static IConfigurationBuilder AddRemoteSource(this IConfigurationBuilder builder, Uri configurationUri, bool optional, RemoteConfigurationEvents events) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } if (configurationUri == null) { throw new ArgumentNullException(nameof(configurationUri)); } if (events == null) { throw new ArgumentNullException(nameof(events)); } var source = new RemoteConfigurationSource { ConfigurationUri = configurationUri, Events = events, Optional = optional, }; return(builder.AddRemoteSource(source)); }
public void AddRemoteSource_ThrowsIfConfigurationPrefixStartsWithColon() { // Arrange var source = new RemoteConfigurationSource { ConfigurationUri = new Uri("http://localhost"), ConfigurationKeyPrefix = ":test", }; // Act and Assert Assert.Throws <ArgumentException>(() => new RemoteConfigurationProvider(source)); }
public void AddRemoteSource_InvalidToken() { // Arrange var source = new RemoteConfigurationSource { ConfigurationUri = new Uri("http://localhost"), AuthenticationType = AuthenticationType.BearerToken }; // Act and Assert ArgumentException ex = Assert.Throws <ArgumentException>(() => new RemoteConfigurationProvider(source)); Assert.Equal("AuthorizationToken can not be null or empty", ex.Message); }
public void AddRemoteSource_InvalidCredentials() { // Arrange var source = new RemoteConfigurationSource { ConfigurationUri = new Uri("http://localhost"), AuthenticationType = AuthenticationType.Basic }; // Act and Assert ArgumentException ex = Assert.Throws <ArgumentException>(() => new RemoteConfigurationProvider(source)); Assert.Equal("UserName or Password can not be null or empty", ex.Message); }
public RemoteConfigurationProvider(RemoteConfigurationSource source) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (!string.IsNullOrEmpty(source.ConfigurationKeyPrefix)) { if (source.ConfigurationKeyPrefix.Trim().StartsWith(":")) { throw new ArgumentException(string.Format(Resource.Error_InvalidStartCharacter, nameof(source.ConfigurationKeyPrefix), ':')); } if (source.ConfigurationKeyPrefix.Trim().EndsWith(":")) { throw new ArgumentException(string.Format(Resource.Error_InvalidEndCharacter, nameof(source.ConfigurationKeyPrefix), ':')); } } Source = source; Backchannel = new HttpClient(source.BackchannelHttpHandler ?? new HttpClientHandler()); Backchannel.DefaultRequestHeaders.UserAgent.ParseAdd("Remote Confiugration Provider"); Backchannel.Timeout = source.BackchannelTimeout; Backchannel.MaxResponseContentBufferSize = 1024 * 1024 * 10; // 10 MB if (source.AuthenticationType == AuthenticationType.Basic) { if (string.IsNullOrEmpty(source.UserName) || string.IsNullOrEmpty(source.Password)) { throw new ArgumentException("UserName or Password can not be null or empty"); } Backchannel.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue( "Basic", Convert.ToBase64String( System.Text.Encoding.ASCII.GetBytes( string.Format("{0}:{1}", source.UserName, source.Password)))); } else if (source.AuthenticationType == AuthenticationType.BearerToken) { if (string.IsNullOrEmpty(source.AuthorizationToken)) { throw new ArgumentException("AuthorizationToken can not be null or empty"); } Backchannel.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", source.AuthorizationToken); } Parser = source.Parser ?? new JsonConfigurationParser(); }
/// <summary> /// Adds a remote configuration source to <paramref name="builder"/>. /// </summary> /// <param name="builder">The <see cref="IConfigurationBuilder"/> to add to.</param> /// <param name="source">The remote configuration source settings</param> /// <returns>The <see cref="IConfigurationBuilder"/>.</returns> public static IConfigurationBuilder AddRemoteSource(this IConfigurationBuilder builder, RemoteConfigurationSource source) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } if (source == null) { throw new ArgumentNullException(nameof(source)); } builder.Add(source); return(builder); }