PageRenderTime 264ms CodeModel.GetById 251ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

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