Added a NotDotNet category attribute for tests that pass on mono and do not pass...
[mono.git] / mcs / class / System.Web / Test / System.Web.Hosting / SimpleWorkerRequestTest.cs
1 //
2 // Tests for System.Web.Hosting.SimpleWorkerRequest.cs 
3 //
4 // Author:
5 //      Miguel de Icaza (miguel@novell.com)
6 //
7
8 //
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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
31 using NUnit.Framework;
32 using System;
33 using System.IO;
34 using System.Globalization;
35 using System.Web;
36 using System.Web.Hosting;
37
38 namespace MonoTests.System.Web.Hosting {
39
40         [TestFixture]
41         public class SimpleWorkerRequestTest {
42
43                 string cwd, bindir;
44                 string assembly;
45                 
46                 [SetUp] public void startup ()
47                 {
48                         cwd = Environment.CurrentDirectory;
49                         bindir = Path.Combine (cwd, "bin");
50
51                         int p = (int) Environment.OSVersion.Platform;
52                         int x = 8;
53                         
54                         if ((p == 4) || (p == 128)) 
55                                 x = 7;
56                                 
57                         assembly = typeof (SimpleWorkerRequestTest).Assembly.CodeBase.Substring (x);
58
59                         if (!Directory.Exists (bindir))
60                                 Directory.CreateDirectory (bindir);
61                         
62                         File.Copy (assembly, Path.Combine (bindir, Path.GetFileName (assembly)), true);
63                 }
64                         
65                 //
66                 // This tests the constructor when the user code creates an HttpContext
67                 //
68                 [Test] public void ConstructorTests ()
69                 {
70                         StringWriter sw = new StringWriter ();
71                         
72                         SimpleWorkerRequest swr;
73
74                         swr = new SimpleWorkerRequest ("/appVirtualDir", cwd, "pageVirtualPath", "querystring", sw);
75                         Assert.AreEqual ("/appVirtualDir", swr.GetAppPath (), "S1");
76                         Assert.AreEqual ("/appVirtualDir/pageVirtualPath", swr.GetFilePath (), "S2");
77                         Assert.AreEqual ("GET", swr.GetHttpVerbName (), "S3");
78                         Assert.AreEqual ("HTTP/1.0", swr.GetHttpVersion (), "S4");
79                         Assert.AreEqual ("127.0.0.1", swr.GetLocalAddress (), "S5");
80                         Assert.AreEqual (80, swr.GetLocalPort (), "S6");
81                         Assert.AreEqual ("querystring", swr.GetQueryString (), "S7");
82                         Assert.AreEqual ("127.0.0.1", swr.GetRemoteAddress (), "S8");
83                         Assert.AreEqual (0, swr.GetRemotePort (), "S9");
84                         Assert.AreEqual ("/appVirtualDir/pageVirtualPath?querystring", swr.GetRawUrl (), "S10");
85                         Assert.AreEqual ("/appVirtualDir/pageVirtualPath", swr.GetUriPath (), "S11");
86                         Assert.AreEqual ("0", swr.GetUserToken ().ToString (), "S12");
87                         Assert.AreEqual (null, swr.MapPath ("x"), "S13");
88                         Assert.AreEqual (null, swr.MachineConfigPath, "S14");
89                         Assert.AreEqual (null, swr.MachineInstallDirectory, "S15");
90                         Assert.AreEqual (Path.Combine (cwd, "pageVirtualPath"), swr.GetFilePathTranslated (), "S16");
91                         Assert.AreEqual ("", swr.GetServerVariable ("AUTH_TYPE"), "S18");
92                         Assert.AreEqual ("", swr.GetServerVariable ("AUTH_USER"), "S19");
93                         Assert.AreEqual ("", swr.GetServerVariable ("REMOTE_USER"), "S20");
94                         Assert.AreEqual ("", swr.GetServerVariable ("SERVER_SOFTWARE"), "S21");
95                         Assert.AreEqual ("/appVirtualDir/pageVirtualPath", swr.GetUriPath (), "S22");
96
97                         //
98                         // MapPath
99                         //
100                         Assert.AreEqual (null, swr.MapPath ("file.aspx"), "MP1");
101                         Assert.AreEqual (null, swr.MapPath ("/appVirtualDir/pageVirtualPath"), "MP2");
102                         Assert.AreEqual (null, swr.MapPath ("appVirtualDir/pageVirtualPath"), "MP3");
103                         Assert.AreEqual (null, swr.MapPath ("/appVirtualDir/pageVirtualPath/page.aspx"), "MP4");
104                         Assert.AreEqual (null, swr.MapPath ("/appVirtualDir/pageVirtualPath/Subdir"), "MP5");
105
106                         swr = new SimpleWorkerRequest ("/appDir", cwd, "/Something/page.aspx", "querystring", sw);
107
108                         //Assert.AreEqual ("c:\\tmp\\page.aspx", swr.GetFilePathTranslated (), "S17");
109
110                         //
111                         // GetUriPath tests, veredict: MS implementation is a bit fragile on this interface
112                         //
113                         swr = new SimpleWorkerRequest ("/appDir", cwd, "/page.aspx", null, sw);
114                         Assert.AreEqual ("/appDir//page.aspx", swr.GetUriPath (), "S23");
115
116                         swr = new SimpleWorkerRequest ("/appDir/", cwd, "/page.aspx", null, sw);
117                         Assert.AreEqual ("/appDir///page.aspx", swr.GetUriPath (), "S24");
118
119                         swr = new SimpleWorkerRequest ("/appDir/", cwd, "page.aspx", null, sw);
120                         Assert.AreEqual ("/appDir//page.aspx", swr.GetUriPath (), "S25");
121
122                         swr = new SimpleWorkerRequest ("/appDir", cwd, "page.aspx", null, sw);
123                         Assert.AreEqual ("/appDir/page.aspx", swr.GetUriPath (), "S26");
124                         
125                 }
126
127                 [Test]
128                 public void GetPathInfo ()
129                 {
130                         StringWriter sw = new StringWriter ();
131                         SimpleWorkerRequest swr = new SimpleWorkerRequest ("appVirtualDir", cwd, "/pageVirtualPath", "querystring", sw);
132                         Assert.AreEqual ("/pageVirtualPath", swr.GetPathInfo (), "GetPathInfo-1");
133
134                         swr = new SimpleWorkerRequest ("appVirtualDir", cwd, "/", "querystring", sw);
135                         Assert.AreEqual ("/", swr.GetPathInfo (), "GetPathInfo-2");
136
137                         swr = new SimpleWorkerRequest ("appVirtualDir", cwd, "pageVirtualPath", "querystring", sw);
138                         Assert.AreEqual (String.Empty, swr.GetPathInfo (), "GetPathInfo-3");
139                 }
140
141                 [Test]
142                 public void GetUriPath ()
143                 {
144                         StringWriter sw = new StringWriter ();
145                         SimpleWorkerRequest swr = new SimpleWorkerRequest ("/", cwd, String.Empty, String.Empty, sw);
146                         Assert.AreEqual ("/", swr.GetUriPath (), "GetUriPath");
147
148                         swr = new SimpleWorkerRequest (String.Empty, cwd, String.Empty, String.Empty, sw);
149                         Assert.AreEqual ("/", swr.GetUriPath (), "GetUriPath-2");
150
151                         swr = new SimpleWorkerRequest (String.Empty, cwd, "/", String.Empty, sw);
152                         Assert.AreEqual ("//", swr.GetUriPath (), "GetUriPath-3");
153
154                         swr = new SimpleWorkerRequest ("/", cwd, "/", String.Empty, sw);
155                         Assert.AreEqual ("//", swr.GetUriPath (), "GetUriPath-4");
156
157                         swr = new SimpleWorkerRequest ("virtual", cwd, "/", String.Empty, sw);
158                         Assert.AreEqual ("virtual//", swr.GetUriPath (), "GetUriPath-5");
159
160                         swr = new SimpleWorkerRequest ("/virtual", cwd, "/", String.Empty, sw);
161                         Assert.AreEqual ("/virtual//", swr.GetUriPath (), "GetUriPath-6");
162
163                         swr = new SimpleWorkerRequest ("/virtual/", cwd, "/", String.Empty, sw);
164                         Assert.AreEqual ("/virtual///", swr.GetUriPath (), "GetUriPath-7");
165
166                         swr = new SimpleWorkerRequest ("virtual", cwd, "page", String.Empty, sw);
167                         Assert.AreEqual ("virtual/page", swr.GetUriPath (), "GetUriPath-8");
168
169                         swr = new SimpleWorkerRequest ("/virtual", cwd, "page", String.Empty, sw);
170                         Assert.AreEqual ("/virtual/page", swr.GetUriPath (), "GetUriPath-9");
171
172                         swr = new SimpleWorkerRequest ("/virtual/", cwd, "page", String.Empty, sw);
173                         Assert.AreEqual ("/virtual//page", swr.GetUriPath (), "GetUriPath-a");
174                 }
175
176                 public class Host : MarshalByRefObject {
177                         string cwd = Environment.CurrentDirectory;
178
179                         
180                         public void Demo ()
181                         {
182                                 StringWriter sw = new StringWriter ();
183                                 SimpleWorkerRequest swr = new SimpleWorkerRequest("file.aspx", "querystring", sw);
184
185                                 Assert.AreEqual ("/appVirtualDir", swr.GetAppPath (), "T1");
186                                 Assert.AreEqual (cwd + Path.DirectorySeparatorChar, swr.GetAppPathTranslated (), "TRANS1");
187                                 Assert.AreEqual ("/appVirtualDir/file.aspx", swr.GetFilePath (), "T2");
188                                 Assert.AreEqual ("GET", swr.GetHttpVerbName (), "T3");
189                                 Assert.AreEqual ("HTTP/1.0", swr.GetHttpVersion (), "T4");
190                                 Assert.AreEqual ("127.0.0.1", swr.GetLocalAddress (), "T5");
191                                 Assert.AreEqual (80, swr.GetLocalPort (), "T6");
192                                 Assert.AreEqual ("querystring", swr.GetQueryString (), "T7");
193                                 Assert.AreEqual ("127.0.0.1", swr.GetRemoteAddress (), "T8");
194                                 Assert.AreEqual (0, swr.GetRemotePort (), "T9");
195                                 Assert.AreEqual ("/appVirtualDir/file.aspx?querystring", swr.GetRawUrl (), "T10");
196                                 Assert.AreEqual ("/appVirtualDir/file.aspx", swr.GetUriPath (), "T11");
197                                 Assert.AreEqual ("0", swr.GetUserToken ().ToString (), "T12");
198                                 Assert.AreEqual ("", swr.GetPathInfo (), "TRANS2");
199
200                                 //
201                                 // On windows:
202                                 // \windows\microsoft.net\framework\v1.1.4322\Config\machine.config
203                                 //
204                                 Assert.AreEqual (true, swr.MachineConfigPath != null, "T14");
205                                 //
206                                 // On windows:
207                                 // \windows\microsoft.net\framework\v1.1.4322
208                                 //
209                                 Assert.AreEqual (true, swr.MachineInstallDirectory != null, "T15");
210
211                                 // Disabled T16. It throws a nullref on MS
212                                 //      Assert.AreEqual (Path.Combine (cwd, "file.aspx"), swr.GetFilePathTranslated (), "T16");
213                                 //
214                                 Assert.AreEqual ("", swr.GetServerVariable ("AUTH_TYPE"), "T18");
215                                 Assert.AreEqual ("", swr.GetServerVariable ("AUTH_USER"), "T19");
216                                 Assert.AreEqual ("", swr.GetServerVariable ("REMOTE_USER"), "T20");
217                                 Assert.AreEqual ("", swr.GetServerVariable ("TERVER_SOFTWARE"), "T21");
218                                 Assert.AreEqual ("/appVirtualDir/file.aspx", swr.GetUriPath (), "T22");
219
220                                 //
221                                 // MapPath
222                                 //
223                                 Assert.AreEqual (Path.Combine (cwd, "file.aspx"), swr.MapPath ("/appVirtualDir/file.aspx"), "TP2");
224                                 Assert.AreEqual (Path.Combine (cwd, "file.aspx"), swr.MapPath ("/appVirtualDir/file.aspx"), "TP4");
225                                 Assert.AreEqual (Path.Combine (cwd, Path.Combine ("Subdir", "file.aspx")), swr.MapPath ("/appVirtualDir/Subdir/file.aspx"), "TP5");
226
227                         }
228
229                         public void Exception1 ()
230                         {
231                                 StringWriter sw = new StringWriter ();
232                                 SimpleWorkerRequest swr = new SimpleWorkerRequest("file.aspx", "querystring", sw);
233
234                                 swr.MapPath ("x");
235                         }
236
237                         public void Exception2 ()
238                         {
239                                 StringWriter sw = new StringWriter ();
240                                 SimpleWorkerRequest swr = new SimpleWorkerRequest("file.aspx", "querystring", sw);
241
242                                 swr.MapPath ("file.aspx");
243                         }
244
245                         public void Exception3 ()
246                         {
247                                 StringWriter sw = new StringWriter ();
248                                 SimpleWorkerRequest swr = new SimpleWorkerRequest("file.aspx", "querystring", sw);
249
250                                 swr.MapPath ("appVirtualDir/file.aspx");
251                         }
252
253                         public void MapPathOnEmptyVirtualDir ()
254                         {
255                                 StringWriter sw = new StringWriter ();
256                                 SimpleWorkerRequest swr = new SimpleWorkerRequest("file.aspx", "querystring", sw);
257
258                                 swr.MapPath ("/");
259                         }
260                 }
261
262                 Host MakeHost (string virtualDir)
263                 {
264                         return (Host) ApplicationHost.CreateApplicationHost (typeof (Host), virtualDir, cwd);
265                 }
266
267                 Host MakeHost ()
268                 {
269                         return MakeHost ("/appVirtualDir");
270                 }
271                 
272                 [Test] public void AppDomainTests ()
273                 {
274                         Host h = MakeHost ();
275
276                         h.Demo ();
277                 }
278
279                 [Test]
280                 [ExpectedException(typeof(ArgumentNullException))]
281                 public void AppDomain_MapPath1 ()
282                 {
283                         Host h = MakeHost ();
284                         
285                         h.Exception1 ();
286                 }
287
288                 [Test]
289                 [ExpectedException(typeof(ArgumentNullException))]
290                 public void AppDomain_MapPath2 ()
291                 {
292                         Host h = MakeHost ();
293                         
294                         h.Exception2 ();
295                 }
296
297                 [Test]
298                 [ExpectedException(typeof(ArgumentNullException))]
299                 public void AppDomain_MapPath3 ()
300                 {
301                         Host h = MakeHost ();
302                         
303                         h.Exception3 ();
304                 }
305
306                 [Test]
307                 public void AppDomain_MapPath4 ()
308                 {
309                         Host h = MakeHost ("/");
310                         
311                         h.MapPathOnEmptyVirtualDir ();
312                 }
313                 
314                 //
315                 // This tests the constructor when the target application domain is created with
316                 // CreateApplicationHost
317                 //
318                 [Test]
319                 public void ConstructorTest_CreateApplicationHost ()
320                 {
321                         // Does not work without a NRE, need to call CreateApplicationHost.
322                         // = new SimpleWorkerRequest ("pageVirtualPath", "querystring", sw);
323                         // Assert.AreEqual ("querystring", swr.GetQueryString ());
324                 }
325
326                 [Test]
327                 public void PathInfos ()
328                 {
329                         SimpleWorkerRequest wr = new SimpleWorkerRequest ("/appDir", "", "page.aspx/pathinfo", "", null);
330                         HttpContext c = new HttpContext (wr);
331                         Assert.AreEqual ("http://127.0.0.1/appDir/page.aspx/pathinfo", c.Request.Url.AbsoluteUri);
332                         Assert.AreEqual ("/appDir/page.aspx", c.Request.FilePath);
333                         Assert.AreEqual ("/appDir/page.aspx/pathinfo", c.Request.Path);
334                         Assert.AreEqual ("/pathinfo", c.Request.PathInfo);
335                 }
336         }
337 }
338