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;
51 using MonoTests.stand_alone.WebHarness;
52 using MonoTests.SystemWeb.Framework;
53 using MonoTests.Common;
54 using MonoTests.ModelProviders;
56 using MetaModel = System.Web.DynamicData.MetaModel;
57 using MetaTable = System.Web.DynamicData.MetaTable;
59 namespace MonoTests.System.Web.DynamicData
62 public class DynamicDataRouteHandlerTest
64 DynamicDataContainerModelProvider <TestDataContext> dynamicModelProvider;
69 dynamicModelProvider = new DynamicDataContainerModelProvider <TestDataContext> ();
70 Utils.RegisterContext (dynamicModelProvider, new ContextConfiguration () { ScaffoldAllTables = true });
74 public void Constructor ()
76 var ddrh = new DynamicDataRouteHandler ();
78 Assert.AreEqual (null, ddrh.Model, "#A1");
82 public void CreateHandlerParams ()
84 MetaModel m = Utils.CommonInitialize ();
86 var route = RouteTable.Routes[0] as DynamicDataRoute;
87 MetaTable t = m.Tables[TestDataContext.TableFooEmpty];
88 var handler = route.RouteHandler = new DynamicDataRouteHandler ();
90 // No null check is made, of course - throws from some internal method
91 AssertExtensions.Throws<NullReferenceException> (() => {
92 handler.CreateHandler (null, t, PageAction.Details);
95 // No null check again - this time throws from GetCustomPageVirtualPath
96 AssertExtensions.Throws<NullReferenceException> (() => {
97 handler.CreateHandler (route, null, PageAction.Details);
100 // And once again, no null check and thrown from GetCustomPageVirtualPath as well
101 AssertExtensions.Throws<NullReferenceException> (() => {
102 handler.CreateHandler (route, t, null);
107 [Ignore ("Throws NREX on .NET - no idea why and how to make it work. Probably needs full request emulation (using Mainsoft test suite)")]
108 // Probably need to simulate a full reqest using similar environment as System.Web tests
109 public void CreateHandler ()
111 MetaModel m = Utils.CommonInitialize ();
113 var route = RouteTable.Routes[0] as DynamicDataRoute;
114 MetaTable t = m.Tables[TestDataContext.TableFooEmpty];
115 Assert.IsNotNull (t, "#A1");
117 var handler = route.RouteHandler = new DynamicDataRouteHandler ();
118 var wrapper = new MyHttpContextWrapper ();
119 var request = wrapper.Request as MyHttpRequestWrapper;
120 request.SetProperty ("AppRelativeCurrentExecutionFilePath", "~/FooEmptyTable/List.aspx");
121 request.SetProperty ("PathInfo", String.Empty);
123 // This must be non-null because DynamicData doesn't care to check whether the returned
124 // value is null or not...
125 request.SetProperty ("QueryString", new NameValueCollection ());
127 // This will assign the route handler's Model property
128 RouteData rd = route.GetRouteData (wrapper);
129 Assert.IsNotNull (handler.Model, "#A2");
131 // Throws a NREX from some internal method - no slightest idea why, as none of the parameters
133 IHttpHandler h = handler.CreateHandler (route, t, PageAction.Details);
134 Assert.IsNotNull (h, "#A3");
135 Assert.AreEqual (typeof (Page), h.GetType (), "#A3-1");
137 var page = h as Page;
138 Assert.AreEqual (String.Empty, page.AppRelativeVirtualPath, "#A3-2");
142 public void GetCustomPageVirtualPath ()
144 MetaModel m = Utils.CommonInitialize ();
145 var route = RouteTable.Routes[0] as DynamicDataRoute;
147 MetaTable t = m.Tables[TestDataContext.TableFooEmpty];
148 Assert.IsNotNull (t, "#A1");
150 // We neeed the handler to have its Model property set
151 route.RouteHandler = new MyDynamicDataRouteHandler ();
152 var handler = route.RouteHandler as MyDynamicDataRouteHandler;
153 Assert.IsNotNull (handler, "#A2");
155 // Lack of null check (for table)
156 AssertExtensions.Throws<NullReferenceException> (() => {
157 handler.DoGetCustomPageVirtualPath (null, null);
160 // Another missing null check (this time for Model)... Are null checks
162 AssertExtensions.Throws<NullReferenceException> (() => {
163 handler.DoGetCustomPageVirtualPath (t, String.Empty);
166 var wrapper = new MyHttpContextWrapper ();
167 var request = wrapper.Request as MyHttpRequestWrapper;
168 request.SetProperty ("AppRelativeCurrentExecutionFilePath", "~/FooEmptyTable/List.aspx");
169 request.SetProperty ("PathInfo", String.Empty);
171 // This must be non-null because DynamicData doesn't care to check whether the returned
172 // value is null or not...
173 request.SetProperty ("QueryString", new NameValueCollection ());
175 // This will assign the route handler's Model property
176 RouteData rd = route.GetRouteData (wrapper);
177 Assert.IsNotNull (handler.Model, "#A3");
179 Assert.AreEqual (handler.Model.DynamicDataFolderVirtualPath + "CustomPages/" + t.Name + "/MyCustomPage.aspx",
180 handler.DoGetCustomPageVirtualPath (t, "MyCustomPage"), "#A4");
182 handler.Model.DynamicDataFolderVirtualPath = "~/MyFolder";
183 Assert.AreEqual (handler.Model.DynamicDataFolderVirtualPath + "CustomPages/" + t.Name + "/MyCustomPage.aspx",
184 handler.DoGetCustomPageVirtualPath (t, "MyCustomPage"), "#A5");
187 Assert.AreEqual (handler.Model.DynamicDataFolderVirtualPath + "CustomPages/" + t.Name + "/.aspx",
188 handler.DoGetCustomPageVirtualPath (t, null), "#A6");
190 Assert.AreEqual (handler.Model.DynamicDataFolderVirtualPath + "CustomPages/" + t.Name + "/.aspx",
191 handler.DoGetCustomPageVirtualPath (t, String.Empty), "#A7");
195 public void GetRequestContext ()
197 AssertExtensions.Throws<ArgumentNullException> (() => {
198 DynamicDataRouteHandler.GetRequestContext (null);
201 var req = new FakeHttpWorkerRequest ();
202 var ctx = new HttpContext (req);
204 RequestContext rc = DynamicDataRouteHandler.GetRequestContext (ctx);
205 Assert.IsNotNull (rc, "#B1");
206 Assert.IsNotNull (rc.HttpContext, "#B1-1");
207 Assert.IsNotNull (rc.RouteData, "#B1-2");
209 Assert.IsNull (rc.RouteData.Route, "#C1");
210 Assert.IsNull (rc.RouteData.RouteHandler, "#C1-1");
211 Assert.IsNotNull (rc.RouteData.Values, "#C1-2");
212 Assert.AreEqual (0, rc.RouteData.Values.Count, "#C1-3");
216 public void GetRequestMetaTable ()
218 MetaModel m = Utils.CommonInitialize ();
219 var route = RouteTable.Routes[0] as DynamicDataRoute;
220 MetaTable t = m.Tables[TestDataContext.TableFooDisplayName];
221 Assert.IsNotNull (t, "#A1");
223 // Surprise! A null check is present!
224 AssertExtensions.Throws<ArgumentNullException> (() => {
225 DynamicDataRouteHandler.GetRequestMetaTable (null);
228 MetaTable t2 = DynamicDataRouteHandler.GetRequestMetaTable (HttpContext.Current);
229 Assert.IsNull (t2, "#A3");
231 DynamicDataRouteHandler.SetRequestMetaTable (HttpContext.Current, t);
232 t2 = DynamicDataRouteHandler.GetRequestMetaTable (HttpContext.Current);
233 Assert.IsNotNull (t2, "#A4");
234 Assert.AreEqual (t, t2, "#A4-1");
238 public void GetScaffoldPageVirtualPath ()
240 MetaModel m = Utils.CommonInitialize ();
241 var route = RouteTable.Routes[0] as DynamicDataRoute;
243 MetaTable t = m.Tables[TestDataContext.TableFooDisplayName];
244 Assert.IsNotNull (t, "#A1");
246 // We neeed the handler to have its Model property set
247 route.RouteHandler = new MyDynamicDataRouteHandler ();
248 var handler = route.RouteHandler as MyDynamicDataRouteHandler;
249 Assert.IsNotNull (handler, "#A2");
251 // Lack of null check (for table)
252 AssertExtensions.Throws<NullReferenceException> (() => {
253 handler.DoGetScaffoldPageVirtualPath (null, null);
256 // Another missing null check (this time for Model)... Are null checks
258 AssertExtensions.Throws<NullReferenceException> (() => {
259 handler.DoGetScaffoldPageVirtualPath (t, String.Empty);
262 var wrapper = new MyHttpContextWrapper ();
263 var request = wrapper.Request as MyHttpRequestWrapper;
264 request.SetProperty ("AppRelativeCurrentExecutionFilePath", "~/FooDisplayNameTable/List.aspx");
265 request.SetProperty ("PathInfo", String.Empty);
267 // This must be non-null because DynamicData doesn't care to check whether the returned
268 // value is null or not...
269 request.SetProperty ("QueryString", new NameValueCollection ());
271 // This will assign the route handler's Model property
272 RouteData rd = route.GetRouteData (wrapper);
273 Assert.IsNotNull (handler.Model, "#A3");
275 Assert.AreEqual (handler.Model.DynamicDataFolderVirtualPath + "PageTemplates/" + "MyCustomPage.aspx",
276 handler.DoGetScaffoldPageVirtualPath (t, "MyCustomPage"), "#A4");
278 handler.Model.DynamicDataFolderVirtualPath = "~/MyFolder";
279 Assert.AreEqual (handler.Model.DynamicDataFolderVirtualPath + "PageTemplates/" + "MyCustomPage.aspx",
280 handler.DoGetScaffoldPageVirtualPath (t, "MyCustomPage"), "#A5");
283 Assert.AreEqual (handler.Model.DynamicDataFolderVirtualPath + "PageTemplates/" + ".aspx",
284 handler.DoGetScaffoldPageVirtualPath (t, null), "#A6");
286 Assert.AreEqual (handler.Model.DynamicDataFolderVirtualPath + "PageTemplates/" + ".aspx",
287 handler.DoGetScaffoldPageVirtualPath (t, String.Empty), "#A7");
293 MetaModel m = Utils.CommonInitialize ();
294 var route = RouteTable.Routes[0] as DynamicDataRoute;
296 Assert.IsNotNull (route, "#A1");
297 Assert.IsNotNull (route.Model, "#A1-1");
298 var handler = route.RouteHandler;
300 Assert.IsNotNull (handler, "#A2");
301 Assert.IsTrue (handler.GetType () == typeof (MyDynamicDataRouteHandler), "#A2-1");
302 Assert.IsNull (handler.Model, "#A2-2");
304 var req = new FakeHttpWorkerRequest ();
305 var ctx = new HttpContext (req);
307 RequestContext rc = DynamicDataRouteHandler.GetRequestContext (ctx);
308 Assert.IsNotNull (rc, "#B1");
309 Assert.IsNull (handler.Model, "#B1-2");
311 var wrapper = new MyHttpContextWrapper ();
312 var request = wrapper.Request as MyHttpRequestWrapper;
314 // It appears .NET checks whether the indicated table exists - if not, GetRouteData will return
315 // null (even though the Route class will find a match)
316 request.SetProperty ("AppRelativeCurrentExecutionFilePath", "~/NoSuchTable/List.aspx");
317 request.SetProperty ("PathInfo", String.Empty);
319 // This must be non-null because DynamicData doesn't care to check whether the returned
320 // value is null or not...
321 request.SetProperty ("QueryString", new NameValueCollection ());
323 // No table FooTable in the context - returns null
324 RouteData rd = route.GetRouteData (wrapper);
325 Assert.IsNull (rd, "#C1");
327 // Apparently Model is set in the above call even though it returns null
328 Assert.IsNotNull (handler.Model, "#C1-1");
329 Assert.AreEqual (route.Model, handler.Model, "#C1-2");
331 request.SetProperty ("AppRelativeCurrentExecutionFilePath", "~/BarTable/List.aspx");
332 rd = route.GetRouteData (wrapper);
333 Assert.IsNotNull (rd, "#D1");
334 Assert.IsNotNull (handler.Model, "#D1-1");
335 Assert.AreEqual (route.Model, handler.Model, "#D1-2");
339 public void SetRequestMetaTable ()
341 MetaModel m = Utils.CommonInitialize ();
342 var route = RouteTable.Routes[0] as DynamicDataRoute;
343 MetaTable t = m.Tables[TestDataContext.TableFooDisplayName];
344 Assert.IsNotNull (t, "#A1");
346 // And following the tradition... [drum roll] - NO NULL CHECK!
347 AssertExtensions.Throws<NullReferenceException> (() => {
348 DynamicDataRouteHandler.SetRequestMetaTable (null, t);
351 DynamicDataRouteHandler.SetRequestMetaTable (HttpContext.Current, t);
352 MetaTable t2 = DynamicDataRouteHandler.GetRequestMetaTable (HttpContext.Current);
353 Assert.IsNotNull (t2, "#A3");
354 Assert.AreEqual (t, t2, "#A3-1");
356 DynamicDataRouteHandler.SetRequestMetaTable (HttpContext.Current, null);
357 t2 = DynamicDataRouteHandler.GetRequestMetaTable (HttpContext.Current);
358 Assert.IsNull (t2, "#A4");