PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/Test/SystemWebMvcTest/Mvc/Test/AcceptVerbsAttributeTest.cs

https://bitbucket.org/markhneedham/aspnet-mvc
C# | 170 lines | 121 code | 28 blank | 21 comment | 5 complexity | 599ec45a5d81c375fcad2899bf85bcb8 MD5 | raw file
  1. namespace System.Web.Mvc.Test {
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Collections.ObjectModel;
  6. using System.Linq;
  7. using System.Web;
  8. using System.Web.Mvc;
  9. using System.Web.Routing;
  10. using System.Web.TestUtil;
  11. using Microsoft.VisualStudio.TestTools.UnitTesting;
  12. using Moq;
  13. [TestClass]
  14. public class AcceptVerbsAttributeTest {
  15. private const string _invalidEnumFormatString = @"The enum '{0}' did not produce the correct array.
  16. Expected: {1}
  17. Actual: {2}";
  18. [TestMethod]
  19. public void ConstructorThrowsIfVerbsIsEmpty() {
  20. // Act & Assert
  21. ExceptionHelper.ExpectArgumentExceptionNullOrEmpty(
  22. delegate {
  23. new AcceptVerbsAttribute(new string[0]);
  24. }, "verbs");
  25. }
  26. [TestMethod]
  27. public void ConstructorThrowsIfVerbsIsNull() {
  28. // Act & Assert
  29. ExceptionHelper.ExpectArgumentExceptionNullOrEmpty(
  30. delegate {
  31. new AcceptVerbsAttribute((string[])null);
  32. }, "verbs");
  33. }
  34. [TestMethod]
  35. public void EnumToArray() {
  36. // Arrange
  37. IDictionary<string, HttpVerbs> enumValues = EnumToDictionary<HttpVerbs>();
  38. var allCombinations = EnumerableToCombinations(enumValues);
  39. // Act & assert
  40. foreach (var combination in allCombinations) {
  41. // generate all the names + values in this combination
  42. List<string> aggrNames = new List<string>();
  43. HttpVerbs aggrValues = (HttpVerbs)0;
  44. foreach (var entry in combination) {
  45. aggrNames.Add(entry.Key);
  46. aggrValues |= entry.Value;
  47. }
  48. // get the resulting array
  49. string[] array = AcceptVerbsAttribute.EnumToArray(aggrValues);
  50. var aggrNamesOrdered = aggrNames.OrderBy(name => name, StringComparer.OrdinalIgnoreCase);
  51. var arrayOrdered = array.OrderBy(name => name, StringComparer.OrdinalIgnoreCase);
  52. bool match = aggrNamesOrdered.SequenceEqual(arrayOrdered, StringComparer.OrdinalIgnoreCase);
  53. if (!match) {
  54. string message = String.Format(_invalidEnumFormatString, aggrValues,
  55. aggrNames.Aggregate((a, b) => a + ", " + b),
  56. array.Aggregate((a, b) => a + ", " + b));
  57. Assert.Fail(message);
  58. }
  59. }
  60. }
  61. [TestMethod]
  62. public void IsValidForRequestReturnsFalseIfHttpVerbIsNotInVerbsCollection() {
  63. // Arrange
  64. AcceptVerbsAttribute attr = new AcceptVerbsAttribute("get", "post");
  65. ControllerContext context = GetControllerContext("HEAD");
  66. // Act
  67. bool result = attr.IsValidForRequest(context, null);
  68. // Assert
  69. Assert.IsFalse(result);
  70. }
  71. [TestMethod]
  72. public void IsValidForRequestReturnsTrueIfHttpVerbIsInVerbsCollection() {
  73. // Arrange
  74. AcceptVerbsAttribute attr = new AcceptVerbsAttribute("get", "post");
  75. ControllerContext context = GetControllerContext("POST");
  76. // Act
  77. bool result = attr.IsValidForRequest(context, null);
  78. // Assert
  79. Assert.IsTrue(result);
  80. }
  81. [TestMethod]
  82. public void IsValidForRequestThrowsIfControllerContextIsNull() {
  83. // Arrange
  84. AcceptVerbsAttribute attr = new AcceptVerbsAttribute("get", "post");
  85. // Act & Assert
  86. ExceptionHelper.ExpectArgumentNullException(
  87. delegate {
  88. attr.IsValidForRequest(null, null);
  89. }, "controllerContext");
  90. }
  91. [TestMethod]
  92. public void VerbsPropertyFromEnumConstructor() {
  93. // Arrange
  94. AcceptVerbsAttribute attr = new AcceptVerbsAttribute(HttpVerbs.Get | HttpVerbs.Post);
  95. // Act
  96. ReadOnlyCollection<string> collection = attr.Verbs as ReadOnlyCollection<string>;
  97. // Assert
  98. Assert.IsNotNull(collection, "Verbs property should have returned read-only collection.");
  99. Assert.AreEqual(2, collection.Count);
  100. Assert.AreEqual("GET", collection[0]);
  101. Assert.AreEqual("POST", collection[1]);
  102. }
  103. [TestMethod]
  104. public void VerbsPropertyFromStringArrayConstructor() {
  105. // Arrange
  106. AcceptVerbsAttribute attr = new AcceptVerbsAttribute("get", "post");
  107. // Act
  108. ReadOnlyCollection<string> collection = attr.Verbs as ReadOnlyCollection<string>;
  109. // Assert
  110. Assert.IsNotNull(collection, "Verbs property should have returned read-only collection.");
  111. Assert.AreEqual(2, collection.Count);
  112. Assert.AreEqual("get", collection[0]);
  113. Assert.AreEqual("post", collection[1]);
  114. }
  115. private static ControllerContext GetControllerContext(string httpVerb) {
  116. Mock<HttpRequestBase> mockHttpRequest = new Mock<HttpRequestBase>();
  117. mockHttpRequest.Expect(r => r.HttpMethod).Returns(httpVerb);
  118. Mock<HttpContextBase> mockHttpContext = new Mock<HttpContextBase>();
  119. mockHttpContext.Expect(c => c.Request).Returns(mockHttpRequest.Object);
  120. return new ControllerContext(mockHttpContext.Object, new RouteData(), new Mock<ControllerBase>().Object);
  121. }
  122. private static IDictionary<string, TEnum> EnumToDictionary<TEnum>() {
  123. // Arrange
  124. var values = Enum.GetValues(typeof(TEnum)).Cast<TEnum>();
  125. return values.ToDictionary(value => Enum.GetName(typeof(TEnum), value), value => value);
  126. }
  127. private static IEnumerable<ICollection<T>> EnumerableToCombinations<T>(IEnumerable<T> elements) {
  128. List<T> allElements = elements.ToList();
  129. int maxCount = 1 << allElements.Count;
  130. for (int idxCombination = 0; idxCombination < maxCount; idxCombination++) {
  131. List<T> thisCollection = new List<T>();
  132. for (int idxBit = 0; idxBit < 32; idxBit++) {
  133. bool bitActive = (((uint)idxCombination >> idxBit) & 1) != 0;
  134. if (bitActive) {
  135. thisCollection.Add(allElements[idxBit]);
  136. }
  137. }
  138. yield return thisCollection;
  139. }
  140. }
  141. }
  142. }