PageRenderTime 91ms CodeModel.GetById 80ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 25
 26using System;
 27using NUnit.Framework;
 28
 29using Manos.Routing;
 30
 31using Manos.Http;
 32using Manos.Testing;
 33using Manos.Http.Testing;
 34using Manos.Routing.Testing;
 35
 36
 37using Manos.ShouldExt;
 38
 39namespace Manos.Routing.Tests
 40{
 41
 42
 43	[TestFixture()]
 44	public class RouteHandlerTest
 45	{
 46		
 47		public class TestApp : ManosApp{
 48			public TestApp()
 49			{
 50				this.Route("/TESTING",new TestModule());	
 51			}
 52			
 53			public class TestModule : ManosModule{
 54				
 55				public void Route1(IManosContext ctx)
 56				{
 57					ctx.Response.Write("Route1");
 58					ctx.Response.End();
 59				}
 60				
 61				[Get("/Route2a/{age}/{name}")]
 62				public void Route2a(TestApp app, IManosContext ctx, String name, int age)
 63				{
 64					ctx.Response.Write("(R2a) Hello '{0}', you are '{1}'",name, age);
 65					ctx.Response.End();
 66				}
 67				
 68				[Get("/Route2b/{name}/{age}")]
 69				public void Route2b(TestApp app, IManosContext ctx, String name, int age)
 70				{
 71					ctx.Response.Write("(R2b) Hello '{0}', you are '{1}'",name, age);
 72					ctx.Response.End();
 73				}
 74				
 75				[Get("/Route3/(?<name>.+?)/(?<age>.+?)")]
 76				public void Route3(TestApp app, IManosContext ctx, String name, int age)
 77				{
 78					ctx.Response.Write("'{0}', you are '{1}'",name, age);
 79					ctx.Response.End();
 80				}
 81			}
 82			
 83		}
 84		
 85		private static void FakeAction (IManosContext ctx)
 86		{
 87		}
 88		
 89		private static void FakeAction2 (IManosContext ctx)
 90		{
 91		}
 92		
 93		[Test]
 94		public void ImplicitRouteWorksWithModuleOnCustomApp()
 95		{
 96			var t = new TestApp();
 97			var req = new MockHttpRequest(HttpMethod.HTTP_GET,"/TESTING/Route1");
 98			var txn = new MockHttpTransaction(req);
 99			t.HandleTransaction(t,txn);
100			
101			Assert.AreEqual("Route1",txn.ResponseString);
102			
103			req = new MockHttpRequest(HttpMethod.HTTP_GET, "/TESTING/Route1/");
104			txn = new MockHttpTransaction(req);
105			t.HandleTransaction(t,txn);
106			Assert.AreEqual("Route1",txn.ResponseString);
107		}
108		
109		[Test]
110		public void RouteWorksWithNamedParametersInModuleOnCustomApp()
111		{
112			var t = new TestApp();
113			var req = new MockHttpRequest(HttpMethod.HTTP_GET,"/TESTING/Route2a/29/Andrew");
114			var txn = new MockHttpTransaction(req);
115			t.HandleTransaction(t,txn);
116			
117			Assert.AreEqual("(R2a) Hello 'Andrew', you are '29'",txn.ResponseString);
118			
119			req = new MockHttpRequest(HttpMethod.HTTP_GET,"/TESTING/Route2b/Andrew/29");
120		    txn = new MockHttpTransaction(req);
121			t.HandleTransaction(t,txn);
122			
123			Assert.AreEqual("(R2b) Hello 'Andrew', you are '29'",txn.ResponseString);
124			
125		}
126		
127		[Test]
128		public void RouteWorksWithRegexParamsInModuleOnCustomApp()
129		{
130			var t = new TestApp();
131			var req = new MockHttpRequest(HttpMethod.HTTP_GET,"/TESTING/Route3/Andrew/29");
132			var txn = new MockHttpTransaction(req);
133			t.HandleTransaction(t,txn);
134			
135			Assert.AreEqual("'Andrew', you are '29'",txn.ResponseString);
136			
137			req = new MockHttpRequest(HttpMethod.HTTP_GET,"/TESTING/Route3/Andrew/29/");
138		    txn = new MockHttpTransaction(req);
139			t.HandleTransaction(t,txn);
140			
141			Assert.AreEqual("'Andrew', you are '29'",txn.ResponseString);
142			
143		}
144		
145		
146		[Test()]
147		public void TestStrMatch ()
148		{
149			var target = new MockManosTarget ();
150			var rh = new RouteHandler ("^foo", HttpMethod.HTTP_GET, target);
151			var request = new MockHttpRequest (HttpMethod.HTTP_GET, "foo");
152			
153			Assert.AreEqual (target, rh.Find (request), "should-match");
154			
155			request = new MockHttpRequest (HttpMethod.HTTP_GET, "garbage-foo");
156			Assert.IsNull (rh.Find (request), "garbage-input");
157		}
158		
159		[Test()]
160		public void TestStrMatchDeep ()
161		{
162			var target = new MockManosTarget ();
163			var rh = new RouteHandler ("foo/", HttpMethod.HTTP_GET) {
164				new RouteHandler ("bar", HttpMethod.HTTP_GET, target),
165			};
166
167			var request = new MockHttpRequest (HttpMethod.HTTP_GET, "foo/bar");
168			Assert.AreEqual (target, rh.Find (request));
169			
170			request = new MockHttpRequest (HttpMethod.HTTP_GET, "foo/foo");
171			Assert.IsNull (rh.Find (request), "repeate-input");
172			
173			request = new MockHttpRequest (HttpMethod.HTTP_GET, "foo/badbar");
174			Assert.IsNull (rh.Find (request), "matched-input");
175		}
176		
177		[Test()]
178		public void TestChangePatterns ()
179		{
180			//
181			// Ensure that changing the patterns property works.
182			// This is a bit of an edge case because internally
183			// the patterns strings are cached as an array of 
184			// regexes.
185			//
186			
187			var target = new MockManosTarget ();
188			var rh = new RouteHandler ("^foo", HttpMethod.HTTP_GET, target);
189			var request = new MockHttpRequest (HttpMethod.HTTP_GET, "foo");
190
191			Assert.AreEqual (target, rh.Find (request), "sanity-1");
192			
193			rh.Patterns [0] = "baz";
194			Assert.IsNull (rh.Find (request), "sanity-2");
195			
196			request = new MockHttpRequest (HttpMethod.HTTP_GET, "baz");
197			Assert.AreEqual (target, rh.Find (request), "changed");
198		}
199		
200		[Test]
201		public void TestSetPatternsNull ()
202		{
203			var target = new MockManosTarget ();
204			var rh = new RouteHandler ("^foo", HttpMethod.HTTP_GET, target);
205			var request = new MockHttpRequest (HttpMethod.HTTP_GET, "foo");
206
207			Assert.AreEqual (target, rh.Find (request), "sanity-1");
208			
209			rh.Patterns = null;
210			
211			Assert.IsNull (rh.Find (request), "is null");
212		}
213		
214		[Test]
215		public void HasPatternsTest ()
216		{
217			var rh = new RouteHandler ("foo", HttpMethod.HTTP_GET);
218			
219			Assert.IsTrue (rh.HasPatterns, "a1");
220			
221			rh.Patterns.Clear ();
222			Assert.IsFalse (rh.HasPatterns, "a2");
223			
224			rh.Patterns.Add ("foobar");
225			Assert.IsTrue (rh.HasPatterns, "a3");
226			
227			rh.Patterns = null;
228			Assert.IsFalse (rh.HasPatterns, "a4");
229		}
230		
231		[Test]
232		public void UriParamsTest ()
233		{
234			var rh = new RouteHandler ("(?<name>.+)", HttpMethod.HTTP_GET, new ActionTarget (FakeAction));
235			var request = new MockHttpRequest (HttpMethod.HTTP_GET, "hello");
236			
237			Should.NotBeNull (rh.Find (request), "target");
238			
239			Should.NotBeNull (request.UriData, "uri-data");
240			
241			Assert.AreEqual ("hello", request.UriData ["name"]);	
242		}
243		
244		[Test]
245		public void UriParamsTestDeep ()
246		{
247			var rh = new RouteHandler ("(?<animal>.+)/", HttpMethod.HTTP_GET) {
248				new RouteHandler ("(?<name>.+)", HttpMethod.HTTP_GET, new ActionTarget (FakeAction)),	                                                         
249			};
250			var request = new MockHttpRequest (HttpMethod.HTTP_GET, "dog/roxy");
251			
252			Should.NotBeNull (rh.Find (request), "target");
253			
254			Should.NotBeNull (request.UriData, "uri-data");
255			
256			Assert.AreEqual ("dog", request.UriData ["animal"]);
257			Assert.AreEqual ("roxy", request.UriData ["name"]);
258		}
259		
260		[Test]
261		public void TestNoChildrenOfTarget ()
262		{
263			var rh = new RouteHandler ("foo", HttpMethod.HTTP_GET, new ActionTarget (FakeAction));
264			
265			Should.Throw<InvalidOperationException> (() => rh.Children.Add (new RouteHandler ("foo", HttpMethod.HTTP_POST)));
266		}
267
268		[Test]
269		public void Find_PartialMatchAtBeginningOfChildlessHandler_ReturnsProperRoute ()
270		{
271			var rh_bad = new RouteHandler ("foo", HttpMethod.HTTP_GET, new ActionTarget (FakeAction));
272			var rh_good = new RouteHandler ("foobar", HttpMethod.HTTP_GET, new ActionTarget (FakeAction2));
273			var rh = new RouteHandler ();
274			
275			rh.Children.Add (rh_bad);
276			rh.Children.Add (rh_good);
277
278			
279			var request = new MockHttpRequest (HttpMethod.HTTP_GET, "foobar");
280			var res = rh.Find (request);
281			
282			Assert.AreEqual (rh_good.Target, res);
283		}
284		
285		[Test]
286		public void Find_PartialMatchAtBeginningOfHandlerWithChildren_ReturnsProperRoute ()
287		{
288			var rh_bad = new RouteHandler ("foo", HttpMethod.HTTP_GET);
289			var rh_good = new RouteHandler ("foobar", HttpMethod.HTTP_GET, new ActionTarget (FakeAction2));
290			var rh = new RouteHandler ();
291			
292			rh_bad.Children.Add (new RouteHandler ("blah", HttpMethod.HTTP_GET, new ActionTarget (FakeAction)));
293			
294			rh.Children.Add (rh_bad);
295			rh.Children.Add (rh_good);
296
297			var request = new MockHttpRequest (HttpMethod.HTTP_GET, "foobar");
298			var res = rh.Find (request);
299			
300			Assert.AreEqual (rh_good.Target, res);
301		}
302	}
303}