-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add header with parameters used when fetching options
- Loading branch information
Ronny Birkeli
committed
Aug 17, 2023
1 parent
cbefbb7
commit 8f77876
Showing
8 changed files
with
134 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
test/Altinn.App.Core.Tests/Extensions/DictionaryExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.