Skip to content

Commit 9c6f684

Browse files
committed
Add additional tests
1 parent 77f1e28 commit 9c6f684

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed

src/Hosting/Hosting/test/StaticWebAssets/ManifestStaticWebAssetsFileProviderTests.cs

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,161 @@ public void ParseWorksWithNodesThatOnlyDifferOnCasing(string path, bool exists)
125125
Assert.Equal(exists, file.Exists);
126126
}
127127

128+
[Theory]
129+
[InlineData("/img/Subdir/icon.png", true)]
130+
[InlineData("/Img/subdir/hero.gif", true)]
131+
// Note that we've changed the casing of the second segment
132+
[InlineData("/img/subdir/icon.png", false)]
133+
[InlineData("/Img/Subdir/hero.gif", false)]
134+
public void ParseWorksWithMergesNodesRecursively(string path, bool exists)
135+
{
136+
// Arrange
137+
exists = exists | OperatingSystem.IsWindows();
138+
var firstLevelCount = OperatingSystem.IsWindows() ? 1 : 2;
139+
using var memoryStream = new MemoryStream();
140+
using var writer = new StreamWriter(memoryStream);
141+
writer.Write(@"{
142+
""ContentRoots"": [
143+
""D:\\path\\"",
144+
""D:\\other\\""
145+
],
146+
""Root"": {
147+
""Children"": {
148+
""img"": {
149+
""Children"": {
150+
""Subdir"": {
151+
""Children"": {
152+
""icon.png"": {
153+
""Asset"": {
154+
""ContentRootIndex"": 0,
155+
""SubPath"": ""icon.png""
156+
}
157+
}
158+
}
159+
}
160+
}
161+
},
162+
""Img"": {
163+
""Children"": {
164+
""subdir"": {
165+
""Children"": {
166+
""hero.gif"": {
167+
""Asset"": {
168+
""ContentRootIndex"": 1,
169+
""SubPath"": ""hero.gif""
170+
}
171+
}
172+
}
173+
}
174+
}
175+
}
176+
}
177+
}
178+
}");
179+
var first = new Mock<IFileProvider>();
180+
first.Setup(s => s.GetFileInfo("icon.png")).Returns(new TestFileInfo() { Name = "icon.png", Exists = true });
181+
var second = new Mock<IFileProvider>();
182+
second.Setup(s => s.GetFileInfo("hero.gif")).Returns(new TestFileInfo() { Name = "hero.gif", Exists = true });
183+
184+
writer.Flush();
185+
memoryStream.Seek(0, SeekOrigin.Begin);
186+
var manifest = ManifestStaticWebAssetFileProvider.StaticWebAssetManifest.Parse(memoryStream);
187+
var comparer = ManifestStaticWebAssetFileProvider.StaticWebAssetManifest.PathComparer;
188+
189+
var provider = new ManifestStaticWebAssetFileProvider(
190+
manifest,
191+
contentRoot => contentRoot switch
192+
{
193+
"D:\\path\\" => first.Object,
194+
"D:\\other\\" => second.Object,
195+
_ => throw new InvalidOperationException("Unknown provider")
196+
});
197+
198+
// Act
199+
var file = provider.GetFileInfo(path);
200+
201+
// Assert
202+
Assert.Equal(exists, file.Exists);
203+
Assert.Equal(firstLevelCount, manifest.Root.Children.Count);
204+
Assert.All(manifest.Root.Children.Values, c => Assert.Single(c.Children));
205+
}
206+
207+
[Theory]
208+
[InlineData("/img/Subdir", true)]
209+
[InlineData("/Img/subdir/hero.gif", true)]
210+
// Note that we've changed the casing of the second segment
211+
[InlineData("/img/subdir", false)]
212+
[InlineData("/Img/Subdir/hero.gif", false)]
213+
public void ParseWorksFolderAndFileWithDiferentCasing(string path, bool exists)
214+
{
215+
// Arrange
216+
exists = exists | OperatingSystem.IsWindows();
217+
var firstLevelCount = OperatingSystem.IsWindows() ? 1 : 2;
218+
using var memoryStream = new MemoryStream();
219+
using var writer = new StreamWriter(memoryStream);
220+
// img/Subdir is a file without extension
221+
writer.Write(@"{
222+
""ContentRoots"": [
223+
""D:\\path\\"",
224+
""D:\\other\\""
225+
],
226+
""Root"": {
227+
""Children"": {
228+
""img"": {
229+
""Children"": {
230+
""Subdir"": {
231+
""Asset"": {
232+
""ContentRootIndex"": 0,
233+
""SubPath"": ""Subdir""
234+
}
235+
}
236+
}
237+
},
238+
""Img"": {
239+
""Children"": {
240+
""subdir"": {
241+
""Children"": {
242+
""hero.gif"": {
243+
""Asset"": {
244+
""ContentRootIndex"": 1,
245+
""SubPath"": ""hero.gif""
246+
}
247+
}
248+
}
249+
}
250+
}
251+
}
252+
}
253+
}
254+
}");
255+
var first = new Mock<IFileProvider>();
256+
first.Setup(s => s.GetFileInfo("Subdir")).Returns(new TestFileInfo() { Name = "Subdir", Exists = true });
257+
var second = new Mock<IFileProvider>();
258+
second.Setup(s => s.GetFileInfo("hero.gif")).Returns(new TestFileInfo() { Name = "hero.gif", Exists = true });
259+
260+
writer.Flush();
261+
memoryStream.Seek(0, SeekOrigin.Begin);
262+
var manifest = ManifestStaticWebAssetFileProvider.StaticWebAssetManifest.Parse(memoryStream);
263+
var comparer = ManifestStaticWebAssetFileProvider.StaticWebAssetManifest.PathComparer;
264+
265+
var provider = new ManifestStaticWebAssetFileProvider(
266+
manifest,
267+
contentRoot => contentRoot switch
268+
{
269+
"D:\\path\\" => first.Object,
270+
"D:\\other\\" => second.Object,
271+
_ => throw new InvalidOperationException("Unknown provider")
272+
});
273+
274+
// Act
275+
var file = provider.GetFileInfo(path);
276+
277+
// Assert
278+
Assert.Equal(exists, file.Exists);
279+
Assert.Equal(firstLevelCount, manifest.Root.Children.Count);
280+
Assert.All(manifest.Root.Children.Values, c => Assert.Single(c.Children));
281+
}
282+
128283
[Fact]
129284
public void CanFindFileListedOnTheManifest()
130285
{

0 commit comments

Comments
 (0)