Skip to content

Commit

Permalink
fix testing requiring Mvc.Testing fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Mpdreamz committed Dec 1, 2023
1 parent 4af01f1 commit 43b15e6
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 70 deletions.
11 changes: 0 additions & 11 deletions test/Elastic.Apm.Tests.Utilities/XUnit/DisabledTestFact.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
}
}

Original file line number Diff line number Diff line change
@@ -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";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ private static IEnumerable<OptionsTestVariant> BuildOptionsTestVariants()
}
}

[Theory]
[TheoryRequiresMvcTestingFix]
[MemberData(nameof(OptionsChangedAfterStartTestVariants))]
public async Task Options_changed_after_start(int startCfgVariantIndex, OptionsTestVariant startCfgVariant)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ public static IEnumerable<object[]> 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" });
Expand Down Expand Up @@ -106,9 +106,9 @@ public static IEnumerable<object[]> 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" });
Expand All @@ -121,47 +121,18 @@ public static IEnumerable<object[]> 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<object[]>();

// 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<object>();
foreach (var item in testDataItem)
newItem.Add(item);
newItem.Add(true);

retVal.Add(newItem.ToArray());
}
{
var newItem = new List<object>();
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)
Expand Down Expand Up @@ -376,14 +347,14 @@ public async Task DefaultsWithKnownHeaders(string headerName, string returnedHea
///// <param name="formName"></param>
///// <returns></returns>
[MemberData(nameof(GetData), Tests.DefaultWithRequestBodyNoError)]
[Theory]
[TheoryRequiresMvcTestingFix]
public async Task DefaultWithRequestBodyNoError(string formName)
{
CreateAgent();

var nvc = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("Input1", "test1"), new KeyValuePair<string, string>(formName, "test2")
new("Input1", "test1"), new(formName, "test2")
};

var req = new HttpRequestMessage(HttpMethod.Post, "api/Home/Post") { Content = new FormUrlEncodedContent(nvc) };
Expand All @@ -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<KeyValuePair<string, string>> { new KeyValuePair<string, string>(formName, "test") };
var nvc = new List<KeyValuePair<string, string>> { new(formName, "test") };

var req = new HttpRequestMessage(HttpMethod.Post, "api/Home/Post") { Content = new FormUrlEncodedContent(nvc) };
var res = await _client.SendAsync(req);
Expand All @@ -427,14 +398,14 @@ public async Task DefaultWithRequestBodySingleValueNoError(string formName, bool
///// <param name="formName"></param>
///// <returns></returns>
[MemberData(nameof(GetData), Tests.DefaultWithRequestBodyWithError)]
[Theory]
[TheoryRequiresMvcTestingFix]
public async Task DefaultWithRequestBodyWithError(string formName)
{
CreateAgent();

var nvc = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("Input1", "test1"), new KeyValuePair<string, string>(formName, "test2")
new("Input1", "test1"), new(formName, "test2")
};

var req = new HttpRequestMessage(HttpMethod.Post, "api/Home/PostError") { Content = new FormUrlEncodedContent(nvc) };
Expand Down Expand Up @@ -464,7 +435,7 @@ public async Task DefaultWithRequestBodyWithError(string formName)
///// <param name="shouldBeSanitized"></param>
///// <returns></returns>
[MemberData(nameof(GetData), Tests.CustomWithRequestBodyNoError)]
[Theory]
[TheoryRequiresMvcTestingFix]
public async Task CustomWithRequestBodyNoError(string sanitizeFieldNames, string formName, bool shouldBeSanitized)
{
CreateAgent(sanitizeFieldNames);
Expand Down

0 comments on commit 43b15e6

Please sign in to comment.