From 239672569562ab641ed11ff2afe0219323c064e7 Mon Sep 17 00:00:00 2001 From: Thomas Clark Date: Thu, 14 Nov 2024 07:31:59 +0000 Subject: [PATCH] fix: invalid content type format exception --- .../ConfigurationSettingFactory.cs | 20 +++++++++-- .../ConfigurationSettingFactoryTests.cs | 35 +++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 tests/AzureAppConfigurationEmulator.Tests/ConfigurationSettings/ConfigurationSettingFactoryTests.cs diff --git a/src/AzureAppConfigurationEmulator/ConfigurationSettings/ConfigurationSettingFactory.cs b/src/AzureAppConfigurationEmulator/ConfigurationSettings/ConfigurationSettingFactory.cs index 06117b2..72f3737 100644 --- a/src/AzureAppConfigurationEmulator/ConfigurationSettings/ConfigurationSettingFactory.cs +++ b/src/AzureAppConfigurationEmulator/ConfigurationSettings/ConfigurationSettingFactory.cs @@ -2,6 +2,7 @@ using System.Security.Cryptography; using System.Text; using AzureAppConfigurationEmulator.Common; +using OpenTelemetry.Trace; namespace AzureAppConfigurationEmulator.ConfigurationSettings; @@ -41,11 +42,21 @@ public ConfigurationSetting Create( string? value = null, IDictionary? tags = null) { + using var activity = Telemetry.ActivitySource.StartActivity($"{nameof(ConfigurationSettingFactory)}.{nameof(Create)}"); + activity?.SetTag(Telemetry.ConfigurationSettingEtag, etag); + activity?.SetTag(Telemetry.ConfigurationSettingKey, key); + activity?.SetTag(Telemetry.ConfigurationSettingLabel, label); + activity?.SetTag(Telemetry.ConfigurationSettingContentType, contentType); + activity?.SetTag(Telemetry.ConfigurationSettingValue, value); + activity?.SetTag(Telemetry.ConfigurationSettingLastModified, lastModified); + activity?.SetTag(Telemetry.ConfigurationSettingLocked, locked); + if (!string.IsNullOrEmpty(contentType) && !string.IsNullOrEmpty(value)) { - switch (new ContentType(contentType).MediaType) + try { - case MediaType.FeatureFlag: + if (new ContentType(contentType).MediaType is MediaType.FeatureFlag) + { return new FeatureFlagConfigurationSetting( etag, key, @@ -55,6 +66,11 @@ public ConfigurationSetting Create( label, contentType, tags); + } + } + catch (Exception e) + { + activity?.RecordException(e); } } diff --git a/tests/AzureAppConfigurationEmulator.Tests/ConfigurationSettings/ConfigurationSettingFactoryTests.cs b/tests/AzureAppConfigurationEmulator.Tests/ConfigurationSettings/ConfigurationSettingFactoryTests.cs new file mode 100644 index 0000000..8fbd30c --- /dev/null +++ b/tests/AzureAppConfigurationEmulator.Tests/ConfigurationSettings/ConfigurationSettingFactoryTests.cs @@ -0,0 +1,35 @@ +using AzureAppConfigurationEmulator.ConfigurationSettings; +using NUnit.Framework; + +namespace AzureAppConfigurationEmulator.Tests.ConfigurationSettings; + +public class ConfigurationSettingFactoryTests +{ + private ConfigurationSettingFactory Factory { get; set; } + + [SetUp] + public void SetUp() + { + Factory = new ConfigurationSettingFactory(); + } + + [TestCase("application/json", typeof(ConfigurationSetting))] + [TestCase("application/json;charset=utf-8", typeof(ConfigurationSetting))] + [TestCase("application/vnd.microsoft.appconfig.ff+json", typeof(FeatureFlagConfigurationSetting))] + [TestCase("application/vnd.microsoft.appconfig.ff+json;charset=utf-8", typeof(FeatureFlagConfigurationSetting))] + [TestCase("Invalid.Content.Type", typeof(ConfigurationSetting))] + [TestCase(null, typeof(ConfigurationSetting))] + public void Create_ConfigurationSetting_ContentType(string? contentType, Type expected) + { + // Arrange + const string key = "TestKey"; + const string label = "TestLabel"; + const string value = "{\"id\":\"TestId\",\"enabled\":true}"; + + // Act + var setting = Factory.Create(key, label, contentType, value); + + // Assert + Assert.That(setting, Is.TypeOf(expected)); + } +}