2
0

SimpleWorkerRequestTest.cs 12 KB

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