|
3 | 3 | using System.Collections.Specialized;
|
4 | 4 | using System.Configuration;
|
5 | 5 | using System.Linq;
|
| 6 | +using System.Net.Http; |
| 7 | +using System.Net; |
| 8 | +using System.Threading; |
| 9 | +using System.Threading.Tasks; |
6 | 10 | using Azure;
|
7 | 11 | using Azure.Core;
|
| 12 | +using Azure.Core.Pipeline; |
8 | 13 | using Azure.Data.AppConfiguration;
|
9 | 14 | using Azure.Identity;
|
10 | 15 | using Azure.Security.KeyVault.Secrets;
|
@@ -679,6 +684,20 @@ public void AzureAppConfig_ErrorsOptional(KeyValueEnabled enabled)
|
679 | 684 | else
|
680 | 685 | Assert.Null(exception);
|
681 | 686 |
|
| 687 | + // Throttling should produce an exception |
| 688 | + // Causing this requires GetValue(), but builder won'te ever call GetValue() when disabled. |
| 689 | + if (enabled != KeyValueEnabled.Disabled) |
| 690 | + { |
| 691 | + exception = Record.Exception(() => |
| 692 | + { |
| 693 | + // Create a subclass that injects a transport returning HTTP 429 |
| 694 | + builder = TestHelper.CreateBuilder<ThrottleTestAppConfigBuilder>(() => new ThrottleTestAppConfigBuilder(), "AzureAppConfigThrottleTest", |
| 695 | + new NameValueCollection() { { "endpoint", AppConfigFixture.CustomEndPoint }, { "enabled", KeyValueEnabled.Enabled.ToString() } }); |
| 696 | + builder.GetValue("anyKey"); |
| 697 | + }); |
| 698 | + TestHelper.ValidateBasicException<RequestFailedException>(exception, "Service request failed.", "Too many requests"); |
| 699 | + } |
| 700 | + |
682 | 701 | // These tests require executing the builder, which needs a valid endpoint.
|
683 | 702 | if (AppConfigFixture.FullStackTestsEnabled)
|
684 | 703 | {
|
@@ -925,5 +944,29 @@ public override ICollection<KeyValuePair<string, string>> GetAllValues(string pr
|
925 | 944 | return base.GetAllValues(prefix);
|
926 | 945 | }
|
927 | 946 | }
|
| 947 | + |
| 948 | + // Minimal override to force a 429 response |
| 949 | + private class ThrottleTestAppConfigBuilder : AzureAppConfigurationBuilder |
| 950 | + { |
| 951 | + protected override Azure.Data.AppConfiguration.ConfigurationClientOptions GetConfigurationClientOptions() |
| 952 | + { |
| 953 | + var httpClient = new HttpClient(new ThrottlingHandler()); |
| 954 | + var options = base.GetConfigurationClientOptions(); |
| 955 | + options.Transport = new HttpClientTransport(httpClient); |
| 956 | + return options; |
| 957 | + } |
| 958 | + |
| 959 | + private class ThrottlingHandler : HttpMessageHandler |
| 960 | + { |
| 961 | + protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) |
| 962 | + { |
| 963 | + var response = new HttpResponseMessage((HttpStatusCode)429) |
| 964 | + { |
| 965 | + Content = new StringContent("Too many requests") |
| 966 | + }; |
| 967 | + return Task.FromResult(response); |
| 968 | + } |
| 969 | + } |
| 970 | + } |
928 | 971 | }
|
929 | 972 | }
|
0 commit comments