Created
October 7, 2016 14:22
-
-
Save richrout/6d3529ee0e985bb1de1fbd71f2e6e040 to your computer and use it in GitHub Desktop.
Testing ASP.NET MVC BundleConfig with VirtualPathProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestVirtualPathProvider : VirtualPathProvider | |
{ | |
private string NormalizeVirtualPath(string virtualPath, bool isDirectory = false) | |
{ | |
if (!virtualPath.StartsWith("~")) | |
{ | |
virtualPath = "~" + virtualPath; | |
} | |
virtualPath = virtualPath.Replace('\\', '/'); | |
// Normalize directories to always have an ending "/" | |
if (isDirectory && !virtualPath.EndsWith("/")) | |
{ | |
return virtualPath + "/"; | |
} | |
return virtualPath; | |
} | |
// Files on disk (virtualPath -> file) | |
private Dictionary<string, VirtualFile> _fileMap = new Dictionary<string, VirtualFile>(); | |
private Dictionary<string, VirtualFile> FileMap | |
{ | |
get { return _fileMap; } | |
} | |
public void AddFile(VirtualFile file) | |
{ | |
FileMap[NormalizeVirtualPath(file.VirtualPath)] = file; | |
} | |
private Dictionary<string, VirtualDirectory> _directoryMap = new Dictionary<string, VirtualDirectory>(); | |
private Dictionary<string, VirtualDirectory> DirectoryMap | |
{ | |
get { return _directoryMap; } | |
} | |
public void AddDirectory(VirtualDirectory dir) | |
{ | |
DirectoryMap[NormalizeVirtualPath(dir.VirtualPath, isDirectory: true)] = dir; | |
} | |
public override bool FileExists(string virtualPath) | |
{ | |
return FileMap.ContainsKey(NormalizeVirtualPath(virtualPath)); | |
} | |
public override bool DirectoryExists(string virtualDir) | |
{ | |
return DirectoryMap.ContainsKey(NormalizeVirtualPath(virtualDir, isDirectory: true)); | |
} | |
public override VirtualFile GetFile(string virtualPath) | |
{ | |
return FileMap[NormalizeVirtualPath(virtualPath)]; | |
} | |
public override VirtualDirectory GetDirectory(string virtualDir) | |
{ | |
return DirectoryMap[NormalizeVirtualPath(virtualDir, isDirectory: true)]; | |
} | |
internal class TestVirtualFile : VirtualFile | |
{ | |
public TestVirtualFile(string virtualPath, string contents) | |
: base(virtualPath) | |
{ | |
Contents = contents; | |
} | |
public string Contents { get; set; } | |
public override Stream Open() | |
{ | |
return new MemoryStream(UTF8Encoding.Default.GetBytes(Contents)); | |
} | |
} | |
internal class TestVirtualDirectory : VirtualDirectory | |
{ | |
public TestVirtualDirectory(string virtualPath) | |
: base(virtualPath) | |
{ | |
} | |
public List<VirtualFile> _directoryFiles = new List<VirtualFile>(); | |
public List<VirtualFile> DirectoryFiles | |
{ | |
get { return _directoryFiles; } | |
} | |
public List<VirtualDirectory> _subDirs = new List<VirtualDirectory>(); | |
public List<VirtualDirectory> SubDirectories | |
{ | |
get { return _subDirs; } | |
} | |
public override IEnumerable Files | |
{ | |
get { return DirectoryFiles; } | |
} | |
public override IEnumerable Children | |
{ | |
get { throw new NotImplementedException(); } | |
} | |
public override IEnumerable Directories | |
{ | |
get { return SubDirectories; } | |
} | |
} | |
[Test] | |
public void StyleBundleIncludesVersion() | |
{ | |
//Setup the vpp to contain the files/directories | |
var vpp = new TestVirtualPathProvider(); | |
vpp.AddFile(new TestVirtualFile("/WebResource/Javascript/knockout-3.4.0.js", "Knockout;")); | |
vpp.AddFile(new TestVirtualFile("/WebResource/TypeScript/App.js", "App;")); | |
vpp.AddFile(new TestVirtualFile("/WebResource/Core/raven.min.js", "Raven;")); | |
// Setup the bundle | |
var bundleCollection = new BundleCollection(); | |
BundleConfig.RegisterBundles(bundleCollection); | |
BundleTable.VirtualPathProvider = vpp; | |
var mockHttpContext = new Mock<HttpContextBase>(); | |
// Verify the bundle repsonse | |
var context = new BundleContext(mockHttpContext.Object, bundleCollection, vpp.ToString()); | |
var response = bundleCollection.First().GenerateBundleResponse(context); | |
Assert.AreEqual(@"Knockout;App;Raven", response.Content); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment