FakeHttpWorkerRequest.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //
  2. // System.Web.HttpResponseTest.cs - Unit tests for System.Web.HttpResponse
  3. //
  4. // Author:
  5. // Miguel de Icaza <[email protected]>
  6. //
  7. // Copyright (C) 2005-2009 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System.Text;
  29. using System.Web;
  30. using System;
  31. using System.Collections;
  32. using System.Collections.Specialized;
  33. using NUnit.Framework;
  34. namespace MonoTests.Common
  35. {
  36. public class FakeHttpWorkerRequest : HttpWorkerRequest
  37. {
  38. const string appPath = "/";
  39. string queryString;
  40. public Hashtable KnownResponseHeaders;
  41. public Hashtable UnknownResponseHeaders;
  42. public int return_kind;
  43. public FakeHttpWorkerRequest ()
  44. : this (1)
  45. {
  46. // for Mono
  47. AppDomain.CurrentDomain.SetData (".appVPath", appPath);
  48. }
  49. public FakeHttpWorkerRequest (int re)
  50. {
  51. KnownResponseHeaders = CollectionsUtil.CreateCaseInsensitiveHashtable ();
  52. UnknownResponseHeaders = CollectionsUtil.CreateCaseInsensitiveHashtable ();
  53. return_kind = re;
  54. queryString = String.Empty;
  55. }
  56. public override string GetUriPath ()
  57. {
  58. return appPath;
  59. }
  60. public override string GetFilePath ()
  61. {
  62. return GetUriPath ();
  63. }
  64. public override string GetQueryString ()
  65. {
  66. return queryString;
  67. }
  68. public void SetQueryString (string queryString)
  69. {
  70. this.queryString = queryString;
  71. }
  72. public override string GetRawUrl ()
  73. {
  74. return "GetRawUrl";
  75. }
  76. public override string GetHttpVerbName ()
  77. {
  78. return "GET";
  79. }
  80. public override string GetHttpVersion ()
  81. {
  82. if (return_kind == 1)
  83. return "HTTP/1.0";
  84. else
  85. return "HTTP/1.1";
  86. }
  87. public override string GetRemoteAddress ()
  88. {
  89. return "__GetRemoteAddress";
  90. }
  91. public override int GetRemotePort ()
  92. {
  93. return 1010;
  94. }
  95. public override string GetLocalAddress ()
  96. {
  97. return "GetLocalAddress";
  98. }
  99. public override string GetAppPath ()
  100. {
  101. return appPath;
  102. }
  103. public override int GetLocalPort ()
  104. {
  105. return 2020;
  106. }
  107. public override string MapPath (string virtualPath)
  108. {
  109. #if TARGET_DOTNET
  110. return "c:\\fakefile";
  111. #else
  112. return "/fakefile";
  113. #endif
  114. }
  115. public bool status_sent;
  116. public int status_code;
  117. public string status_string;
  118. public override void SendStatus (int s, string x)
  119. {
  120. status_sent = true;
  121. status_code = s;
  122. status_string = x;
  123. }
  124. void AddHeader (Hashtable table, string header_name, object header)
  125. {
  126. object o = table[header_name];
  127. if (o == null)
  128. table.Add (header_name, header);
  129. else {
  130. ArrayList al = o as ArrayList;
  131. if (al == null) {
  132. al = new ArrayList ();
  133. al.Add (o);
  134. table[header_name] = al;
  135. } else
  136. al = o as ArrayList;
  137. al.Add (header);
  138. }
  139. }
  140. bool headers_sent;
  141. public override void SendKnownResponseHeader (int x, string j)
  142. {
  143. string header_name = HttpWorkerRequest.GetKnownRequestHeaderName (x);
  144. AddHeader (KnownResponseHeaders, header_name, new KnownResponseHeader (x, j));
  145. headers_sent = true;
  146. }
  147. public override void SendUnknownResponseHeader (string a, string b)
  148. {
  149. AddHeader (UnknownResponseHeaders, a, new UnknownResponseHeader (a, b));
  150. headers_sent = true;
  151. }
  152. bool data_sent;
  153. public byte[] data;
  154. public int data_len;
  155. public int total = 0;
  156. public override void SendResponseFromMemory (byte[] arr, int x)
  157. {
  158. data_sent = true;
  159. data = new byte[x];
  160. for (int i = 0; i < x; i++)
  161. data[i] = arr[i];
  162. data_len = x;
  163. total += data_len;
  164. }
  165. public override void SendResponseFromFile (string a, long b, long c)
  166. {
  167. data_sent = true;
  168. }
  169. public override void SendResponseFromFile (IntPtr a, long b, long c)
  170. {
  171. data_sent = true;
  172. }
  173. public override void FlushResponse (bool x)
  174. {
  175. }
  176. public override void EndOfRequest ()
  177. {
  178. }
  179. public override string GetKnownRequestHeader (int index)
  180. {
  181. return null;
  182. }
  183. public bool OutputProduced
  184. {
  185. get
  186. {
  187. return headers_sent || data_sent;
  188. }
  189. }
  190. }
  191. }