/src/Manos/Manos.Http.Testing/MockHttpResponse.cs

http://github.com/jacksonh/manos · C# · 254 lines · 200 code · 54 blank · 0 comment · 9 complexity · 856b863469f45cc463aa011894922155 MD5 · raw file

  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using System.Collections.Generic;
  5. using Manos.Http;
  6. using Manos.IO;
  7. namespace Manos.Http.Testing
  8. {
  9. public class MockHttpResponse : IHttpResponse
  10. {
  11. StringBuilder builder = new StringBuilder ();
  12. Dictionary<string, HttpCookie> cookies = new Dictionary<string, HttpCookie> ();
  13. public MockHttpResponse ()
  14. {
  15. this.Headers = new HttpHeaders ();
  16. Properties = new Dictionary<string,object> ();
  17. }
  18. public void Dispose ()
  19. {
  20. }
  21. public IHttpTransaction Transaction { get; set; }
  22. public HttpHeaders Headers { get; set; }
  23. public HttpStream Stream {
  24. get {
  25. throw new NotImplementedException ();
  26. }
  27. }
  28. public String ResponseString()
  29. {
  30. return this.builder.ToString();
  31. }
  32. public StreamWriter Writer { get{throw new NotImplementedException();} }
  33. public Encoding ContentEncoding {
  34. get { return Headers.ContentEncoding; }
  35. set { Headers.ContentEncoding = value; }
  36. }
  37. public int StatusCode { get; set; }
  38. public bool WriteHeaders { get; set; }
  39. public void Write (string str)
  40. {
  41. this.builder.Append(str);
  42. }
  43. public void Write (string str, params object[] prms)
  44. {
  45. this.builder.AppendFormat (str, prms);
  46. }
  47. public void WriteLine (string str)
  48. {
  49. this.builder.AppendLine (str);
  50. }
  51. public void WriteLine (string str, params object[] prms)
  52. {
  53. this.builder.AppendLine(String.Format(str, prms));
  54. }
  55. public void End ()
  56. {
  57. }
  58. public void End (string str)
  59. {
  60. this.Write (str);
  61. }
  62. public void End (byte[] data)
  63. {
  64. this.Write (data);
  65. }
  66. public void End (string str, params object[] prms)
  67. {
  68. this.Write (str, prms);
  69. }
  70. public void Write (byte[] data)
  71. {
  72. throw new NotImplementedException();
  73. }
  74. public void Write (byte [] data, int offset, int length)
  75. {
  76. throw new NotImplementedException ();
  77. }
  78. public void End (byte [] data, int offset, int length)
  79. {
  80. throw new NotImplementedException ();
  81. }
  82. public void SendFile (string file)
  83. {
  84. SentFile = file;
  85. }
  86. public string SentFile
  87. {
  88. get;
  89. private set;
  90. }
  91. public string RedirectedUrl
  92. {
  93. get;
  94. private set;
  95. }
  96. public void Redirect (string url)
  97. {
  98. StatusCode = 302;
  99. RedirectedUrl = url;
  100. }
  101. public void Read ()
  102. {
  103. throw new NotImplementedException ();
  104. }
  105. public void SetHeader (string name, string value)
  106. {
  107. this.Headers.SetHeader (name, value);
  108. }
  109. public Dictionary<string,HttpCookie> Cookies {
  110. get { return cookies; }
  111. }
  112. public void SetCookie (string name, HttpCookie cookie)
  113. {
  114. cookies [name] = cookie;
  115. }
  116. public HttpCookie SetCookie (string name, string value)
  117. {
  118. var cookie = new HttpCookie (name, value);
  119. SetCookie (name, cookie);
  120. return cookie;
  121. }
  122. public HttpCookie SetCookie (string name, string value, string domain)
  123. {
  124. var cookie = new HttpCookie (name, value);
  125. cookie.Domain = domain;
  126. SetCookie (name, cookie);
  127. return cookie;
  128. }
  129. public HttpCookie SetCookie (string name, string value, DateTime expires)
  130. {
  131. return SetCookie (name, value, null, expires);
  132. }
  133. public HttpCookie SetCookie (string name, string value, string domain, DateTime expires)
  134. {
  135. var cookie = new HttpCookie (name, value);
  136. cookie.Domain = domain;
  137. cookie.Expires = expires;
  138. SetCookie (name, cookie);
  139. return cookie;
  140. }
  141. public HttpCookie SetCookie (string name, string value, TimeSpan max_age)
  142. {
  143. return SetCookie (name, value, DateTime.Now + max_age);
  144. }
  145. public HttpCookie SetCookie (string name, string value, string domain, TimeSpan max_age)
  146. {
  147. return SetCookie (name, value, domain, DateTime.Now + max_age);
  148. }
  149. public void RemoveCookie(string name)
  150. {
  151. var cookie = new HttpCookie (name, "");
  152. cookie.Expires = DateTime.Now.AddYears(-1);
  153. SetCookie (name, cookie);
  154. }
  155. public void Complete (Action callback)
  156. {
  157. }
  158. public Dictionary<string,object> Properties {
  159. get;
  160. set;
  161. }
  162. public string PostBody {
  163. get;
  164. set;
  165. }
  166. public void WriteMetadata (StringBuilder builder)
  167. {
  168. throw new NotImplementedException ();
  169. }
  170. public void SetProperty (string name, object o)
  171. {
  172. if (name == null)
  173. throw new ArgumentNullException ("name");
  174. if (o == null) {
  175. Properties.Remove (name);
  176. return;
  177. }
  178. Properties [name] = o;
  179. }
  180. public object GetProperty (string name)
  181. {
  182. if (name == null)
  183. throw new ArgumentNullException ("name");
  184. object res = null;
  185. if (Properties.TryGetValue (name, out res))
  186. return null;
  187. return res;
  188. }
  189. public T GetProperty<T> (string name)
  190. {
  191. object res = GetProperty (name);
  192. if (res == null)
  193. return default (T);
  194. return (T) res;
  195. }
  196. public event Action OnCompleted;
  197. public event Action<byte [], int, int> BodyData;
  198. public event Action OnEnd;
  199. }
  200. }