2 // DynamicDataRouteHandlerTest.cs
5 // Marek Habersack <mhabersack@novell.com>
7 // Copyright (C) 2009 Novell Inc. http://novell.com
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections;
31 using System.Collections.Generic;
32 using System.Collections.ObjectModel;
33 using System.Collections.Specialized;
34 using System.ComponentModel.DataAnnotations;
35 using System.Data.SqlClient;
36 using System.Data.Linq;
37 using System.Data.Linq.Mapping;
38 using System.Globalization;
40 using System.Reflection;
41 using System.Security.Permissions;
42 using System.Security.Principal;
45 using System.Web.DynamicData;
46 using System.Web.DynamicData.ModelProviders;
47 using System.Web.Routing;
49 using NUnit.Framework;
50 using MonoTests.stand_alone.WebHarness;
51 using MonoTests.SystemWeb.Framework;
52 using MonoTests.Common;
53 using MonoTests.ModelProviders;
55 using MetaModel = System.Web.DynamicData.MetaModel;
56 using MetaTable = System.Web.DynamicData.MetaTable;
58 namespace MonoTests.System.Web.DynamicData
61 public class DynamicDataRouteHandlerTest
63 DynamicDataContainerModelProvider <TestDataContext> dynamicModelProvider;
68 dynamicModelProvider = new DynamicDataContainerModelProvider <TestDataContext> ();
69 Utils.RegisterContext (dynamicModelProvider, new ContextConfiguration () { ScaffoldAllTables = true });
73 public void Constructor ()
75 var ddrh = new DynamicDataRouteHandler ();
77 Assert.AreEqual (null, ddrh.Model, "#A1");
81 public void CreateHandlerParams ()
83 MetaModel m = Utils.CommonInitialize ();
85 var route = RouteTable.Routes[0] as DynamicDataRoute;
86 MetaTable t = m.Tables[TestDataContext.TableFooEmpty];
87 var handler = route.RouteHandler = new DynamicDataRouteHandler ();
89 // No null check is made, of course - throws from some internal method
90 AssertExtensions.Throws<NullReferenceException> (() => {
91 handler.CreateHandler (null, t, PageAction.Details);
94 // No null check again - this time throws from GetCustomPageVirtualPath
95 AssertExtensions.Throws<NullReferenceException> (() => {
96 handler.CreateHandler (route, null, PageAction.Details);
99 // And once again, no null check and thrown from GetCustomPageVirtualPath as well
100 AssertExtensions.Throws<NullReferenceException> (() => {
101 handler.CreateHandler (route, t, null);
106 [Ignore ("Throws NREX on .NET - no idea why and how to make it work. Probably needs full request emulation (using Mainsoft test suite)")]
107 // Probably need to simulate a full reqest using similar environment as System.Web tests
108 public void CreateHandler ()
110 MetaModel m = Utils.CommonInitialize ();
112 var route = RouteTable.Routes[0] as DynamicDataRoute;
113 MetaTable t = m.Tables[TestDataContext.TableFooEmpty];
114 Assert.IsNotNull (t, "#A1");
116 var handler = route.RouteHandler = new DynamicDataRouteHandler ();
117 var wrapper = new MyHttpContextWrapper ();
118 var request = wrapper.Request as MyHttpRequestWrapper;
119 request.SetProperty ("AppRelativeCurrentExecutionFilePath", "~/FooEmptyTable/List.aspx");
120 request.SetProperty ("PathInfo", String.Empty);
122 // This must be non-null because DynamicData doesn't care to check whether the returned
123 // value is null or not...
124 request.SetProperty ("QueryString", new NameValueCollection ());
126 // This will assign the route handler's Model property
127 RouteData rd = route.GetRouteData (wrapper);
128 Assert.IsNotNull (handler.Model, "#A2");
130 // Throws a NREX from some internal method - no slightest idea why, as none of the parameters
132 IHttpHandler h = handler.CreateHandler (route, t, PageAction.Details);
133 Assert.IsNotNull (h, "#A3");
134 Assert.AreEqual (typeof (Page), h.GetType (), "#A3-1");
136 var page = h as Page;
137 Assert.AreEqual (String.Empty, page.AppRelativeVirtualPath, "#A3-2");
141 public void GetCustomPageVirtualPath ()
143 MetaModel m = Utils.CommonInitialize ();
144 var route = RouteTable.Routes[0] as DynamicDataRoute;
146 MetaTable t = m.Tables[TestDataContext.TableFooEmpty];
147 Assert.IsNotNull (t, "#A1");
149 // We neeed the handler to have its Model property set
150 route.RouteHandler = new MyDynamicDataRouteHandler ();
151 var handler = route.RouteHandler as MyDynamicDataRouteHandler;
152 Assert.IsNotNull (handler, "#A2");
154 // Lack of null check (for table)
155 AssertExtensions.Throws<NullReferenceException> (() => {
156 handler.DoGetCustomPageVirtualPath (null, null);
159 // Another missing null check (this time for Model)... Are null checks
161 AssertExtensions.Throws<NullReferenceException> (() => {
162 handler.DoGetCustomPageVirtualPath (t, String.Empty);
165 var wrapper = new MyHttpContextWrapper ();
166 var request = wrapper.Request as MyHttpRequestWrapper;
167 request.SetProperty ("AppRelativeCurrentExecutionFilePath", "~/FooEmptyTable/List.aspx");
168 request.SetProperty ("PathInfo", String.Empty);
170 // This must be non-null because DynamicData doesn't care to check whether the returned
171 // value is null or not...
172 request.SetProperty ("QueryString", new NameValueCollection ());
174 // This will assign the route handler's Model property
175 RouteData rd = route.GetRouteData (wrapper);
176 Assert.IsNotNull (handler.Model, "#A3");
178 Assert.AreEqual (handler.Model.DynamicDataFolderVirtualPath + "CustomPages/" + t.Name + "/MyCustomPage.aspx",
179 handler.DoGetCustomPageVirtualPath (t, "MyCustomPage"), "#A4");
181 handler.Model.DynamicDataFolderVirtualPath = "~/MyFolder";
182 Assert.AreEqual (handler.Model.DynamicDataFolderVirtualPath + "CustomPages/" + t.Name + "/MyCustomPage.aspx",
183 handler.DoGetCustomPageVirtualPath (t, "MyCustomPage"), "#A5");
186 Assert.AreEqual (handler.Model.DynamicDataFolderVirtualPath + "CustomPages/" + t.Name + "/.aspx",
187 handler.DoGetCustomPageVirtualPath (t, null), "#A6");
189 Assert.AreEqual (handler.Model.DynamicDataFolderVirtualPath + "CustomPages/" + t.Name + "/.aspx",
190 handler.DoGetCustomPageVirtualPath (t, String.Empty), "#A7");
194 public void GetRequestContext ()
196 AssertExtensions.Throws<ArgumentNullException> (() => {
197 DynamicDataRouteHandler.GetRequestContext (null);
200 var req = new FakeHttpWorkerRequest ();
201 var ctx = new HttpContext (req);
203 RequestContext rc = DynamicDataRouteHandler.GetRequestContext (ctx);
204 Assert.IsNotNull (rc, "#B1");
205 Assert.IsNotNull (rc.HttpContext, "#B1-1");
206 Assert.IsNotNull (rc.RouteData, "#B1-2");
208 Assert.IsNull (rc.RouteData.Route, "#C1");
209 Assert.IsNull (rc.RouteData.RouteHandler, "#C1-1");
210 Assert.IsNotNull (rc.RouteData.Values, "#C1-2");
211 Assert.AreEqual (0, rc.RouteData.Values.Count, "#C1-3");
215 public void GetRequestMetaTable ()
217 MetaModel m = Utils.CommonInitialize ();
218 var route = RouteTable.Routes[0] as DynamicDataRoute;
219 MetaTable t = m.Tables[TestDataContext.TableFooDisplayName];
220 Assert.IsNotNull (t, "#A1");
222 // Surprise! A null check is present!
223 AssertExtensions.Throws<ArgumentNullException> (() => {
224 DynamicDataRouteHandler.GetRequestMetaTable (null);
227 MetaTable t2 = DynamicDataRouteHandler.GetRequestMetaTable (HttpContext.Current);
228 Assert.IsNull (t2, "#A3");
230 DynamicDataRouteHandler.SetRequestMetaTable (HttpContext.Current, t);
231 t2 = DynamicDataRouteHandler.GetRequestMetaTable (HttpContext.Current);
232 Assert.IsNotNull (t2, "#A4");
233 Assert.AreEqual (t, t2, "#A4-1");
237 public void GetScaffoldPageVirtualPath ()
239 MetaModel m = Utils.CommonInitialize ();
240 var route = RouteTable.Routes[0] as DynamicDataRoute;
242 MetaTable t = m.Tables[TestDataContext.TableFooDisplayName];
243 Assert.IsNotNull (t, "#A1");
245 // We neeed the handler to have its Model property set
246 route.RouteHandler = new MyDynamicDataRouteHandler ();
247 var handler = route.RouteHandler as MyDynamicDataRouteHandler;
248 Assert.IsNotNull (handler, "#A2");
250 // Lack of null check (for table)
251 AssertExtensions.Throws<NullReferenceException> (() => {
252 handler.DoGetScaffoldPageVirtualPath (null, null);
255 // Another missing null check (this time for Model)... Are null checks
257 AssertExtensions.Throws<NullReferenceException> (() => {
258 handler.DoGetScaffoldPageVirtualPath (t, String.Empty);
261 var wrapper = new MyHttpContextWrapper ();
262 var request = wrapper.Request as MyHttpRequestWrapper;
263 request.SetProperty ("AppRelativeCurrentExecutionFilePath", "~/FooDisplayNameTable/List.aspx");
264 request.SetProperty ("PathInfo", String.Empty);
266 // This must be non-null because DynamicData doesn't care to check whether the returned
267 // value is null or not...
268 request.SetProperty ("QueryString", new NameValueCollection ());
270 // This will assign the route handler's Model property
271 RouteData rd = route.GetRouteData (wrapper);
272 Assert.IsNotNull (handler.Model, "#A3");
274 Assert.AreEqual (handler.Model.DynamicDataFolderVirtualPath + "PageTemplates/" + "MyCustomPage.aspx",
275 handler.DoGetScaffoldPageVirtualPath (t, "MyCustomPage"), "#A4");
277 handler.Model.DynamicDataFolderVirtualPath = "~/MyFolder";
278 Assert.AreEqual (handler.Model.DynamicDataFolderVirtualPath + "PageTemplates/" + "MyCustomPage.aspx",
279 handler.DoGetScaffoldPageVirtualPath (t, "MyCustomPage"), "#A5");
282 Assert.AreEqual (handler.Model.DynamicDataFolderVirtualPath + "PageTemplates/" + ".aspx",
283 handler.DoGetScaffoldPageVirtualPath (t, null), "#A6");
285 Assert.AreEqual (handler.Model.DynamicDataFolderVirtualPath + "PageTemplates/" + ".aspx",
286 handler.DoGetScaffoldPageVirtualPath (t, String.Empty), "#A7");
292 MetaModel m = Utils.CommonInitialize ();
293 var route = RouteTable.Routes[0] as DynamicDataRoute;
295 Assert.IsNotNull (route, "#A1");
296 Assert.IsNotNull (route.Model, "#A1-1");
297 var handler = route.RouteHandler;
299 Assert.IsNotNull (handler, "#A2");
300 Assert.IsTrue (handler.GetType () == typeof (MyDynamicDataRouteHandler), "#A2-1");
301 Assert.IsNull (handler.Model, "#A2-2");
303 var req = new FakeHttpWorkerRequest ();
304 var ctx = new HttpContext (req);
306 RequestContext rc = DynamicDataRouteHandler.GetRequestContext (ctx);
307 Assert.IsNotNull (rc, "#B1");
308 Assert.IsNull (handler.Model, "#B1-2");
310 var wrapper = new MyHttpContextWrapper ();
311 var request = wrapper.Request as MyHttpRequestWrapper;
313 // It appears .NET checks whether the indicated table exists - if not, GetRouteData will return
314 // null (even though the Route class will find a match)
315 request.SetProperty ("AppRelativeCurrentExecutionFilePath", "~/NoSuchTable/List.aspx");
316 request.SetProperty ("PathInfo", String.Empty);
318 // This must be non-null because DynamicData doesn't care to check whether the returned
319 // value is null or not...
320 request.SetProperty ("QueryString", new NameValueCollection ());
322 // No table FooTable in the context - returns null
323 RouteData rd = route.GetRouteData (wrapper);
324 Assert.IsNull (rd, "#C1");
326 // Apparently Model is set in the above call even though it returns null
327 Assert.IsNotNull (handler.Model, "#C1-1");
328 Assert.AreEqual (route.Model, handler.Model, "#C1-2");
330 request.SetProperty ("AppRelativeCurrentExecutionFilePath", "~/BarTable/List.aspx");
331 rd = route.GetRouteData (wrapper);
332 Assert.IsNotNull (rd, "#D1");
333 Assert.IsNotNull (handler.Model, "#D1-1");
334 Assert.AreEqual (route.Model, handler.Model, "#D1-2");
338 public void SetRequestMetaTable ()
340 MetaModel m = Utils.CommonInitialize ();
341 var route = RouteTable.Routes[0] as DynamicDataRoute;
342 MetaTable t = m.Tables[TestDataContext.TableFooDisplayName];
343 Assert.IsNotNull (t, "#A1");
345 // And following the tradition... [drum roll] - NO NULL CHECK!
346 AssertExtensions.Throws<NullReferenceException> (() => {
347 DynamicDataRouteHandler.SetRequestMetaTable (null, t);
350 DynamicDataRouteHandler.SetRequestMetaTable (HttpContext.Current, t);
351 MetaTable t2 = DynamicDataRouteHandler.GetRequestMetaTable (HttpContext.Current);
352 Assert.IsNotNull (t2, "#A3");
353 Assert.AreEqual (t, t2, "#A3-1");
355 DynamicDataRouteHandler.SetRequestMetaTable (HttpContext.Current, null);
356 t2 = DynamicDataRouteHandler.GetRequestMetaTable (HttpContext.Current);
357 Assert.IsNull (t2, "#A4");