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 . Collections . Generic ;
6
7
using Microsoft . AspNetCore . Builder ;
7
8
using Microsoft . AspNetCore . Hosting ;
@@ -34,8 +35,9 @@ public void ApplyConfigurationSettingsUsesTheCorrectKeys()
34
35
35
36
Assert . Equal ( WebHostDefaults . ApplicationKey , env . ApplicationName ) ;
36
37
Assert . Equal ( WebHostDefaults . EnvironmentKey , env . EnvironmentName ) ;
37
- Assert . Equal ( WebHostDefaults . ContentRootKey , env . ContentRootPath ) ;
38
- Assert . Equal ( WebHostDefaults . WebRootKey , env . WebRootPath ) ;
38
+ Assert . Equal ( Path . Combine ( Directory . GetCurrentDirectory ( ) , WebHostDefaults . ContentRootKey ) , env . ContentRootPath ) ;
39
+ var fullWebRootPath = Path . Combine ( env . ContentRootPath , env . WebRootPath ) ;
40
+ Assert . Equal ( fullWebRootPath , env . WebRootPath ) ;
39
41
}
40
42
41
43
[ Fact ]
@@ -58,16 +60,95 @@ public void ApplyEnvironmentSettingsUsesTheCorrectKeysAndProperties()
58
60
59
61
Assert . Equal ( WebHostDefaults . ApplicationKey , settings [ WebHostDefaults . ApplicationKey ] ) ;
60
62
Assert . Equal ( WebHostDefaults . EnvironmentKey , settings [ WebHostDefaults . EnvironmentKey ] ) ;
61
- Assert . Equal ( WebHostDefaults . ContentRootKey , settings [ WebHostDefaults . ContentRootKey ] ) ;
62
- Assert . Equal ( WebHostDefaults . WebRootKey , settings [ WebHostDefaults . WebRootKey ] ) ;
63
+ Assert . Equal ( Path . Combine ( Directory . GetCurrentDirectory ( ) , WebHostDefaults . ContentRootKey ) , settings [ WebHostDefaults . ContentRootKey ] ) ;
64
+ var fullWebRootPath = Path . Combine ( settings [ WebHostDefaults . ContentRootKey ] , settings [ WebHostDefaults . WebRootKey ] ) ;
65
+ Assert . Equal ( fullWebRootPath , settings [ WebHostDefaults . WebRootKey ] ) ;
63
66
64
67
Assert . Equal ( WebHostDefaults . ApplicationKey , webHostBuilderEnvironment . ApplicationName ) ;
65
68
Assert . Equal ( WebHostDefaults . EnvironmentKey , webHostBuilderEnvironment . EnvironmentName ) ;
66
- Assert . Equal ( WebHostDefaults . ContentRootKey , webHostBuilderEnvironment . ContentRootPath ) ;
67
- Assert . Equal ( WebHostDefaults . WebRootKey , webHostBuilderEnvironment . WebRootPath ) ;
69
+ Assert . Equal ( Path . Combine ( Directory . GetCurrentDirectory ( ) , WebHostDefaults . ContentRootKey ) , webHostBuilderEnvironment . ContentRootPath ) ;
70
+ Assert . Equal ( fullWebRootPath , webHostBuilderEnvironment . WebRootPath ) ;
68
71
69
- Assert . Same ( originalEnvironment . ContentRootFileProvider , webHostBuilderEnvironment . ContentRootFileProvider ) ;
70
- Assert . Same ( originalEnvironment . WebRootFileProvider , webHostBuilderEnvironment . WebRootFileProvider ) ;
72
+ Assert . NotEqual ( originalEnvironment . ContentRootFileProvider , webHostBuilderEnvironment . ContentRootFileProvider ) ;
73
+ Assert . NotEqual ( originalEnvironment . WebRootFileProvider , webHostBuilderEnvironment . WebRootFileProvider ) ;
74
+ }
75
+
76
+ [ Fact ]
77
+ public void SettingPathsSetsContentProviders ( )
78
+ {
79
+ var environment = new WebHostEnvironment ( ) ;
80
+ var tempPath = Path . GetTempPath ( ) ;
81
+
82
+ environment . ContentRootPath = tempPath ;
83
+ environment . WebRootPath = tempPath ;
84
+
85
+ Assert . Equal ( tempPath , environment . WebRootPath ) ;
86
+ Assert . Equal ( tempPath , environment . ContentRootPath ) ;
87
+
88
+ Assert . IsType < PhysicalFileProvider > ( environment . ContentRootFileProvider ) ;
89
+ Assert . IsType < PhysicalFileProvider > ( environment . WebRootFileProvider ) ;
90
+
91
+ Assert . Equal ( EnsureTrailingSlash ( tempPath ) , ( ( PhysicalFileProvider ) environment . ContentRootFileProvider ) . Root ) ;
92
+ Assert . Equal ( EnsureTrailingSlash ( tempPath ) , ( ( PhysicalFileProvider ) environment . WebRootFileProvider ) . Root ) ;
93
+ }
94
+
95
+ [ Fact ]
96
+ public void RelativePathsAreMappedToFullPaths ( )
97
+ {
98
+ var environment = new WebHostEnvironment ( ) ;
99
+ var relativeRootPath = "some-relative-path" ;
100
+ var relativeSubPath = "some-other-relative-path" ;
101
+ var fullContentRoot = Path . Combine ( AppContext . BaseDirectory , relativeRootPath ) ;
102
+
103
+ // ContentRootPath is mapped relative to AppContext.BaseDirectory
104
+ environment . ContentRootPath = relativeRootPath ;
105
+ Assert . Equal ( fullContentRoot , environment . ContentRootPath ) ;
106
+
107
+ // WebRootPath is mapped relative to ContentRootPath
108
+ environment . WebRootPath = relativeSubPath ;
109
+ Assert . Equal ( Path . Combine ( fullContentRoot , relativeSubPath ) , environment . WebRootPath ) ;
110
+ }
111
+
112
+ [ Fact ]
113
+ public void UnsettingPathsFallsBackToDefaults ( )
114
+ {
115
+ var environment = new WebHostEnvironment ( ) ;
116
+ var defaultWebRootPath = Path . Combine ( environment . ContentRootPath , "wwwroot" ) ;
117
+ var webRootPath = Path . GetTempPath ( ) ;
118
+
119
+ environment . WebRootPath = webRootPath ;
120
+
121
+ Assert . Equal ( webRootPath , environment . WebRootPath ) ;
122
+ Assert . Equal ( EnsureTrailingSlash ( webRootPath ) , ( ( PhysicalFileProvider ) environment . WebRootFileProvider ) . Root ) ;
123
+
124
+ // Setting WebRootPath to fallsback to default
125
+ environment . WebRootPath = null ;
126
+ Assert . Equal ( defaultWebRootPath , environment . WebRootPath ) ;
127
+
128
+ // Setting ContentRootPath to null falls back to CurrentDirectory
129
+ environment . ContentRootPath = null ;
130
+ Assert . Equal ( Directory . GetCurrentDirectory ( ) , environment . ContentRootPath ) ;
131
+ Assert . Equal ( EnsureTrailingSlash ( Directory . GetCurrentDirectory ( ) ) , ( ( PhysicalFileProvider ) environment . ContentRootFileProvider ) . Root ) ;
132
+ }
133
+
134
+ [ Fact ]
135
+ public void SetContentRootAfterRelativeWebRoot ( )
136
+ {
137
+ var environment = new WebHostEnvironment ( ) ;
138
+ var webRootPath = "some-relative-path" ;
139
+ var tempPath = Path . GetTempPath ( ) ;
140
+
141
+ environment . WebRootPath = webRootPath ;
142
+
143
+ Assert . Equal ( Path . Combine ( Directory . GetCurrentDirectory ( ) , webRootPath ) , environment . WebRootPath ) ;
144
+ Assert . Equal ( Directory . GetCurrentDirectory ( ) , environment . ContentRootPath ) ;
145
+
146
+ // Setting the ContentRootPath after setting a relative WebRootPath
147
+ environment . ContentRootPath = tempPath ;
148
+
149
+ Assert . Equal ( tempPath , environment . ContentRootPath ) ;
150
+ Assert . Equal ( EnsureTrailingSlash ( tempPath ) , ( ( PhysicalFileProvider ) environment . ContentRootFileProvider ) . Root ) ;
151
+ Assert . Equal ( Path . Combine ( tempPath , webRootPath ) , environment . WebRootPath ) ;
71
152
}
72
153
73
154
private class TestWebHostBuilder : IWebHostBuilder
@@ -122,5 +203,8 @@ public IWebHostBuilder UseSetting(string key, string value)
122
203
return this ;
123
204
}
124
205
}
206
+
207
+ private static string EnsureTrailingSlash ( string path )
208
+ => path . EndsWith ( Path . DirectorySeparatorChar ) ? path : path + Path . DirectorySeparatorChar ;
125
209
}
126
210
}
0 commit comments