Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@ -0,0 +1,152 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Web.WebPages.ApplicationParts;
using Moq;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;
namespace System.Web.WebPages.Test.ApplicationModule
{
public class ApplicationPartRegistryTest
{
[Fact]
public void ApplicationModuleGeneratesRootRelativePaths()
{
// Arrange
var path1 = "foo/bar";
var path2 = "~/xyz/pqr";
var root1 = "~/myappmodule";
var root2 = "~/myappmodule2/";
// Act
var actualPath11 = ApplicationPartRegistry.GetRootRelativeVirtualPath(root1, path1);
var actualPath12 = ApplicationPartRegistry.GetRootRelativeVirtualPath(root1, path2);
var actualPath21 = ApplicationPartRegistry.GetRootRelativeVirtualPath(root2, path1);
var actualPath22 = ApplicationPartRegistry.GetRootRelativeVirtualPath(root2, path2);
// Assert
Assert.Equal(actualPath11, root1 + "/" + path1);
Assert.Equal(actualPath12, root1 + path2.TrimStart('~'));
Assert.Equal(actualPath21, root2 + path1);
Assert.Equal(actualPath22, root2 + path2.TrimStart('~', '/'));
}
[Fact]
public void ApplicationPartRegistryLooksUpPartsByName()
{
// Arrange
var part = new ApplicationPart(BuildAssembly(), "~/mymodule");
var dictionary = new DictionaryBasedVirtualPathFactory();
var registry = new ApplicationPartRegistry(dictionary);
Func<object> myFunc = () => "foo";
// Act
registry.Register(part, myFunc);
// Assert
Assert.Equal(registry["my-assembly"], part);
Assert.Equal(registry["MY-aSSembly"], part);
}
[Fact]
public void ApplicationPartRegistryLooksUpPartsByAssembly()
{
// Arrange
var assembly = BuildAssembly();
var part = new ApplicationPart(assembly, "~/mymodule");
var dictionary = new DictionaryBasedVirtualPathFactory();
var registry = new ApplicationPartRegistry(dictionary);
Func<object> myFunc = () => "foo";
// Act
registry.Register(part, myFunc);
// Assert
Assert.Equal(registry[assembly], part);
}
[Fact]
public void RegisterThrowsIfAssemblyAlreadyRegistered()
{
// Arrange
var part = new ApplicationPart(BuildAssembly(), "~/mymodule");
var dictionary = new DictionaryBasedVirtualPathFactory();
var registry = new ApplicationPartRegistry(dictionary);
Func<object> myFunc = () => "foo";
// Act
registry.Register(part, myFunc);
// Assert
Assert.Throws<InvalidOperationException>(() => registry.Register(part, myFunc),
String.Format("The assembly \"{0}\" is already registered.", part.Assembly.ToString()));
}
[Fact]
public void RegisterThrowsIfPathAlreadyRegistered()
{
// Arrange
var part = new ApplicationPart(BuildAssembly(), "~/mymodule");
var dictionary = new DictionaryBasedVirtualPathFactory();
var registry = new ApplicationPartRegistry(dictionary);
Func<object> myFunc = () => "foo";
// Act
registry.Register(part, myFunc);
// Assert
var newPart = new ApplicationPart(BuildAssembly("different-assembly"), "~/mymodule");
Assert.Throws<InvalidOperationException>(() => registry.Register(newPart, myFunc),
"An application module is already registered for virtual path \"~/mymodule/\".");
}
[Fact]
public void RegisterCreatesRoutesForValidPages()
{
// Arrange
var part = new ApplicationPart(BuildAssembly(), "~/mymodule");
var dictionary = new DictionaryBasedVirtualPathFactory();
var registry = new ApplicationPartRegistry(dictionary);
Func<object> myFunc = () => "foo";
// Act
registry.Register(part, myFunc);
// Assert
Assert.True(dictionary.Exists("~/mymodule/Page1"));
Assert.Equal(dictionary.CreateInstance("~/mymodule/Page1"), "foo");
Assert.False(dictionary.Exists("~/mymodule/Page2"));
Assert.False(dictionary.Exists("~/mymodule/Page3"));
}
private static IResourceAssembly BuildAssembly(string name = "my-assembly")
{
Mock<TestResourceAssembly> assembly = new Mock<TestResourceAssembly>();
assembly.SetupGet(c => c.Name).Returns(name);
assembly.Setup(c => c.GetHashCode()).Returns(name.GetHashCode());
assembly.Setup(c => c.Equals(It.IsAny<TestResourceAssembly>())).Returns((TestResourceAssembly c) => c.Name == name);
assembly.Setup(c => c.GetTypes()).Returns(new[]
{
BuildPageType(inherits: true, virtualPath: "~/Page1"),
BuildPageType(inherits: true, virtualPath: null),
BuildPageType(inherits: false, virtualPath: "~/Page3"),
});
return assembly.Object;
}
private static Type BuildPageType(bool inherits, string virtualPath)
{
Mock<Type> type = new Mock<Type>();
type.Setup(c => c.IsSubclassOf(typeof(WebPageRenderingBase))).Returns(inherits);
if (virtualPath != null)
{
type.Setup(c => c.GetCustomAttributes(typeof(PageVirtualPathAttribute), false))
.Returns(new[] { new PageVirtualPathAttribute(virtualPath) });
}
return type.Object;
}
}
}

