Skip to content

Commit dac30af

Browse files
authored
Update fluent assertions (#252)
* Update fluent assertions and add analyzers * Convert xunit assertions to fluent assertions
1 parent a8eb5db commit dac30af

13 files changed

+82
-77
lines changed

test/NetEscapades.AspNetCore.SecurityHeaders.TagHelpers.Test/AttributeHashTagHelperTests.cs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Threading.Tasks;
5+
using FluentAssertions;
56
using Microsoft.AspNetCore.Http;
67
using Microsoft.AspNetCore.Mvc;
78
using Microsoft.AspNetCore.Mvc.Abstractions;
@@ -45,9 +46,9 @@ public async Task ProcessAsync_StyleAttribute_GeneratesExpectedOutput()
4546
await tagHelper.ProcessAsync(fixture.Context, fixture.Output);
4647

4748
// Assert
48-
Assert.Equal(tagName, fixture.Output.TagName);
49-
Assert.Equal([styleAttribute], fixture.Output.Attributes);
50-
Assert.Empty(fixture.Output.Content.GetContent());
49+
fixture.Output.TagName.Should().Be(tagName);
50+
fixture.Output.Attributes.Should().BeEquivalentTo([styleAttribute]);
51+
fixture.Output.Content.GetContent().Should().BeEmpty();
5152
}
5253

5354
[Fact]
@@ -68,9 +69,9 @@ public async Task ProcessAsync_StyleAttribute_AddsHashToHttpContext()
6869
await tagHelper.ProcessAsync(fixture.Context, fixture.Output);
6970

7071
// Assert
71-
var hash = Assert.Single(tagHelper.ViewContext.HttpContext.GetStyleCSPHashes());
7272
var expected = "'sha256-NerDAUWfwD31YdZHveMrq0GLjsNFMwxLpZl0dPUeCcw='";
73-
Assert.Equal(expected, hash);
73+
var hash = tagHelper.ViewContext.HttpContext.GetStyleCSPHashes().Should().ContainSingle()
74+
.Which.Should().Be(expected);
7475
}
7576

7677
[Fact]
@@ -91,9 +92,9 @@ public async Task ProcessAsync_StyleAttributeWithExplicitHashType_AddsHashToHttp
9192
await tagHelper.ProcessAsync(fixture.Context, fixture.Output);
9293

9394
// Assert
94-
var hash = Assert.Single(tagHelper.ViewContext.HttpContext.GetStyleCSPHashes());
9595
var expected = "'sha384-YoSV9pxydVBLyyDpluNe9tQWgtUWlnzHS/zCvuNc30tEu0YwLQPRgNAXk+h06DXU'";
96-
Assert.Equal(expected, hash);
96+
tagHelper.ViewContext.HttpContext.GetStyleCSPHashes().Should().ContainSingle()
97+
.Which.Should().Be(expected);
9798
}
9899

99100
[Fact]
@@ -114,9 +115,9 @@ public async Task ProcessAsync_StyleAttributeWithMultiLine_AddsHashToHttpContext
114115
await tagHelper.ProcessAsync(fixture.Context, fixture.Output);
115116

116117
// Assert
117-
var hash = Assert.Single(tagHelper.ViewContext.HttpContext.GetStyleCSPHashes());
118118
var expected = "'sha256-ly/Q8sGjROqYelSQCwIsD00L09JdMcVcMFTDyK7N7GM='";
119-
Assert.Equal(expected, hash);
119+
tagHelper.ViewContext.HttpContext.GetStyleCSPHashes().Should().ContainSingle()
120+
.Which.Should().Be(expected);
120121
}
121122

122123
[Fact]
@@ -136,9 +137,9 @@ public async Task ProcessAsync_StyleAttributeTargetingNonExistingAttribute_Doesn
136137
await tagHelper.ProcessAsync(fixture.Context, fixture.Output);
137138

