Permalink
Browse files

Checkin for ASP.NET MVC Codeplex Preview 5

git-svn-id: https://aspnet.svn.codeplex.com/svn@15419 e17a0e51-4ae3-4d35-97c3-1a29b211df97
  • Loading branch information...
1 parent 874a6ff commit 1a2a53bf464ab43fb7a03bff47c50e859f6cac87 SND\AspNetExtensions_cp committed Sep 2, 2008
Showing 305 changed files with 19,903 additions and 6,906 deletions.
@@ -1,91 +0,0 @@
-namespace Microsoft.Web.Mvc {
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- using System.Text;
- using System.Web;
- using System.Web.Routing;
-
- // TODO: Make the *real* TagBuilder public and delete this copy.
- internal class TagBuilder : IHtmlElement {
-
- private const string _attributeFormat = @" {0}=""{1}""";
- private const string _elementFormatClosed = "<{0}{1} />";
- private const string _elementFormatOpen = "<{0}{1}>{2}</{0}>";
-
- private IDictionary<string, string> _attributes;
-
- public TagBuilder(string tagName) {
- if (String.IsNullOrEmpty(tagName)) {
- throw new ArgumentException("// TODO: NULL OR EMPTY", "tagName");
- }
-
- TagName = tagName;
- }
-
- public IDictionary<string, string> Attributes {
- get {
- if (_attributes == null) {
- _attributes = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
- }
- return _attributes;
- }
- set {
- _attributes = value;
- }
- }
-
- public string InnerHtml {
- get;
- set;
- }
-
- public string TagName {
- get;
- private set;
- }
-
- public override string ToString() {
- string attributesString = String.Empty;
- if (Attributes != null) {
- StringBuilder sb = new StringBuilder();
- foreach (var attribute in Attributes) {
- string key = attribute.Key;
- string value = HttpUtility.HtmlAttributeEncode(attribute.Value);
- sb.AppendFormat(CultureInfo.InvariantCulture, _attributeFormat, key, value);
- }
- attributesString = sb.ToString();
- }
-
- return (InnerHtml != null) ?
- String.Format(CultureInfo.InvariantCulture, _elementFormatOpen, TagName, attributesString, InnerHtml) :
- String.Format(CultureInfo.InvariantCulture, _elementFormatClosed, TagName, attributesString);
- }
-
- internal static Dictionary<string, string> ToStringDictionary<TKey, TValue>(IDictionary<TKey, TValue> dictionary) {
- if (dictionary == null) {
- return new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
- }
-
- return dictionary.ToDictionary(
- entry => Convert.ToString(entry.Key, CultureInfo.InvariantCulture),
- entry => Convert.ToString(entry.Value, CultureInfo.InvariantCulture),
- StringComparer.OrdinalIgnoreCase);
- }
-
- internal static bool TryAddValue<TKey, TValue>(IDictionary<TKey, TValue> dict, TKey key, TValue value) {
- if (dict.ContainsKey(key)) {
- return false;
- }
- else {
- dict[key] = value;
- return true;
- }
- }
-
- internal static IDictionary<string, object> ToDictionary(object values) {
- return new RouteValueDictionary(values);
- }
- }
-}
@@ -1,14 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-
-namespace Microsoft.Web.Mvc
-{
- public interface IHtmlElement
- {
- IDictionary<string, string> Attributes { get; }
- string TagName { get; }
- string InnerHtml { get; set; }
- }
-}
@@ -1,34 +0,0 @@
-namespace Microsoft.Web.Mvc {
- using System;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using System.Collections.Generic;
-
- public class RequireHttpMethodAttribute : ActionFilterAttribute {
- public RequireHttpMethodAttribute() : this(null) {
- }
-
- public RequireHttpMethodAttribute(params string[] methods) {
- if (methods == null) {
- methods = new string[0];
- }
- Methods = methods;
- }
-
- public ICollection<string> Methods {
- get;
- private set;
- }
-
- public override void OnActionExecuting(ActionExecutingContext filterContext) {
- string currentMethod = filterContext.HttpContext.Request.HttpMethod;
-
- if (Methods.Any(method => method != null && String.Equals(method.Trim(), currentMethod, StringComparison.OrdinalIgnoreCase))) {
- return;
- }
-
- throw new HttpException(405, "Method Not Allowed"); //TODO: Resource string.
- }
- }
-}
View
@@ -1,22 +1,17 @@

