Пример #1
0
        public void GetVirtualPath6()
        {
            var r = new MyRoute("summary/{action}-{type}/{page}", new MyRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { action = "Index", page = 1 })
            };
            var hc     = new HttpContextStub2("~/summary/Index-test/1", String.Empty);
            var rd     = r.GetRouteData(hc);
            var values = new RouteValueDictionary(new { page = 2 });

            Assert.IsNotNull(rd, "#1");
            var vp = r.GetVirtualPath(new RequestContext(hc, rd), values);

            Assert.IsNotNull(vp, "#2");
            Assert.AreEqual("summary/Index-test/2", vp.VirtualPath, "#2-1");
            Assert.AreEqual(r, vp.Route, "#2-2");
            Assert.AreEqual(0, vp.DataTokens.Count, "#2-3");

            values = new RouteValueDictionary(new { page = 2, extra = "stuff" });
            vp     = r.GetVirtualPath(new RequestContext(hc, rd), values);

            Assert.IsNotNull(vp, "#3");
            Assert.AreEqual("summary/Index-test/2?extra=stuff", vp.VirtualPath, "#3-2");
            Assert.AreEqual(0, vp.DataTokens.Count, "#3-3");
        }
Пример #2
0
        public void ProcessConstraint()
        {
            var route = new MyRoute("hello/{name}", new MyRouteHandler());

            Assert.IsFalse(route.DoProcessConstraint(null, "regex", "parameter", new RouteValueDictionary(), RouteDirection.IncomingRequest), "#1");

            // constraint is null
            AssertExtensions.Throws <InvalidOperationException> (
                () => route.DoProcessConstraint(null, null, "parameter", new RouteValueDictionary(), RouteDirection.IncomingRequest),
                "#2"
                );

            // constraint is neither a string or an IRouteConstraint instance
            AssertExtensions.Throws <InvalidOperationException> (
                () => route.DoProcessConstraint(null, 1, "parameter", new RouteValueDictionary(), RouteDirection.IncomingRequest),
                "#3"
                );

            AssertExtensions.Throws <ArgumentNullException> (
                () => route.DoProcessConstraint(null, "regex", null, new RouteValueDictionary(), RouteDirection.IncomingRequest),
                "#4"
                );

            Assert.IsFalse(route.DoProcessConstraint(null, "regex", String.Empty, new RouteValueDictionary(), RouteDirection.IncomingRequest), "#5");

            // This is a .NET programming error, so not sure if we should test for this...
            AssertExtensions.Throws <NullReferenceException> (
                () => route.DoProcessConstraint(null, "regex", "parameter", null, RouteDirection.IncomingRequest),
                "#6"
                );
        }
Пример #3
0
        public void GetVirtualPath3()
        {
            var r  = new MyRoute("{foo}/{bar}", new MyRouteHandler());
            var hc = new HttpContextStub2("~/x/y", String.Empty);
            var rd = r.GetRouteData(hc);
            var vp = r.GetVirtualPath(new RequestContext(hc, rd), rd.Values);

            Assert.IsNotNull(vp, "#1");
            Assert.AreEqual("x/y", vp.VirtualPath, "#2");
            Assert.AreEqual(r, vp.Route, "#3");
            Assert.AreEqual(0, vp.DataTokens.Count, "#4");
        }
Пример #4
0
        public void GetVirtualPath4()
        {
            var r  = new MyRoute("{foo}/{bar}", new MyRouteHandler());
            var hc = new HttpContextStub2("~/x/y", String.Empty);
            var rd = r.GetRouteData(hc);

            // override a value incompletely
            var values = new RouteValueDictionary();

            values ["foo"] = "A";

            var vp = r.GetVirtualPath(new RequestContext(hc, rd), values);

            Assert.IsNull(vp);
        }
