Route.GetRouteData() and HttpMethodConstraint.Match() with tests.
[mono.git] / mcs / class / System.Web.Routing / Test / System.Web.Routing / RouteCollectionTest.cs
1 //
2 // RouteCollectionTest.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2008 Novell Inc. http://novell.com
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 using System;
31 using System.Web;
32 using System.Web.Routing;
33 using NUnit.Framework;
34
35 namespace MonoTests.System.Web.Routing
36 {
37         [TestFixture]
38         public class RouteCollectionTest
39         {
40                 [Test]
41                 public void ConstructorNullArgs ()
42                 {
43                         // allowed
44                         new RouteCollection (null);
45                 }
46
47                 [Test]
48                 public void AddNullMame ()
49                 {
50                         var c = new RouteCollection ();
51                         // when name is null, no duplicate check is done.
52                         c.Add (null, new Route (null, null));
53                         c.Add (null, new Route (null, null));
54                 }
55
56                 [Test]
57                 public void AddDuplicateEmpty ()
58                 {
59                         var c = new RouteCollection ();
60                         // when name is "", no duplicate check is done.
61                         c.Add ("", new Route (null, null));
62                         c.Add ("", new Route (null, null));
63                 }
64
65                 [Test]
66                 [ExpectedException (typeof (ArgumentException))]
67                 public void AddDuplicateName ()
68                 {
69                         var c = new RouteCollection ();
70                         c.Add ("x", new Route (null, null));
71                         c.Add ("x", new Route (null, null));
72                 }
73
74                 [Test]
75                 public void IndexForNonExistent ()
76                 {
77                         Assert.IsNull (new RouteCollection () [null]);
78                 }
79
80                 [Test]
81                 public void IndexForExistent ()
82                 {
83                         var c = new RouteCollection ();
84                         var r = new Route (null, null);
85                         c.Add ("x", r);
86                         Assert.AreEqual (r, c ["x"]);
87                 }
88
89                 [Test]
90                 public void IndexForNonExistentAfterRemoval ()
91                 {
92                         var c = new RouteCollection ();
93                         var r = new Route (null, null);
94                         c.Add ("x", r);
95                         c.Remove (r);
96                         Assert.IsNull(c ["x"]);
97                 }
98
99                 [Test]
100                 [ExpectedException (typeof (ArgumentNullException))]
101                 public void GetRouteDataNullArg ()
102                 {
103                         new RouteCollection ().GetRouteData (null);
104                 }
105
106                 [Test]
107                 public void GetRouteDataForNonExistent ()
108                 {
109                         var rd = new RouteCollection ().GetRouteData (new HttpContextStub ("~/foo"));
110                         Assert.IsNull (rd);
111                 }
112
113                 [Test]
114                 public void GetRouteDataWrongPathNoRoute ()
115                 {
116                         new RouteCollection ().GetRouteData (new HttpContextStub (String.Empty, String.Empty));
117                 }
118
119                 /*
120                 comment out those tests; I cannot explain those tests.
121
122                 [Test]
123                 [ExpectedException (typeof (ArgumentOutOfRangeException))]
124                 public void GetRouteDataWrongPathOneRoute ()
125                 {
126                         var c = new RouteCollection ();
127                         var r = new Route ("foo", null);
128                         c.Add (null, r);
129                         // it somehow causes ArgumentOutOfRangeException for 
130                         // Request.AppRelativeCurrentExecutionFilePath.
131                         c.GetRouteData (new HttpContextStub (String.Empty, String.Empty));
132                 }
133
134                 [Test]
135                 public void GetRouteDataWrongPathOneRoute2 ()
136                 {
137                         var c = new RouteCollection ();
138                         var r = new Route ("foo", null);
139                         c.Add (null, r);
140                         c.GetRouteData (new HttpContextStub ("/~", String.Empty));
141                 }
142                 */
143
144                 [Test]
145                 [ExpectedException (typeof (NotImplementedException))]
146                 public void GetRouteDataForPathInfoNIE ()
147                 {
148                         var c = new RouteCollection ();
149                         var r = new Route ("foo", null);
150                         c.Add (null, r);
151                         // it retrieves PathInfo and then dies.
152                         var rd = c.GetRouteData (new HttpContextStub ("~/foo"));
153                 }
154
155                 [Test]
156                 public void GetRouteDataForNullHandler ()
157                 {
158                         var c = new RouteCollection ();
159                         var r = new Route ("foo", null); // allowed
160                         c.Add (null, r);
161                         var rd = c.GetRouteData (new HttpContextStub ("~/foo", String.Empty));
162                         Assert.IsNotNull (rd, "#1");
163                         Assert.AreEqual (r, rd.Route, "#2");
164                 }
165         }
166 }