Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: invalid content type format exception #102

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Security.Cryptography;
using System.Text;
using AzureAppConfigurationEmulator.Common;
using OpenTelemetry.Trace;

namespace AzureAppConfigurationEmulator.ConfigurationSettings;

Expand Down Expand Up @@ -41,11 +42,21 @@ public ConfigurationSetting Create(
string? value = null,
IDictionary<string, string>? 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,
Expand All @@ -55,6 +66,11 @@ public ConfigurationSetting Create(
label,
contentType,
tags);
}
}
catch (Exception e)
{
activity?.RecordException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}
Loading