Пример #5
0
        public void GetVirtualPath8()
        {
            var r = new MyRoute("todo/{action}/{page}", new MyRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { controller = "todo", action = "list", page = 0 })
            };
            var hc = new HttpContextStub2("~/todo/list/2", String.Empty);
            var rd = r.GetRouteData(hc);

            Assert.IsNotNull(rd, "#1");

            var vp = r.GetVirtualPath(new RequestContext(hc, rd), new RouteValueDictionary(new { page = 3 }));

            Assert.IsNotNull(vp, "#2");
            Assert.AreEqual("todo/list/3", vp.VirtualPath, "#2-1");
        }
Пример #6
0
        public void GetVirtualPath5()
        {
            var r  = new MyRoute("{foo}/{bar}", new MyRouteHandler());
            var hc = new HttpContextStub2("~/x/y", String.Empty);
            var rd = r.GetRouteData(hc);

            // override values completely.
            var values = new RouteValueDictionary();

            values ["foo"] = "A";
            values ["bar"] = "B";

            var vp = r.GetVirtualPath(new RequestContext(hc, rd), values);

            Assert.IsNotNull(vp, "#1");
            Assert.AreEqual("A/B", vp.VirtualPath, "#2");
            Assert.AreEqual(r, vp.Route, "#3");
            Assert.AreEqual(0, vp.DataTokens.Count, "#4");
        }
Пример #7
0
        public void GetVirtualPath12()
        {
            var r = new MyRoute("{foo}/{bar}", new MyRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { bar = "baz" })
            };

            var hc     = new HttpContextStub2("~/x/y", String.Empty);
            var rd     = r.GetRouteData(hc);
            var values = new RouteValueDictionary();

            // Partial override is possible if defaults are specified
            values ["foo"] = "A";
            values ["baz"] = "B";

            var vp = r.GetVirtualPath(new RequestContext(hc, rd), values);

            Assert.IsNotNull(vp, "#1");
            Assert.AreEqual("A?baz=B", vp.VirtualPath, "#1-1");
        }
Пример #8
0
        public void GetVirtualPath10()
        {
            var r  = new MyRoute("{foo}/{bar}", new MyRouteHandler());
            var hc = new HttpContextStub("~/foo/bar", String.Empty);
            var rd = r.GetRouteData(hc);

            Assert.IsNotNull(rd, "#1");

            var vp = r.GetVirtualPath(new RequestContext(hc, rd), new RouteValueDictionary(new { page = 3 }));

            Assert.IsNotNull(vp, "#2");
            Assert.AreEqual("foo/bar?page=3", vp.VirtualPath, "#2-1");

            vp = r.GetVirtualPath(new RequestContext(hc, rd), new RouteValueDictionary(new { page = 3, another = "stuff" }));
            Assert.IsNotNull(vp, "#3");
            Assert.AreEqual("foo/bar?page=3&another=stuff", vp.VirtualPath, "#3-1");

            vp = r.GetVirtualPath(new RequestContext(hc, rd), new RouteValueDictionary(new { page = 3, another = "stuff", value = "with ; spaces & other chars" }));
            Assert.IsNotNull(vp, "#4");
            Assert.AreEqual("foo/bar?page=3&another=stuff&value=with%20%3B%20spaces%20%26%20other%20chars", vp.VirtualPath, "#4-1");
        }
Пример #9
0
        public void GetVirtualPath14()
        {
            var r  = new MyRoute("{table}/{action}.aspx", new MyRouteHandler());
            var hc = new HttpContextStub2("~/x/y.aspx", String.Empty);
            var rd = r.GetRouteData(hc);

            // override a value incompletely
            var values = new RouteValueDictionary(new {
                emptyValue    = String.Empty,
                nullValue     = (string)null,
                nonEmptyValue = "SomeValue"
            });

            var vp = r.GetVirtualPath(new RequestContext(hc, rd), values);

            Assert.IsNotNull(vp, "#A1");
            Assert.AreEqual("x/y.aspx?nonEmptyValue=SomeValue", vp.VirtualPath, "#A1-1");

            values["nonEmptyValue"] = "Some Value + encoding &";
            vp = r.GetVirtualPath(new RequestContext(hc, rd), values);
            Assert.IsNotNull(vp, "#B1");
            Assert.AreEqual("x/y.aspx?nonEmptyValue=Some%20Value%20%2B%20encoding%20%26", vp.VirtualPath, "#B1-1");
        }
