Fix the build.
[castle.git] / MonoRail / Castle.MonoRail.Framework / Test / MockResponse.cs
blob9571bb629da4a4abc5f71fff57039148cc5128ce
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MonoRail.Framework.Test
17 using System;
18 using System.Collections;
19 using System.Collections.Specialized;
20 using System.IO;
21 using System.Web;
22 using Internal;
24 /// <summary>
25 /// Represents a mock implementation of <see cref="IMockResponse"/> for unit test purposes.
26 /// </summary>
27 public class MockResponse : IMockResponse
29 private readonly IDictionary cookies;
30 private int statusCode = 400;
31 private string contentType = "text/html";
32 private string cacheControlHeader = null;
33 private string charset = "ISO-8859-1";
34 private string redirectedTo;
35 private bool wasRedirected = false;
36 private bool isClientConnected = false;
37 private TextWriter output = new StringWriter();
38 private Stream outputStream = new MemoryStream();
39 private TextWriter outputStreamWriter;
40 private HttpCachePolicy cachePolicy = null;
41 private NameValueCollection headers = new NameValueCollection();
43 /// <summary>
44 /// Initializes a new instance of the <see cref="MockResponse"/> class.
45 /// </summary>
46 /// <param name="cookies">The cookies.</param>
47 public MockResponse(IDictionary cookies)
49 this.cookies = cookies;
50 outputStreamWriter = new StreamWriter(outputStream);
53 /// <summary>
54 /// Gets the urls the request was redirected to.
55 /// </summary>
56 /// <value>The redirected to.</value>
57 public virtual string RedirectedTo
59 get { return redirectedTo; }
62 /// <summary>
63 /// Gets the http headers.
64 /// </summary>
65 /// <value>The headers.</value>
66 public virtual NameValueCollection Headers
68 get { return headers; }
71 #region IResponse Related
73 /// <summary>
74 /// Appends the header.
75 /// </summary>
76 /// <param name="name">The name.</param>
77 /// <param name="value">The value.</param>
78 public void AppendHeader(string name, string value)
80 headers[name] = value;
83 /// <summary>
84 /// Writes the buffer to the browser
85 /// </summary>
86 /// <param name="buffer">The buffer.</param>
87 public virtual void BinaryWrite(byte[] buffer)
89 outputStream.Write(buffer, 0, buffer.Length);
92 /// <summary>
93 /// Writes the stream to the browser
94 /// </summary>
95 /// <param name="stream">The stream.</param>
96 public virtual void BinaryWrite(Stream stream)
98 byte[] buffer = new byte[stream.Length];
100 stream.Read(buffer, 0, buffer.Length);
102 BinaryWrite(buffer);
105 /// <summary>
106 /// Clears the response (only works if buffered)
107 /// </summary>
108 public virtual void Clear()
110 outputStream.SetLength(0);
113 /// <summary>
114 /// Clears the response content (only works if buffered).
115 /// </summary>
116 public virtual void ClearContent()
118 outputStreamWriter.Flush();
121 /// <summary>
122 /// Writes the specified string.
123 /// </summary>
124 /// <param name="s">The string.</param>
125 public virtual void Write(string s)
127 outputStreamWriter.Write(s);
130 /// <summary>
131 /// Writes the specified obj.
132 /// </summary>
133 /// <param name="obj">The obj.</param>
134 public virtual void Write(object obj)
136 outputStreamWriter.Write(obj);
139 /// <summary>
140 /// Writes the specified char.
141 /// </summary>
142 /// <param name="ch">The char.</param>
143 public virtual void Write(char ch)
145 outputStreamWriter.Write(ch);
148 /// <summary>
149 /// Writes the specified buffer.
150 /// </summary>
151 /// <param name="buffer">The buffer.</param>
152 /// <param name="index">The index.</param>
153 /// <param name="count">The count.</param>
154 public virtual void Write(char[] buffer, int index, int count)
156 outputStreamWriter.Write(buffer, index, count);
159 /// <summary>
160 /// Writes the file.
161 /// </summary>
162 /// <param name="fileName">Name of the file.</param>
163 public virtual void WriteFile(string fileName)
165 throw new NotImplementedException();
168 /// <summary>
169 /// Redirects the specified controller.
170 /// </summary>
171 /// <param name="controller">The controller.</param>
172 /// <param name="action">The action.</param>
173 public virtual void Redirect(string controller, string action)
175 Redirect(BuildMockUrl(null, controller, action));
178 /// <summary>
179 /// Redirects the specified area.
180 /// </summary>
181 /// <param name="area">The area.</param>
182 /// <param name="controller">The controller.</param>
183 /// <param name="action">The action.</param>
184 public virtual void Redirect(string area, string controller, string action)
186 Redirect(BuildMockUrl(area, controller, action));
189 /// <summary>
190 /// Redirects to another controller and action with the specified paramters.
191 /// </summary>
192 /// <param name="controller">Controller name</param>
193 /// <param name="action">Action name</param>
194 /// <param name="parameters">Key/value pairings</param>
195 public void Redirect(string controller, string action, NameValueCollection parameters)
197 Redirect(BuildMockUrl(controller, action, parameters));
200 /// <summary>
201 /// Redirects to another controller and action with the specified paramters.
202 /// </summary>
203 /// <param name="area">Area name</param>
204 /// <param name="controller">Controller name</param>
205 /// <param name="action">Action name</param>
206 /// <param name="parameters">Key/value pairings</param>
207 public void Redirect(string area, string controller, string action, NameValueCollection parameters)
209 Redirect(BuildMockUrl(area, controller, action, parameters));
212 /// <summary>
213 /// Redirects to another controller and action with the specified paramters.
214 /// </summary>
215 /// <param name="controller">Controller name</param>
216 /// <param name="action">Action name</param>
217 /// <param name="parameters">Key/value pairings</param>
218 public void Redirect(string controller, string action, IDictionary parameters)
220 Redirect(BuildMockUrl(controller, action, parameters));
223 /// <summary>
224 /// Redirects to another controller and action with the specified paramters.
225 /// </summary>
226 /// <param name="area">Area name</param>
227 /// <param name="controller">Controller name</param>
228 /// <param name="action">Action name</param>
229 /// <param name="parameters">Key/value pairings</param>
230 public void Redirect(string area, string controller, string action, IDictionary parameters)
232 Redirect(BuildMockUrl(area, controller, action, parameters));
235 /// <summary>
236 /// Redirects the specified URL.
237 /// </summary>
238 /// <param name="url">The URL.</param>
239 public virtual void Redirect(string url)
241 wasRedirected = true;
242 redirectedTo = url;
245 /// <summary>
246 /// Redirects the specified URL.
247 /// </summary>
248 /// <param name="url">The URL.</param>
249 /// <param name="endProcess">if set to <c>true</c> [end process].</param>
250 public virtual void Redirect(string url, bool endProcess)
252 Redirect(url);
255 /// <summary>
256 /// Creates a cookie.
257 /// </summary>
258 /// <param name="name">The name.</param>
259 /// <param name="value">The value.</param>
260 public virtual void CreateCookie(string name, string value)
262 cookies.Add(name, value);
265 /// <summary>
266 /// Creates a cookie.
267 /// </summary>
268 /// <param name="name">The name.</param>
269 /// <param name="value">The value.</param>
270 /// <param name="expiration">The expiration.</param>
271 public virtual void CreateCookie(string name, string value, DateTime expiration)
273 CreateCookie(name, value);
276 /// <summary>
277 /// Creates a cookie.
278 /// </summary>
279 /// <param name="cookie">The cookie.</param>
280 public virtual void CreateCookie(HttpCookie cookie)
282 throw new NotSupportedException();
285 /// <summary>
286 /// Removes a cookie.
287 /// </summary>
288 /// <param name="name">The name.</param>
289 public virtual void RemoveCookie(string name)
291 cookies.Remove(name);
294 /// <summary>
295 /// Gets or sets the status code.
296 /// </summary>
297 /// <value>The status code.</value>
298 public int StatusCode
300 get { return statusCode; }
301 set { statusCode = value; }
304 /// <summary>
305 /// Gets or sets the type of the content.
306 /// </summary>
307 /// <value>The type of the content.</value>
308 public string ContentType
310 get { return contentType; }
311 set { contentType = value; }
314 /// <summary>
315 /// Gets the caching policy (expiration time, privacy,
316 /// vary clauses) of a Web page.
317 /// </summary>
318 /// <value></value>
319 public HttpCachePolicy CachePolicy
321 get { return cachePolicy; }
324 /// <summary>
325 /// Sets the Cache-Control HTTP header to Public or Private.
326 /// </summary>
327 /// <value></value>
328 public string CacheControlHeader
330 get { return cacheControlHeader; }
331 set { cacheControlHeader = value; }
334 /// <summary>
335 /// Gets or sets the HTTP character set of the output stream.
336 /// </summary>
337 /// <value></value>
338 public string Charset
340 get { return charset; }
341 set { charset = value; }
344 /// <summary>
345 /// Gets the output.
346 /// </summary>
347 /// <value>The output.</value>
348 public virtual TextWriter Output
350 get { return output; }
353 /// <summary>
354 /// Gets the output stream.
355 /// </summary>
356 /// <value>The output stream.</value>
357 public virtual Stream OutputStream
359 get { return outputStream; }
362 /// <summary>
363 /// Gets a value indicating whether the response sent a redirect.
364 /// </summary>
365 /// <value><c>true</c> if was redirected; otherwise, <c>false</c>.</value>
366 public virtual bool WasRedirected
368 get { return wasRedirected; }
371 /// <summary>
372 /// Gets a value indicating whether this instance is client connected.
373 /// </summary>
374 /// <value>
375 /// <c>true</c> if this instance is client connected; otherwise, <c>false</c>.
376 /// </value>
377 public virtual bool IsClientConnected
379 get { return isClientConnected; }
382 #endregion
384 private static string BuildMockUrl(string area, string controller, string action, string querystring)
386 string mockUrl = "/";
388 if (area != null)
390 mockUrl += area + "/";
393 mockUrl += controller + "/" + action + ".rails";
395 if (querystring != null)
397 mockUrl += "?" + querystring;
400 return mockUrl;
403 private static string BuildMockUrl(string area, string controller, string action)
405 return BuildMockUrl(area, controller, action, (string) null);
408 private static string BuildMockUrl(string area, string controller, string action, IDictionary parameters)
410 return BuildMockUrl(area, controller, action, ToQueryString(parameters));
413 private static string BuildMockUrl(string controller, string action, IDictionary parameters)
415 return BuildMockUrl(controller, action, ToQueryString(parameters));
418 private static string BuildMockUrl(string area, string controller, string action, NameValueCollection parameters)
420 return BuildMockUrl(area, controller, action, ToQueryString(parameters));
423 private static string BuildMockUrl(string controller, string action, NameValueCollection parameters)
425 return BuildMockUrl(controller, action, ToQueryString(parameters));
428 /// <summary>
429 /// Creates a querystring string representation of the namevalue collection.
430 /// </summary>
431 /// <param name="parameters">The parameters.</param>
432 /// <returns></returns>
433 private static string ToQueryString(NameValueCollection parameters)
435 return CommonUtils.BuildQueryString(new MockServerUtility(), parameters, false);
438 /// <summary>
439 /// Creates a querystring string representation of the entries in the dictionary.
440 /// </summary>
441 /// <param name="parameters">The parameters.</param>
442 /// <returns></returns>
443 private static string ToQueryString(IDictionary parameters)
445 return CommonUtils.BuildQueryString(new MockServerUtility(), parameters, false);