Microsoft Visual Studio Solution File, Format Version 10.00
# Visual Studio 2008
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Web.Mvc", "v1\System.Web.Mvc.csproj", "{3D3FFD8A-624D-4E9B-954B-E1C105507975}"
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Web.Mvc", "src\SystemWebMvc\System.Web.Mvc.csproj", "{3D3FFD8A-624D-4E9B-954B-E1C105507975}"
EndProject
-Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ReferenceAssemblies", "ReferenceAssemblies", "{749C28D8-5653-4DCA-9C11-ADE63BA1F766}"
- ProjectSection(SolutionItems) = preProject
- ..\ReferenceAssemblies\Moq.dll = ..\ReferenceAssemblies\Moq.dll
- ..\ReferenceAssemblies\System.Web.Abstractions.dll = ..\ReferenceAssemblies\System.Web.Abstractions.dll
- ..\ReferenceAssemblies\System.Web.Routing.dll = ..\ReferenceAssemblies\System.Web.Routing.dll
- EndProjectSection
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MicrosoftMvcAjaxScript", "src\MicrosoftMvcAjaxScript\MicrosoftMvcAjaxScript.csproj", "{256C18D8-0A46-4DEC-A9D6-D8073D15F453}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Web.Mvc.Test", "Test\SystemWebMvcTest\System.Web.Mvc.Test.csproj", "{8AC2A2E4-2F11-4D40-A887-62E2583A65E6}"
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MvcFutures", "src\MvcFutures\MvcFutures.csproj", "{D3CF7430-6DA4-42B0-BD90-CA39D16687B2}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MvcFutures", "Futures\MvcFutures\MvcFutures.csproj", "{D3CF7430-6DA4-42B0-BD90-CA39D16687B2}"
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MvcFuturesTest", "test\MvcFuturesTest\MvcFuturesTest.csproj", "{6C28DA70-60F1-4442-967F-591BF3962EC5}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTestUtil", "Test\UnitTestUtil\UnitTestUtil.csproj", "{57FFC134-7F9D-4B97-8692-8D2C16714663}"
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Web.Mvc.Test", "test\SystemWebMvcTest\System.Web.Mvc.Test.csproj", "{8AC2A2E4-2F11-4D40-A887-62E2583A65E6}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MvcFuturesTest", "Test\MvcFuturesTest\MvcFuturesTest.csproj", "{6C28DA70-60F1-4442-967F-591BF3962EC5}"
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTestUtil", "test\UnitTestUtil\UnitTestUtil.csproj", "{57FFC134-7F9D-4B97-8692-8D2C16714663}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -28,22 +23,26 @@ Global
{3D3FFD8A-624D-4E9B-954B-E1C105507975}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3D3FFD8A-624D-4E9B-954B-E1C105507975}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3D3FFD8A-624D-4E9B-954B-E1C105507975}.Release|Any CPU.Build.0 = Release|Any CPU
- {8AC2A2E4-2F11-4D40-A887-62E2583A65E6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
- {8AC2A2E4-2F11-4D40-A887-62E2583A65E6}.Debug|Any CPU.Build.0 = Debug|Any CPU
- {8AC2A2E4-2F11-4D40-A887-62E2583A65E6}.Release|Any CPU.ActiveCfg = Release|Any CPU
- {8AC2A2E4-2F11-4D40-A887-62E2583A65E6}.Release|Any CPU.Build.0 = Release|Any CPU
+ {256C18D8-0A46-4DEC-A9D6-D8073D15F453}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {256C18D8-0A46-4DEC-A9D6-D8073D15F453}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {256C18D8-0A46-4DEC-A9D6-D8073D15F453}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {256C18D8-0A46-4DEC-A9D6-D8073D15F453}.Release|Any CPU.Build.0 = Release|Any CPU
{D3CF7430-6DA4-42B0-BD90-CA39D16687B2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D3CF7430-6DA4-42B0-BD90-CA39D16687B2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D3CF7430-6DA4-42B0-BD90-CA39D16687B2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D3CF7430-6DA4-42B0-BD90-CA39D16687B2}.Release|Any CPU.Build.0 = Release|Any CPU
- {57FFC134-7F9D-4B97-8692-8D2C16714663}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
- {57FFC134-7F9D-4B97-8692-8D2C16714663}.Debug|Any CPU.Build.0 = Debug|Any CPU
- {57FFC134-7F9D-4B97-8692-8D2C16714663}.Release|Any CPU.ActiveCfg = Release|Any CPU
- {57FFC134-7F9D-4B97-8692-8D2C16714663}.Release|Any CPU.Build.0 = Release|Any CPU
{6C28DA70-60F1-4442-967F-591BF3962EC5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6C28DA70-60F1-4442-967F-591BF3962EC5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6C28DA70-60F1-4442-967F-591BF3962EC5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6C28DA70-60F1-4442-967F-591BF3962EC5}.Release|Any CPU.Build.0 = Release|Any CPU
+ {8AC2A2E4-2F11-4D40-A887-62E2583A65E6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {8AC2A2E4-2F11-4D40-A887-62E2583A65E6}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {8AC2A2E4-2F11-4D40-A887-62E2583A65E6}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {8AC2A2E4-2F11-4D40-A887-62E2583A65E6}.Release|Any CPU.Build.0 = Release|Any CPU
+ {57FFC134-7F9D-4B97-8692-8D2C16714663}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {57FFC134-7F9D-4B97-8692-8D2C16714663}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {57FFC134-7F9D-4B97-8692-8D2C16714663}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {57FFC134-7F9D-4B97-8692-8D2C16714663}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Binary file not shown.
Binary file not shown.
@@ -1,20 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Web;
-using Moq;
-
-namespace MvcFuturesTest {
- public static class MockHelper {
- public static StringBuilder SwitchResponseMockOutputToStringBuilder(HttpResponseBase response) {
- return SwitchResponseMockOutputToStringBuilder(Mock.Get(response));
- }
-
- public static StringBuilder SwitchResponseMockOutputToStringBuilder(Mock<HttpResponseBase> responseMock) {
- var sb = new StringBuilder();
- responseMock.Expect(response => response.Write(It.IsAny<string>())).Callback<string>(output => sb.Append(output));
- return sb;
- }
- }
-}
@@ -0,0 +1,122 @@
+namespace MvcFuturesTest.Mvc.Test {
+ using System;
+ using System.Globalization;
+ using System.Text.RegularExpressions;
+ using System.Web;
+ using System.Web.Mvc;
+ using System.Web.Routing;
+ using System.Web.TestUtil;
+ using Microsoft.VisualStudio.TestTools.UnitTesting;
+ using Microsoft.Web.Mvc;
+ using Moq;
+
+ [TestClass]
+ public class AntiForgeryHelperTest {
+
+ private static AntiForgeryHelper _helper = new SubclassedAntiForgeryHelper(new SubclassedAntiForgeryTokenSerializer());
+ private const string _serializedValuePrefix = @"<input name=""__MVC_AntiForgeryToken"" type=""hidden"" value=""Creation: ";
+ private const string _someValueSuffix = @", Value: some value, Salt: some other salt"" />";
+ private readonly Regex _randomFormValueSuffixRegex = new Regex(@", Value: (?<value>[A-Za-z0-9/\+=]{24}), Salt: some other salt"" />$");
+ private readonly Regex _randomCookieValueSuffixRegex = new Regex(@", Value: (?<value>[A-Za-z0-9/\+=]{24}), Salt: $");
+
+ [TestMethod]
+ public void AntiForgeryTokenThrowsIfHelperIsNull() {
+ // Act & Assert
+ ExceptionHelper.ExpectArgumentNullException(
+ delegate {
+ _helper.AntiForgeryToken(null, null);
+ }, "helper");
+ }
+
+ [TestMethod]
+ public void AntiForgeryTokenSetsCookieValueIfDoesNotExist() {
+ // Arrange
+ HtmlHelper htmlHelper = GetHtmlHelper(null);
+
+ // Act
+ string formValue = _helper.AntiForgeryToken(htmlHelper, "some other salt");
+
+ // Assert
+ Assert.IsTrue(formValue.StartsWith(_serializedValuePrefix), "Form value prefix did not match.");
+
+ Match formMatch = _randomFormValueSuffixRegex.Match(formValue);
+ string formTokenValue = formMatch.Groups["value"].Value;
+
+ HttpCookie cookie = htmlHelper.ViewContext.HttpContext.Response.Cookies["__MVC_AntiForgeryToken"];
+ Assert.IsNotNull(cookie, "Cookie was not set correctly.");
+
+ Match cookieMatch = _randomCookieValueSuffixRegex.Match(cookie.Value);
+ string cookieTokenValue = cookieMatch.Groups["value"].Value;
+
+ Assert.AreEqual(formTokenValue, cookieTokenValue, "Form and cookie token values did not match.");
+ }
+
+ [TestMethod]
+ public void AntiForgeryTokenUsesCookieValueIfExists() {
+ // Arrange
+ HtmlHelper htmlHelper = GetHtmlHelper("2001-01-01:some value:some salt");
+
+ // Act
+ string formValue = _helper.AntiForgeryToken(htmlHelper, "some other salt");
+
+ // Assert
+ Assert.IsTrue(formValue.StartsWith(_serializedValuePrefix), "Form value prefix did not match.");
+ Assert.IsTrue(formValue.EndsWith(_someValueSuffix), "Form value suffix did not match.");
+ Assert.AreEqual(0, htmlHelper.ViewContext.HttpContext.Response.Cookies.Count, "Cookie should not have been added to response.");
+ }
+
+ [TestMethod]
+ public void SerializerProperty() {
+ // Arrange
+ AntiForgeryHelper helper = new AntiForgeryHelper();
+ AntiForgeryTokenSerializer defaultSerializer = DefaultAntiForgeryTokenSerializer.Instance;
+ AntiForgeryTokenSerializer newSerializer = new Mock<AntiForgeryTokenSerializer>().Object;
+
+ // Act & Assert
+ MemberHelper.TestPropertyWithDefaultInstance(helper, "Serializer", newSerializer, defaultSerializer);
+ }
+
+ public static HtmlHelper GetHtmlHelper(string cookieValue) {
+ Mock<HttpRequestBase> mockRequest = new Mock<HttpRequestBase>();
+ HttpCookieCollection requestCookies = new HttpCookieCollection();
+ mockRequest.Expect(r => r.Cookies).Returns(requestCookies);
+ if (!String.IsNullOrEmpty(cookieValue)) {
+ requestCookies.Set(new HttpCookie("__MVC_AntiForgeryToken", cookieValue));
+ }
+
+ Mock<HttpResponseBase> mockResponse = new Mock<HttpResponseBase>();
+ HttpCookieCollection responseCookies = new HttpCookieCollection();
+ mockResponse.Expect(r => r.Cookies).Returns(responseCookies);
+
+ Mock<HttpContextBase> mockHttpContext = new Mock<HttpContextBase>();
+ mockHttpContext.Expect(c => c.Request).Returns(mockRequest.Object);
+ mockHttpContext.Expect(c => c.Response).Returns(mockResponse.Object);
+
+ ViewContext viewContext = new ViewContext(mockHttpContext.Object, new RouteData(),
+ new Mock<ControllerBase>().Object, "someView", new ViewDataDictionary(), new TempDataDictionary());
+ return new HtmlHelper(viewContext, new Mock<IViewDataContainer>().Object);
+ }
+
+ private class SubclassedAntiForgeryHelper : AntiForgeryHelper {
+ public SubclassedAntiForgeryHelper(AntiForgeryTokenSerializer serializer) {
+ Serializer = serializer;
+ }
+ }
+
+ public class SubclassedAntiForgeryTokenSerializer : AntiForgeryTokenSerializer {
+ public override string Serialize(AntiForgeryToken token) {
+ return String.Format(CultureInfo.InvariantCulture, "Creation: {0}, Value: {1}, Salt: {2}",
+ token.CreationDate, token.Value, token.Salt);
+ }
+ public override AntiForgeryToken Deserialize(string serializedToken) {
+ string[] parts = serializedToken.Split(':');
+ return new AntiForgeryToken() {
+ CreationDate = DateTime.Parse(parts[0], CultureInfo.InvariantCulture),
+ Value = parts[1],
+ Salt = parts[2]
+ };
+ }
+ }
+
+ }
+}
Oops, something went wrong.

0 comments on commit 1a2a53b

Please sign in to comment.