Пример #10
0
		public void GetVirtualPath3 ()
		{
			var r = new MyRoute ("{foo}/{bar}", new MyRouteHandler ());
			var hc = new HttpContextStub2 ("~/x/y", String.Empty);
			var rd = r.GetRouteData (hc);
			var vp = r.GetVirtualPath (new RequestContext (hc, rd), rd.Values);

			Assert.IsNotNull (vp, "#1");
			Assert.AreEqual ("x/y", vp.VirtualPath, "#2");
			Assert.AreEqual (r, vp.Route, "#3");
			Assert.AreEqual (0, vp.DataTokens.Count, "#4");
		}
Пример #11
0
		public void ProcessConstraint ()
		{
			var route = new MyRoute ("hello/{name}", new MyRouteHandler ());

			Assert.IsFalse (route.DoProcessConstraint (null, "regex", "parameter", new RouteValueDictionary (), RouteDirection.IncomingRequest), "#1");

			// constraint is null
			AssertExtensions.Throws <InvalidOperationException> (
				() => route.DoProcessConstraint (null, null, "parameter", new RouteValueDictionary (), RouteDirection.IncomingRequest),
				"#2"
			);

			// constraint is neither a string or an IRouteConstraint instance
			AssertExtensions.Throws <InvalidOperationException> (
				() => route.DoProcessConstraint (null, 1, "parameter", new RouteValueDictionary (), RouteDirection.IncomingRequest),
				"#3"
			);

			AssertExtensions.Throws <ArgumentNullException> (
				() => route.DoProcessConstraint (null, "regex", null, new RouteValueDictionary (), RouteDirection.IncomingRequest),
				"#4"
			);

			Assert.IsFalse (route.DoProcessConstraint (null, "regex", String.Empty, new RouteValueDictionary (), RouteDirection.IncomingRequest), "#5");
			
			// This is a .NET programming error, so not sure if we should test for this...
			AssertExtensions.Throws <NullReferenceException> (
				() => route.DoProcessConstraint (null, "regex", "parameter", null, RouteDirection.IncomingRequest),
				"#6"
			);
		}
Пример #12
0
		public void GetVirtualPath14 ()
		{
			var r = new MyRoute ("{table}/{action}.aspx", new MyRouteHandler ());
			var hc = new HttpContextStub2 ("~/x/y.aspx", String.Empty);
			var rd = r.GetRouteData (hc);

			// override a value incompletely
			var values = new RouteValueDictionary (new {
				emptyValue = String.Empty,
				nullValue = (string)null,
				nonEmptyValue = "SomeValue"
			});

			var vp = r.GetVirtualPath (new RequestContext (hc, rd), values);
			Assert.IsNotNull (vp, "#A1");
			Assert.AreEqual ("x/y.aspx?nonEmptyValue=SomeValue", vp.VirtualPath, "#A1-1");

			values["nonEmptyValue"] = "Some Value + encoding &";
			vp = r.GetVirtualPath (new RequestContext (hc, rd), values);
			Assert.IsNotNull (vp, "#B1");
			Assert.AreEqual ("x/y.aspx?nonEmptyValue=Some%20Value%20%2B%20encoding%20%26", vp.VirtualPath, "#B1-1");

		}
Пример #13
0
		public void GetVirtualPath12 ()
		{
			var r = new MyRoute ("{foo}/{bar}", new MyRouteHandler ()) {
					Defaults = new RouteValueDictionary (new { bar = "baz" })
						};
						
			var hc = new HttpContextStub2 ("~/x/y", String.Empty);
			var rd = r.GetRouteData (hc);
			var values = new RouteValueDictionary ();

			// Partial override is possible if defaults are specified
			values ["foo"] = "A";
			values ["baz"] = "B";
			
			var vp = r.GetVirtualPath (new RequestContext (hc, rd), values);
			Assert.IsNotNull (vp, "#1");
			Assert.AreEqual ("A?baz=B", vp.VirtualPath, "#1-1");
		}
