Skip to content

Commit

Permalink
Add header with parameters used when fetching options
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronny Birkeli committed Aug 17, 2023
1 parent cbefbb7 commit 8f77876
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 37 deletions.
5 changes: 5 additions & 0 deletions src/Altinn.App.Api/Controllers/OptionsController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Altinn.App.Core.Extensions;
using Altinn.App.Core.Features.Options;
using Altinn.App.Core.Models;
using Microsoft.AspNetCore.Authorization;
Expand Down Expand Up @@ -46,6 +47,8 @@ public async Task<IActionResult> Get(
return NotFound();
}

HttpContext.Response.Headers.Add("Altinn-DownstreamParameters", appOptions.Parameters.ToNameValueString(','));

return Ok(appOptions.Options);
}

Expand Down Expand Up @@ -85,6 +88,8 @@ public async Task<IActionResult> Get(
return NotFound();
}

HttpContext.Response.Headers.Add("Altinn-DownstreamParameters", appOptions.Parameters.ToNameValueString(','));

return Ok(appOptions.Options);
}
}
Expand Down
32 changes: 32 additions & 0 deletions src/Altinn.App.Core/Extensions/DictionaryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Text;

namespace Altinn.App.Core.Extensions
{
/// <summary>
/// Extension methods for <see cref="Dictionary{TKey, TValue}"/>
/// </summary>
public static class DictionaryExtensions
{
/// <summary>
/// Converts a dictionary to a name value string on the form key1=value1,key2=value2
/// </summary>
public static string ToNameValueString(this Dictionary<string, string> parameters, char separator)
{
if (parameters == null)
{
return string.Empty;
}

StringBuilder builder = new();
foreach (var param in parameters)
{
if (builder.Length > 0)
{
builder.Append(separator);
}
builder.Append(param.Key + "=" + param.Value);
}
return builder.ToString();
}
}
}
1 change: 1 addition & 0 deletions test/Altinn.App.Core.Tests/Altinn.App.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
</ItemGroup>

<ItemGroup>
<Watch Remove="Extensions\DictionaryExtensionsTests.cs" />
<Watch Remove="Internal\Pdf\PdfServiceTests.cs" />
<Watch Remove="Mocks\RequestInterceptor.cs" />
</ItemGroup>
Expand Down
62 changes: 62 additions & 0 deletions test/Altinn.App.Core.Tests/Extensions/DictionaryExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using Altinn.App.Core.Extensions;
using Altinn.App.Core.Models;
using Microsoft.AspNetCore.Http;
using Xunit;

