1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
18 using System
.Collections
.Generic
;
19 using System
.Collections
.Specialized
;
27 /// Represents a mock implementation of <see cref="IMockResponse"/> for unit test purposes.
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();
45 /// Initializes a new instance of the <see cref="MockResponse"/> class.
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
)
58 /// Initializes a new instance of the <see cref="MockResponse"/> class.
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)
70 /// Initializes a new instance of the <see cref="MockResponse"/> class.
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();
82 /// Initializes a new instance of the <see cref="MockResponse"/> class.
84 /// <param name="cookies">The cookies.</param>
85 public MockResponse(IDictionary
<string, HttpCookie
> cookies
)
86 : this(cookies
, new UrlInfo("", "controller", "action", "/", ".castle"))
91 /// Initializes a new instance of the <see cref="MockResponse"/> class.
93 public MockResponse(): this(new Dictionary
<string, HttpCookie
>(StringComparer
.InvariantCultureIgnoreCase
))
98 /// Gets the urls the request was redirected to.
100 /// <value>The redirected to.</value>
101 public override string RedirectedTo
103 get { return redirectedTo; }
107 // /// Gets the http headers.
109 // /// <value>The headers.</value>
110 // public NameValueCollection Headers
112 // get { return headers; }
115 #region IResponse Related
118 /// Appends the header.
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;
128 /// Writes the buffer to the browser
130 /// <param name="buffer">The buffer.</param>
131 public override void BinaryWrite(byte[] buffer
)
133 // output.Write(buffer, 0, buffer.Length);
137 /// Writes the stream to the browser
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
);
150 /// Clears the response (only works if buffered)
152 public override void Clear()
154 output
.GetStringBuilder().Length
= 0;
158 /// Clears the response content (only works if buffered).
160 public override void ClearContent()
165 /// Writes the specified string.
167 /// <param name="s">The string.</param>
168 public override void Write(string s
)
174 /// Writes the specified obj.
176 /// <param name="obj">The obj.</param>
177 public override void Write(object obj
)
183 /// Writes the specified char.
185 /// <param name="ch">The char.</param>
186 public override void Write(char ch
)
192 /// Writes the specified buffer.
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
);
205 /// <param name="fileName">Name of the file.</param>
206 public override void WriteFile(string fileName
)
208 // TODO: record file name
212 /// Redirects to the specified url
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;
224 /// Creates a http cookie.
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
);
235 cookies
[cookie
.Name
] = cookie
;
240 /// Gets or sets the status code.
242 /// <value>The status code.</value>
243 public override int StatusCode
245 get { return statusCode; }
246 set { statusCode = value; }
250 /// Gets or sets the status description.
252 /// <value>The status code.</value>
253 public override string StatusDescription
255 get { return statusDescription; }
256 set { statusDescription = value; }
260 /// Gets or sets the type of the content.
262 /// <value>The type of the content.</value>
263 public override string ContentType
265 get { return contentType; }
266 set { contentType = value; }
270 /// Gets the caching policy (expiration time, privacy,
271 /// vary clauses) of a Web page.
274 public override HttpCachePolicy CachePolicy
276 get { return cachePolicy; }
277 set { cachePolicy = value; }
281 /// Sets the Cache-Control HTTP header to Public or Private.
284 public override string CacheControlHeader
286 get { return cacheControlHeader; }
287 set { cacheControlHeader = value; }
291 /// Gets or sets the HTTP character set of the output stream.
294 public override string Charset
296 get { return charset; }
297 set { charset = value; }
303 /// <value>The output.</value>
304 public override TextWriter Output
306 get { return output; }
307 set { output = (StringWriter) value; }
311 /// Gets the output stream.
313 /// <value>The output stream.</value>
314 public override Stream OutputStream
316 get { return new MemoryStream(); }
320 /// Gets a value indicating whether the response sent a redirect.
322 /// <value><c>true</c> if was redirected; otherwise, <c>false</c>.</value>
323 public override bool WasRedirected
325 get { return wasRedirected; }
329 /// Gets a value indicating whether this instance is client connected.
332 /// <c>true</c> if this instance is client connected; otherwise, <c>false</c>.
334 public override bool IsClientConnected
336 get { return isClientConnected; }
342 /// <value>The output.</value>
343 public override string OutputContent
345 get { return output.GetStringBuilder().ToString(); }