Пример #14
0
		public void GetVirtualPath11 ()
		{
			var r = new MyRoute ("summary/{action}/{page}", new MyRouteHandler ()) {
					Defaults = new RouteValueDictionary (new { action = "Index", page = 1 })
						};
			var hc = new HttpContextStub2 ("~/summary/test/1", String.Empty);
			var rd = r.GetRouteData (hc);
			var values = new RouteValueDictionary (new { page = 2 });

			Assert.IsNotNull (rd, "#1");
			var vp = r.GetVirtualPath (new RequestContext (hc, rd), values);

			Assert.IsNotNull (vp, "#2");
			Assert.AreEqual ("summary/test/2", vp.VirtualPath, "#2-1");
			Assert.AreEqual (r, vp.Route, "#2-2");
			Assert.AreEqual (0, vp.DataTokens.Count, "#2-3");

			values = new RouteValueDictionary (new { page = 2, extra = "stuff" });
			vp = r.GetVirtualPath (new RequestContext (hc, rd), values);

			Assert.IsNotNull (vp, "#3");
			Assert.AreEqual ("summary/test/2?extra=stuff", vp.VirtualPath, "#3-2");
			Assert.AreEqual (0, vp.DataTokens.Count, "#3-3");
		}
Пример #15
0
		public void GetVirtualPath10 ()
		{
			var r = new MyRoute ("{foo}/{bar}", new MyRouteHandler ());
			var hc = new HttpContextStub ("~/foo/bar", String.Empty);
			var rd = r.GetRouteData (hc);

			Assert.IsNotNull (rd, "#1");

			var vp = r.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary (new { page = 3 }));
			Assert.IsNotNull (vp, "#2");
			Assert.AreEqual ("foo/bar?page=3", vp.VirtualPath, "#2-1");

			vp = r.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary (new { page = 3, another = "stuff" }));
			Assert.IsNotNull (vp, "#3");
			Assert.AreEqual ("foo/bar?page=3&another=stuff", vp.VirtualPath, "#3-1");

			vp = r.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary (new { page = 3, another = "stuff", value = "with ; spaces & other chars" }));
			Assert.IsNotNull (vp, "#4");
			Assert.AreEqual ("foo/bar?page=3&another=stuff&value=with%20%3B%20spaces%20%26%20other%20chars", vp.VirtualPath, "#4-1");
		}
Пример #16
0
		public void GetVirtualPath9 ()
		{
			var r = new MyRoute ("todo/{action}/{page}", new MyRouteHandler ()) {
					Defaults = new RouteValueDictionary {
							{"controller", "todo"},
							{"action", null},
							{"page", null}
						}
				};
			
			var hc = new HttpContextStub2 ("~/todo/list/2", String.Empty);
			var rd = r.GetRouteData (hc);

			Assert.IsNotNull (rd, "#1");
			
			var vp = r.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary (new { page = 3 }));
			Assert.IsNotNull (vp, "#2");
			Assert.AreEqual ("todo/list/3", vp.VirtualPath, "#2-1");
		}
Пример #17
0
		public void GetVirtualPath23 ()
		{
			var r0 = new MyRoute ("summary/{id}", new MyRouteHandler());
			var r1 = new MyRoute ("summary/{controller}/{id}/{action}", new MyRouteHandler())
			{
				Defaults = new RouteValueDictionary (new { action = "Index" })
			};
			var hc = new HttpContextStub2 ("~/summary/90941a4f-daf3-4c89-a6dc-83e8de4e3db5", String.Empty);
			var rd = r0.GetRouteData (hc);
			Assert.IsNotNull (rd, "#0");
			Assert.AreEqual ("90941a4f-daf3-4c89-a6dc-83e8de4e3db5", rd.Values["id"]);

			var values = new RouteValueDictionary ()
			{
				{ "controller", "SomeThing" },
				{ "action", "Index" }
			};
			var vp = r1.GetVirtualPath (new RequestContext (hc, rd), values);

			Assert.IsNotNull (vp, "#1");
			Assert.AreEqual ("summary/SomeThing/90941a4f-daf3-4c89-a6dc-83e8de4e3db5", vp.VirtualPath, "#1-1");
			Assert.AreEqual (r1, vp.Route, "#1-2");
			Assert.AreEqual (0, vp.DataTokens.Count, "#1-3");
		}
