Skip to content

Commit 2565cb8

Browse files
Add throttling test for AppConfig to verify error. (#275)
1 parent 98bd142 commit 2565cb8

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

test/Microsoft.Configuration.ConfigurationBuilders.Test/AzureAppConfigTests.cs

+43
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
using System.Collections.Specialized;
44
using System.Configuration;
55
using System.Linq;
6+
using System.Net.Http;
7+
using System.Net;
8+
using System.Threading;
9+
using System.Threading.Tasks;
610
using Azure;
711
using Azure.Core;
12+
using Azure.Core.Pipeline;
813
using Azure.Data.AppConfiguration;
914
using Azure.Identity;
1015
using Azure.Security.KeyVault.Secrets;
@@ -679,6 +684,20 @@ public void AzureAppConfig_ErrorsOptional(KeyValueEnabled enabled)
679684
else
680685
Assert.Null(exception);
681686

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+
682701
// These tests require executing the builder, which needs a valid endpoint.
683702
if (AppConfigFixture.FullStackTestsEnabled)
684703
{
@@ -925,5 +944,29 @@ public override ICollection<KeyValuePair<string, string>> GetAllValues(string pr
925944
return base.GetAllValues(prefix);
926945
}
927946
}
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+
}
928971
}
929972
}

0 commit comments

Comments
 (0)