/src/Manos.Tests/Manos.Routing/RouteHandlerTest.cs

http://github.com/jacksonh/manos · C# · 303 lines · 201 code · 73 blank · 29 comment · 0 complexity · 10b75d4454f903a237e1c408ae168ba2 MD5 · raw file

  1. //
  2. // Copyright (C) 2010 Jackson Harper (jackson@manosdemono.com)
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. //
  24. using System;
  25. using NUnit.Framework;
  26. using Manos.Routing;
  27. using Manos.Http;
  28. using Manos.Testing;
  29. using Manos.Http.Testing;
  30. using Manos.Routing.Testing;
  31. using Manos.ShouldExt;
  32. namespace Manos.Routing.Tests
  33. {
  34. [TestFixture()]
  35. public class RouteHandlerTest
  36. {
  37. public class TestApp : ManosApp{
  38. public TestApp()
  39. {
  40. this.Route("/TESTING",new TestModule());
  41. }
  42. public class TestModule : ManosModule{
  43. public void Route1(IManosContext ctx)
  44. {
  45. ctx.Response.Write("Route1");
  46. ctx.Response.End();
  47. }
  48. [Get("/Route2a/{age}/{name}")]
  49. public void Route2a(TestApp app, IManosContext ctx, String name, int age)
  50. {
  51. ctx.Response.Write("(R2a) Hello '{0}', you are '{1}'",name, age);
  52. ctx.Response.End();
  53. }
  54. [Get("/Route2b/{name}/{age}")]
  55. public void Route2b(TestApp app, IManosContext ctx, String name, int age)
  56. {
  57. ctx.Response.Write("(R2b) Hello '{0}', you are '{1}'",name, age);
  58. ctx.Response.End();
  59. }
  60. [Get("/Route3/(?<name>.+?)/(?<age>.+?)")]
  61. public void Route3(TestApp app, IManosContext ctx, String name, int age)
  62. {
  63. ctx.Response.Write("'{0}', you are '{1}'",name, age);
  64. ctx.Response.End();
  65. }
  66. }
  67. }
  68. private static void FakeAction (IManosContext ctx)
  69. {
  70. }
  71. private static void FakeAction2 (IManosContext ctx)
  72. {
  73. }
  74. [Test]
  75. public void ImplicitRouteWorksWithModuleOnCustomApp()
  76. {
  77. var t = new TestApp();
  78. var req = new MockHttpRequest(HttpMethod.HTTP_GET,"/TESTING/Route1");
  79. var txn = new MockHttpTransaction(req);
  80. t.HandleTransaction(t,txn);
  81. Assert.AreEqual("Route1",txn.ResponseString);
  82. req = new MockHttpRequest(HttpMethod.HTTP_GET, "/TESTING/Route1/");
  83. txn = new MockHttpTransaction(req);
  84. t.HandleTransaction(t,txn);
  85. Assert.AreEqual("Route1",txn.ResponseString);
  86. }
  87. [Test]
  88. public void RouteWorksWithNamedParametersInModuleOnCustomApp()
  89. {
  90. var t = new TestApp();
  91. var req = new MockHttpRequest(HttpMethod.HTTP_GET,"/TESTING/Route2a/29/Andrew");
  92. var txn = new MockHttpTransaction(req);
  93. t.HandleTransaction(t,txn);
  94. Assert.AreEqual("(R2a) Hello 'Andrew', you are '29'",txn.ResponseString);
  95. req = new MockHttpRequest(HttpMethod.HTTP_GET,"/TESTING/Route2b/Andrew/29");
  96. txn = new MockHttpTransaction(req);
  97. t.HandleTransaction(t,txn);
  98. Assert.AreEqual("(R2b) Hello 'Andrew', you are '29'",txn.ResponseString);
  99. }
  100. [Test]
  101. public void RouteWorksWithRegexParamsInModuleOnCustomApp()
  102. {
  103. var t = new TestApp();
  104. var req = new MockHttpRequest(HttpMethod.HTTP_GET,"/TESTING/Route3/Andrew/29");
  105. var txn = new MockHttpTransaction(req);
  106. t.HandleTransaction(t,txn);
  107. Assert.AreEqual("'Andrew', you are '29'",txn.ResponseString);
  108. req = new MockHttpRequest(HttpMethod.HTTP_GET,"/TESTING/Route3/Andrew/29/");
  109. txn = new MockHttpTransaction(req);
  110. t.HandleTransaction(t,txn);
  111. Assert.AreEqual("'Andrew', you are '29'",txn.ResponseString);
  112. }
  113. [Test()]
  114. public void TestStrMatch ()
  115. {
  116. var target = new MockManosTarget ();
  117. var rh = new RouteHandler ("^foo", HttpMethod.HTTP_GET, target);
  118. var request = new MockHttpRequest (HttpMethod.HTTP_GET, "foo");
  119. Assert.AreEqual (target, rh.Find (request), "should-match");
  120. request = new MockHttpRequest (HttpMethod.HTTP_GET, "garbage-foo");
  121. Assert.IsNull (rh.Find (request), "garbage-input");
  122. }
  123. [Test()]
  124. public void TestStrMatchDeep ()
  125. {
  126. var target = new MockManosTarget ();
  127. var rh = new RouteHandler ("foo/", HttpMethod.HTTP_GET) {
  128. new RouteHandler ("bar", HttpMethod.HTTP_GET, target),
  129. };
  130. var request = new MockHttpRequest (HttpMethod.HTTP_GET, "foo/bar");
  131. Assert.AreEqual (target, rh.Find (request));
  132. request = new MockHttpRequest (HttpMethod.HTTP_GET, "foo/foo");
  133. Assert.IsNull (rh.Find (request), "repeate-input");
  134. request = new MockHttpRequest (HttpMethod.HTTP_GET, "foo/badbar");
  135. Assert.IsNull (rh.Find (request), "matched-input");
  136. }
  137. [Test()]
  138. public void TestChangePatterns ()
  139. {
  140. //
  141. // Ensure that changing the patterns property works.
  142. // This is a bit of an edge case because internally
  143. // the patterns strings are cached as an array of
  144. // regexes.
  145. //
  146. var target = new MockManosTarget ();
  147. var rh = new RouteHandler ("^foo", HttpMethod.HTTP_GET, target);
  148. var request = new MockHttpRequest (HttpMethod.HTTP_GET, "foo");
  149. Assert.AreEqual (target, rh.Find (request), "sanity-1");
  150. rh.Patterns [0] = "baz";
  151. Assert.IsNull (rh.Find (request), "sanity-2");
  152. request = new MockHttpRequest (HttpMethod.HTTP_GET, "baz");
  153. Assert.AreEqual (target, rh.Find (request), "changed");
  154. }
  155. [Test]
  156. public void TestSetPatternsNull ()
  157. {
  158. var target = new MockManosTarget ();
  159. var rh = new RouteHandler ("^foo", HttpMethod.HTTP_GET, target);
  160. var request = new MockHttpRequest (HttpMethod.HTTP_GET, "foo");
  161. Assert.AreEqual (target, rh.Find (request), "sanity-1");
  162. rh.Patterns = null;
  163. Assert.IsNull (rh.Find (request), "is null");
  164. }
  165. [Test]
  166. public void HasPatternsTest ()
  167. {
  168. var rh = new RouteHandler ("foo", HttpMethod.HTTP_GET);
  169. Assert.IsTrue (rh.HasPatterns, "a1");
  170. rh.Patterns.Clear ();
  171. Assert.IsFalse (rh.HasPatterns, "a2");
  172. rh.Patterns.Add ("foobar");
  173. Assert.IsTrue (rh.HasPatterns, "a3");
  174. rh.Patterns = null;
  175. Assert.IsFalse (rh.HasPatterns, "a4");
  176. }
  177. [Test]
  178. public void UriParamsTest ()
  179. {
  180. var rh = new RouteHandler ("(?<name>.+)", HttpMethod.HTTP_GET, new ActionTarget (FakeAction));
  181. var request = new MockHttpRequest (HttpMethod.HTTP_GET, "hello");
  182. Should.NotBeNull (rh.Find (request), "target");
  183. Should.NotBeNull (request.UriData, "uri-data");
  184. Assert.AreEqual ("hello", request.UriData ["name"]);
  185. }
  186. [Test]
  187. public void UriParamsTestDeep ()
  188. {
  189. var rh = new RouteHandler ("(?<animal>.+)/", HttpMethod.HTTP_GET) {
  190. new RouteHandler ("(?<name>.+)", HttpMethod.HTTP_GET, new ActionTarget (FakeAction)),
  191. };
  192. var request = new MockHttpRequest (HttpMethod.HTTP_GET, "dog/roxy");
  193. Should.NotBeNull (rh.Find (request), "target");
  194. Should.NotBeNull (request.UriData, "uri-data");
  195. Assert.AreEqual ("dog", request.UriData ["animal"]);
  196. Assert.AreEqual ("roxy", request.UriData ["name"]);
  197. }
  198. [Test]
  199. public void TestNoChildrenOfTarget ()
  200. {
  201. var rh = new RouteHandler ("foo", HttpMethod.HTTP_GET, new ActionTarget (FakeAction));
  202. Should.Throw<InvalidOperationException> (() => rh.Children.Add (new RouteHandler ("foo", HttpMethod.HTTP_POST)));
  203. }
  204. [Test]
  205. public void Find_PartialMatchAtBeginningOfChildlessHandler_ReturnsProperRoute ()
  206. {
  207. var rh_bad = new RouteHandler ("foo", HttpMethod.HTTP_GET, new ActionTarget (FakeAction));
  208. var rh_good = new RouteHandler ("foobar", HttpMethod.HTTP_GET, new ActionTarget (FakeAction2));
  209. var rh = new RouteHandler ();
  210. rh.Children.Add (rh_bad);
  211. rh.Children.Add (rh_good);
  212. var request = new MockHttpRequest (HttpMethod.HTTP_GET, "foobar");
  213. var res = rh.Find (request);
  214. Assert.AreEqual (rh_good.Target, res);
  215. }
  216. [Test]
  217. public void Find_PartialMatchAtBeginningOfHandlerWithChildren_ReturnsProperRoute ()
  218. {
  219. var rh_bad = new RouteHandler ("foo", HttpMethod.HTTP_GET);
  220. var rh_good = new RouteHandler ("foobar", HttpMethod.HTTP_GET, new ActionTarget (FakeAction2));
  221. var rh = new RouteHandler ();
  222. rh_bad.Children.Add (new RouteHandler ("blah", HttpMethod.HTTP_GET, new ActionTarget (FakeAction)));
  223. rh.Children.Add (rh_bad);
  224. rh.Children.Add (rh_good);
  225. var request = new MockHttpRequest (HttpMethod.HTTP_GET, "foobar");
  226. var res = rh.Find (request);
  227. Assert.AreEqual (rh_good.Target, res);
  228. }
  229. }
  230. }