Skip to content

Commit b41959a

Browse files
committed
Add additional logging to TempDataInCookiesTest to diagnose test failure
Diagnosis for https://github.com/aspnet/AspNetCore-Internal/issues/1803
1 parent 44c66c8 commit b41959a

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

src/Mvc/test/Mvc.FunctionalTests/TempDataInCookiesTest.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,58 @@
66
using System.Linq;
77
using System.Net;
88
using System.Net.Http;
9+
using System.Text;
910
using System.Threading.Tasks;
1011
using Microsoft.AspNetCore.Authentication.Cookies;
12+
using Microsoft.AspNetCore.Hosting;
1113
using Microsoft.AspNetCore.Mvc.ViewFeatures;
14+
using Microsoft.AspNetCore.Mvc.ViewFeatures.Infrastructure;
15+
using Microsoft.AspNetCore.TestHost;
16+
using Microsoft.Extensions.DependencyInjection;
1217
using Microsoft.Net.Http.Headers;
1318
using Xunit;
1419

1520
namespace Microsoft.AspNetCore.Mvc.FunctionalTests
1621
{
1722
public class TempDataInCookiesTest : TempDataTestBase, IClassFixture<MvcTestFixture<BasicWebSite.StartupWithoutEndpointRouting>>
1823
{
24+
private IServiceCollection _serviceCollection;
25+
1926
public TempDataInCookiesTest(MvcTestFixture<BasicWebSite.StartupWithoutEndpointRouting> fixture)
2027
{
21-
Client = fixture.CreateDefaultClient();
28+
var factory = fixture.Factories.FirstOrDefault() ?? fixture.WithWebHostBuilder(b => b.UseStartup<BasicWebSite.StartupWithoutEndpointRouting>());
29+
factory = factory.WithWebHostBuilder(b => b.ConfigureTestServices(serviceCollection => _serviceCollection = serviceCollection));
30+
31+
Client = factory.CreateDefaultClient();
2232
}
2333

2434
protected override HttpClient Client { get; }
2535

36+
[Fact]
37+
public void VerifyNewtonsoftJsonTempDataSerializer()
38+
{
39+
// Arrange
40+
// This test could provide some diagnostics for the test failure reported in https://github.com/aspnet/AspNetCore-Internal/issues/1803.
41+
// AddNewtonsoftJson attempts to replace the DefaultTempDataSerializer. The test failure indicates this failed but it's not clear why.
42+
// We'll capture the application's ServiceCollection and inspect the instance of ITempDataSerializer instances here. It might give us some
43+
// clues if the test fails again in the future.
44+
45+
// Intentionally avoiding using Xunit.Assert to get more diagnostics.
46+
var tempDataSerializers = _serviceCollection.Where(f => f.ServiceType == typeof(TempDataSerializer)).ToList();
47+
if (tempDataSerializers.Count == 1 && tempDataSerializers[0].ImplementationType.FullName == "Microsoft.AspNetCore.Mvc.NewtonsoftJson.BsonTempDataSerializer")
48+
{
49+
return;
50+
}
51+
52+
var builder = new StringBuilder();
53+
foreach (var serializer in tempDataSerializers)
54+
{
55+
var type = serializer.ImplementationType;
56+
builder.Append(serializer.ImplementationType.AssemblyQualifiedName);
57+
}
58+
59+
throw new Exception($"Expected exactly one instance of TempDataSerializer based on NewtonsoftJson, but found {tempDataSerializers.Count} instance(s):" + Environment.NewLine + builder);
60+
}
2661

2762
[Theory]
2863
[InlineData(ChunkingCookieManager.DefaultChunkSize)]

0 commit comments

Comments
 (0)