1 // Copyright 2004-2007 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
;
19 using System
.Collections
.Specialized
;
25 /// Represents a mock implementation of <see cref="IMockResponse"/> for unit test purposes.
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();
44 /// Initializes a new instance of the <see cref="MockResponse"/> class.
46 /// <param name="cookies">The cookies.</param>
47 public MockResponse(IDictionary cookies
)
49 this.cookies
= cookies
;
50 outputStreamWriter
= new StreamWriter(outputStream
);
54 /// Gets the urls the request was redirected to.
56 /// <value>The redirected to.</value>
57 public virtual string RedirectedTo
59 get { return redirectedTo; }
63 /// Gets the http headers.
65 /// <value>The headers.</value>
66 public virtual NameValueCollection Headers
68 get { return headers; }
71 #region IResponse Related
74 /// Appends the header.
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;
84 /// Writes the buffer to the browser
86 /// <param name="buffer">The buffer.</param>
87 public virtual void BinaryWrite(byte[] buffer
)
89 outputStream
.Write(buffer
, 0, buffer
.Length
);
93 /// Writes the stream to the browser
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
);
106 /// Clears the response (only works if buffered)
108 public virtual void Clear()
110 outputStream
.SetLength(0);
114 /// Clears the response content (only works if buffered).
116 public virtual void ClearContent()
118 outputStreamWriter
.Flush();
122 /// Writes the specified string.
124 /// <param name="s">The string.</param>
125 public virtual void Write(string s
)
127 outputStreamWriter
.Write(s
);
131 /// Writes the specified obj.
133 /// <param name="obj">The obj.</param>
134 public virtual void Write(object obj
)
136 outputStreamWriter
.Write(obj
);
140 /// Writes the specified char.
142 /// <param name="ch">The char.</param>
143 public virtual void Write(char ch
)
145 outputStreamWriter
.Write(ch
);
149 /// Writes the specified buffer.
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
);
162 /// <param name="fileName">Name of the file.</param>
163 public virtual void WriteFile(string fileName
)
165 throw new NotImplementedException();
169 /// Redirects the specified controller.
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
));
179 /// Redirects the specified area.
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
));
190 /// Redirects to another controller and action with the specified paramters.
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
));
201 /// Redirects to another controller and action with the specified paramters.
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
));
213 /// Redirects to another controller and action with the specified paramters.
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
));
224 /// Redirects to another controller and action with the specified paramters.
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
));
236 /// Redirects the specified URL.
238 /// <param name="url">The URL.</param>
239 public virtual void Redirect(string url
)
241 wasRedirected
= true;
246 /// Redirects the specified URL.
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
)
256 /// Creates a cookie.
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);
266 /// Creates a cookie.
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);
277 /// Creates a cookie.
279 /// <param name="cookie">The cookie.</param>
280 public virtual void CreateCookie(HttpCookie cookie
)
282 throw new NotSupportedException();
286 /// Removes a cookie.
288 /// <param name="name">The name.</param>
289 public virtual void RemoveCookie(string name
)
291 cookies
.Remove(name
);
295 /// Gets or sets the status code.
297 /// <value>The status code.</value>
298 public int StatusCode
300 get { return statusCode; }
301 set { statusCode = value; }
305 /// Gets or sets the type of the content.
307 /// <value>The type of the content.</value>
308 public string ContentType
310 get { return contentType; }
311 set { contentType = value; }
315 /// Gets the caching policy (expiration time, privacy,
316 /// vary clauses) of a Web page.
319 public HttpCachePolicy CachePolicy
321 get { return cachePolicy; }
325 /// Sets the Cache-Control HTTP header to Public or Private.
328 public string CacheControlHeader
330 get { return cacheControlHeader; }
331 set { cacheControlHeader = value; }
335 /// Gets or sets the HTTP character set of the output stream.
338 public string Charset
340 get { return charset; }
341 set { charset = value; }
347 /// <value>The output.</value>
348 public virtual TextWriter Output
350 get { return output; }
354 /// Gets the output stream.
356 /// <value>The output stream.</value>
357 public virtual Stream OutputStream
359 get { return outputStream; }
363 /// Gets a value indicating whether the response sent a redirect.
365 /// <value><c>true</c> if was redirected; otherwise, <c>false</c>.</value>
366 public virtual bool WasRedirected
368 get { return wasRedirected; }
372 /// Gets a value indicating whether this instance is client connected.
375 /// <c>true</c> if this instance is client connected; otherwise, <c>false</c>.
377 public virtual bool IsClientConnected
379 get { return isClientConnected; }
384 private static string BuildMockUrl(string area
, string controller
, string action
, string querystring
)
386 string mockUrl
= "/";
390 mockUrl
+= area
+ "/";
393 mockUrl
+= controller
+ "/" + action
+ ".rails";
395 if (querystring
!= null)
397 mockUrl
+= "?" + querystring
;
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
));
429 /// Creates a querystring string representation of the namevalue collection.
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);
439 /// Creates a querystring string representation of the entries in the dictionary.
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);