From 06b699ec5c6df6200b393c4f418be347bf3650fc Mon Sep 17 00:00:00 2001 From: Waldek Mastykarz Date: Wed, 21 Oct 2026 15:55:41 +0200 Subject: [PATCH] Report unset API Center environment variables. Closes #1968 When an API Center setting references an environment variable with @NAME and the variable isn't set, throw an error naming the variable instead of silently using the literal @NAME value. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 53023532-b4f0-4cd7-a238-b890b15ce83a --- .../ApiCenterClientTests.cs | 52 +++++++++++++++++++ DevProxy.Plugins/ApiCenter/ApiCenterClient.cs | 34 ++++++------ 2 files changed, 70 insertions(+), 16 deletions(-) create mode 100644 DevProxy.Integration.Tests/ApiCenterClientTests.cs diff --git a/DevProxy.Integration.Tests/ApiCenterClientTests.cs b/DevProxy.Integration.Tests/ApiCenterClientTests.cs new file mode 100644 index 000000000..f1f6164ca --- /dev/null +++ b/DevProxy.Integration.Tests/ApiCenterClientTests.cs @@ -0,0 +1,52 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using DevProxy.Plugins.ApiCenter; +using Microsoft.Extensions.Logging.Abstractions; +using Xunit; + +namespace DevProxy.Integration.Tests; + +public class ApiCenterClientTests +{ + private const string VariableName = "DEVPROXY_TEST_APICENTER_SUBSCRIPTION_ID"; + + [Fact] + public void Constructor_UnsetEnvironmentVariable_Throws() + { + Environment.SetEnvironmentVariable(VariableName, null); + + var ex = Assert.Throws(() => CreateClient($"@{VariableName}")); + + Assert.Contains(VariableName, ex.Message, StringComparison.Ordinal); + Assert.Contains("subscriptionId", ex.Message, StringComparison.Ordinal); + } + + [Fact] + public void Constructor_SetEnvironmentVariable_UsesItsValue() + { + Environment.SetEnvironmentVariable(VariableName, "00000000-0000-0000-0000-000000000000"); + try + { + var configuration = NewConfiguration($"@{VariableName}"); + using var client = new ApiCenterClient(configuration, NullLogger.Instance); + + Assert.Equal("00000000-0000-0000-0000-000000000000", configuration.SubscriptionId); + } + finally + { + Environment.SetEnvironmentVariable(VariableName, null); + } + } + + private static ApiCenterClient CreateClient(string subscriptionId) => + new(NewConfiguration(subscriptionId), NullLogger.Instance); + + private static ApiCenterClientConfiguration NewConfiguration(string subscriptionId) => new() + { + SubscriptionId = subscriptionId, + ResourceGroupName = "rg", + ServiceName = "svc" + }; +} diff --git a/DevProxy.Plugins/ApiCenter/ApiCenterClient.cs b/DevProxy.Plugins/ApiCenter/ApiCenterClient.cs index 0116c9bdc..0ca431749 100644 --- a/DevProxy.Plugins/ApiCenter/ApiCenterClient.cs +++ b/DevProxy.Plugins/ApiCenter/ApiCenterClient.cs @@ -56,22 +56,10 @@ internal ApiCenterClient(ApiCenterClientConfiguration configuration, ILogger GetAccessTokenAsync(CancellationToken cancellationToken) => _authenticationHandler.GetAccessTokenAsync(cancellationToken);