|
6 | 6 | using System.Linq;
|
7 | 7 | using System.Net;
|
8 | 8 | using System.Net.Http;
|
| 9 | +using System.Text; |
9 | 10 | using System.Threading.Tasks;
|
10 | 11 | using Microsoft.AspNetCore.Authentication.Cookies;
|
| 12 | +using Microsoft.AspNetCore.Hosting; |
11 | 13 | using Microsoft.AspNetCore.Mvc.ViewFeatures;
|
| 14 | +using Microsoft.AspNetCore.Mvc.ViewFeatures.Infrastructure; |
| 15 | +using Microsoft.AspNetCore.TestHost; |
| 16 | +using Microsoft.Extensions.DependencyInjection; |
12 | 17 | using Microsoft.Net.Http.Headers;
|
13 | 18 | using Xunit;
|
14 | 19 |
|
15 | 20 | namespace Microsoft.AspNetCore.Mvc.FunctionalTests
|
16 | 21 | {
|
17 | 22 | public class TempDataInCookiesTest : TempDataTestBase, IClassFixture<MvcTestFixture<BasicWebSite.StartupWithoutEndpointRouting>>
|
18 | 23 | {
|
| 24 | + private IServiceCollection _serviceCollection; |
| 25 | + |
19 | 26 | public TempDataInCookiesTest(MvcTestFixture<BasicWebSite.StartupWithoutEndpointRouting> fixture)
|
20 | 27 | {
|
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(); |
22 | 32 | }
|
23 | 33 |
|
24 | 34 | protected override HttpClient Client { get; }
|
25 | 35 |
|
| 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 | + } |
26 | 61 |
|
27 | 62 | [Theory]
|
28 | 63 | [InlineData(ChunkingCookieManager.DefaultChunkSize)]
|
|
0 commit comments