SimpleWorkerRequestTest.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. //
  2. // Tests for System.Web.Hosting.SimpleWorkerRequest.cs
  3. //
  4. // Author:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. //
  8. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  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 NUnit.Framework;
  30. using System;
  31. using System.IO;
  32. using System.Globalization;
  33. using System.Web;
  34. using System.Web.Hosting;
  35. namespace MonoTests.System.Web.Hosting {
  36. [TestFixture]
  37. public class SimpleWorkerTests {
  38. string cwd, bindir;
  39. string assembly;
  40. [SetUp] public void startup ()
  41. {
  42. cwd = Environment.CurrentDirectory;
  43. bindir = Path.Combine (cwd, "bin");
  44. int p = (int) Environment.OSVersion.Platform;
  45. int x = 8;
  46. if ((p == 4) || (p == 128))
  47. x = 7;
  48. assembly = typeof (SimpleWorkerTests).Assembly.CodeBase.Substring (x);
  49. if (!Directory.Exists (bindir))
  50. Directory.CreateDirectory (bindir);
  51. File.Copy (assembly, Path.Combine (bindir, Path.GetFileName (assembly)), true);
  52. }
  53. //
  54. // This tests the constructor when the user code creates an HttpContext
  55. //
  56. [Test] public void ConstructorTests ()
  57. {
  58. StringWriter sw = new StringWriter ();
  59. SimpleWorkerRequest swr;
  60. string ppath;
  61. swr = new SimpleWorkerRequest ("/appVirtualDir", cwd, "pageVirtualPath", "querystring", sw);
  62. Assert.AreEqual ("/appVirtualDir", swr.GetAppPath (), "S1");
  63. Assert.AreEqual ("/appVirtualDir/pageVirtualPath", swr.GetFilePath (), "S2");
  64. Assert.AreEqual ("GET", swr.GetHttpVerbName (), "S3");
  65. Assert.AreEqual ("HTTP/1.0", swr.GetHttpVersion (), "S4");
  66. Assert.AreEqual ("127.0.0.1", swr.GetLocalAddress (), "S5");
  67. Assert.AreEqual (80, swr.GetLocalPort (), "S6");
  68. Assert.AreEqual ("querystring", swr.GetQueryString (), "S7");
  69. Assert.AreEqual ("127.0.0.1", swr.GetRemoteAddress (), "S8");
  70. Assert.AreEqual (0, swr.GetRemotePort (), "S9");
  71. Assert.AreEqual ("/appVirtualDir/pageVirtualPath?querystring", swr.GetRawUrl (), "S10");
  72. Assert.AreEqual ("/appVirtualDir/pageVirtualPath", swr.GetUriPath (), "S11");
  73. Assert.AreEqual ("0", swr.GetUserToken ().ToString (), "S12");
  74. Assert.AreEqual (null, swr.MapPath ("x"), "S13");
  75. Assert.AreEqual (null, swr.MachineConfigPath, "S14");
  76. Assert.AreEqual (null, swr.MachineInstallDirectory, "S15");
  77. Assert.AreEqual (Path.Combine (cwd, "pageVirtualPath"), swr.GetFilePathTranslated (), "S16");
  78. Assert.AreEqual ("", swr.GetServerVariable ("AUTH_TYPE"), "S18");
  79. Assert.AreEqual ("", swr.GetServerVariable ("AUTH_USER"), "S19");
  80. Assert.AreEqual ("", swr.GetServerVariable ("REMOTE_USER"), "S20");
  81. Assert.AreEqual ("", swr.GetServerVariable ("SERVER_SOFTWARE"), "S21");
  82. Assert.AreEqual ("/appVirtualDir/pageVirtualPath", swr.GetUriPath (), "S22");
  83. //
  84. // MapPath
  85. //
  86. Assert.AreEqual (null, swr.MapPath ("file.aspx"), "MP1");
  87. Assert.AreEqual (null, swr.MapPath ("/appVirtualDir/pageVirtualPath"), "MP2");
  88. Assert.AreEqual (null, swr.MapPath ("appVirtualDir/pageVirtualPath"), "MP3");
  89. Assert.AreEqual (null, swr.MapPath ("/appVirtualDir/pageVirtualPath/page.aspx"), "MP4");
  90. Assert.AreEqual (null, swr.MapPath ("/appVirtualDir/pageVirtualPath/Subdir"), "MP5");
  91. swr = new SimpleWorkerRequest ("/appDir", cwd, "/Something/page.aspx", "querystring", sw);
  92. //Assert.AreEqual ("c:\\tmp\\page.aspx", swr.GetFilePathTranslated (), "S17");
  93. //
  94. // GetUriPath tests, veredict: MS implementation is a bit fragile on this interface
  95. //
  96. swr = new SimpleWorkerRequest ("/appDir", cwd, "/page.aspx", null, sw);
  97. Assert.AreEqual ("/appDir//page.aspx", swr.GetUriPath (), "S23");
  98. swr = new SimpleWorkerRequest ("/appDir/", cwd, "/page.aspx", null, sw);
  99. Assert.AreEqual ("/appDir///page.aspx", swr.GetUriPath (), "S24");
  100. swr = new SimpleWorkerRequest ("/appDir/", cwd, "page.aspx", null, sw);
  101. Assert.AreEqual ("/appDir//page.aspx", swr.GetUriPath (), "S25");
  102. swr = new SimpleWorkerRequest ("/appDir", cwd, "page.aspx", null, sw);
  103. Assert.AreEqual ("/appDir/page.aspx", swr.GetUriPath (), "S26");
  104. }
  105. [Test]
  106. public void GetPathInfo ()
  107. {
  108. StringWriter sw = new StringWriter ();
  109. SimpleWorkerRequest swr = new SimpleWorkerRequest ("appVirtualDir", cwd, "/pageVirtualPath", "querystring", sw);
  110. Assert.AreEqual ("/pageVirtualPath", swr.GetPathInfo (), "GetPathInfo-1");
  111. swr = new SimpleWorkerRequest ("appVirtualDir", cwd, "/", "querystring", sw);
  112. Assert.AreEqual ("/", swr.GetPathInfo (), "GetPathInfo-2");
  113. swr = new SimpleWorkerRequest ("appVirtualDir", cwd, "pageVirtualPath", "querystring", sw);
  114. Assert.AreEqual (String.Empty, swr.GetPathInfo (), "GetPathInfo-3");
  115. }
  116. public class Host : MarshalByRefObject {
  117. string cwd = Environment.CurrentDirectory;
  118. public void Demo ()
  119. {
  120. StringWriter sw = new StringWriter ();
  121. SimpleWorkerRequest swr = new SimpleWorkerRequest("file.aspx", "querystring", sw);
  122. Assert.AreEqual ("/appVirtualDir", swr.GetAppPath (), "T1");
  123. Assert.AreEqual (cwd, swr.GetAppPathTranslated (), "TRANS1");
  124. Assert.AreEqual ("/appVirtualDir/file.aspx", swr.GetFilePath (), "T2");
  125. Assert.AreEqual ("GET", swr.GetHttpVerbName (), "T3");
  126. Assert.AreEqual ("HTTP/1.0", swr.GetHttpVersion (), "T4");
  127. Assert.AreEqual ("127.0.0.1", swr.GetLocalAddress (), "T5");
  128. Assert.AreEqual (80, swr.GetLocalPort (), "T6");
  129. Assert.AreEqual ("querystring", swr.GetQueryString (), "T7");
  130. Assert.AreEqual ("127.0.0.1", swr.GetRemoteAddress (), "T8");
  131. Assert.AreEqual (0, swr.GetRemotePort (), "T9");
  132. Assert.AreEqual ("/appVirtualDir/file.aspx?querystring", swr.GetRawUrl (), "T10");
  133. Assert.AreEqual ("/appVirtualDir/file.aspx", swr.GetUriPath (), "T11");
  134. Assert.AreEqual ("0", swr.GetUserToken ().ToString (), "T12");
  135. Assert.AreEqual ("", swr.GetPathInfo (), "TRANS2");
  136. //
  137. // On windows:
  138. // \windows\microsoft.net\framework\v1.1.4322\Config\machine.config
  139. //
  140. Assert.AreEqual (true, swr.MachineConfigPath != null, "T14");
  141. //
  142. // On windows:
  143. // \windows\microsoft.net\framework\v1.1.4322
  144. //
  145. Assert.AreEqual (true, swr.MachineInstallDirectory != null, "T15");
  146. Assert.AreEqual (Path.Combine (cwd, "file.aspx"), swr.GetFilePathTranslated (), "T16");
  147. Assert.AreEqual ("", swr.GetServerVariable ("AUTH_TYPE"), "T18");
  148. Assert.AreEqual ("", swr.GetServerVariable ("AUTH_USER"), "T19");
  149. Assert.AreEqual ("", swr.GetServerVariable ("REMOTE_USER"), "T20");
  150. Assert.AreEqual ("", swr.GetServerVariable ("TERVER_SOFTWARE"), "T21");
  151. Assert.AreEqual ("/appVirtualDir/file.aspx", swr.GetUriPath (), "T22");
  152. //
  153. // MapPath
  154. //
  155. Assert.AreEqual (Path.Combine (cwd, "file.aspx"), swr.MapPath ("/appVirtualDir/file.aspx"), "TP2");
  156. Assert.AreEqual (Path.Combine (cwd, "file.aspx"), swr.MapPath ("/appVirtualDir/file.aspx"), "TP4");
  157. Assert.AreEqual (Path.Combine (cwd, Path.Combine ("Subdir", "file.aspx")), swr.MapPath ("/appVirtualDir/Subdir/file.aspx"), "TP5");
  158. }
  159. public void Exception1 ()
  160. {
  161. StringWriter sw = new StringWriter ();
  162. SimpleWorkerRequest swr = new SimpleWorkerRequest("file.aspx", "querystring", sw);
  163. swr.MapPath ("x");
  164. }
  165. public void Exception2 ()
  166. {
  167. StringWriter sw = new StringWriter ();
  168. SimpleWorkerRequest swr = new SimpleWorkerRequest("file.aspx", "querystring", sw);
  169. swr.MapPath ("file.aspx");
  170. }
  171. public void Exception3 ()
  172. {
  173. StringWriter sw = new StringWriter ();
  174. SimpleWorkerRequest swr = new SimpleWorkerRequest("file.aspx", "querystring", sw);
  175. swr.MapPath ("appVirtualDir/file.aspx");
  176. }
  177. }
  178. Host MakeHost ()
  179. {
  180. return (Host) ApplicationHost.CreateApplicationHost (typeof (Host), "/appVirtualDir", cwd);
  181. }
  182. [Test] public void AppDomainTests ()
  183. {
  184. Host h = MakeHost ();
  185. h.Demo ();
  186. }
  187. [Test]
  188. [ExpectedException(typeof(ArgumentNullException))]
  189. public void AppDomain_MapPath1 ()
  190. {
  191. Host h = MakeHost ();
  192. h.Exception1 ();
  193. }
  194. [Test]
  195. [ExpectedException(typeof(ArgumentNullException))]
  196. public void AppDomain_MapPath2 ()
  197. {
  198. Host h = MakeHost ();
  199. h.Exception2 ();
  200. }
  201. [Test]
  202. [ExpectedException(typeof(ArgumentNullException))]
  203. public void AppDomain_MapPath3 ()
  204. {
  205. Host h = MakeHost ();
  206. h.Exception3 ();
  207. }
  208. //
  209. // This tests the constructor when the target application domain is created with
  210. // CreateApplicationHost
  211. //
  212. [Test] public void ConstructorTest_CreateApplicationHost ()
  213. {
  214. // Does not work without a NRE, need to call CreateApplicationHost.
  215. // = new SimpleWorkerRequest ("pageVirtualPath", "querystring", sw);
  216. // Assert.AreEqual ("querystring", swr.GetQueryString ());
  217. }
  218. }
  219. }