2 // Tests for System.Web.Hosting.SimpleWorkerRequest.cs
5 // Miguel de Icaza (miguel@novell.com)
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
31 using NUnit.Framework;
34 using System.Globalization;
36 using System.Web.Hosting;
38 namespace MonoTests.System.Web.Hosting {
41 public class SimpleWorkerRequestTest {
46 [SetUp] public void startup ()
48 cwd = Environment.CurrentDirectory;
49 bindir = Path.Combine (cwd, "bin");
51 int p = (int) Environment.OSVersion.Platform;
54 if ((p == 4) || (p == 128))
57 assembly = typeof (SimpleWorkerRequestTest).Assembly.CodeBase.Substring (x);
59 if (!Directory.Exists (bindir))
60 Directory.CreateDirectory (bindir);
62 File.Copy (assembly, Path.Combine (bindir, Path.GetFileName (assembly)), true);
66 // This tests the constructor when the user code creates an HttpContext
68 [Test] public void ConstructorTests ()
70 StringWriter sw = new StringWriter ();
72 SimpleWorkerRequest swr;
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");
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");
106 swr = new SimpleWorkerRequest ("/appDir", cwd, "/Something/page.aspx", "querystring", sw);
108 //Assert.AreEqual ("c:\\tmp\\page.aspx", swr.GetFilePathTranslated (), "S17");
111 // GetUriPath tests, veredict: MS implementation is a bit fragile on this interface
113 swr = new SimpleWorkerRequest ("/appDir", cwd, "/page.aspx", null, sw);
114 Assert.AreEqual ("/appDir//page.aspx", swr.GetUriPath (), "S23");
116 swr = new SimpleWorkerRequest ("/appDir/", cwd, "/page.aspx", null, sw);
117 Assert.AreEqual ("/appDir///page.aspx", swr.GetUriPath (), "S24");
119 swr = new SimpleWorkerRequest ("/appDir/", cwd, "page.aspx", null, sw);
120 Assert.AreEqual ("/appDir//page.aspx", swr.GetUriPath (), "S25");
122 swr = new SimpleWorkerRequest ("/appDir", cwd, "page.aspx", null, sw);
123 Assert.AreEqual ("/appDir/page.aspx", swr.GetUriPath (), "S26");
128 public void GetPathInfo ()
130 StringWriter sw = new StringWriter ();
131 SimpleWorkerRequest swr = new SimpleWorkerRequest ("appVirtualDir", cwd, "/pageVirtualPath", "querystring", sw);
132 Assert.AreEqual ("/pageVirtualPath", swr.GetPathInfo (), "GetPathInfo-1");
134 swr = new SimpleWorkerRequest ("appVirtualDir", cwd, "/", "querystring", sw);
135 Assert.AreEqual ("/", swr.GetPathInfo (), "GetPathInfo-2");
137 swr = new SimpleWorkerRequest ("appVirtualDir", cwd, "pageVirtualPath", "querystring", sw);
138 Assert.AreEqual (String.Empty, swr.GetPathInfo (), "GetPathInfo-3");
142 public void GetUriPath ()
144 StringWriter sw = new StringWriter ();
145 SimpleWorkerRequest swr = new SimpleWorkerRequest ("/", cwd, String.Empty, String.Empty, sw);
146 Assert.AreEqual ("/", swr.GetUriPath (), "GetUriPath");
148 swr = new SimpleWorkerRequest (String.Empty, cwd, String.Empty, String.Empty, sw);
149 Assert.AreEqual ("/", swr.GetUriPath (), "GetUriPath-2");
151 swr = new SimpleWorkerRequest (String.Empty, cwd, "/", String.Empty, sw);
152 Assert.AreEqual ("//", swr.GetUriPath (), "GetUriPath-3");
154 swr = new SimpleWorkerRequest ("/", cwd, "/", String.Empty, sw);
155 Assert.AreEqual ("//", swr.GetUriPath (), "GetUriPath-4");
157 swr = new SimpleWorkerRequest ("virtual", cwd, "/", String.Empty, sw);
158 Assert.AreEqual ("virtual//", swr.GetUriPath (), "GetUriPath-5");
160 swr = new SimpleWorkerRequest ("/virtual", cwd, "/", String.Empty, sw);
161 Assert.AreEqual ("/virtual//", swr.GetUriPath (), "GetUriPath-6");
163 swr = new SimpleWorkerRequest ("/virtual/", cwd, "/", String.Empty, sw);
164 Assert.AreEqual ("/virtual///", swr.GetUriPath (), "GetUriPath-7");
166 swr = new SimpleWorkerRequest ("virtual", cwd, "page", String.Empty, sw);
167 Assert.AreEqual ("virtual/page", swr.GetUriPath (), "GetUriPath-8");
169 swr = new SimpleWorkerRequest ("/virtual", cwd, "page", String.Empty, sw);
170 Assert.AreEqual ("/virtual/page", swr.GetUriPath (), "GetUriPath-9");
172 swr = new SimpleWorkerRequest ("/virtual/", cwd, "page", String.Empty, sw);
173 Assert.AreEqual ("/virtual//page", swr.GetUriPath (), "GetUriPath-a");
176 public class Host : MarshalByRefObject {
177 string cwd = Environment.CurrentDirectory;
182 StringWriter sw = new StringWriter ();
183 SimpleWorkerRequest swr = new SimpleWorkerRequest("file.aspx", "querystring", sw);
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");
202 // \windows\microsoft.net\framework\v1.1.4322\Config\machine.config
204 Assert.AreEqual (true, swr.MachineConfigPath != null, "T14");
207 // \windows\microsoft.net\framework\v1.1.4322
209 Assert.AreEqual (true, swr.MachineInstallDirectory != null, "T15");
211 // Disabled T16. It throws a nullref on MS
212 // Assert.AreEqual (Path.Combine (cwd, "file.aspx"), swr.GetFilePathTranslated (), "T16");
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");
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");
229 public void Exception1 ()
231 StringWriter sw = new StringWriter ();
232 SimpleWorkerRequest swr = new SimpleWorkerRequest("file.aspx", "querystring", sw);
237 public void Exception2 ()
239 StringWriter sw = new StringWriter ();
240 SimpleWorkerRequest swr = new SimpleWorkerRequest("file.aspx", "querystring", sw);
242 swr.MapPath ("file.aspx");
245 public void Exception3 ()
247 StringWriter sw = new StringWriter ();
248 SimpleWorkerRequest swr = new SimpleWorkerRequest("file.aspx", "querystring", sw);
250 swr.MapPath ("appVirtualDir/file.aspx");
253 public void MapPathOnEmptyVirtualDir ()
255 StringWriter sw = new StringWriter ();
256 SimpleWorkerRequest swr = new SimpleWorkerRequest("file.aspx", "querystring", sw);
262 Host MakeHost (string virtualDir)
264 return (Host) ApplicationHost.CreateApplicationHost (typeof (Host), virtualDir, cwd);
269 return MakeHost ("/appVirtualDir");
272 [Test] public void AppDomainTests ()
274 Host h = MakeHost ();
280 [ExpectedException(typeof(ArgumentNullException))]
281 public void AppDomain_MapPath1 ()
283 Host h = MakeHost ();
289 [ExpectedException(typeof(ArgumentNullException))]
290 public void AppDomain_MapPath2 ()
292 Host h = MakeHost ();
298 [ExpectedException(typeof(ArgumentNullException))]
299 public void AppDomain_MapPath3 ()
301 Host h = MakeHost ();
307 public void AppDomain_MapPath4 ()
309 Host h = MakeHost ("/");
311 h.MapPathOnEmptyVirtualDir ();
315 // This tests the constructor when the target application domain is created with
316 // CreateApplicationHost
319 public void ConstructorTest_CreateApplicationHost ()
321 // Does not work without a NRE, need to call CreateApplicationHost.
322 // = new SimpleWorkerRequest ("pageVirtualPath", "querystring", sw);
323 // Assert.AreEqual ("querystring", swr.GetQueryString ());
327 public void PathInfos ()
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);