From 43b15e6e1c4ad065eff16daec983b55d3f03e739 Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Fri, 1 Dec 2023 13:54:24 +0100 Subject: [PATCH] fix testing requiring Mvc.Testing fix --- .../XUnit/DisabledTestFact.cs | 11 --- .../XUnit/FactRequiresMvcTestingFix.cs | 27 ++++++ .../BodyCapturingTests.cs | 2 +- .../SanitizeFieldNamesTests.cs | 87 +++++++------------ 4 files changed, 57 insertions(+), 70 deletions(-) create mode 100644 test/Elastic.Apm.Tests.Utilities/XUnit/FactRequiresMvcTestingFix.cs diff --git a/test/Elastic.Apm.Tests.Utilities/XUnit/DisabledTestFact.cs b/test/Elastic.Apm.Tests.Utilities/XUnit/DisabledTestFact.cs index 24f2355cd..0b41a5508 100644 --- a/test/Elastic.Apm.Tests.Utilities/XUnit/DisabledTestFact.cs +++ b/test/Elastic.Apm.Tests.Utilities/XUnit/DisabledTestFact.cs @@ -3,7 +3,6 @@ // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. // See the LICENSE file in the project root for more information -using System; using Xunit; namespace Elastic.Apm.Tests.Utilities.XUnit; @@ -17,13 +16,3 @@ public DisabledTestFact(string reason, string issueLink = null) Skip += $", issue link: {issueLink}"; } } - -public sealed class FactRequiresMvcTestingFix : FactAttribute -{ - public FactRequiresMvcTestingFix() - { - if (Environment.Version.Major < 7) return; - Skip = $"This Test is disabled on .NET 7 until https://github.com/dotnet/aspnetcore/issues/45233"; - } -} - diff --git a/test/Elastic.Apm.Tests.Utilities/XUnit/FactRequiresMvcTestingFix.cs b/test/Elastic.Apm.Tests.Utilities/XUnit/FactRequiresMvcTestingFix.cs new file mode 100644 index 000000000..51b985fbb --- /dev/null +++ b/test/Elastic.Apm.Tests.Utilities/XUnit/FactRequiresMvcTestingFix.cs @@ -0,0 +1,27 @@ +// Licensed to Elasticsearch B.V under +// one or more agreements. +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. +// See the LICENSE file in the project root for more information + +using System; +using Xunit; + +namespace Elastic.Apm.Tests.Utilities.XUnit; + +public sealed class FactRequiresMvcTestingFix : FactAttribute +{ + public FactRequiresMvcTestingFix() + { + if (Environment.Version.Major < 7) return; + Skip = $"This test is disabled on .NET 7 until https://github.com/dotnet/aspnetcore/issues/45233"; + } +} + +public sealed class TheoryRequiresMvcTestingFix : TheoryAttribute +{ + public TheoryRequiresMvcTestingFix() + { + if (Environment.Version.Major < 7) return; + Skip = $"This test is disabled on .NET 7 until https://github.com/dotnet/aspnetcore/issues/45233"; + } +} diff --git a/test/integrations/Elastic.Apm.AspNetCore.Tests/BodyCapturingTests.cs b/test/integrations/Elastic.Apm.AspNetCore.Tests/BodyCapturingTests.cs index c70b0a609..dd6f5ca78 100644 --- a/test/integrations/Elastic.Apm.AspNetCore.Tests/BodyCapturingTests.cs +++ b/test/integrations/Elastic.Apm.AspNetCore.Tests/BodyCapturingTests.cs @@ -310,7 +310,7 @@ private static IEnumerable BuildOptionsTestVariants() } } - [Theory] + [TheoryRequiresMvcTestingFix] [MemberData(nameof(OptionsChangedAfterStartTestVariants))] public async Task Options_changed_after_start(int startCfgVariantIndex, OptionsTestVariant startCfgVariant) { diff --git a/test/integrations/Elastic.Apm.AspNetCore.Tests/SanitizeFieldNamesTests.cs b/test/integrations/Elastic.Apm.AspNetCore.Tests/SanitizeFieldNamesTests.cs index c4b93c5a1..c612d0c04 100644 --- a/test/integrations/Elastic.Apm.AspNetCore.Tests/SanitizeFieldNamesTests.cs +++ b/test/integrations/Elastic.Apm.AspNetCore.Tests/SanitizeFieldNamesTests.cs @@ -63,15 +63,15 @@ public static IEnumerable GetData(Tests test) testData.Add(new object[] { "mySecurityHeader", new[] { "mySECURITYHeader" } }); break; case Tests.CustomSanitizeFieldNameSettingWithCaseSensitivityWithHeaders: - testData.Add(new object[] { "mysecurityheader", "mySECURITYHeader" }); - testData.Add(new object[] { "(?-i)mysecurityheader", "mySECURITYHeader" }); - testData.Add(new object[] { "(?-i)mySECURITYheader", "mySECURITYheader" }); - testData.Add(new object[] { "(?-i)*mySECURITYheader", "TestmySECURITYheader" }); - testData.Add(new object[] { "(?-i)*mySECURITYheader", "TestmysecURITYheader" }); - testData.Add(new object[] { "(?-i)mySECURITYheader*", "mySECURITYheaderTest" }); - testData.Add(new object[] { "(?-i)mySECURITYheader*", "mysecURITYheaderTest" }); - testData.Add(new object[] { "(?-i)*mySECURITYheader*", "TestmySECURITYheaderTest" }); - testData.Add(new object[] { "(?-i)*mySECURITYheader*", "TestmysecURITYheaderTest" }); + testData.Add(new object[] { "mysecurityheader", "mySECURITYHeader", true }); + testData.Add(new object[] { "(?-i)mysecurityheader", "mySECURITYHeader", false }); + testData.Add(new object[] { "(?-i)mySECURITYheader", "mySECURITYheader", true }); + testData.Add(new object[] { "(?-i)*mySECURITYheader", "TestmySECURITYheader", true }); + testData.Add(new object[] { "(?-i)*mySECURITYheader", "TestmysecURITYheader", false }); + testData.Add(new object[] { "(?-i)mySECURITYheader*", "mySECURITYheaderTest", true }); + testData.Add(new object[] { "(?-i)mySECURITYheader*", "mysecURITYheaderTest", false }); + testData.Add(new object[] { "(?-i)*mySECURITYheader*", "TestmySECURITYheaderTest", true }); + testData.Add(new object[] { "(?-i)*mySECURITYheader*", "TestmysecURITYheaderTest", false }); break; case Tests.DefaultsWithHeaders: testData.Add(new object[] { "password" }); @@ -106,9 +106,9 @@ public static IEnumerable GetData(Tests test) testData.Add(new object[] { "creditcardnumber" }); //*card break; case Tests.DefaultWithRequestBodySingleValueNoError: - testData.Add(new object[] { "password" }); - testData.Add(new object[] { "pwd" }); - testData.Add(new object[] { "Input" }); + testData.Add(new object[] { "password", true }); + testData.Add(new object[] { "pwd", true }); + testData.Add(new object[] { "Input", false }); break; case Tests.DefaultWithRequestBodyWithError: testData.Add(new object[] { "password" }); @@ -121,47 +121,18 @@ public static IEnumerable GetData(Tests test) testData.Add(new object[] { "creditcardnumber" }); //*card break; case Tests.CustomWithRequestBodyNoError: - testData.Add(new object[] { "mysecurityheader", "mySECURITYHeader" }); - testData.Add(new object[] { "(?-i)mysecurityheader", "mySECURITYHeader" }); - testData.Add(new object[] { "(?-i)mySECURITYheader", "mySECURITYheader" }); - testData.Add(new object[] { "(?-i)*mySECURITYheader", "TestmySECURITYheader" }); - testData.Add(new object[] { "(?-i)*mySECURITYheader", "TestmysecURITYheader" }); - testData.Add(new object[] { "(?-i)mySECURITYheader*", "mySECURITYheaderTest" }); - testData.Add(new object[] { "(?-i)mySECURITYheader*", "mysecURITYheaderTest" }); - testData.Add(new object[] { "(?-i)*mySECURITYheader*", "TestmySECURITYheaderTest" }); - testData.Add(new object[] { "(?-i)*mySECURITYheader*", "TestmysecURITYheaderTest" }); + testData.Add(new object[] { "mysecurityheader", "mySECURITYHeader", true }); + testData.Add(new object[] { "(?-i)mysecurityheader", "mySECURITYHeader", false }); + testData.Add(new object[] { "(?-i)mySECURITYheader", "mySECURITYheader", true }); + testData.Add(new object[] { "(?-i)*mySECURITYheader", "TestmySECURITYheader", true }); + testData.Add(new object[] { "(?-i)*mySECURITYheader", "TestmysecURITYheader", false }); + testData.Add(new object[] { "(?-i)mySECURITYheader*", "mySECURITYheaderTest", true }); + testData.Add(new object[] { "(?-i)mySECURITYheader*", "mysecURITYheaderTest", false }); + testData.Add(new object[] { "(?-i)*mySECURITYheader*", "TestmySECURITYheaderTest", true }); + testData.Add(new object[] { "(?-i)*mySECURITYheader*", "TestmysecURITYheaderTest", false }); break; } - - var retVal = new List(); - - // Add true and false to the end of each test data, so we test it both with middleware and with diagnosticsource - foreach (var testDataItem in testData) - { - // - // Skip "DiagnosticSourceOnly" tests on .NET 7 - // until https://github.com/dotnet/aspnetcore/issues/45233 is resolved. - // - if (Environment.Version.Major < 7) - { - var newItem = new List(); - foreach (var item in testDataItem) - newItem.Add(item); - newItem.Add(true); - - retVal.Add(newItem.ToArray()); - } - { - var newItem = new List(); - foreach (var item in testDataItem) - newItem.Add(item); - newItem.Add(false); - - retVal.Add(newItem.ToArray()); - } - } - - return retVal; + return testData; } private void CreateAgent(string sanitizeFieldNames = null) @@ -376,14 +347,14 @@ public async Task DefaultsWithKnownHeaders(string headerName, string returnedHea ///// ///// [MemberData(nameof(GetData), Tests.DefaultWithRequestBodyNoError)] - [Theory] + [TheoryRequiresMvcTestingFix] public async Task DefaultWithRequestBodyNoError(string formName) { CreateAgent(); var nvc = new List> { - new KeyValuePair("Input1", "test1"), new KeyValuePair(formName, "test2") + new("Input1", "test1"), new(formName, "test2") }; var req = new HttpRequestMessage(HttpMethod.Post, "api/Home/Post") { Content = new FormUrlEncodedContent(nvc) }; @@ -401,12 +372,12 @@ public async Task DefaultWithRequestBodyNoError(string formName) } [MemberData(nameof(GetData), Tests.DefaultWithRequestBodySingleValueNoError)] - [Theory] + [TheoryRequiresMvcTestingFix] public async Task DefaultWithRequestBodySingleValueNoError(string formName, bool shouldBeSanitized) { CreateAgent(); - var nvc = new List> { new KeyValuePair(formName, "test") }; + var nvc = new List> { new(formName, "test") }; var req = new HttpRequestMessage(HttpMethod.Post, "api/Home/Post") { Content = new FormUrlEncodedContent(nvc) }; var res = await _client.SendAsync(req); @@ -427,14 +398,14 @@ public async Task DefaultWithRequestBodySingleValueNoError(string formName, bool ///// ///// [MemberData(nameof(GetData), Tests.DefaultWithRequestBodyWithError)] - [Theory] + [TheoryRequiresMvcTestingFix] public async Task DefaultWithRequestBodyWithError(string formName) { CreateAgent(); var nvc = new List> { - new KeyValuePair("Input1", "test1"), new KeyValuePair(formName, "test2") + new("Input1", "test1"), new(formName, "test2") }; var req = new HttpRequestMessage(HttpMethod.Post, "api/Home/PostError") { Content = new FormUrlEncodedContent(nvc) }; @@ -464,7 +435,7 @@ public async Task DefaultWithRequestBodyWithError(string formName) ///// ///// [MemberData(nameof(GetData), Tests.CustomWithRequestBodyNoError)] - [Theory] + [TheoryRequiresMvcTestingFix] public async Task CustomWithRequestBodyNoError(string sanitizeFieldNames, string formName, bool shouldBeSanitized) { CreateAgent(sanitizeFieldNames);