138139
// Assert
139-
Assert.Equal(tagName, fixture.Output.TagName);
140-
Assert.Empty(fixture.Output.Attributes);
141-
Assert.Empty(fixture.Output.Content.GetContent());
140+
fixture.Output.TagName.Should().Be(tagName);
141+
fixture.Output.Attributes.Should().BeEmpty();
142+
fixture.Output.Content.GetContent().Should().BeEmpty();
142143
}
143144

144145
[Fact]
@@ -159,9 +160,9 @@ public async Task ProcessAsync_InlineScriptAttribute_GeneratesExpectedOutput()
159160
await tagHelper.ProcessAsync(fixture.Context, fixture.Output);
160161

161162
// Assert
162-
Assert.Equal(tagName, fixture.Output.TagName);
163-
Assert.Equal([inlineScriptAttribute], fixture.Output.Attributes);
164-
Assert.Empty(fixture.Output.Content.GetContent());
163+
fixture.Output.TagName.Should().Be(tagName);
164+
fixture.Output.Attributes.Should().BeEquivalentTo([inlineScriptAttribute]);
165+
fixture.Output.Content.GetContent().Should().BeEmpty();
165166
}
166167

167168
[Fact]
@@ -182,9 +183,9 @@ public async Task ProcessAsync_InlineScriptAttribute_AddsHashToHttpContext()
182183
await tagHelper.ProcessAsync(fixture.Context, fixture.Output);
183184

184185
// Assert
185-
var hash = Assert.Single(tagHelper.ViewContext.HttpContext.GetScriptCSPHashes());
186186
var expected = "'sha256-1lzfyKjJuCLGsHTaOB3al0SElf3ats68l7XOAdrWd+E='";
187-
Assert.Equal(expected, hash);
187+
tagHelper.ViewContext.HttpContext.GetScriptCSPHashes().Should().ContainSingle()
188+
.Which.Should().Be(expected);
188189
}
189190

190191
[Fact]
@@ -215,13 +216,12 @@ public async Task ProcessAsync_MultipleAttributes_AddsAllHashesToHttpContext()
215216
await tagHelper.ProcessAsync(fixture.Context, fixture.Output);
216217

217218
// Assert
218-
var styleHash = Assert.Single(tagHelper.ViewContext.HttpContext.GetStyleCSPHashes());
219-
var scriptHash = Assert.Single(tagHelper.ViewContext.HttpContext.GetScriptCSPHashes());
220219
var expectedStyleHash = "'sha256-NerDAUWfwD31YdZHveMrq0GLjsNFMwxLpZl0dPUeCcw='";
221220
var expectedScriptHash = "'sha256-1lzfyKjJuCLGsHTaOB3al0SElf3ats68l7XOAdrWd+E='";
222-
223-
Assert.Equal(expectedStyleHash, styleHash);
224-
Assert.Equal(expectedScriptHash, scriptHash);
221+
tagHelper.ViewContext.HttpContext.GetStyleCSPHashes().Should().ContainSingle()
222+
.Which.Should().Be(expectedStyleHash);
223+
tagHelper.ViewContext.HttpContext.GetScriptCSPHashes().Should().ContainSingle()
224+
.Which.Should().Be(expectedScriptHash);
225225
}
226226

227227
private static Fixture CreateFixture(string id, string tagName, params TagHelperAttribute[] attributes)

test/NetEscapades.AspNetCore.SecurityHeaders.TagHelpers.Test/HashTagHelperTests.cs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Threading.Tasks;
5+
using FluentAssertions;
16
using Microsoft.AspNetCore.Http;
27
using Microsoft.AspNetCore.Mvc;
38
using Microsoft.AspNetCore.Mvc.Abstractions;
@@ -8,10 +13,6 @@
813
using Microsoft.AspNetCore.Razor.TagHelpers;
914
using Microsoft.AspNetCore.Routing;
1015
using Moq;
11-
using System;
12-
using System.Collections.Generic;
13-
using System.IO;
14-
using System.Threading.Tasks;
1516
using NetEscapades.AspNetCore.SecurityHeaders.Infrastructure;
1617
using Xunit;
1718

