Merge pull request #3913 from omwok/master
[mono.git] / mcs / class / System.Web.DynamicData / Test / System.Web.DynamicData / DynamicDataRouteHandlerTest.cs
1 //
2 // DynamicDataRouteHandlerTest.cs
3 //
4 // Authors:
5 //      Marek Habersack <mhabersack@novell.com>
6 //
7 // Copyright (C) 2009 Novell Inc. http://novell.com
8 //
9 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29 using System;
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;
39 using System.Linq;
40 using System.Reflection;
41 using System.Security.Permissions;
42 using System.Security.Principal;
43 using System.Web;
44 using System.Web.UI;
45 using System.Web.DynamicData;
46 using System.Web.DynamicData.ModelProviders;
47 using System.Web.Routing;
48
49 using NUnit.Framework;
50 using MonoTests.stand_alone.WebHarness;
51 using MonoTests.SystemWeb.Framework;
52 using MonoTests.Common;
53 using MonoTests.ModelProviders;
54
55 using MetaModel = System.Web.DynamicData.MetaModel;
56 using MetaTable = System.Web.DynamicData.MetaTable;
57
58 namespace MonoTests.System.Web.DynamicData
59 {
60         [TestFixture]
61         public class DynamicDataRouteHandlerTest
62         {
63                 DynamicDataContainerModelProvider <TestDataContext> dynamicModelProvider;
64
65                 [TestFixtureSetUp]
66                 public void SetUp ()
67                 {
68                         dynamicModelProvider = new DynamicDataContainerModelProvider <TestDataContext> ();
69                         Utils.RegisterContext (dynamicModelProvider, new ContextConfiguration () { ScaffoldAllTables = true });
70                 }
71
72                 [Test]
73                 public void Constructor ()
74                 {
75                         var ddrh = new DynamicDataRouteHandler ();
76
77                         Assert.AreEqual (null, ddrh.Model, "#A1");
78                 }
79
80                 [Test]
81                 public void CreateHandlerParams ()
82                 {
83                         MetaModel m = Utils.CommonInitialize ();
84
85                         var route = RouteTable.Routes[0] as DynamicDataRoute;
86                         MetaTable t = m.Tables[TestDataContext.TableFooEmpty];
87                         var handler = route.RouteHandler = new DynamicDataRouteHandler ();
88
89                         // No null check is made, of course - throws from some internal method
90                         AssertExtensions.Throws<NullReferenceException> (() => {
91                                 handler.CreateHandler (null, t, PageAction.Details);
92                         }, "#A1");
93
94                         // No null check again - this time throws from GetCustomPageVirtualPath
95                         AssertExtensions.Throws<NullReferenceException> (() => {
96                                 handler.CreateHandler (route, null, PageAction.Details);
97                         }, "#A2");
98
99                         // And once again, no null check and thrown from GetCustomPageVirtualPath as well
100                         AssertExtensions.Throws<NullReferenceException> (() => {
101                                 handler.CreateHandler (route, t, null);
102                         }, "#A3");
103                 }
104
105                 [Test]
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 ()
109                 {
110                         MetaModel m = Utils.CommonInitialize ();
111
112                         var route = RouteTable.Routes[0] as DynamicDataRoute;
113                         MetaTable t = m.Tables[TestDataContext.TableFooEmpty];
114                         Assert.IsNotNull (t, "#A1");
115
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);
121
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 ());
125
126                         // This will assign the route handler's Model property
127                         RouteData rd = route.GetRouteData (wrapper);
128                         Assert.IsNotNull (handler.Model, "#A2");
129
130                         // Throws a NREX from some internal method - no slightest idea why, as none of the parameters
131                         // passed are null
132                         IHttpHandler h = handler.CreateHandler (route, t, PageAction.Details);
133                         Assert.IsNotNull (h, "#A3");
134                         Assert.AreEqual (typeof (Page), h.GetType (), "#A3-1");
135
136                         var page = h as Page;
137                         Assert.AreEqual (String.Empty, page.AppRelativeVirtualPath, "#A3-2");
138                 }
139
140                 [Test]
141                 public void GetCustomPageVirtualPath ()
142                 {
143                         MetaModel m = Utils.CommonInitialize ();
144                         var route = RouteTable.Routes[0] as DynamicDataRoute;
145
146                         MetaTable t = m.Tables[TestDataContext.TableFooEmpty];
147                         Assert.IsNotNull (t, "#A1");
148
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");
153
154                         // Lack of null check (for table)
155                         AssertExtensions.Throws<NullReferenceException> (() => {
156                                 handler.DoGetCustomPageVirtualPath (null, null);
157                         }, "#A2-1");
158
159                         // Another missing null check (this time for Model)... Are null checks
160                         // out of fashion?
161                         AssertExtensions.Throws<NullReferenceException> (() => {
162                                 handler.DoGetCustomPageVirtualPath (t, String.Empty);
163                         }, "#A2-2");
164
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);
169
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 ());
173
174                         // This will assign the route handler's Model property
175                         RouteData rd = route.GetRouteData (wrapper);
176                         Assert.IsNotNull (handler.Model, "#A3");
177
178                         Assert.AreEqual (handler.Model.DynamicDataFolderVirtualPath + "CustomPages/" + t.Name + "/MyCustomPage.aspx", 
179                                 handler.DoGetCustomPageVirtualPath (t, "MyCustomPage"), "#A4");
180
181                         handler.Model.DynamicDataFolderVirtualPath = "~/MyFolder";
182                         Assert.AreEqual (handler.Model.DynamicDataFolderVirtualPath + "CustomPages/" + t.Name + "/MyCustomPage.aspx",
183                                 handler.DoGetCustomPageVirtualPath (t, "MyCustomPage"), "#A5");
184
185                         // doh!
186                         Assert.AreEqual (handler.Model.DynamicDataFolderVirtualPath + "CustomPages/" + t.Name + "/.aspx",
187                                 handler.DoGetCustomPageVirtualPath (t, null), "#A6");
188
189                         Assert.AreEqual (handler.Model.DynamicDataFolderVirtualPath + "CustomPages/" + t.Name + "/.aspx",
190                                 handler.DoGetCustomPageVirtualPath (t, String.Empty), "#A7");
191                 }
192
193                 [Test]
194                 public void GetRequestContext ()
195                 {
196                         AssertExtensions.Throws<ArgumentNullException> (() => {
197                                 DynamicDataRouteHandler.GetRequestContext (null);
198                         }, "#A1");
199
200                         var req = new FakeHttpWorkerRequest ();
201                         var ctx = new HttpContext (req);
202
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");
207
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");
212                 }
213
214                 [Test]
215                 public void GetRequestMetaTable ()
216                 {
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");
221
222                         // Surprise! A null check is present!
223                         AssertExtensions.Throws<ArgumentNullException> (() => {
224                                 DynamicDataRouteHandler.GetRequestMetaTable (null);
225                         }, "#A2");
226                         
227                         MetaTable t2 = DynamicDataRouteHandler.GetRequestMetaTable (HttpContext.Current);
228                         Assert.IsNull (t2, "#A3");
229
230                         DynamicDataRouteHandler.SetRequestMetaTable (HttpContext.Current, t);
231                         t2 = DynamicDataRouteHandler.GetRequestMetaTable (HttpContext.Current);
232                         Assert.IsNotNull (t2, "#A4");
233                         Assert.AreEqual (t, t2, "#A4-1");
234                 }
235
236                 [Test]
237                 public void GetScaffoldPageVirtualPath ()
238                 {
239                         MetaModel m = Utils.CommonInitialize ();
240                         var route = RouteTable.Routes[0] as DynamicDataRoute;
241
242                         MetaTable t = m.Tables[TestDataContext.TableFooDisplayName];
243                         Assert.IsNotNull (t, "#A1");
244
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");
249
250                         // Lack of null check (for table)
251                         AssertExtensions.Throws<NullReferenceException> (() => {
252                                 handler.DoGetScaffoldPageVirtualPath (null, null);
253                         }, "#A2-1");
254
255                         // Another missing null check (this time for Model)... Are null checks
256                         // out of fashion?
257                         AssertExtensions.Throws<NullReferenceException> (() => {
258                                 handler.DoGetScaffoldPageVirtualPath (t, String.Empty);
259                         }, "#A2-2");
260
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);
265
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 ());
269
270                         // This will assign the route handler's Model property
271                         RouteData rd = route.GetRouteData (wrapper);
272                         Assert.IsNotNull (handler.Model, "#A3");
273
274                         Assert.AreEqual (handler.Model.DynamicDataFolderVirtualPath + "PageTemplates/" + "MyCustomPage.aspx",
275                                 handler.DoGetScaffoldPageVirtualPath (t, "MyCustomPage"), "#A4");
276
277                         handler.Model.DynamicDataFolderVirtualPath = "~/MyFolder";
278                         Assert.AreEqual (handler.Model.DynamicDataFolderVirtualPath + "PageTemplates/" + "MyCustomPage.aspx",
279                                 handler.DoGetScaffoldPageVirtualPath (t, "MyCustomPage"), "#A5");
280
281                         // doh!
282                         Assert.AreEqual (handler.Model.DynamicDataFolderVirtualPath + "PageTemplates/" + ".aspx",
283                                 handler.DoGetScaffoldPageVirtualPath (t, null), "#A6");
284
285                         Assert.AreEqual (handler.Model.DynamicDataFolderVirtualPath + "PageTemplates/" + ".aspx",
286                                 handler.DoGetScaffoldPageVirtualPath (t, String.Empty), "#A7");
287                 }
288
289                 [Test]
290                 public void Model ()
291                 {
292                         MetaModel m = Utils.CommonInitialize ();
293                         var route = RouteTable.Routes[0] as DynamicDataRoute;
294
295                         Assert.IsNotNull (route, "#A1");
296                         Assert.IsNotNull (route.Model, "#A1-1");
297                         var handler = route.RouteHandler;
298
299                         Assert.IsNotNull (handler, "#A2");
300                         Assert.IsTrue (handler.GetType () == typeof (MyDynamicDataRouteHandler), "#A2-1");
301                         Assert.IsNull (handler.Model, "#A2-2");
302
303                         var req = new FakeHttpWorkerRequest ();
304                         var ctx = new HttpContext (req);
305
306                         RequestContext rc = DynamicDataRouteHandler.GetRequestContext (ctx);
307                         Assert.IsNotNull (rc, "#B1");
308                         Assert.IsNull (handler.Model, "#B1-2");
309
310                         var wrapper = new MyHttpContextWrapper ();
311                         var request = wrapper.Request as MyHttpRequestWrapper;
312
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);
317
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 ());
321                         
322                         // No table FooTable in the context - returns null
323                         RouteData rd = route.GetRouteData (wrapper);
324                         Assert.IsNull (rd, "#C1");
325
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");
329
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");
335                 }
336
337                 [Test]
338                 public void SetRequestMetaTable ()
339                 {
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");
344
345                         // And following the tradition... [drum roll] - NO NULL CHECK!
346                         AssertExtensions.Throws<NullReferenceException> (() => {
347                                 DynamicDataRouteHandler.SetRequestMetaTable (null, t);
348                         }, "#A2");
349
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");
354
355                         DynamicDataRouteHandler.SetRequestMetaTable (HttpContext.Current, null);
356                         t2 = DynamicDataRouteHandler.GetRequestMetaTable (HttpContext.Current);
357                         Assert.IsNull (t2, "#A4");
358                 }
359         }
360 }