View File

@ -0,0 +1,200 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using Moq;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;
namespace System.Web.WebPages.Test
{
public class ApplicationPartTest
{
[Fact]
public void ApplicationPartThrowsIfRootVirtualPathIsNullOrEmpty()
{
// Arrange
var assembly = new Mock<TestResourceAssembly>().Object;
Assert.ThrowsArgumentNullOrEmptyString(() => new ApplicationPart(assembly, rootVirtualPath: null), "rootVirtualPath");
Assert.ThrowsArgumentNullOrEmptyString(() => new ApplicationPart(assembly, rootVirtualPath: String.Empty), "rootVirtualPath");
}
[Fact]
public void ResolveVirtualPathResolvesRegularPathsUsingBaseVirtualPath()
{
// Arrange
var basePath = "~/base/";
var path = "somefile";
var appPartRoot = "~/app/";
// Act
var virtualPath = ApplicationPart.ResolveVirtualPath(appPartRoot, basePath, path);
// Assert
Assert.Equal(virtualPath, "~/base/somefile");
}
[Fact]
public void ResolveVirtualPathResolvesAppRelativePathsUsingAppVirtualPath()
{
// Arrange
var basePath = "~/base";
var path = "@/somefile";
var appPartRoot = "~/app/";
// Act
var virtualPath = ApplicationPart.ResolveVirtualPath(appPartRoot, basePath, path);
// Assert
Assert.Equal(virtualPath, "~/app/somefile");
}
[Fact]
public void ResolveVirtualPathDoesNotAffectRootRelativePaths()
{
// Arrange
var basePath = "~/base";
var path = "~/somefile";
var appPartRoot = "~/app/";
// Act
var virtualPath = ApplicationPart.ResolveVirtualPath(appPartRoot, basePath, path);
// Assert
Assert.Equal(virtualPath, "~/somefile");
}
[Fact]
public void GetResourceNameFromVirtualPathForTopLevelPath()
{
// Arrange
var moduleName = "my-module";
var path = "foo.baz";
// Act
var name = ApplicationPart.GetResourceNameFromVirtualPath(moduleName, path);
// Assert
Assert.Equal(name, moduleName + "." + path);
}
[Fact]
public void GetResourceNameFromVirtualPathForItemInSubDir()
{
// Arrange
var moduleName = "my-module";
var path = "/bar/foo";
// Act
var name = ApplicationPart.GetResourceNameFromVirtualPath(moduleName, path);
// Assert
Assert.Equal(name, "my-module.bar.foo");
}
[Fact]
public void GetResourceNameFromVirtualPathForItemWithSpaces()
{
// Arrange
var moduleName = "my-module";
var path = "/program files/data files/my file .foo";
// Act
var name = ApplicationPart.GetResourceNameFromVirtualPath(moduleName, path);
// Assert
Assert.Equal(name, "my-module.program_files.data_files.my file .foo");
}
[Fact]
public void GetResourceVirtualPathForTopLevelItem()
{
// Arrange
var moduleName = "my-module";
var moduleRoot = "~/root-path";
var path = moduleRoot + "/foo.txt";
// Act
var virtualPath = ApplicationPart.GetResourceVirtualPath(moduleName, moduleRoot, path);
// Assert
Assert.Equal(virtualPath, "~/r.ashx/" + moduleName + "/" + "foo.txt");
}
[Fact]
public void GetResourceVirtualPathForTopLevelItemAndModuleRootWithTrailingSlash()
{
// Arrange
var moduleName = "my-module";
var moduleRoot = "~/root-path/";
var path = moduleRoot + "/foo.txt";
// Act
var virtualPath = ApplicationPart.GetResourceVirtualPath(moduleName, moduleRoot, path);
// Assert
Assert.Equal(virtualPath, "~/r.ashx/" + moduleName + "/" + "foo.txt");
}
[Fact]
public void GetResourceVirtualPathForTopLevelItemAndNestedModuleRootPath()
{
// Arrange
var moduleName = "my-module";
var moduleRoot = "~/root-path/sub-path";
var path = moduleRoot + "/foo.txt";
// Act
var virtualPath = ApplicationPart.GetResourceVirtualPath(moduleName, moduleRoot, path);
// Assert
Assert.Equal(virtualPath, "~/r.ashx/" + moduleName + "/" + "foo.txt");
}
[Fact]
public void GetResourceVirtualPathEncodesModuleName()
{
// Arrange
var moduleName = "Debugger Package v?&%";
var moduleRoot = "~/root-path/sub-path";
var path = moduleRoot + "/foo.txt";
// Act
var virtualPath = ApplicationPart.GetResourceVirtualPath(moduleName, moduleRoot, path);
// Assert
Assert.Equal(virtualPath, "~/r.ashx/" + "Debugger%20Package%20v?&%" + "/" + "foo.txt");
}
[Fact]
public void GetResourceVirtualPathForNestedItemPath()
{
// Arrange
var moduleName = "DebuggerPackage";
var moduleRoot = "~/root-path/sub-path";
var itemPath = "some-path/some-more-please/foo.txt";
var path = moduleRoot + "/" + itemPath;
// Act
var virtualPath = ApplicationPart.GetResourceVirtualPath(moduleName, moduleRoot, path);
// Assert
Assert.Equal(virtualPath, "~/r.ashx/" + moduleName + "/" + itemPath);
}
[Fact]
public void GetResourceVirtualPathForItemPathWithParameters()
{
// Arrange
var moduleName = "DebuggerPackage";
var moduleRoot = "~/root-path/sub-path";
var itemPath = "some-path/some-more-please/foo.jpg?size=45&height=20";
var path = moduleRoot + "/" + itemPath;
// Act
var virtualPath = ApplicationPart.GetResourceVirtualPath(moduleName, moduleRoot, path);
// Assert
Assert.Equal(virtualPath, "~/r.ashx/" + moduleName + "/" + itemPath);
}
}
}