@@ -58,9 +59,9 @@ public async Task ProcessAsync_Script_GeneratesExpectedOutput()
5859
await tagHelper.ProcessAsync(tagHelperContext, output);
5960

6061
// Assert
61-
Assert.Equal(tagName, output.TagName);
62-
Assert.Empty(output.Attributes);
63-
Assert.Equal(shortSnippet, output.Content.GetContent());
62+
output.TagName.Should().Be(tagName);
63+
output.Attributes.Should().BeEmpty();
64+
output.Content.GetContent().Should().Be(shortSnippet);
6465
}
6566

6667
[Fact]
@@ -90,9 +91,9 @@ public async Task ProcessAsync_Script_AddsHashToHttpContextForOneLineSnippets()
9091
await tagHelper.ProcessAsync(tagHelperContext, output);
9192

9293
// Assert
93-
var hash = Assert.Single(tagHelper.ViewContext.HttpContext.GetScriptCSPHashes());
9494
var expected = "'sha256-1yLcYHZUiV92moZ6snTrg6e0yBO8emEEpUSB2wlMFz8='";
95-
Assert.Equal(expected, hash);
95+
tagHelper.ViewContext.HttpContext.GetScriptCSPHashes().Should().ContainSingle()
96+
.Which.Should().Be(expected);
9697
}
9798

9899
[Fact]
@@ -122,9 +123,9 @@ public async Task ProcessAsync_Script_AddsHashToHttpContextForMultiLineSnippets(
122123
await tagHelper.ProcessAsync(tagHelperContext, output);
123124

124125
// Assert
125-
var hash = Assert.Single(tagHelper.ViewContext.HttpContext.GetScriptCSPHashes());
126126
var expected = "'sha256-Oro8tit8euyKzxqyJteBRTdQBlipvXGQWfS5epMHmUU='";
127-
Assert.Equal(expected, hash);
127+
tagHelper.ViewContext.HttpContext.GetScriptCSPHashes().Should().ContainSingle()
128+
.Which.Should().Be(expected);
128129
}
129130

130131
[Fact]
@@ -156,9 +157,9 @@ public async Task ProcessAsync_Style_GeneratesExpectedOutput()
156157
await tagHelper.ProcessAsync(tagHelperContext, output);
157158

158159
// Assert
159-
Assert.Equal(tagName, output.TagName);
160-
Assert.Empty(output.Attributes);
161-
Assert.Equal(styleSnippet, output.Content.GetContent());
160+
output.TagName.Should().Be(tagName);
161+
output.Attributes.Should().BeEmpty();
162+
output.Content.GetContent().Should().Be(styleSnippet);
162163
}
163164

164165
[Fact]
@@ -188,9 +189,9 @@ public async Task ProcessAsync_Style_AddsHashToHttpContextForOneLineSnippets()
188189
await tagHelper.ProcessAsync(tagHelperContext, output);
189190

190191
// Assert
191-
var hash = Assert.Single(tagHelper.ViewContext.HttpContext.GetStyleCSPHashes());
192192
var expected = "'sha256-Wz9o8J/ijdXtAzs95rmQ8OtBacYk6JfYTXQlM8yxIjg='";
193-
Assert.Equal(expected, hash);
193+
tagHelper.ViewContext.HttpContext.GetStyleCSPHashes().Should().ContainSingle()
194+
.Which.Should().Be(expected);
194195
}
195196

196197
private static ViewContext GetViewContext()

test/NetEscapades.AspNetCore.SecurityHeaders.TagHelpers.Test/NetEscapades.AspNetCore.SecurityHeaders.TagHelpers.Test.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9+
<PackageReference Include="FluentAssertions" Version="7.2.0" />
10+
<PackageReference Include="FluentAssertions.Analyzers" Version="0.34.1" PrivateAssets="all" IncludeAssets="analyzers" />
911
<PackageReference Include="PublicApiGenerator" Version="11.1.0" />
1012
<PackageReference Include="Verify.Xunit" Version="18.4.0" />
1113
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />

test/NetEscapades.AspNetCore.SecurityHeaders.TagHelpers.Test/NonceTagHelperTests.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Threading.Tasks;
5+
using FluentAssertions;
16
using Microsoft.AspNetCore.Http;
27
using Microsoft.AspNetCore.Mvc;
38
using Microsoft.AspNetCore.Mvc.Abstractions;
@@ -10,10 +15,6 @@
1015
using Microsoft.Extensions.DependencyInjection;
1116
using Microsoft.Extensions.Logging;
1217
using Moq;
13-
using System;
14-
using System.Collections.Generic;
15-
using System.IO;
16-
using System.Threading.Tasks;
1718
using Xunit;
1819

1920
namespace NetEscapades.AspNetCore.SecurityHeaders.TagHelpers.Test;
@@ -49,11 +50,11 @@ public async Task ProcessAsync_GeneratesExpectedOutput()
4950
await nonceTagHelper.ProcessAsync(tagHelperContext, output);
5051

5152
// Assert
52-
Assert.Equal(tagName, output.TagName);
53-
Assert.Single(output.Attributes);
54-
var attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("nonce"));
55-
Assert.Equal(nonceValue, attribute.Value);
56-
Assert.Equal("Something Else", output.Content.GetContent());
53+
output.TagName.Should().Be(tagName);
54+
var attribute = output.Attributes.Should().ContainSingle().Subject;
55+
attribute.Name.Should().Be("nonce");
56+
attribute.Value.Should().Be(nonceValue);
57+
output.Content.GetContent().Should().Be("Something Else");
5758
}
5859

5960
[Fact]
@@ -85,9 +86,9 @@ public async Task ProcessAsync_WhenNoNonceDoesNotAdd()
8586
await nonceTagHelper.ProcessAsync(tagHelperContext, output);
8687

8788
// Assert
88-
Assert.Equal(tagName, output.TagName);
89-
Assert.Empty(output.Attributes);
90-
Assert.Equal("Something Else", output.Content.GetContent());
89+
output.TagName.Should().Be(tagName);
90+
output.Attributes.Should().BeEmpty();
91+
output.Content.GetContent().Should().Be("Something Else");
9192
}
9293

9394
private static ViewContext GetViewContext(string nonce)

test/NetEscapades.AspNetCore.SecurityHeaders.Test/CustomHeaderExtensionsTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.AspNetCore.Builder;
22
using System;
3+
using FluentAssertions;
34
using Xunit;
45

56
namespace NetEscapades.AspNetCore.SecurityHeaders.Test;
@@ -12,8 +13,8 @@ public void AddCustomHeader_WhenNullHeader_IncludesHeaderInTheMessage()
1213
// https://github.com/andrewlock/NetEscapades.AspNetCore.SecurityHeaders/issues/35
1314
var collection = new HeaderPolicyCollection();
1415

15-
Assert.Throws<ArgumentNullException>(
16-
"header", () => collection.AddCustomHeader(null!, "asdf"));
16+
var add = () => collection.AddCustomHeader(null!, "asdf");
17+
add.Should().Throw<ArgumentNullException>();
1718
}
1819

1920
[Fact]

test/NetEscapades.AspNetCore.SecurityHeaders.Test/CustomHeadersResultTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Xunit;
1+
using FluentAssertions;
2+
using Xunit;
23

34
namespace NetEscapades.AspNetCore.SecurityHeaders.Infrastructure;
45

@@ -10,7 +11,7 @@ public void Default_Constructor()
1011
// Arrange & Act
1112
var result = new CustomHeadersResult();
1213

13-
Assert.Empty(result.SetHeaders);
14-
Assert.Empty(result.RemoveHeaders);
14+
result.SetHeaders.Should().BeEmpty();
15+
result.RemoveHeaders.Should().BeEmpty();
1516
}
1617
}

test/NetEscapades.AspNetCore.SecurityHeaders.Test/FeaturePolicyBuilderTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ public void Build_AddAccelerometer_WhenIncludesAllAndNone_ThrowsInvalidOperation
6666
.None()
6767
.All();
6868