Пример #18
0
		public void GetVirtualPath24 ()
		{
			var r = new MyRoute ("{controller}/{country}-{locale}/{action}", new MyRouteHandler())
			{
				Defaults = new RouteValueDictionary (new { action = "Index", country = "us", locale = "en" })
			};
			var hc = new HttpContextStub2 ("~/login", String.Empty);
			var rd = r.GetRouteData (hc);
			Assert.IsNull (rd, "#0");

			var values = new RouteValueDictionary ()
			{
				{ "controller", "SomeThing" },
				{ "action", "Index" },
				{ "country", "es" }
			};
			var vp = r.GetVirtualPath (new RequestContext (hc, new RouteData()), values);

			Assert.IsNotNull (vp, "#1");
			Assert.AreEqual ("SomeThing/es-en", vp.VirtualPath, "#1-1");
			Assert.AreEqual (r, vp.Route, "#1-2");
			Assert.AreEqual (0, vp.DataTokens.Count, "#1-3");

			// Case #2: pass no country, but locale as user value.
			values.Remove("country");
			values.Add("locale", "xx");
			vp = r.GetVirtualPath(new RequestContext(hc, new RouteData()), values);

			Assert.IsNotNull(vp, "#2");
			Assert.AreEqual("SomeThing/us-xx", vp.VirtualPath, "#2-1");
			Assert.AreEqual(r, vp.Route, "#2-2");
			Assert.AreEqual(0, vp.DataTokens.Count, "#2-3");

			// Case #3: make contry required.
			r = new MyRoute("{controller}/{country}-{locale}/{action}", new MyRouteHandler())
			{
				Defaults = new RouteValueDictionary(new { action = "Index", locale = "en" })
			};
			vp = r.GetVirtualPath(new RequestContext(hc, new RouteData()), values);

			Assert.IsNull(vp, "#3");
		}
Пример #19
0
		public void GetVirtualPath22 ()
		{
			var r = new MyRoute("summary/{controller}/{id}/{action}", new MyRouteHandler())
			{
				Defaults = new RouteValueDictionary(new { action = "Index" })
			};
			var hc = new HttpContextStub2("~/summary/kind/90941a4f-daf3-4c89-a6dc-83e8de4e3db5/test", String.Empty);
			var rd = r.GetRouteData(hc);
			Assert.IsNotNull(rd, "#0");
			Assert.AreEqual("90941a4f-daf3-4c89-a6dc-83e8de4e3db5", rd.Values["id"]);

			var values = new RouteValueDictionary(new { action = "Index" });
			var vp = r.GetVirtualPath(new RequestContext(hc, rd), values);

			Assert.IsNotNull(vp, "#1");
			Assert.AreEqual("summary/kind/90941a4f-daf3-4c89-a6dc-83e8de4e3db5", vp.VirtualPath, "#1-1");
			Assert.AreEqual(r, vp.Route, "#1-2");
			Assert.AreEqual(0, vp.DataTokens.Count, "#1-3");

			values = new RouteValueDictionary(new { action = "save" });
			vp = r.GetVirtualPath(new RequestContext(hc, rd), values);

			Assert.IsNotNull(vp, "#2");
			Assert.AreEqual("summary/kind/90941a4f-daf3-4c89-a6dc-83e8de4e3db5/save", vp.VirtualPath, "#2-1");
			Assert.AreEqual(r, vp.Route, "#2-2");
			Assert.AreEqual(0, vp.DataTokens.Count, "#2-3");

			values = new RouteValueDictionary(new { action = "save", extra = "stuff" });
			vp = r.GetVirtualPath(new RequestContext(hc, rd), values);

			Assert.IsNotNull(vp, "#3");
			Assert.AreEqual("summary/kind/90941a4f-daf3-4c89-a6dc-83e8de4e3db5/save?extra=stuff", vp.VirtualPath, "#3-2");
			Assert.AreEqual(0, vp.DataTokens.Count, "#3-3");
		}
