FakeHttpWorkerRequest.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. string queryString;
  39. public Hashtable KnownResponseHeaders;
  40. public Hashtable UnknownResponseHeaders;
  41. public int return_kind;
  42. public FakeHttpWorkerRequest ()
  43. : this (1)
  44. {
  45. }
  46. public FakeHttpWorkerRequest (int re)
  47. {
  48. KnownResponseHeaders = CollectionsUtil.CreateCaseInsensitiveHashtable ();
  49. UnknownResponseHeaders = CollectionsUtil.CreateCaseInsensitiveHashtable ();
  50. return_kind = re;
  51. queryString = String.Empty;
  52. }
  53. public override string GetUriPath ()
  54. {
  55. return "/fake";
  56. }
  57. public override string GetQueryString ()
  58. {
  59. return queryString;
  60. }
  61. public void SetQueryString (string queryString)
  62. {
  63. this.queryString = queryString;
  64. }
  65. public override string GetRawUrl ()
  66. {
  67. return "GetRawUrl";
  68. }
  69. public override string GetHttpVerbName ()
  70. {
  71. return "GET";
  72. }
  73. public override string GetHttpVersion ()
  74. {
  75. if (return_kind == 1)
  76. return "HTTP/1.0";
  77. else
  78. return "HTTP/1.1";
  79. }
  80. public override string GetRemoteAddress ()
  81. {
  82. return "__GetRemoteAddress";
  83. }
  84. public override int GetRemotePort ()
  85. {
  86. return 1010;
  87. }
  88. public override string GetLocalAddress ()
  89. {
  90. return "GetLocalAddress";
  91. }
  92. public override string GetAppPath ()
  93. {
  94. return "AppPath";
  95. }
  96. public override int GetLocalPort ()
  97. {
  98. return 2020;
  99. }
  100. public bool status_sent;
  101. public int status_code;
  102. public string status_string;
  103. public override void SendStatus (int s, string x)
  104. {
  105. status_sent = true;
  106. status_code = s;
  107. status_string = x;
  108. }
  109. void AddHeader (Hashtable table, string header_name, object header)
  110. {
  111. object o = table[header_name];
  112. if (o == null)
  113. table.Add (header_name, header);
  114. else {
  115. ArrayList al = o as ArrayList;
  116. if (al == null) {
  117. al = new ArrayList ();
  118. al.Add (o);
  119. table[header_name] = al;
  120. } else
  121. al = o as ArrayList;
  122. al.Add (header);
  123. }
  124. }
  125. bool headers_sent;
  126. public override void SendKnownResponseHeader (int x, string j)
  127. {
  128. string header_name = HttpWorkerRequest.GetKnownRequestHeaderName (x);
  129. AddHeader (KnownResponseHeaders, header_name, new KnownResponseHeader (x, j));
  130. headers_sent = true;
  131. }
  132. public override void SendUnknownResponseHeader (string a, string b)
  133. {
  134. AddHeader (UnknownResponseHeaders, a, new UnknownResponseHeader (a, b));
  135. headers_sent = true;
  136. }
  137. bool data_sent;
  138. public byte[] data;
  139. public int data_len;
  140. public int total = 0;
  141. public override void SendResponseFromMemory (byte[] arr, int x)
  142. {
  143. data_sent = true;
  144. data = new byte[x];
  145. for (int i = 0; i < x; i++)
  146. data[i] = arr[i];
  147. data_len = x;
  148. total += data_len;
  149. }
  150. public override void SendResponseFromFile (string a, long b, long c)
  151. {
  152. data_sent = true;
  153. }
  154. public override void SendResponseFromFile (IntPtr a, long b, long c)
  155. {
  156. data_sent = true;
  157. }
  158. public override void FlushResponse (bool x)
  159. {
  160. }
  161. public override void EndOfRequest ()
  162. {
  163. }
  164. public override string GetKnownRequestHeader (int index)
  165. {
  166. return null;
  167. }
  168. public bool OutputProduced
  169. {
  170. get
  171. {
  172. return headers_sent || data_sent;
  173. }
  174. }
  175. }
  176. }