|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.IO; |
| 7 | +using System.Linq; |
| 8 | +using System.Text; |
| 9 | +using Microsoft.Extensions.FileProviders; |
| 10 | +using Microsoft.Extensions.Primitives; |
| 11 | +using Xunit; |
| 12 | + |
| 13 | +namespace Microsoft.AspNetCore.Components.WebView |
| 14 | +{ |
| 15 | + public class StaticContentProviderTests |
| 16 | + { |
| 17 | + [Fact] |
| 18 | + public void TryGetResponseContentReturnsCorrectContentTypeForNonPhysicalFile() |
| 19 | + { |
| 20 | + // Arrange |
| 21 | + const string cssFilePath = "folder/file.css"; |
| 22 | + const string cssFileContent = "this is css"; |
| 23 | + var inMemoryFileProvider = new InMemoryFileProvider( |
| 24 | + new Dictionary<string, string> |
| 25 | + { |
| 26 | + { cssFilePath, cssFileContent }, |
| 27 | + }); |
| 28 | + var appBase = "fake://0.0.0.0/"; |
| 29 | + var scp = new StaticContentProvider(inMemoryFileProvider, new Uri(appBase), "fakehost.html"); |
| 30 | + |
| 31 | + // Act |
| 32 | + Assert.True(scp.TryGetResponseContent( |
| 33 | + requestUri: appBase + cssFilePath, |
| 34 | + allowFallbackOnHostPage: false, |
| 35 | + out var statusCode, |
| 36 | + out var statusMessage, |
| 37 | + out var content, |
| 38 | + out var headers)); |
| 39 | + |
| 40 | + // Assert |
| 41 | + var contentString = new StreamReader(content).ReadToEnd(); |
| 42 | + Assert.Equal(200, statusCode); |
| 43 | + Assert.Equal("OK", statusMessage); |
| 44 | + Assert.Equal("this is css", contentString); |
| 45 | + Assert.True(headers.TryGetValue("Content-Type", out var contentTypeValue)); |
| 46 | + Assert.Equal("text/css", contentTypeValue); |
| 47 | + } |
| 48 | + |
| 49 | + private sealed class InMemoryFileProvider : IFileProvider |
| 50 | + { |
| 51 | + public InMemoryFileProvider(IDictionary<string, string> filePathsAndContents) |
| 52 | + { |
| 53 | + if (filePathsAndContents is null) |
| 54 | + { |
| 55 | + throw new ArgumentNullException(nameof(filePathsAndContents)); |
| 56 | + } |
| 57 | + |
| 58 | + FilePathsAndContents = filePathsAndContents; |
| 59 | + } |
| 60 | + |
| 61 | + public IDictionary<string, string> FilePathsAndContents { get; } |
| 62 | + |
| 63 | + public IDirectoryContents GetDirectoryContents(string subpath) |
| 64 | + { |
| 65 | + return new InMemoryDirectoryContents(this, subpath); |
| 66 | + } |
| 67 | + |
| 68 | + public IFileInfo GetFileInfo(string subpath) |
| 69 | + { |
| 70 | + return FilePathsAndContents |
| 71 | + .Where(kvp => kvp.Key == subpath) |
| 72 | + .Select(x => new InMemoryFileInfo(x.Key, x.Value)) |
| 73 | + .Single(); |
| 74 | + } |
| 75 | + |
| 76 | + public IChangeToken Watch(string filter) |
| 77 | + { |
| 78 | + return null; |
| 79 | + } |
| 80 | + |
| 81 | + private sealed class InMemoryDirectoryContents : IDirectoryContents |
| 82 | + { |
| 83 | + private readonly InMemoryFileProvider _inMemoryFileProvider; |
| 84 | + private readonly string _subPath; |
| 85 | + |
| 86 | + public InMemoryDirectoryContents(InMemoryFileProvider inMemoryFileProvider, string subPath) |
| 87 | + { |
| 88 | + _inMemoryFileProvider = inMemoryFileProvider ?? throw new ArgumentNullException(nameof(inMemoryFileProvider)); |
| 89 | + _subPath = subPath ?? throw new ArgumentNullException(nameof(inMemoryFileProvider)); |
| 90 | + } |
| 91 | + |
| 92 | + public bool Exists => true; |
| 93 | + |
| 94 | + public IEnumerator<IFileInfo> GetEnumerator() |
| 95 | + { |
| 96 | + return |
| 97 | + _inMemoryFileProvider |
| 98 | + .FilePathsAndContents |
| 99 | + .Where(kvp => kvp.Key.StartsWith(_subPath, StringComparison.Ordinal)) |
| 100 | + .Select(x => new InMemoryFileInfo(x.Key, x.Value)) |
| 101 | + .GetEnumerator(); |
| 102 | + } |
| 103 | + |
| 104 | + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() |
| 105 | + { |
| 106 | + return GetEnumerator(); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private sealed class InMemoryFileInfo : IFileInfo |
| 111 | + { |
| 112 | + private readonly string _filePath; |
| 113 | + private readonly string _fileContents; |
| 114 | + |
| 115 | + public InMemoryFileInfo(string filePath, string fileContents) |
| 116 | + { |
| 117 | + _filePath = filePath; |
| 118 | + _fileContents = fileContents; |
| 119 | + } |
| 120 | + |
| 121 | + public bool Exists => true; |
| 122 | + |
| 123 | + public long Length => _fileContents.Length; |
| 124 | + |
| 125 | + public string PhysicalPath => null; |
| 126 | + |
| 127 | + public string Name => Path.GetFileName(_filePath); |
| 128 | + |
| 129 | + public DateTimeOffset LastModified => DateTimeOffset.Now; |
| 130 | + |
| 131 | + public bool IsDirectory => false; |
| 132 | + |
| 133 | + public Stream CreateReadStream() |
| 134 | + { |
| 135 | + return new MemoryStream(Encoding.UTF8.GetBytes(_fileContents)); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments