public static MetaModel CommonInitialize(bool myDynamicDataRoute) { MetaModel m = MetaModel.Default; var req = new FakeHttpWorkerRequest(); var ctx = new HttpContext(req); HttpContext.Current = ctx; RouteCollection routes = RouteTable.Routes; routes.Clear(); if (myDynamicDataRoute) { routes.Add( new MyDynamicDataRoute("{table}/{action}.aspx") { Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }), Model = m, RouteHandler = new MyDynamicDataRouteHandler() }); } else { routes.Add( new DynamicDataRoute("{table}/{action}.aspx") { Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }), Model = m, RouteHandler = new MyDynamicDataRouteHandler() }); } return(m); }
public void GetVirtualPath7 () { var c = new RouteCollection (); c.Add (new MyRoute ("{table}/{action}.aspx", new MyRouteHandler ()) { Constraints = new RouteValueDictionary (new { action = "List|Details|Edit|Insert" }), }); var req = new FakeHttpWorkerRequest (); var ctx = new HttpContext (req); HttpContext.Current = ctx; var rd = new RouteData (); var hc = new HttpContextWrapper (ctx); var vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary { {"Table", "FooTable"}, {"Action", "Details"} }); Assert.IsNotNull (vp, "#A1"); Assert.AreEqual ("/FooTable/Details.aspx", vp.VirtualPath, "#A1-1"); vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary { {"Table", "FooTable"}, {"Action", String.Empty} }); Assert.IsNull (vp, "#B1"); vp = c.GetVirtualPath (new RequestContext (hc, rd), new RouteValueDictionary { {"Table", "FooTable"}, {"Action", null} }); Assert.IsNull (vp, "#C1"); }
public void Model () { MetaModel m = Utils.CommonInitialize (); var route = RouteTable.Routes[0] as DynamicDataRoute; Assert.IsNotNull (route, "#A1"); Assert.IsNotNull (route.Model, "#A1-1"); var handler = route.RouteHandler; Assert.IsNotNull (handler, "#A2"); Assert.IsTrue (handler.GetType () == typeof (MyDynamicDataRouteHandler), "#A2-1"); Assert.IsNull (handler.Model, "#A2-2"); var req = new FakeHttpWorkerRequest (); var ctx = new HttpContext (req); RequestContext rc = DynamicDataRouteHandler.GetRequestContext (ctx); Assert.IsNotNull (rc, "#B1"); Assert.IsNull (handler.Model, "#B1-2"); var wrapper = new MyHttpContextWrapper (); var request = wrapper.Request as MyHttpRequestWrapper; // It appears .NET checks whether the indicated table exists - if not, GetRouteData will return // null (even though the Route class will find a match) request.SetProperty ("AppRelativeCurrentExecutionFilePath", "~/NoSuchTable/List.aspx"); request.SetProperty ("PathInfo", String.Empty); // This must be non-null because DynamicData doesn't care to check whether the returned // value is null or not... request.SetProperty ("QueryString", new NameValueCollection ()); // No table FooTable in the context - returns null RouteData rd = route.GetRouteData (wrapper); Assert.IsNull (rd, "#C1"); // Apparently Model is set in the above call even though it returns null Assert.IsNotNull (handler.Model, "#C1-1"); Assert.AreEqual (route.Model, handler.Model, "#C1-2"); request.SetProperty ("AppRelativeCurrentExecutionFilePath", "~/BarTable/List.aspx"); rd = route.GetRouteData (wrapper); Assert.IsNotNull (rd, "#D1"); Assert.IsNotNull (handler.Model, "#D1-1"); Assert.AreEqual (route.Model, handler.Model, "#D1-2"); }
public void GetRequestContext () { AssertExtensions.Throws<ArgumentNullException> (() => { DynamicDataRouteHandler.GetRequestContext (null); }, "#A1"); var req = new FakeHttpWorkerRequest (); var ctx = new HttpContext (req); RequestContext rc = DynamicDataRouteHandler.GetRequestContext (ctx); Assert.IsNotNull (rc, "#B1"); Assert.IsNotNull (rc.HttpContext, "#B1-1"); Assert.IsNotNull (rc.RouteData, "#B1-2"); Assert.IsNull (rc.RouteData.Route, "#C1"); Assert.IsNull (rc.RouteData.RouteHandler, "#C1-1"); Assert.IsNotNull (rc.RouteData.Values, "#C1-2"); Assert.AreEqual (0, rc.RouteData.Values.Count, "#C1-3"); }
public void BaseDefaultsModification_10 () { MetaModel m = MetaModel.Default; var req = new FakeHttpWorkerRequest (); var ctx = new HttpContext (req); HttpContext.Current = ctx; RouteCollection routes = RouteTable.Routes; routes.Clear (); var ddr = new DynamicDataRoute ("{table}/{action}.aspx") { Defaults = new RouteValueDictionary () { {"Table", "FooWithDefaultsTable"} }, Table = "BazTable", Model = m, RouteHandler = new MyDynamicDataRouteHandler () }; routes.Add (ddr); Assert.IsNotNull (ddr, "#A1"); Assert.IsNotNull (ddr.Defaults, "#A1-1"); var rd = new RouteData (); var hc = new HttpContextWrapper (HttpContext.Current); ddr.GetVirtualPath (new RequestContext (hc, rd), null); Assert.IsNotNull (ddr.Defaults, "#B1"); Assert.AreEqual (1, ddr.Defaults.Count, "#B1-1"); Assert.AreEqual ("BazTable", ddr.Defaults["Table"], "#B1-2"); ddr.Table = "AnotherTable"; ddr.GetVirtualPath (new RequestContext (hc, rd), null); Assert.IsNotNull (ddr.Defaults, "#C1"); Assert.AreEqual (1, ddr.Defaults.Count, "#C1-1"); Assert.AreEqual ("BazTable", ddr.Defaults["Table"], "#C1-2"); }
public void BaseDefaultsModification_9 () { MetaModel m = MetaModel.Default; var req = new FakeHttpWorkerRequest (); var ctx = new HttpContext (req); HttpContext.Current = ctx; RouteCollection routes = RouteTable.Routes; routes.Clear (); var ddr = new DynamicDataRoute ("{table}/{action}.aspx") { Defaults = new RouteValueDictionary () { {"Action", "InitialAction"} }, Action = PageAction.Details, Model = m, RouteHandler = new MyDynamicDataRouteHandler () }; routes.Add (ddr); Assert.IsNotNull (ddr, "#A1"); Assert.IsNotNull (ddr.Defaults, "#A1-1"); var rd = new RouteData (); var hc = new HttpContextWrapper (HttpContext.Current); ddr.GetVirtualPath (new RequestContext (hc, rd), null); Assert.IsNotNull (ddr.Defaults, "#B1"); Assert.AreEqual (1, ddr.Defaults.Count, "#B1-1"); Assert.AreEqual (PageAction.Details, ddr.Defaults["Action"], "#B1-2"); ddr.Action = "MyAction"; ddr.GetVirtualPath (new RequestContext (hc, rd), null); Assert.IsNotNull (ddr.Defaults, "#C1"); Assert.AreEqual (1, ddr.Defaults.Count, "#C1-1"); Assert.AreEqual (PageAction.Details, ddr.Defaults["Action"], "#B1-2"); }
public void BaseDefaultsModification_8 () { MetaModel m = MetaModel.Default; var req = new FakeHttpWorkerRequest (); var ctx = new HttpContext (req); HttpContext.Current = ctx; RouteCollection routes = RouteTable.Routes; routes.Clear (); var ddr = new DynamicDataRoute ("{table}/{action}.aspx") { Table = String.Empty, Model = m, RouteHandler = new MyDynamicDataRouteHandler () }; routes.Add (ddr); var rd = new RouteData (); var hc = new HttpContextWrapper (HttpContext.Current); AssertExtensions.Throws<ArgumentException> (() => { ddr.GetVirtualPath (new RequestContext (hc, rd), null); }, "#A1"); }
public void BaseDefaultsModification_7 () { MetaModel m = MetaModel.Default; var req = new FakeHttpWorkerRequest (); var ctx = new HttpContext (req); HttpContext.Current = ctx; RouteCollection routes = RouteTable.Routes; routes.Clear (); var ddr = new DynamicDataRoute ("{table}/{action}.aspx") { Table = null, Model = m, RouteHandler = new MyDynamicDataRouteHandler () }; routes.Add (ddr); Assert.IsNotNull (ddr, "#A1"); Assert.IsNull (ddr.Defaults, "#A1-1"); var rd = new RouteData (); var hc = new HttpContextWrapper (HttpContext.Current); ddr.GetVirtualPath (new RequestContext (hc, rd), null); Assert.IsNull (ddr.Defaults, "#B1"); }
public void IsValidRequestString () { var rv = new TestRequestValidator (); int validationFailureIndex; var fr = new FakeHttpWorkerRequest ("http://localhost/default.aspx?key=invalid%value"); var ctx = new HttpContext (fr); Assert.IsFalse (rv.DoIsValidRequestString (null, "<script>invalid%value", RequestValidationSource.QueryString, "key", out validationFailureIndex), "#A1"); Assert.IsFalse (rv.DoIsValidRequestString (ctx, "<script>invalid%value", RequestValidationSource.QueryString, "key", out validationFailureIndex), "#A2"); }
public void GetActionPath2 () { var foo = new Foo (true); var req = new FakeHttpWorkerRequest (); var ctx = new HttpContext (req); HttpContext.Current = ctx; MetaModel m = Utils.GetModel<MyDataContext2> (); RouteTable.Routes.Add ( new DynamicDataRoute ("{table}/{action}.aspx") { Constraints = new RouteValueDictionary (new { action = "List|Details|Edit|Insert" }), Model = m, RouteHandler = new MyDynamicDataRouteHandler () }); // .NET stacktrace: // // at System.Web.DynamicData.MetaModel.TryGetTable(String uniqueTableName, MetaTable& table) // at System.Web.DynamicData.MetaModel.GetTable(String uniqueTableName) AssertExtensions.Throws<ArgumentNullException> (() => m.GetActionPath (null, PageAction.List, foo), "#A1"); Assert.AreEqual (String.Empty, m.GetActionPath ("FooTable", null, foo), "#A2"); Assert.AreEqual ("/FooTable/List.aspx", m.GetActionPath ("FooTable", PageAction.List, null), "#A3"); AssertExtensions.Throws<ArgumentException> (() => m.GetActionPath ("NoSuchTable", PageAction.List, foo), "#A4"); Assert.AreEqual ("/FooTable/List.aspx", m.GetActionPath ("FooTable", "List", foo), "#B1"); }
public void RegisterContext5 () { // In the process of several experiments (as the docs lack any good explanation), // I determined that this test needs the following for succesful completion: // // - a worker request // - a HttpContext // - a fake route handler derived from DynamicDataRouteHandler, so that its CreateHandler // returns a handler without attempting to actually find a View for the requested action // - a route which can match table actions (taken from the skeleton DynamicData project // generated by VisualStudio) // - _empty_ query string returned from the fake worker request, or .NET will populate the // HttpRequest.QueryString collection with one null item, and .NET's DynamicData will happily // assume any entry in that collection is not null (it seems null checks aren't very popular // in DynamicData code) var req = new FakeHttpWorkerRequest (); var ctx = new HttpContext (req); HttpContext.Current = ctx; MetaModel m = Utils.GetModel<MyDataContext2> (); RouteTable.Routes.Add ( new DynamicDataRoute ("{table}/{action}.aspx") { Constraints = new RouteValueDictionary (new { action = "List|Details|Edit|Insert" }), Model = m, RouteHandler = new MyDynamicDataRouteHandler () }); MetaTable t = m.Tables[0]; Assert.AreEqual (1, m.Tables.Count, "#1-1"); Assert.AreEqual (1, m.VisibleTables.Count, "#1-2"); Assert.AreEqual (typeof (Foo), t.EntityType, "#1-3"); // Those names are only the last part before '.' (i.e. without schema name). Assert.AreEqual ("FooTable", t.Name, "#2-1"); Assert.AreEqual ("FooTable", t.DisplayName, "#2-2"); Assert.AreEqual ("FooTable", t.DataContextPropertyName, "#2-3"); Assert.AreEqual ("/FooTable/List.aspx", t.ListActionPath, "#2-4"); Assert.AreEqual ("FooTable", t.Provider.Name, "#3-1"); }
public void GetQuery_Context () { MetaModel m = Utils.GetModel<MyDataContext2> (); var req = new FakeHttpWorkerRequest (); var ctx = new HttpContext (req); HttpContext.Current = ctx; RouteCollection routes = RouteTable.Routes; routes.Clear (); var route = new MyDynamicDataRoute ("{table}/{action}.aspx") { Constraints = new RouteValueDictionary (new { action = "List|Details|Edit|Insert" }), Model = m, RouteHandler = new MyDynamicDataRouteHandler () }; routes.Add (route); MetaTable t = m.GetTable ("FooTable"); ; IQueryable query = t.GetQuery (null); Assert.IsNotNull (query, "#A1"); Assert.IsTrue (query.GetType () == typeof (Table<Foo>), "#A2"); var foo = new Foo (true); AssertExtensions.Throws (() => t.GetQuery (foo), "#B1"); }
public void GetActionPath_Action_3 () { MetaModel m = MetaModel.Default; var req = new FakeHttpWorkerRequest (); var ctx = new HttpContext (req); HttpContext.Current = ctx; RouteCollection routes = RouteTable.Routes; routes.Clear (); routes.Add (new DynamicDataRoute ("{table}/ListDetails.aspx") { Action = PageAction.List, ViewName = "ListDetails", Model = m, RouteHandler = new MyDynamicDataRouteHandler () }); routes.Add (new DynamicDataRoute ("{table}/ListDetails.aspx") { Action = PageAction.Details, ViewName = "ListDetails", Model = m, RouteHandler = new MyDynamicDataRouteHandler () }); MetaTable t = m.Tables[TestDataContext.TableFooWithDefaults]; Assert.AreEqual (t.Model, m, "#A0"); Assert.AreEqual (Utils.BuildActionName (t, "ListDetails"), t.GetActionPath (PageAction.Details), "#A1"); Assert.AreEqual (Utils.BuildActionName (t, "ListDetails"), t.GetActionPath (PageAction.List), "#A2"); // Missing routes Assert.AreEqual (String.Empty, t.GetActionPath (PageAction.Edit), "#A3"); Assert.AreEqual (String.Empty, t.GetActionPath (PageAction.Insert), "#A4"); // Add routes for the two above tests routes.Add (new DynamicDataRoute ("{table}/EditInsert.aspx") { Action = PageAction.Edit, ViewName = "MyEditInsert", Model = m, RouteHandler = new MyDynamicDataRouteHandler () }); routes.Add (new DynamicDataRoute ("{table}/InsertEdit.aspx") { Action = PageAction.Insert, ViewName = "MyEditInsert", Model = m, RouteHandler = new MyDynamicDataRouteHandler () }); Assert.AreEqual (Utils.BuildActionName (t, "ListDetails"), t.GetActionPath (PageAction.Details), "#B1"); Assert.AreEqual (Utils.BuildActionName (t, "ListDetails"), t.GetActionPath (PageAction.List), "#B2"); Assert.AreEqual (Utils.BuildActionName (t, "EditInsert"), t.GetActionPath (PageAction.Edit), "#B3"); Assert.AreEqual (Utils.BuildActionName (t, "InsertEdit"), t.GetActionPath (PageAction.Insert), "#B4"); }