2
0

SimpleWorkerRequestTest.cs 11 KB

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