|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Net.Http; |
| 5 | +using Microsoft.AspNetCore.Hosting; |
| 6 | +using Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation; |
| 7 | +using Microsoft.AspNetCore.TestHost; |
| 8 | +using Microsoft.Extensions.DependencyInjection; |
| 9 | + |
| 10 | +namespace Microsoft.AspNetCore.Mvc.FunctionalTests; |
| 11 | + |
| 12 | +public class RazorRuntimeCompilationHostingStartupTest : IClassFixture<MvcTestFixture<RazorBuildWebSite.StartupWithHostingStartup>> |
| 13 | +{ |
| 14 | + public RazorRuntimeCompilationHostingStartupTest(MvcTestFixture<RazorBuildWebSite.StartupWithHostingStartup> fixture) |
| 15 | + { |
| 16 | + var factory = fixture.Factories.FirstOrDefault() ?? fixture.WithWebHostBuilder(b => b.UseStartup<RazorBuildWebSite.StartupWithHostingStartup>()); |
| 17 | + factory = factory.WithWebHostBuilder(b => b.ConfigureTestServices(serviceCollection => serviceCollection.Configure<MvcRazorRuntimeCompilationOptions>(ConfigureRuntimeCompilationOptions))); |
| 18 | + |
| 19 | + Client = factory.CreateDefaultClient(); |
| 20 | + |
| 21 | + static void ConfigureRuntimeCompilationOptions(MvcRazorRuntimeCompilationOptions options) |
| 22 | + { |
| 23 | + // Workaround for incorrectly generated deps file. The build output has all of the binaries required to compile. We'll grab these and |
| 24 | + // add it to the list of assemblies runtime compilation uses. |
| 25 | + foreach (var path in Directory.EnumerateFiles(AppContext.BaseDirectory, "*.dll")) |
| 26 | + { |
| 27 | + options.AdditionalReferencePaths.Add(path); |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + public HttpClient Client { get; } |
| 33 | + |
| 34 | + [Fact] |
| 35 | + public async Task RazorViews_CanBeServedAndUpdatedViaRuntimeCompilation() |
| 36 | + { |
| 37 | + // Arrange |
| 38 | + var expected1 = "Original content"; |
| 39 | + var path = "/Views/UpdateableViews/Index.cshtml"; |
| 40 | + |
| 41 | + // Act - 1 |
| 42 | + var body = await Client.GetStringAsync("/UpdateableViews"); |
| 43 | + |
| 44 | + // Assert - 1 |
| 45 | + Assert.Equal(expected1, body.Trim(), ignoreLineEndingDifferences: true); |
| 46 | + |
| 47 | + // Act - 2 |
| 48 | + await UpdateFile(path, "@GetType().Assembly"); |
| 49 | + body = await Client.GetStringAsync("/UpdateableViews"); |
| 50 | + |
| 51 | + // Assert - 2 |
| 52 | + var actual2 = body.Trim(); |
| 53 | + Assert.NotEqual(expected1, actual2); |
| 54 | + |
| 55 | + // Act - 3 |
| 56 | + // With all things being the same, expect a cached compilation |
| 57 | + body = await Client.GetStringAsync("/UpdateableViews"); |
| 58 | + |
| 59 | + // Assert - 3 |
| 60 | + Assert.Equal(actual2, body.Trim(), ignoreLineEndingDifferences: true); |
| 61 | + |
| 62 | + // Act - 4 |
| 63 | + // Trigger a change in ViewImports |
| 64 | + await UpdateFile("/Views/UpdateableViews/_ViewImports.cshtml", "new content"); |
| 65 | + body = await Client.GetStringAsync("/UpdateableViews"); |
| 66 | + |
| 67 | + // Assert - 4 |
| 68 | + Assert.NotEqual(actual2, body.Trim()); |
| 69 | + } |
| 70 | + |
| 71 | + [Fact] |
| 72 | + public async Task RazorPages_CanBeServedAndUpdatedViaRuntimeCompilation() |
| 73 | + { |
| 74 | + // Arrange |
| 75 | + var expected1 = "Original content"; |
| 76 | + |
| 77 | + // Act - 1 |
| 78 | + var body = await Client.GetStringAsync("/UpdateablePage"); |
| 79 | + |
| 80 | + // Assert - 1 |
| 81 | + Assert.Equal(expected1, body.Trim(), ignoreLineEndingDifferences: true); |
| 82 | + |
| 83 | + // Act - 2 |
| 84 | + await UpdateRazorPages(); |
| 85 | + await UpdateFile("/Pages/UpdateablePage.cshtml", "@page" + Environment.NewLine + "@GetType().Assembly"); |
| 86 | + body = await Client.GetStringAsync("/UpdateablePage"); |
| 87 | + |
| 88 | + // Assert - 2 |
| 89 | + var actual2 = body.Trim(); |
| 90 | + Assert.NotEqual(expected1, actual2); |
| 91 | + |
| 92 | + // Act - 3 |
| 93 | + // With all things being unchanged, we should get the cached page. |
| 94 | + body = await Client.GetStringAsync("/UpdateablePage"); |
| 95 | + |
| 96 | + // Assert - 3 |
| 97 | + Assert.Equal(actual2, body.Trim(), ignoreLineEndingDifferences: true); |
| 98 | + } |
| 99 | + |
| 100 | + private async Task UpdateFile(string path, string content) |
| 101 | + { |
| 102 | + var updateContent = new FormUrlEncodedContent(new Dictionary<string, string> |
| 103 | + { |
| 104 | + { "path", path }, |
| 105 | + { "content", content }, |
| 106 | + }); |
| 107 | + |
| 108 | + var response = await Client.PostAsync($"/UpdateableViews/Update", updateContent); |
| 109 | + response.EnsureSuccessStatusCode(); |
| 110 | + } |
| 111 | + |
| 112 | + private async Task UpdateRazorPages() |
| 113 | + { |
| 114 | + var response = await Client.PostAsync($"/UpdateableViews/UpdateRazorPages", new StringContent(string.Empty)); |
| 115 | + response.EnsureSuccessStatusCode(); |
| 116 | + } |
| 117 | +} |
0 commit comments