View File

@ -0,0 +1,63 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using Microsoft.Internal.Web.Utils;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;
namespace System.Web.WebPages.Test
{
public class MimeMappingTest
{
[Fact]
public void MimeMappingThrowsForNullFileName()
{
// Arrange
string fileName = null;
// Act and Assert
Assert.ThrowsArgumentNull(() => MimeMapping.GetMimeMapping(fileName), "fileName");
}
[Fact]
public void MimeMappingReturnsGenericTypeForUnknownExtensions()
{
// Arrange
string fileName = "file.does-not-exist";
// Act
string mimeType = MimeMapping.GetMimeMapping(fileName);
// Assert
Assert.Equal("application/octet-stream", mimeType);
}
[Fact]
public void MimeMappingReturnsGenericTypeForNoExtensions()
{
// Arrange
string fileName = "file";
// Act
string mimeType = MimeMapping.GetMimeMapping(fileName);
// Assert
Assert.Equal("application/octet-stream", mimeType);
}
[Fact]
public void MimeMappingPerformsCaseInsensitiveSearches()
{
// Arrange
string fileName1 = "file.doc";
string fileName2 = "file.dOC";
// Act
string mimeType1 = MimeMapping.GetMimeMapping(fileName1);
string mimeType2 = MimeMapping.GetMimeMapping(fileName2);
// Assert
Assert.Equal("application/msword", mimeType1);
Assert.Equal("application/msword", mimeType2);
}
}
}

View File

@ -0,0 +1,64 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.IO;
using System.Text;
using System.Web.WebPages.ApplicationParts;
using Moq;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;
namespace System.Web.WebPages.Test
{
public class ResourceHandlerTest
{
private const string _fileContent = "contents of jpeg file";
[Fact]
public void ResourceHandlerWritesContentsOfFileToStream()
{
// Arrange
var applicationPart = new ApplicationPart(BuildAssembly(), "~/my-app-assembly");
MemoryStream stream = new MemoryStream();
var response = new Mock<HttpResponseBase>();
response.SetupGet(c => c.OutputStream).Returns(stream);
response.SetupSet(c => c.ContentType = "image/jpeg").Verifiable();
var resourceHandler = new ResourceHandler(applicationPart, "bar.foo.jpg");
// Act
resourceHandler.ProcessRequest(response.Object);
// Assert
response.Verify();
Assert.Equal(Encoding.Default.GetString(stream.ToArray()), _fileContent);
}
[Fact]
public void ResourceHandlerThrows404IfResourceNotFound()
{
// Arrange
var applicationPart = new ApplicationPart(BuildAssembly(), "~/my-app-assembly");
MemoryStream stream = new MemoryStream();
var response = new Mock<HttpResponseBase>();
response.SetupGet(c => c.OutputStream).Returns(stream);
response.SetupSet(c => c.ContentType = "image/jpeg").Verifiable();
var resourceHandler = new ResourceHandler(applicationPart, "does-not-exist");
// Act and Assert
Assert.Throws<HttpException>(() => resourceHandler.ProcessRequest(response.Object),
"The resource file \"does-not-exist\" could not be found.");
}
private static IResourceAssembly BuildAssembly(string name = "my-assembly")
{
Mock<TestResourceAssembly> assembly = new Mock<TestResourceAssembly>();
assembly.SetupGet(c => c.Name).Returns("my-assembly");
byte[] content = Encoding.Default.GetBytes(_fileContent);
assembly.Setup(c => c.GetManifestResourceStream("my-assembly.bar.foo.jpg")).Returns(new MemoryStream(content));
assembly.Setup(c => c.GetManifestResourceNames()).Returns(new[] { "my-assembly.bar.foo.jpg" });
return assembly.Object;
}
}
}

View File

@ -0,0 +1,19 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.IO;
using System.Web.WebPages.ApplicationParts;
namespace System.Web.WebPages.Test
{
public abstract class TestResourceAssembly : IResourceAssembly
{
public abstract string Name { get; }
public abstract Stream GetManifestResourceStream(string name);
public abstract IEnumerable<string> GetManifestResourceNames();
public abstract IEnumerable<Type> GetTypes();
}
}