PageRenderTime 198ms CodeModel.GetById 93ms RepoModel.GetById 1ms app.codeStats 0ms

/test/System.Web.Mvc.Test/Html/Test/FormExtensionsTest.cs

https://bitbucket.org/mdavid/aspnetwebstack
C# | 423 lines | 348 code | 59 blank | 16 comment | 0 complexity | e6fbb2046343ca09959bbd8f2e400e3f MD5 | raw file
  1. using System.Collections;
  2. using System.Collections.Specialized;
  3. using System.IO;
  4. using System.Web.Routing;
  5. using Microsoft.Web.UnitTestUtil;
  6. using Moq;
  7. using Xunit;
  8. namespace System.Web.Mvc.Html.Test
  9. {
  10. public class FormExtensionsTest
  11. {
  12. private static void BeginFormHelper(Func<HtmlHelper, MvcForm> beginForm, string expectedFormTag)
  13. {
  14. // Arrange
  15. StringWriter writer;
  16. HtmlHelper htmlHelper = GetFormHelper(out writer);
  17. // Act
  18. IDisposable formDisposable = beginForm(htmlHelper);
  19. formDisposable.Dispose();
  20. // Assert
  21. Assert.Equal(expectedFormTag + "</form>", writer.ToString());
  22. }
  23. [Fact]
  24. public void BeginFormParameterDictionaryMerging()
  25. {
  26. BeginFormHelper(
  27. htmlHelper => htmlHelper.BeginForm("bar", "foo", FormMethod.Get, new RouteValueDictionary(new { method = "post" })),
  28. @"<form action=""" + MvcHelper.AppPathModifier + @"/foo/bar"" method=""get"">");
  29. }
  30. [Fact]
  31. public void BeginFormSetsAndRestoresToDefault()
  32. {
  33. // Arrange
  34. StringWriter writer;
  35. HtmlHelper htmlHelper = GetFormHelper(out writer);
  36. htmlHelper.ViewContext.FormContext = null;
  37. FormContext defaultFormContext = htmlHelper.ViewContext.FormContext;
  38. // Act & assert - push
  39. MvcForm theForm = htmlHelper.BeginForm();
  40. Assert.NotNull(htmlHelper.ViewContext.FormContext);
  41. Assert.NotEqual(defaultFormContext, htmlHelper.ViewContext.FormContext);
  42. // Act & assert - pop
  43. theForm.Dispose();
  44. Assert.Equal(defaultFormContext, htmlHelper.ViewContext.FormContext);
  45. Assert.Equal(@"<form action=""/some/path"" method=""post""></form>", writer.ToString());
  46. }
  47. [Fact]
  48. public void BeginFormWithClientValidationEnabled()
  49. {
  50. // Arrange
  51. StringWriter writer;
  52. HtmlHelper htmlHelper = GetFormHelper(out writer);
  53. htmlHelper.ViewContext.ClientValidationEnabled = true;
  54. htmlHelper.ViewContext.FormContext = null;
  55. FormContext defaultFormContext = htmlHelper.ViewContext.FormContext;
  56. // Act & assert - push
  57. MvcForm theForm = htmlHelper.BeginForm();
  58. Assert.NotNull(htmlHelper.ViewContext.FormContext);
  59. Assert.NotEqual(defaultFormContext, htmlHelper.ViewContext.FormContext);
  60. Assert.Equal("form_id", htmlHelper.ViewContext.FormContext.FormId);
  61. // Act & assert - pop
  62. theForm.Dispose();
  63. Assert.Equal(defaultFormContext, htmlHelper.ViewContext.FormContext);
  64. Assert.Equal(@"<form action=""/some/path"" id=""form_id"" method=""post""></form><script type=""text/javascript"">
  65. //<![CDATA[
  66. if (!window.mvcClientValidationMetadata) { window.mvcClientValidationMetadata = []; }
  67. window.mvcClientValidationMetadata.push({""Fields"":[],""FormId"":""form_id"",""ReplaceValidationSummary"":false});
  68. //]]>
  69. </script>", writer.ToString());
  70. }
  71. [Fact]
  72. public void BeginFormWithClientValidationAndUnobtrusiveJavaScriptEnabled()
  73. {
  74. // Arrange
  75. StringWriter writer;
  76. HtmlHelper htmlHelper = GetFormHelper(out writer);
  77. htmlHelper.ViewContext.ClientValidationEnabled = true;
  78. htmlHelper.ViewContext.UnobtrusiveJavaScriptEnabled = true;
  79. // Act & assert - push
  80. MvcForm theForm = htmlHelper.BeginForm();
  81. Assert.Null(htmlHelper.ViewContext.FormContext.FormId);
  82. // Act & assert - pop
  83. theForm.Dispose();
  84. Assert.Equal(@"<form action=""/some/path"" method=""post""></form>", writer.ToString());
  85. }
  86. [Fact]
  87. public void BeginFormWithActionControllerInvalidFormMethodHtmlValues()
  88. {
  89. BeginFormHelper(
  90. htmlHelper => htmlHelper.BeginForm("bar", "foo", (FormMethod)2, new RouteValueDictionary(new { baz = "baz" })),
  91. @"<form action=""" + MvcHelper.AppPathModifier + @"/foo/bar"" baz=""baz"" method=""post"">");
  92. }
  93. [Fact]
  94. public void BeginFormWithActionController()
  95. {
  96. BeginFormHelper(
  97. htmlHelper => htmlHelper.BeginForm("bar", "foo"),
  98. @"<form action=""" + MvcHelper.AppPathModifier + @"/foo/bar"" method=""post"">");
  99. }
  100. [Fact]
  101. public void BeginFormWithActionControllerFormMethodHtmlDictionary()
  102. {
  103. BeginFormHelper(
  104. htmlHelper => htmlHelper.BeginForm("bar", "foo", FormMethod.Get, new RouteValueDictionary(new { baz = "baz" })),
  105. @"<form action=""" + MvcHelper.AppPathModifier + @"/foo/bar"" baz=""baz"" method=""get"">");
  106. }
  107. [Fact]
  108. public void BeginFormWithActionControllerFormMethodHtmlValues()
  109. {
  110. BeginFormHelper(
  111. htmlHelper => htmlHelper.BeginForm("bar", "foo", FormMethod.Get, new { baz = "baz" }),
  112. @"<form action=""" + MvcHelper.AppPathModifier + @"/foo/bar"" baz=""baz"" method=""get"">");
  113. }
  114. [Fact]
  115. public void BeginFormWithActionControllerFormMethodHtmlValuesWithUnderscores()
  116. {
  117. BeginFormHelper(
  118. htmlHelper => htmlHelper.BeginForm("bar", "foo", FormMethod.Get, new { data_test = "value" }),
  119. @"<form action=""" + MvcHelper.AppPathModifier + @"/foo/bar"" data-test=""value"" method=""get"">");
  120. }
  121. [Fact]
  122. public void BeginFormWithActionControllerRouteDictionaryFormMethodHtmlDictionary()
  123. {
  124. BeginFormHelper(
  125. htmlHelper => htmlHelper.BeginForm("bar", "foo", new RouteValueDictionary(new { id = "id" }), FormMethod.Get, new RouteValueDictionary(new { baz = "baz" })),
  126. @"<form action=""" + MvcHelper.AppPathModifier + @"/foo/bar/id"" baz=""baz"" method=""get"">");
  127. }
  128. [Fact]
  129. public void BeginFormWithActionControllerRouteValuesFormMethodHtmlValues()
  130. {
  131. BeginFormHelper(
  132. htmlHelper => htmlHelper.BeginForm("bar", "foo", new { id = "id" }, FormMethod.Get, new { baz = "baz" }),
  133. @"<form action=""" + MvcHelper.AppPathModifier + @"/foo/bar/id"" baz=""baz"" method=""get"">");
  134. }
  135. [Fact]
  136. public void BeginFormWithActionControllerRouteValuesFormMethodHtmlValuesWithUnderscores()
  137. {
  138. BeginFormHelper(
  139. htmlHelper => htmlHelper.BeginForm("bar", "foo", new { id = "id" }, FormMethod.Get, new { foo_baz = "baz" }),
  140. @"<form action=""" + MvcHelper.AppPathModifier + @"/foo/bar/id"" foo-baz=""baz"" method=""get"">");
  141. }
  142. [Fact]
  143. public void BeginFormWithActionControllerNullRouteValuesFormMethodNullHtmlValues()
  144. {
  145. BeginFormHelper(
  146. htmlHelper => htmlHelper.BeginForm("bar", "foo", null, FormMethod.Get, null),
  147. @"<form action=""" + MvcHelper.AppPathModifier + @"/foo/bar"" method=""get"">");
  148. }
  149. [Fact]
  150. public void BeginFormWithRouteValues()
  151. {
  152. BeginFormHelper(
  153. htmlHelper => htmlHelper.BeginForm(new { action = "someOtherAction", id = "id" }),
  154. @"<form action=""" + MvcHelper.AppPathModifier + @"/home/someOtherAction/id"" method=""post"">");
  155. }
  156. [Fact]
  157. public void BeginFormWithRouteDictionary()
  158. {
  159. BeginFormHelper(
  160. htmlHelper => htmlHelper.BeginForm(new RouteValueDictionary { { "action", "someOtherAction" }, { "id", "id" } }),
  161. @"<form action=""" + MvcHelper.AppPathModifier + @"/home/someOtherAction/id"" method=""post"">");
  162. }
  163. [Fact]
  164. public void BeginFormWithActionControllerRouteValues()
  165. {
  166. BeginFormHelper(
  167. htmlHelper => htmlHelper.BeginForm("myAction", "myController", new { id = "id", pageNum = "123" }),
  168. @"<form action=""" + MvcHelper.AppPathModifier + @"/myController/myAction/id?pageNum=123"" method=""post"">");
  169. }
  170. [Fact]
  171. public void BeginFormWithActionControllerRouteDictionary()
  172. {
  173. BeginFormHelper(
  174. htmlHelper => htmlHelper.BeginForm("myAction", "myController", new RouteValueDictionary { { "pageNum", "123" }, { "id", "id" } }),
  175. @"<form action=""" + MvcHelper.AppPathModifier + @"/myController/myAction/id?pageNum=123"" method=""post"">");
  176. }
  177. [Fact]
  178. public void BeginFormWithActionControllerMethod()
  179. {
  180. BeginFormHelper(
  181. htmlHelper => htmlHelper.BeginForm("myAction", "myController", FormMethod.Get),
  182. @"<form action=""" + MvcHelper.AppPathModifier + @"/myController/myAction"" method=""get"">");
  183. }
  184. [Fact]
  185. public void BeginFormWithActionControllerRouteValuesMethod()
  186. {
  187. BeginFormHelper(
  188. htmlHelper => htmlHelper.BeginForm("myAction", "myController", new { id = "id", pageNum = "123" }, FormMethod.Get),
  189. @"<form action=""" + MvcHelper.AppPathModifier + @"/myController/myAction/id?pageNum=123"" method=""get"">");
  190. }
  191. [Fact]
  192. public void BeginFormWithActionControllerRouteDictionaryMethod()
  193. {
  194. BeginFormHelper(
  195. htmlHelper => htmlHelper.BeginForm("myAction", "myController", new RouteValueDictionary { { "pageNum", "123" }, { "id", "id" } }, FormMethod.Get),
  196. @"<form action=""" + MvcHelper.AppPathModifier + @"/myController/myAction/id?pageNum=123"" method=""get"">");
  197. }
  198. [Fact]
  199. public void BeginFormWithNoParams()
  200. {
  201. BeginFormHelper(
  202. htmlHelper => htmlHelper.BeginForm(),
  203. @"<form action=""/some/path"" method=""post"">");
  204. }
  205. [Fact]
  206. public void BeginRouteFormWithRouteNameInvalidFormMethodHtmlValues()
  207. {
  208. BeginFormHelper(
  209. htmlHelper => htmlHelper.BeginRouteForm("namedroute", (FormMethod)2, new RouteValueDictionary(new { baz = "baz" })),
  210. @"<form action=""" + MvcHelper.AppPathModifier + @"/named/home/oldaction"" baz=""baz"" method=""post"">");
  211. }
  212. [Fact]
  213. public void BeginRouteFormWithRouteName()
  214. {
  215. BeginFormHelper(
  216. htmlHelper => htmlHelper.BeginRouteForm("namedroute"),
  217. @"<form action=""" + MvcHelper.AppPathModifier + @"/named/home/oldaction"" method=""post"">");
  218. }
  219. [Fact]
  220. public void BeginRouteFormWithRouteNameFormMethodHtmlDictionary()
  221. {
  222. BeginFormHelper(
  223. htmlHelper => htmlHelper.BeginRouteForm("namedroute", FormMethod.Get, new RouteValueDictionary(new { baz = "baz" })),
  224. @"<form action=""" + MvcHelper.AppPathModifier + @"/named/home/oldaction"" baz=""baz"" method=""get"">");
  225. }
  226. [Fact]
  227. public void BeginRouteFormWithRouteNameFormMethodHtmlValues()
  228. {
  229. BeginFormHelper(
  230. htmlHelper => htmlHelper.BeginRouteForm("namedroute", FormMethod.Get, new { baz = "baz" }),
  231. @"<form action=""" + MvcHelper.AppPathModifier + @"/named/home/oldaction"" baz=""baz"" method=""get"">");
  232. }
  233. [Fact]
  234. public void BeginRouteFormWithRouteNameFormMethodHtmlValuesWithUnderscores()
  235. {
  236. BeginFormHelper(
  237. htmlHelper => htmlHelper.BeginRouteForm("namedroute", FormMethod.Get, new { foo_baz = "baz" }),
  238. @"<form action=""" + MvcHelper.AppPathModifier + @"/named/home/oldaction"" foo-baz=""baz"" method=""get"">");
  239. }
  240. [Fact]
  241. public void BeginRouteFormWithRouteNameRouteDictionaryFormMethodHtmlDictionary()
  242. {
  243. BeginFormHelper(
  244. htmlHelper => htmlHelper.BeginRouteForm("namedroute", new RouteValueDictionary(new { id = "id" }), FormMethod.Get, new RouteValueDictionary(new { baz = "baz" })),
  245. @"<form action=""" + MvcHelper.AppPathModifier + @"/named/home/oldaction/id"" baz=""baz"" method=""get"">");
  246. }
  247. [Fact]
  248. public void BeginRouteFormWithRouteNameRouteValuesFormMethodHtmlValues()
  249. {
  250. BeginFormHelper(
  251. htmlHelper => htmlHelper.BeginRouteForm("namedroute", new { id = "id" }, FormMethod.Get, new { baz = "baz" }),
  252. @"<form action=""" + MvcHelper.AppPathModifier + @"/named/home/oldaction/id"" baz=""baz"" method=""get"">");
  253. }
  254. [Fact]
  255. public void BeginRouteFormWithRouteNameRouteValuesFormMethodHtmlValuesWithUnderscores()
  256. {
  257. BeginFormHelper(
  258. htmlHelper => htmlHelper.BeginRouteForm("namedroute", new { id = "id" }, FormMethod.Get, new { foo_baz = "baz" }),
  259. @"<form action=""" + MvcHelper.AppPathModifier + @"/named/home/oldaction/id"" foo-baz=""baz"" method=""get"">");
  260. }
  261. [Fact]
  262. public void BeginRouteFormWithRouteNameNullRouteValuesFormMethodNullHtmlValues()
  263. {
  264. BeginFormHelper(
  265. htmlHelper => htmlHelper.BeginRouteForm("namedroute", null, FormMethod.Get, null),
  266. @"<form action=""" + MvcHelper.AppPathModifier + @"/named/home/oldaction"" method=""get"">");
  267. }
  268. [Fact]
  269. public void BeginRouteFormWithRouteValues()
  270. {
  271. BeginFormHelper(
  272. htmlHelper => htmlHelper.BeginRouteForm(new { action = "someOtherAction", id = "id" }),
  273. @"<form action=""" + MvcHelper.AppPathModifier + @"/home/someOtherAction/id"" method=""post"">");
  274. }
  275. [Fact]
  276. public void BeginRouteFormWithRouteDictionary()
  277. {
  278. BeginFormHelper(
  279. htmlHelper => htmlHelper.BeginRouteForm(new RouteValueDictionary { { "action", "someOtherAction" }, { "id", "id" } }),
  280. @"<form action=""" + MvcHelper.AppPathModifier + @"/home/someOtherAction/id"" method=""post"">");
  281. }
  282. [Fact]
  283. public void BeginRouteFormWithRouteNameRouteValues()
  284. {
  285. BeginFormHelper(
  286. htmlHelper => htmlHelper.BeginRouteForm("namedroute", new { id = "id", pageNum = "123" }),
  287. @"<form action=""" + MvcHelper.AppPathModifier + @"/named/home/oldaction/id?pageNum=123"" method=""post"">");
  288. }
  289. [Fact]
  290. public void BeginRouteFormWithActionControllerRouteDictionary()
  291. {
  292. BeginFormHelper(
  293. htmlHelper => htmlHelper.BeginRouteForm("namedroute", new RouteValueDictionary { { "pageNum", "123" }, { "id", "id" } }),
  294. @"<form action=""" + MvcHelper.AppPathModifier + @"/named/home/oldaction/id?pageNum=123"" method=""post"">");
  295. }
  296. [Fact]
  297. public void BeginRouteFormCanUseNamedRouteWithoutSpecifyingDefaults()
  298. {
  299. // DevDiv 217072: Non-mvc specific helpers should not give default values for controller and action
  300. BeginFormHelper(
  301. htmlHelper =>
  302. {
  303. htmlHelper.RouteCollection.MapRoute("MyRouteName", "any/url", new { controller = "Charlie" });
  304. return htmlHelper.BeginRouteForm("MyRouteName");
  305. }, @"<form action=""" + MvcHelper.AppPathModifier + @"/any/url"" method=""post"">");
  306. }
  307. [Fact]
  308. public void BeginRouteFormWithActionControllerMethod()
  309. {
  310. BeginFormHelper(
  311. htmlHelper => htmlHelper.BeginRouteForm("namedroute", FormMethod.Get),
  312. @"<form action=""" + MvcHelper.AppPathModifier + @"/named/home/oldaction"" method=""get"">");
  313. }
  314. [Fact]
  315. public void BeginRouteFormWithActionControllerRouteValuesMethod()
  316. {
  317. BeginFormHelper(
  318. htmlHelper => htmlHelper.BeginRouteForm("namedroute", new { id = "id", pageNum = "123" }, FormMethod.Get),
  319. @"<form action=""" + MvcHelper.AppPathModifier + @"/named/home/oldaction/id?pageNum=123"" method=""get"">");
  320. }
  321. [Fact]
  322. public void BeginRouteFormWithActionControllerRouteDictionaryMethod()
  323. {
  324. BeginFormHelper(
  325. htmlHelper => htmlHelper.BeginRouteForm("namedroute", new RouteValueDictionary { { "pageNum", "123" }, { "id", "id" } }, FormMethod.Get),
  326. @"<form action=""" + MvcHelper.AppPathModifier + @"/named/home/oldaction/id?pageNum=123"" method=""get"">");
  327. }
  328. [Fact]
  329. public void EndFormWritesCloseTag()
  330. {
  331. // Arrange
  332. StringWriter writer;
  333. HtmlHelper htmlHelper = GetFormHelper(out writer);
  334. // Act
  335. htmlHelper.EndForm();
  336. // Assert
  337. Assert.Equal("</form>", writer.ToString());
  338. }
  339. private static HtmlHelper GetFormHelper(out StringWriter writer)
  340. {
  341. Mock<ViewContext> mockViewContext = new Mock<ViewContext>() { CallBase = true };
  342. mockViewContext.Setup(c => c.HttpContext.Request.Url).Returns(new Uri("http://www.contoso.com/some/path"));
  343. mockViewContext.Setup(c => c.HttpContext.Request.RawUrl).Returns("/some/path");
  344. mockViewContext.Setup(c => c.HttpContext.Request.ApplicationPath).Returns("/");
  345. mockViewContext.Setup(c => c.HttpContext.Request.Path).Returns("/");
  346. mockViewContext.Setup(c => c.HttpContext.Request.ServerVariables).Returns((NameValueCollection)null);
  347. mockViewContext.Setup(c => c.HttpContext.Response.Write(It.IsAny<string>())).Throws(new Exception("Should not be called"));
  348. mockViewContext.Setup(c => c.HttpContext.Items).Returns(new Hashtable());
  349. writer = new StringWriter();
  350. mockViewContext.Setup(c => c.Writer).Returns(writer);
  351. mockViewContext.Setup(c => c.HttpContext.Response.ApplyAppPathModifier(It.IsAny<string>())).Returns<string>(r => MvcHelper.AppPathModifier + r);
  352. RouteCollection rt = new RouteCollection();
  353. rt.Add(new Route("{controller}/{action}/{id}", null) { Defaults = new RouteValueDictionary(new { id = "defaultid" }) });
  354. rt.Add("namedroute", new Route("named/{controller}/{action}/{id}", null) { Defaults = new RouteValueDictionary(new { id = "defaultid" }) });
  355. RouteData rd = new RouteData();
  356. rd.Values.Add("controller", "home");
  357. rd.Values.Add("action", "oldaction");
  358. mockViewContext.Setup(c => c.RouteData).Returns(rd);
  359. HtmlHelper helper = new HtmlHelper(mockViewContext.Object, new Mock<IViewDataContainer>().Object, rt);
  360. helper.ViewContext.FormIdGenerator = () => "form_id";
  361. return helper;
  362. }
  363. }
  364. }