Skip to content

Commit 2396725

Browse files
committed
fix: invalid content type format exception
1 parent 86bc28a commit 2396725

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

src/AzureAppConfigurationEmulator/ConfigurationSettings/ConfigurationSettingFactory.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Security.Cryptography;
33
using System.Text;
44
using AzureAppConfigurationEmulator.Common;
5+
using OpenTelemetry.Trace;
56

67
namespace AzureAppConfigurationEmulator.ConfigurationSettings;
78

@@ -41,11 +42,21 @@ public ConfigurationSetting Create(
4142
string? value = null,
4243
IDictionary<string, string>? tags = null)
4344
{
45+
using var activity = Telemetry.ActivitySource.StartActivity($"{nameof(ConfigurationSettingFactory)}.{nameof(Create)}");
46+
activity?.SetTag(Telemetry.ConfigurationSettingEtag, etag);
47+
activity?.SetTag(Telemetry.ConfigurationSettingKey, key);
48+
activity?.SetTag(Telemetry.ConfigurationSettingLabel, label);
49+
activity?.SetTag(Telemetry.ConfigurationSettingContentType, contentType);
50+
activity?.SetTag(Telemetry.ConfigurationSettingValue, value);
51+
activity?.SetTag(Telemetry.ConfigurationSettingLastModified, lastModified);
52+
activity?.SetTag(Telemetry.ConfigurationSettingLocked, locked);
53+
4454
if (!string.IsNullOrEmpty(contentType) && !string.IsNullOrEmpty(value))
4555
{
46-
switch (new ContentType(contentType).MediaType)
56+
try
4757
{
48-
case MediaType.FeatureFlag:
58+
if (new ContentType(contentType).MediaType is MediaType.FeatureFlag)
59+
{
4960
return new FeatureFlagConfigurationSetting(
5061
etag,
5162
key,
@@ -55,6 +66,11 @@ public ConfigurationSetting Create(
5566
label,
5667
contentType,
5768
tags);
69+
}
70+
}
71+
catch (Exception e)
72+
{
73+
activity?.RecordException(e);
5874
}
5975
}
6076

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using AzureAppConfigurationEmulator.ConfigurationSettings;
2+
using NUnit.Framework;
3+
4+
namespace AzureAppConfigurationEmulator.Tests.ConfigurationSettings;
5+
6+
public class ConfigurationSettingFactoryTests
7+
{
8+
private ConfigurationSettingFactory Factory { get; set; }
9+
10+
[SetUp]
11+
public void SetUp()
12+
{
13+
Factory = new ConfigurationSettingFactory();
14+
}
15+
16+
[TestCase("application/json", typeof(ConfigurationSetting))]
17+
[TestCase("application/json;charset=utf-8", typeof(ConfigurationSetting))]
18+
[TestCase("application/vnd.microsoft.appconfig.ff+json", typeof(FeatureFlagConfigurationSetting))]
19+
[TestCase("application/vnd.microsoft.appconfig.ff+json;charset=utf-8", typeof(FeatureFlagConfigurationSetting))]
20+
[TestCase("Invalid.Content.Type", typeof(ConfigurationSetting))]
21+
[TestCase(null, typeof(ConfigurationSetting))]
22+
public void Create_ConfigurationSetting_ContentType(string? contentType, Type expected)
23+
{
24+
// Arrange
25+
const string key = "TestKey";
26+
const string label = "TestLabel";
27+
const string value = "{\"id\":\"TestId\",\"enabled\":true}";
28+
29+
// Act
30+
var setting = Factory.Create(key, label, contentType, value);
31+
32+
// Assert
33+
Assert.That(setting, Is.TypeOf(expected));
34+
}
35+
}

0 commit comments

Comments
 (0)