Fixing an issue with output parameters that are of type IntPtr
[castle.git] / MonoRail / Castle.MonoRail.Framework / Test / MockResponse.cs
blob8fe96d62188956544f1a4577ff25710cb7317ab4
1 // Copyright 2004-2008 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.Generic;
19 using System.Collections.Specialized;
20 using System.IO;
21 using System.Web;
22 using Internal;
23 using Routing;
24 using Services;
26 /// <summary>
27 /// Represents a mock implementation of <see cref="IMockResponse"/> for unit test purposes.
28 /// </summary>
29 public class MockResponse : BaseResponse, IMockResponse
31 private readonly IDictionary<string, HttpCookie> cookies;
32 private int statusCode = 200;
33 private string statusDescription = "OK";
34 private string contentType = "text/html";
35 private string cacheControlHeader;
36 private string charset = "ISO-8859-1";
37 private string redirectedTo;
38 private bool wasRedirected = false;
39 private bool isClientConnected = false;
40 private StringWriter output;
41 private HttpCachePolicy cachePolicy;
42 private NameValueCollection headers = new NameValueCollection();
44 /// <summary>
45 /// Initializes a new instance of the <see cref="MockResponse"/> class.
46 /// </summary>
47 /// <param name="currentUrl">The current URL.</param>
48 /// <param name="urlBuilder">The URL builder.</param>
49 /// <param name="serverUtility">The server utility.</param>
50 /// <param name="routeMatch">The route match.</param>
51 /// <param name="referrer">The referrer.</param>
52 public MockResponse(UrlInfo currentUrl, IUrlBuilder urlBuilder, IServerUtility serverUtility, RouteMatch routeMatch, string referrer)
53 : base(currentUrl, urlBuilder, serverUtility, routeMatch, referrer)
57 /// <summary>
58 /// Initializes a new instance of the <see cref="MockResponse"/> class.
59 /// </summary>
60 /// <param name="currentUrl">The current URL.</param>
61 /// <param name="urlBuilder">The URL builder.</param>
62 /// <param name="serverUtility">The server utility.</param>
63 /// <param name="routeMatch">The route match.</param>
64 public MockResponse(UrlInfo currentUrl, IUrlBuilder urlBuilder, IServerUtility serverUtility, RouteMatch routeMatch)
65 : this(currentUrl, urlBuilder, serverUtility, routeMatch, null)
69 /// <summary>
70 /// Initializes a new instance of the <see cref="MockResponse"/> class.
71 /// </summary>
72 /// <param name="cookies">The cookies.</param>
73 /// <param name="info">Current url</param>
74 public MockResponse(IDictionary<string, HttpCookie> cookies, UrlInfo info): this(
75 info, new DefaultUrlBuilder(), new MockServerUtility(), new RouteMatch())
77 this.cookies = cookies;
78 output = new StringWriter();
81 /// <summary>
82 /// Initializes a new instance of the <see cref="MockResponse"/> class.
83 /// </summary>
84 /// <param name="cookies">The cookies.</param>
85 public MockResponse(IDictionary<string, HttpCookie> cookies)
86 : this(cookies, new UrlInfo("", "controller", "action", "/", ".castle"))
90 /// <summary>
91 /// Initializes a new instance of the <see cref="MockResponse"/> class.
92 /// </summary>
93 public MockResponse(): this(new Dictionary<string, HttpCookie>(StringComparer.InvariantCultureIgnoreCase))
97 /// <summary>
98 /// Gets the urls the request was redirected to.
99 /// </summary>
100 /// <value>The redirected to.</value>
101 public override string RedirectedTo
103 get { return redirectedTo; }
106 // /// <summary>
107 // /// Gets the http headers.
108 // /// </summary>
109 // /// <value>The headers.</value>
110 // public NameValueCollection Headers
111 // {
112 // get { return headers; }
113 // }
115 #region IResponse Related
117 /// <summary>
118 /// Appends the header.
119 /// </summary>
120 /// <param name="name">The name.</param>
121 /// <param name="value">The value.</param>
122 public override void AppendHeader(string name, string value)
124 headers[name] = value;
127 /// <summary>
128 /// Writes the buffer to the browser
129 /// </summary>
130 /// <param name="buffer">The buffer.</param>
131 public override void BinaryWrite(byte[] buffer)
133 // output.Write(buffer, 0, buffer.Length);
136 /// <summary>
137 /// Writes the stream to the browser
138 /// </summary>
139 /// <param name="stream">The stream.</param>
140 public override void BinaryWrite(Stream stream)
142 byte[] buffer = new byte[stream.Length];
144 stream.Read(buffer, 0, buffer.Length);
146 BinaryWrite(buffer);
149 /// <summary>
150 /// Clears the response (only works if buffered)
151 /// </summary>
152 public override void Clear()
154 output.GetStringBuilder().Length = 0;
157 /// <summary>
158 /// Clears the response content (only works if buffered).
159 /// </summary>
160 public override void ClearContent()
164 /// <summary>
165 /// Writes the specified string.
166 /// </summary>
167 /// <param name="s">The string.</param>
168 public override void Write(string s)
170 output.Write(s);
173 /// <summary>
174 /// Writes the specified obj.
175 /// </summary>
176 /// <param name="obj">The obj.</param>
177 public override void Write(object obj)
179 output.Write(obj);
182 /// <summary>
183 /// Writes the specified char.
184 /// </summary>
185 /// <param name="ch">The char.</param>
186 public override void Write(char ch)
188 output.Write(ch);
191 /// <summary>
192 /// Writes the specified buffer.
193 /// </summary>
194 /// <param name="buffer">The buffer.</param>
195 /// <param name="index">The index.</param>
196 /// <param name="count">The count.</param>
197 public override void Write(char[] buffer, int index, int count)
199 output.Write(buffer, index, count);
202 /// <summary>
203 /// Writes the file.
204 /// </summary>
205 /// <param name="fileName">Name of the file.</param>
206 public override void WriteFile(string fileName)
208 // TODO: record file name
211 /// <summary>
212 /// Redirects to the specified url
213 /// </summary>
214 /// <param name="url">An relative or absolute URL to redirect the client to</param>
215 /// <param name="endProcess">if set to <c>true</c>, sends the redirect and
216 /// kills the current request process.</param>
217 public override void RedirectToUrl(string url, bool endProcess)
219 wasRedirected = true;
220 redirectedTo = url;
223 /// <summary>
224 /// Creates a http cookie.
225 /// </summary>
226 /// <param name="cookie">The cookie.</param>
227 public override void CreateCookie(HttpCookie cookie)
229 if (cookie.Value == string.Empty)
231 cookies.Remove(cookie.Name);
233 else
235 cookies[cookie.Name] = cookie;
239 /// <summary>
240 /// Gets or sets the status code.
241 /// </summary>
242 /// <value>The status code.</value>
243 public override int StatusCode
245 get { return statusCode; }
246 set { statusCode = value; }
249 /// <summary>
250 /// Gets or sets the status description.
251 /// </summary>
252 /// <value>The status code.</value>
253 public override string StatusDescription
255 get { return statusDescription; }
256 set { statusDescription = value; }
259 /// <summary>
260 /// Gets or sets the type of the content.
261 /// </summary>
262 /// <value>The type of the content.</value>
263 public override string ContentType
265 get { return contentType; }
266 set { contentType = value; }
269 /// <summary>
270 /// Gets the caching policy (expiration time, privacy,
271 /// vary clauses) of a Web page.
272 /// </summary>
273 /// <value></value>
274 public override HttpCachePolicy CachePolicy
276 get { return cachePolicy; }
277 set { cachePolicy = value; }
280 /// <summary>
281 /// Sets the Cache-Control HTTP header to Public or Private.
282 /// </summary>
283 /// <value></value>
284 public override string CacheControlHeader
286 get { return cacheControlHeader; }
287 set { cacheControlHeader = value; }
290 /// <summary>
291 /// Gets or sets the HTTP character set of the output stream.
292 /// </summary>
293 /// <value></value>
294 public override string Charset
296 get { return charset; }
297 set { charset = value; }
300 /// <summary>
301 /// Gets the output.
302 /// </summary>
303 /// <value>The output.</value>
304 public override TextWriter Output
306 get { return output; }
307 set { output = (StringWriter) value; }
310 /// <summary>
311 /// Gets the output stream.
312 /// </summary>
313 /// <value>The output stream.</value>
314 public override Stream OutputStream
316 get { return new MemoryStream(); }
319 /// <summary>
320 /// Gets a value indicating whether the response sent a redirect.
321 /// </summary>
322 /// <value><c>true</c> if was redirected; otherwise, <c>false</c>.</value>
323 public override bool WasRedirected
325 get { return wasRedirected; }
328 /// <summary>
329 /// Gets a value indicating whether this instance is client connected.
330 /// </summary>
331 /// <value>
332 /// <c>true</c> if this instance is client connected; otherwise, <c>false</c>.
333 /// </value>
334 public override bool IsClientConnected
336 get { return isClientConnected; }
339 /// <summary>
340 /// Gets the output.
341 /// </summary>
342 /// <value>The output.</value>
343 public override string OutputContent
345 get { return output.GetStringBuilder().ToString(); }
348 #endregion