Skip to content

Commit bfed8a6

Browse files
authored
Additional overloads (#242)
* Add additional overload to avoid allocations where possible * Update public API
1 parent 36cdbf1 commit bfed8a6

File tree

3 files changed

+105
-2
lines changed

3 files changed

+105
-2
lines changed

src/NetEscapades.AspNetCore.SecurityHeaders/Headers/SourceCollection.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections;
23
using System.Collections.Generic;
34

@@ -31,11 +32,25 @@ public void Add(string source)
3132

3233
/// <summary>
3334
/// Adds a source to the collection for the given directive.
34-
/// Calls to <see cref="AddRange"/> are idempotent;
35+
/// Calls to <see cref="AddRange(System.ReadOnlySpan{string})"/> are idempotent;
3536
/// if the source has already been added, it will not be added again.
3637
/// </summary>
3738
/// <param name="sources">The sources to add</param>
38-
public void AddRange(IEnumerable<string> sources)
39+
public void AddRange(params ReadOnlySpan<string> sources)
40+
{
41+
foreach (var source in sources)
42+
{
43+
Add(source);
44+
}
45+
}
46+
47+
/// <summary>
48+
/// Adds a source to the collection for the given directive.
49+
/// Calls to <see cref="AddRange(IEnumerable{string})"/> are idempotent;
50+
/// if the source has already been added, it will not be added again.
51+
/// </summary>
52+
/// <param name="sources">The sources to add</param>
53+
public void AddRange(params IEnumerable<string> sources)
3954
{
4055
foreach (var source in sources)
4156
{

test/NetEscapades.AspNetCore.SecurityHeaders.Test/PublicApiTest.PublicApiHasNotChanged.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,7 @@ namespace NetEscapades.AspNetCore.SecurityHeaders.Headers
600600
public int Count { get; }
601601
public void Add(string source) { }
602602
public void AddRange(System.Collections.Generic.IEnumerable<string> sources) { }
603+
public void AddRange(System.ReadOnlySpan<string> sources) { }
603604
public System.Collections.Generic.IEnumerator<string> GetEnumerator() { }
604605
public bool Remove(string source) { }
605606
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using System.Collections.Generic;
2+
using FluentAssertions;
3+
using NetEscapades.AspNetCore.SecurityHeaders.Headers;
4+
using Xunit;
5+
6+
namespace NetEscapades.AspNetCore.SecurityHeaders.Test;
7+
8+
public class SourceCollectionTests
9+
{
10+
[Fact]
11+
public void AddsSourceInExpectedOrder()
12+
{
13+
string[] expected = ["Source 1", "Source 2", "Source 3"];
14+
var col = new SourceCollection();
15+
foreach (var source in expected)
16+
{
17+
col.Add(source);
18+
}
19+
20+
VerifyAsExpected(col, expected);
21+
}
22+
23+
[Fact]
24+
public void DuplicateSourcesAreNotAdded()
25+
{
26+
var col = new SourceCollection();
27+
col.Add("Source 1");
28+
col.Add("Source 1");
29+
col.Add("Source 2");
30+
col.Add("Source 3");
31+
col.Add("Source 3");
32+
33+
VerifyAsExpected(col, ["Source 1", "Source 2", "Source 3"]);
34+
}
35+
36+
[Fact]
37+
public void AddRageAddsSourceInExpectedOrder()
38+
{
39+
List<string> sources = ["Source 1", "Source 2", "Source 3"];
40+
var col = new SourceCollection();
41+
col.AddRange(sources);
42+
43+
VerifyAsExpected(col, sources);
44+
}
45+
46+
[Fact]
47+
public void AddRangeDuplicateSourcesAreNotAdded()
48+
{
49+
string[] expected = ["Source 1", "Source 2", "Source 3"];
50+
var col = new SourceCollection();
51+
col.AddRange([
52+
"Source 1",
53+
"Source 1",
54+
"Source 2",
55+
"Source 3",
56+
"Source 3",
57+
]);
58+
59+
VerifyAsExpected(col, expected);
60+
}
61+
62+
[Fact]
63+
public void RemoveRemovesSources()
64+
{
65+
string[] expected = ["Source 1", "Source 2", "Source 3"];
66+
var col = new SourceCollection();
67+
col.Add("To move");
68+
foreach (var source in expected)
69+
{
70+
col.Add(source);
71+
}
72+
73+
col.Remove("To move");
74+
75+
VerifyAsExpected(col, expected);
76+
}
77+
78+
private static void VerifyAsExpected(SourceCollection collection, IList<string> expected)
79+
{
80+
var i = 0;
81+
foreach (var source in collection)
82+
{
83+
source.Should().Be(expected[i]);
84+
i++;
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)