namespace Altinn.App.Core.Tests.Extensions
{
public class DictionaryExtensionsTests
{
[Fact]
public void ToNameValueString_OptionParameters_ShouldConvertToHttpHeaderFormat()
{
var options = new AppOptions
{
Parameters = new Dictionary<string, string>
{
{ "lang", "nb" },
{ "level", "1" }
},
};

IHeaderDictionary headers = new HeaderDictionary
{
{ "Altinn-DownstreamParameters", options.Parameters.ToNameValueString(',') }
};

Assert.Equal("lang=nb,level=1", headers["Altinn-DownstreamParameters"]);
}

[Fact]
public void ToNameValueString_OptionParametersWithEmptyValue_ShouldConvertToHttpHeaderFormat()
{
var options = new AppOptions
{
Parameters = new Dictionary<string, string>()
};

IHeaderDictionary headers = new HeaderDictionary
{
{ "Altinn-DownstreamParameters", options.Parameters.ToNameValueString(',') }
};

Assert.Equal(string.Empty, headers["Altinn-DownstreamParameters"]);
}

[Fact]
public void ToNameValueString_OptionParametersWithNullValue_ShouldConvertToHttpHeaderFormat()
{
var options = new AppOptions
{
Parameters = null
};

IHeaderDictionary headers = new HeaderDictionary
{
{ "Altinn-DownstreamParameters", options.Parameters.ToNameValueString(',') }
};

Assert.Equal(string.Empty, headers["Altinn-DownstreamParameters"]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ private static ValidationAppSI ConfigureMockServicesForValidation()
var appResourcesMock = new Mock<IAppResources>();
var appMetadataMock = new Mock<IAppMetadata>();
var objectModelValidatorMock = new Mock<IObjectModelValidator>();
var layoutEvaluatorStateInitializer = new LayoutEvaluatorStateInitializer(appResourcesMock.Object, Options.Create(new Configuration.FrontEndSettings()));
var layoutEvaluatorStateInitializer = new LayoutEvaluatorStateInitializer(appResourcesMock.Object, Microsoft.Extensions.Options.Options.Create(new Configuration.FrontEndSettings()));
var httpContextAccessorMock = new Mock<IHttpContextAccessor>();
var generalSettings = Options.Create(new Configuration.GeneralSettings());
var appSettings = Options.Create(new Configuration.AppSettings());
var generalSettings = Microsoft.Extensions.Options.Options.Create(new Configuration.GeneralSettings());
var appSettings = Microsoft.Extensions.Options.Options.Create(new Configuration.AppSettings());

var validationAppSI = new ValidationAppSI(
loggerMock.Object,
Expand Down
36 changes: 18 additions & 18 deletions test/Altinn.App.Core.Tests/Implementation/AppResourcesSITests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public class AppResourcesSITests
public void GetApplication_desrializes_file_from_disk()
{
AppSettings appSettings = GetAppSettings("AppMetadata", "default.applicationmetadata.json");
var settings = Options.Create<AppSettings>(appSettings);
IAppMetadata appMetadata = SetupAppMedata(Options.Create(appSettings));
var settings = Microsoft.Extensions.Options.Options.Create<AppSettings>(appSettings);
IAppMetadata appMetadata = SetupAppMedata(Microsoft.Extensions.Options.Options.Create(appSettings));
IAppResources appResources = new AppResourcesSI(settings, appMetadata, null, new NullLogger<AppResourcesSI>());
Application expected = new Application()
{
Expand Down Expand Up @@ -72,8 +72,8 @@ public void GetApplication_desrializes_file_from_disk()
public void GetApplication_handles_onEntry_null()
{
AppSettings appSettings = GetAppSettings("AppMetadata", "no-on-entry.applicationmetadata.json");
var settings = Options.Create<AppSettings>(appSettings);
IAppMetadata appMetadata = SetupAppMedata(Options.Create(appSettings));
var settings = Microsoft.Extensions.Options.Options.Create<AppSettings>(appSettings);
IAppMetadata appMetadata = SetupAppMedata(Microsoft.Extensions.Options.Options.Create(appSettings));
IAppResources appResources = new AppResourcesSI(settings, appMetadata, null, new NullLogger<AppResourcesSI>());
Application expected = new Application()
{
Expand Down Expand Up @@ -121,8 +121,8 @@ public void GetApplication_second_read_from_cache()
AppSettings appSettings = GetAppSettings("AppMetadata", "default.applicationmetadata.json");
Mock<IFrontendFeatures> appFeaturesMock = new Mock<IFrontendFeatures>();
appFeaturesMock.Setup(af => af.GetFrontendFeatures()).ReturnsAsync(new Dictionary<string, bool>() { { "footer", true } });
var settings = Options.Create<AppSettings>(appSettings);
IAppMetadata appMetadata = SetupAppMedata(Options.Create(appSettings), appFeaturesMock.Object);
var settings = Microsoft.Extensions.Options.Options.Create<AppSettings>(appSettings);
IAppMetadata appMetadata = SetupAppMedata(Microsoft.Extensions.Options.Options.Create(appSettings), appFeaturesMock.Object);
IAppResources appResources = new AppResourcesSI(settings, appMetadata, null, new NullLogger<AppResourcesSI>());
Application expected = new Application()
{
Expand Down Expand Up @@ -176,8 +176,8 @@ public void GetApplication_second_read_from_cache()
public void GetApplicationMetadata_throws_ApplicationConfigException_if_file_not_found()
{
AppSettings appSettings = GetAppSettings("AppMetadata", "notfound.applicationmetadata.json");
var settings = Options.Create<AppSettings>(appSettings);
IAppMetadata appMetadata = SetupAppMedata(Options.Create(appSettings));
var settings = Microsoft.Extensions.Options.Options.Create<AppSettings>(appSettings);
IAppMetadata appMetadata = SetupAppMedata(Microsoft.Extensions.Options.Options.Create(appSettings));
IAppResources appResources = new AppResourcesSI(settings, appMetadata, null, new NullLogger<AppResourcesSI>());
Assert.Throws<ApplicationConfigException>(() => appResources.GetApplication());
}
Expand All @@ -186,8 +186,8 @@ public void GetApplicationMetadata_throws_ApplicationConfigException_if_file_not
public void GetApplicationMetadata_throws_ApplicationConfigException_if_deserialization_fails()
{
AppSettings appSettings = GetAppSettings("AppMetadata", "invalid.applicationmetadata.json");
var settings = Options.Create<AppSettings>(appSettings);
IAppMetadata appMetadata = SetupAppMedata(Options.Create(appSettings));
var settings = Microsoft.Extensions.Options.Options.Create<AppSettings>(appSettings);
IAppMetadata appMetadata = SetupAppMedata(Microsoft.Extensions.Options.Options.Create(appSettings));
IAppResources appResources = new AppResourcesSI(settings, appMetadata, null, new NullLogger<AppResourcesSI>());
Assert.Throws<ApplicationConfigException>(() => appResources.GetApplication());
}
Expand All @@ -196,8 +196,8 @@ public void GetApplicationMetadata_throws_ApplicationConfigException_if_deserial
public void GetApplicationXACMLPolicy_return_policyfile_as_string()
{
AppSettings appSettings = GetAppSettings(subfolder: "AppPolicy", policyFilename: "policy.xml");
var settings = Options.Create<AppSettings>(appSettings);
IAppMetadata appMetadata = SetupAppMedata(Options.Create(appSettings));
var settings = Microsoft.Extensions.Options.Options.Create<AppSettings>(appSettings);
IAppMetadata appMetadata = SetupAppMedata(Microsoft.Extensions.Options.Options.Create(appSettings));
IAppResources appResources = new AppResourcesSI(settings, appMetadata, null, new NullLogger<AppResourcesSI>());
string expected = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + Environment.NewLine + "<root>policy</root>";
var actual = appResources.GetApplicationXACMLPolicy();
Expand All @@ -208,8 +208,8 @@ public void GetApplicationXACMLPolicy_return_policyfile_as_string()
public void GetApplicationXACMLPolicy_return_null_if_file_not_found()
{
AppSettings appSettings = GetAppSettings(subfolder: "AppPolicy", policyFilename: "notfound.xml");
var settings = Options.Create<AppSettings>(appSettings);
IAppMetadata appMetadata = SetupAppMedata(Options.Create(appSettings));
var settings = Microsoft.Extensions.Options.Options.Create<AppSettings>(appSettings);
IAppMetadata appMetadata = SetupAppMedata(Microsoft.Extensions.Options.Options.Create(appSettings));
IAppResources appResources = new AppResourcesSI(settings, appMetadata, null, new NullLogger<AppResourcesSI>());
var actual = appResources.GetApplicationXACMLPolicy();
actual.Should().BeNull();
Expand All @@ -219,8 +219,8 @@ public void GetApplicationXACMLPolicy_return_null_if_file_not_found()
public void GetApplicationBPMNProcess_return_process_as_string()
{
AppSettings appSettings = GetAppSettings(subfolder: "AppProcess", bpmnFilename: "process.bpmn");
var settings = Options.Create<AppSettings>(appSettings);
IAppMetadata appMetadata = SetupAppMedata(Options.Create(appSettings));
var settings = Microsoft.Extensions.Options.Options.Create<AppSettings>(appSettings);
IAppMetadata appMetadata = SetupAppMedata(Microsoft.Extensions.Options.Options.Create(appSettings));
IAppResources appResources = new AppResourcesSI(settings, appMetadata, null, new NullLogger<AppResourcesSI>());
string expected = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + Environment.NewLine + "<root>process</root>";
var actual = appResources.GetApplicationBPMNProcess();
Expand All @@ -231,8 +231,8 @@ public void GetApplicationBPMNProcess_return_process_as_string()
public void GetApplicationBPMNProcess_return_null_if_file_not_found()
{
AppSettings appSettings = GetAppSettings(subfolder: "AppProcess", policyFilename: "notfound.xml");
var settings = Options.Create<AppSettings>(appSettings);
IAppMetadata appMetadata = SetupAppMedata(Options.Create(appSettings));
var settings = Microsoft.Extensions.Options.Options.Create<AppSettings>(appSettings);
IAppMetadata appMetadata = SetupAppMedata(Microsoft.Extensions.Options.Options.Create(appSettings));
IAppResources appResources = new AppResourcesSI(settings, appMetadata, null, new NullLogger<AppResourcesSI>());
var actual = appResources.GetApplicationBPMNProcess();
actual.Should().BeNull();
Expand Down
Loading

0 comments on commit 8f77876

Please sign in to comment.