2
2
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
3
4
4
using System ;
5
+ using System . IO ;
5
6
using System . Linq ;
6
7
using System . Net ;
7
8
using System . Net . Http ;
8
9
using System . Net . Http . Headers ;
9
10
using System . Text . Encodings . Web ;
10
11
using System . Threading . Tasks ;
11
12
using Microsoft . AspNetCore . Hosting ;
13
+ using Microsoft . AspNetCore . Mvc . Razor . RuntimeCompilation ;
14
+ using Microsoft . AspNetCore . TestHost ;
12
15
using Microsoft . Extensions . DependencyInjection ;
13
16
using Microsoft . Extensions . Logging ;
14
17
using Microsoft . Extensions . Logging . Testing ;
@@ -27,6 +30,8 @@ public class ErrorPageTests : IClassFixture<MvcTestFixture<ErrorPageMiddlewareWe
27
30
"If you're seeing this in a published application, set 'CopyRefAssembliesToPublishDirectory' to true in your project file to ensure files in the refs directory are published." ) ;
28
31
private readonly AssemblyTestLog _assemblyTestLog ;
29
32
33
+ private readonly MvcTestFixture < ErrorPageMiddlewareWebSite . Startup > _fixture ;
34
+
30
35
public ErrorPageTests (
31
36
MvcTestFixture < ErrorPageMiddlewareWebSite . Startup > fixture ,
32
37
ITestOutputHelper testOutputHelper )
@@ -41,6 +46,8 @@ public ErrorPageTests(
41
46
. CreateDefaultClient ( ) ;
42
47
// These tests want to verify runtime compilation and formatting in the HTML of the error page
43
48
Client . DefaultRequestHeaders . Accept . Add ( new MediaTypeWithQualityHeaderValue ( "text/html" ) ) ;
49
+
50
+ _fixture = fixture ;
44
51
}
45
52
46
53
public HttpClient Client { get ; }
@@ -49,12 +56,18 @@ public ErrorPageTests(
49
56
public async Task CompilationFailuresAreListedByErrorPageMiddleware ( )
50
57
{
51
58
// Arrange
59
+ var factory = _fixture . Factories . FirstOrDefault ( ) ?? _fixture . WithWebHostBuilder ( b => b . UseStartup < ErrorPageMiddlewareWebSite . Startup > ( ) ) ;
60
+ factory = factory . WithWebHostBuilder ( b => b . ConfigureTestServices ( serviceCollection => serviceCollection . Configure < MvcRazorRuntimeCompilationOptions > ( ConfigureRuntimeCompilationOptions ) ) ) ;
61
+
62
+ var client = factory . CreateDefaultClient ( ) ;
63
+ client . DefaultRequestHeaders . Accept . Add ( new MediaTypeWithQualityHeaderValue ( "text/html" ) ) ;
64
+
52
65
var action = "CompilationFailure" ;
53
66
var expected = "Cannot implicitly convert type 'int' to 'string'" ;
54
67
var expectedMediaType = MediaTypeHeaderValue . Parse ( "text/html; charset=utf-8" ) ;
55
68
56
69
// Act
57
- var response = await Client . GetAsync ( "http://localhost/" + action ) ;
70
+ var response = await client . GetAsync ( "http://localhost/" + action ) ;
58
71
59
72
// Assert
60
73
Assert . Equal ( HttpStatusCode . InternalServerError , response . StatusCode ) ;
@@ -63,6 +76,16 @@ public async Task CompilationFailuresAreListedByErrorPageMiddleware()
63
76
Assert . Contains ( $ "{ action } .cshtml", content ) ;
64
77
Assert . Contains ( expected , content ) ;
65
78
Assert . DoesNotContain ( PreserveCompilationContextMessage , content ) ;
79
+
80
+ static void ConfigureRuntimeCompilationOptions ( MvcRazorRuntimeCompilationOptions options )
81
+ {
82
+ // Workaround for incorrectly generated deps file. The build output has all of the binaries required to compile. We'll grab these and
83
+ // add it to the list of assemblies runtime compilation uses.
84
+ foreach ( var path in Directory . EnumerateFiles ( AppContext . BaseDirectory , "*.dll" ) )
85
+ {
86
+ options . AdditionalReferencePaths . Add ( path ) ;
87
+ }
88
+ }
66
89
}
67
90
68
91
[ Fact ]
0 commit comments