69-
Assert.Throws<InvalidOperationException>(() => builder.Build());
69+
var act = () => builder.Build();
70+
act.Should().ThrowExactly<InvalidOperationException>();
7071
}
7172

7273
[Fact]

test/NetEscapades.AspNetCore.SecurityHeaders.Test/HeaderAssertionHelpers.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@ public static void AssertHttpRequestDefaultSecurityHeaders(this HttpResponseHead
2424
headers.Should().ContainKey("Cross-Origin-Resource-Policy")
2525
.WhoseValue.Should().ContainSingle("same-origin");
2626

27-
Assert.False(headers.Contains("Server"),
28-
"Should not contain server header");
29-
Assert.False(headers.Contains("Strict-Transport-Security"),
30-
"Should not contain Strict-Transport-Security header over http");
27+
headers.Contains("Server").Should().BeFalse("Should not contain server header");
28+
headers.Contains("Strict-Transport-Security").Should().BeFalse("Should not contain Strict-Transport-Security header over http");
3129
}
3230

3331
public static void AssertSecureRequestDefaultSecurityHeaders(this HttpResponseHeaders headers)
@@ -47,8 +45,7 @@ public static void AssertSecureRequestDefaultSecurityHeaders(this HttpResponseHe
4745
headers.Should().ContainKey("Cross-Origin-Resource-Policy")
4846
.WhoseValue.Should().ContainSingle("same-origin");
4947

50-
Assert.False(headers.Contains("Server"),
51-
"Should not contain server header");
48+
headers.Contains("Server").Should().BeFalse("Should not contain server header");
5249
}
5350

5451
public static void AssertHttpRequestDefaultApiSecurityHeaders(this HttpResponseHeaders headers)
@@ -70,9 +67,7 @@ public static void AssertHttpRequestDefaultApiSecurityHeaders(this HttpResponseH
7067
headers.Should().ContainKey("Cross-Origin-Resource-Policy")
7168
.WhoseValue.Should().ContainSingle("same-site");
7269

73-
Assert.False(headers.Contains("Server"),
74-
"Should not contain server header");
75-
Assert.False(headers.Contains("Strict-Transport-Security"),
76-
"Should not contain Strict-Transport-Security header over http");
70+
headers.Contains("Server").Should().BeFalse("Should not contain server header");
71+
headers.Contains("Strict-Transport-Security").Should().BeFalse("Should not contain Strict-Transport-Security header over http");
7772
}
7873
}

test/NetEscapades.AspNetCore.SecurityHeaders.Test/HttpsSecurityHeadersMiddlewareFunctionalTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ public async Task AllMethods_AddSecurityHeaders_IncludingStrict(string method, s
3535
var response = await Client.SendAsync(request);
3636

3737
// Assert
38-
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
38+
response.StatusCode.Should().Be(HttpStatusCode.OK);
3939
var content = await response.Content.ReadAsStringAsync();
40-
Assert.Equal(path, content);
40+
content.Should().Be(path);
4141
var responseHeaders = response.Headers;
4242

4343
responseHeaders.AssertSecureRequestDefaultSecurityHeaders();

test/NetEscapades.AspNetCore.SecurityHeaders.Test/NetEscapades.AspNetCore.SecurityHeaders.Test.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
<ItemGroup>
1616
<PackageReference Include="PublicApiGenerator" Version="11.1.0" />
1717
<PackageReference Include="Verify.Xunit" Version="18.4.0" />
18-
<PackageReference Include="FluentAssertions" Version="6.12.0" />
18+
<PackageReference Include="FluentAssertions" Version="7.2.0" />
19+
<PackageReference Include="FluentAssertions.Analyzers" Version="0.34.1" PrivateAssets="all" IncludeAssets="analyzers" />
1920
<PackageReference Include="xunit" Version="2.4.2" />
2021
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" />
2122
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />

0 commit comments

Comments
 (0)