Пример #20
0
		public void GetVirtualPath4 ()
		{
			var r = new MyRoute ("{foo}/{bar}", new MyRouteHandler ());
			var hc = new HttpContextStub2 ("~/x/y", String.Empty);
			var rd = r.GetRouteData (hc);

			// override a value incompletely
			var values = new RouteValueDictionary ();
			values ["foo"] = "A";

			var vp = r.GetVirtualPath (new RequestContext (hc, rd), values);
			Assert.IsNull (vp);
		}
Пример #21
0
		public void GetVirtualPath25()
		{
			var r = new MyRoute("{year}/{month}/{slug}", new MyRouteHandler())
			{
				Defaults = new RouteValueDictionary(new { controller = "Blog", action = "View" }),
				Constraints = new RouteValueDictionary(new { year = @"\d{4}", month = @"\d{2}" }),
			};
			var hc = new HttpContextStub2("~/", String.Empty);
			var values = new RouteValueDictionary()
			{
				{ "area", string.Empty },
				{ "controller", "Blog" },
				{ "action", "View" },
				{ "year", 2013 }, // Year as an int, not a string
				{ "month", "08" },
				{ "slug", "hello-world" },
			};
			var vp = r.GetVirtualPath(new RequestContext(hc, new RouteData()), values);

			Assert.IsNotNull(vp, "#1");
			Assert.AreEqual("2013/08/hello-world", vp.VirtualPath, "#2");
		}
Пример #22
0
		public void GetVirtualPathWithCatchall2()
		{
			var r = new MyRoute("HelloWorld/{*path}", new MyRouteHandler());
			var hc = new HttpContextStub2("~/", String.Empty);
			var values = new RouteValueDictionary()
			{
				{ "path", "foo/bar/baz" },
			};
			var vp = r.GetVirtualPath(new RequestContext(hc, new RouteData()), values);

			Assert.IsNotNull(vp, "#1");
			Assert.AreEqual("HelloWorld/foo/bar/baz", vp.VirtualPath, "#2");
		}
Пример #23
0
		public void GetVirtualPath5 ()
		{
			var r = new MyRoute ("{foo}/{bar}", new MyRouteHandler ());
			var hc = new HttpContextStub2 ("~/x/y", String.Empty);
			var rd = r.GetRouteData (hc);

			// override values completely.
			var values = new RouteValueDictionary ();
			values ["foo"] = "A";
			values ["bar"] = "B";

			var vp = r.GetVirtualPath (new RequestContext (hc, rd), values);

			Assert.IsNotNull (vp, "#1");
			Assert.AreEqual ("A/B", vp.VirtualPath, "#2");
			Assert.AreEqual (r, vp.Route, "#3");
			Assert.AreEqual (0, vp.DataTokens.Count, "#4");
		}
Пример #24
0
		public void GetVirtualPath20 ()
		{
			var r = new MyRoute("summary/{controller}/{id}/{action}", new MyRouteHandler())
			{
				Defaults = new RouteValueDictionary(new { action = "Index" })
			};
			var hc = new HttpContextStub2("~/summary/kind/1/test", String.Empty);
			var rd = r.GetRouteData(hc);
			Assert.IsNotNull(rd, "#1");

			var values = new RouteValueDictionary(new { id = "2", action = "save" });
			var vp = r.GetVirtualPath(new RequestContext(hc, rd), values);

			Assert.IsNotNull(vp, "#2");
			Assert.AreEqual("summary/kind/2/save", vp.VirtualPath, "#2-1");
			Assert.AreEqual(r, vp.Route, "#2-2");
			Assert.AreEqual(0, vp.DataTokens.Count, "#2-3");

			values = new RouteValueDictionary(new { id = "3", action = "save", extra = "stuff" });
			vp = r.GetVirtualPath(new RequestContext(hc, rd), values);

			Assert.IsNotNull(vp, "#3");
			Assert.AreEqual("summary/kind/3/save?extra=stuff", vp.VirtualPath, "#3-2");
			Assert.AreEqual(0, vp.DataTokens.Count, "#3-3");
		}