SimpleWorkerRequestTest.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. sw = new StringWriter ();
  61. string ppath;
  62. swr = new SimpleWorkerRequest ("/appVirtualDir", cwd, "pageVirtualPath", "querystring", sw);
  63. Assert.AreEqual ("/appVirtualDir", swr.GetAppPath (), "S1");
  64. Assert.AreEqual ("/appVirtualDir/pageVirtualPath", swr.GetFilePath (), "S2");
  65. Assert.AreEqual ("GET", swr.GetHttpVerbName (), "S3");
  66. Assert.AreEqual ("HTTP/1.0", swr.GetHttpVersion (), "S4");
  67. Assert.AreEqual ("127.0.0.1", swr.GetLocalAddress (), "S5");
  68. Assert.AreEqual (80, swr.GetLocalPort (), "S6");
  69. Assert.AreEqual ("querystring", swr.GetQueryString (), "S7");
  70. Assert.AreEqual ("127.0.0.1", swr.GetRemoteAddress (), "S8");
  71. Assert.AreEqual (0, swr.GetRemotePort (), "S9");
  72. Assert.AreEqual ("/appVirtualDir/pageVirtualPath?querystring", swr.GetRawUrl (), "S10");
  73. Assert.AreEqual ("/appVirtualDir/pageVirtualPath", swr.GetUriPath (), "S11");
  74. Assert.AreEqual ("0", swr.GetUserToken ().ToString (), "S12");
  75. Assert.AreEqual (null, swr.MapPath ("x"), "S13");
  76. Assert.AreEqual (null, swr.MachineConfigPath, "S14");
  77. Assert.AreEqual (null, swr.MachineInstallDirectory, "S15");
  78. Assert.AreEqual (Path.Combine (cwd, "pageVirtualPath"), swr.GetFilePathTranslated (), "S16");
  79. Assert.AreEqual ("", swr.GetServerVariable ("AUTH_TYPE"), "S18");
  80. Assert.AreEqual ("", swr.GetServerVariable ("AUTH_USER"), "S19");
  81. Assert.AreEqual ("", swr.GetServerVariable ("REMOTE_USER"), "S20");
  82. Assert.AreEqual ("", swr.GetServerVariable ("SERVER_SOFTWARE"), "S21");
  83. Assert.AreEqual ("/appVirtualDir/pageVirtualPath", swr.GetUriPath (), "S22");
  84. //
  85. // MapPath
  86. //
  87. Assert.AreEqual (null, swr.MapPath ("file.aspx"), "MP1");
  88. Assert.AreEqual (null, swr.MapPath ("/appVirtualDir/pageVirtualPath"), "MP2");
  89. Assert.AreEqual (null, swr.MapPath ("appVirtualDir/pageVirtualPath"), "MP3");
  90. Assert.AreEqual (null, swr.MapPath ("/appVirtualDir/pageVirtualPath/page.aspx"), "MP4");
  91. Assert.AreEqual (null, swr.MapPath ("/appVirtualDir/pageVirtualPath/Subdir"), "MP5");
  92. swr = new SimpleWorkerRequest ("/appDir", cwd, "/Something/page.aspx", "querystring", sw);
  93. //Assert.AreEqual ("c:\\tmp\\page.aspx", swr.GetFilePathTranslated (), "S17");
  94. //
  95. // GetUriPath tests, veredict: MS implementation is a bit fragile on this interface
  96. //
  97. swr = new SimpleWorkerRequest ("/appDir", cwd, "/page.aspx", null, sw);
  98. Assert.AreEqual ("/appDir//page.aspx", swr.GetUriPath (), "S23");
  99. swr = new SimpleWorkerRequest ("/appDir/", cwd, "/page.aspx", null, sw);
  100. Assert.AreEqual ("/appDir///page.aspx", swr.GetUriPath (), "S24");
  101. swr = new SimpleWorkerRequest ("/appDir/", cwd, "page.aspx", null, sw);
  102. Assert.AreEqual ("/appDir//page.aspx", swr.GetUriPath (), "S25");
  103. swr = new SimpleWorkerRequest ("/appDir", cwd, "page.aspx", null, sw);
  104. Assert.AreEqual ("/appDir/page.aspx", swr.GetUriPath (), "S26");
  105. }
  106. public class Host : MarshalByRefObject {
  107. string cwd = Environment.CurrentDirectory;
  108. public void Demo ()
  109. {
  110. StringWriter sw = new StringWriter ();
  111. SimpleWorkerRequest swr = new SimpleWorkerRequest("file.aspx", "querystring", sw);
  112. Assert.AreEqual ("/appVirtualDir", swr.GetAppPath (), "T1");
  113. Assert.AreEqual (cwd, swr.GetAppPathTranslated (), "TRANS1");
  114. Assert.AreEqual ("/appVirtualDir/file.aspx", swr.GetFilePath (), "T2");
  115. Assert.AreEqual ("GET", swr.GetHttpVerbName (), "T3");
  116. Assert.AreEqual ("HTTP/1.0", swr.GetHttpVersion (), "T4");
  117. Assert.AreEqual ("127.0.0.1", swr.GetLocalAddress (), "T5");
  118. Assert.AreEqual (80, swr.GetLocalPort (), "T6");
  119. Assert.AreEqual ("querystring", swr.GetQueryString (), "T7");
  120. Assert.AreEqual ("127.0.0.1", swr.GetRemoteAddress (), "T8");
  121. Assert.AreEqual (0, swr.GetRemotePort (), "T9");
  122. Assert.AreEqual ("/appVirtualDir/file.aspx?querystring", swr.GetRawUrl (), "T10");
  123. Assert.AreEqual ("/appVirtualDir/file.aspx", swr.GetUriPath (), "T11");
  124. Assert.AreEqual ("0", swr.GetUserToken ().ToString (), "T12");
  125. Assert.AreEqual ("", swr.GetPathInfo (), "TRANS2");
  126. //
  127. // On windows:
  128. // \windows\microsoft.net\framework\v1.1.4322\Config\machine.config
  129. //
  130. Assert.AreEqual (true, swr.MachineConfigPath != null, "T14");
  131. //
  132. // On windows:
  133. // \windows\microsoft.net\framework\v1.1.4322
  134. //
  135. Assert.AreEqual (true, swr.MachineInstallDirectory != null, "T15");
  136. Assert.AreEqual (Path.Combine (cwd, "file.aspx"), swr.GetFilePathTranslated (), "T16");
  137. Assert.AreEqual ("", swr.GetServerVariable ("AUTH_TYPE"), "T18");
  138. Assert.AreEqual ("", swr.GetServerVariable ("AUTH_USER"), "T19");
  139. Assert.AreEqual ("", swr.GetServerVariable ("REMOTE_USER"), "T20");
  140. Assert.AreEqual ("", swr.GetServerVariable ("TERVER_SOFTWARE"), "T21");
  141. Assert.AreEqual ("/appVirtualDir/file.aspx", swr.GetUriPath (), "T22");
  142. //
  143. // MapPath
  144. //
  145. Assert.AreEqual (Path.Combine (cwd, "file.aspx"), swr.MapPath ("/appVirtualDir/file.aspx"), "TP2");
  146. Assert.AreEqual (Path.Combine (cwd, "file.aspx"), swr.MapPath ("/appVirtualDir/file.aspx"), "TP4");
  147. Console.WriteLine ("5. HOLA");
  148. Assert.AreEqual (Path.Combine (cwd, Path.Combine ("Subdir", "file.aspx")), swr.MapPath ("/appVirtualDir/Subdir/file.aspx"), "TP5");
  149. }
  150. public void Exception1 ()
  151. {
  152. StringWriter sw = new StringWriter ();
  153. SimpleWorkerRequest swr = new SimpleWorkerRequest("file.aspx", "querystring", sw);
  154. swr.MapPath ("x");
  155. }
  156. public void Exception2 ()
  157. {
  158. StringWriter sw = new StringWriter ();
  159. SimpleWorkerRequest swr = new SimpleWorkerRequest("file.aspx", "querystring", sw);
  160. swr.MapPath ("file.aspx");
  161. }
  162. public void Exception3 ()
  163. {
  164. StringWriter sw = new StringWriter ();
  165. SimpleWorkerRequest swr = new SimpleWorkerRequest("file.aspx", "querystring", sw);
  166. swr.MapPath ("appVirtualDir/file.aspx");
  167. }
  168. }
  169. Host MakeHost ()
  170. {
  171. return (Host) ApplicationHost.CreateApplicationHost (typeof (Host), "/appVirtualDir", cwd);
  172. }
  173. [Test] public void AppDomainTests ()
  174. {
  175. Host h = MakeHost ();
  176. h.Demo ();
  177. }
  178. [Test]
  179. [ExpectedException(typeof(ArgumentNullException))]
  180. public void AppDomain_MapPath1 ()
  181. {
  182. Host h = MakeHost ();
  183. h.Exception1 ();
  184. }
  185. [Test]
  186. [ExpectedException(typeof(ArgumentNullException))]
  187. public void AppDomain_MapPath2 ()
  188. {
  189. Host h = MakeHost ();
  190. h.Exception2 ();
  191. }
  192. [Test]
  193. [ExpectedException(typeof(ArgumentNullException))]
  194. public void AppDomain_MapPath3 ()
  195. {
  196. Host h = MakeHost ();
  197. h.Exception3 ();
  198. }
  199. //
  200. // This tests the constructor when the target application domain is created with
  201. // CreateApplicationHost
  202. //
  203. [Test] public void ConstructorTest_CreateApplicationHost ()
  204. {
  205. // Does not work without a NRE, need to call CreateApplicationHost.
  206. // = new SimpleWorkerRequest ("pageVirtualPath", "querystring", sw);
  207. // Assert.AreEqual ("querystring", swr.GetQueryString ());
  208. }
  209. }
  210. }