Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / MonoRail / Castle.MonoRail.Framework / Test / MockRequest.cs
blob0e5480dbfe96d3cae227c7c3d67ade08d9de3467
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;
19 using System.Collections.Generic;
20 using System.Collections.Specialized;
21 using System.Web;
22 using Castle.Components.Binder;
24 /// <summary>
25 /// Represents a mock implementation of <see cref="IRequest"/> for unit test purposes.
26 /// </summary>
27 public class MockRequest : IMockRequest
29 private NameValueCollection form = new NameValueCollection();
30 private NameValueCollection headers = new NameValueCollection();
31 private NameValueCollection queryString = new NameValueCollection();
32 private NameValueCollection @params = new NameValueCollection();
33 private string urlReferrer;
34 private IDictionary<string, HttpCookie> cookies;
35 private IDictionary files = new Hashtable();
36 private bool isLocal = true;
37 private string httpMethod = "GET";
38 private string[] userLanguages = new string[] {"en-ES", "pt-BR"};
39 private string rawUrl = null;
40 private string filePath = null;
41 private Uri uri = null;
42 private string userHostAddress = "127.0.0.1";
43 private string pathInfo;
45 /// <summary>
46 /// Initializes a new instance of the <see cref="MockRequest"/> class.
47 /// </summary>
48 /// <param name="cookies">The cookies.</param>
49 public MockRequest(IDictionary<string, HttpCookie> cookies)
51 this.cookies = cookies;
54 /// <summary>
55 /// Initializes a new instance of the <see cref="MockRequest"/> class.
56 /// </summary>
57 /// <param name="httpMethod">The HTTP method.</param>
58 public MockRequest(string httpMethod) : this()
60 this.httpMethod = httpMethod;
63 /// <summary>
64 /// Initializes a new instance of the <see cref="MockRequest"/> class.
65 /// </summary>
66 public MockRequest() : this(new Dictionary<string, HttpCookie>(StringComparer.InvariantCultureIgnoreCase))
70 /// <summary>
71 /// Gets or sets the accept header.
72 /// </summary>
73 /// <value>The accept header.</value>
74 public string AcceptHeader
76 get { return headers["Accept"]; }
77 set { headers["Accept"] = value; }
80 /// <summary>
81 /// Gets the referring URL.
82 /// </summary>
83 /// <value></value>
84 public string UrlReferrer
86 get { return urlReferrer; }
87 set { urlReferrer = value; }
90 // /// <summary>
91 // /// Reads the request data as a byte array.
92 // /// </summary>
93 // /// <param name="count">How many bytes.</param>
94 // /// <returns></returns>
95 // public virtual byte[] BinaryRead(int count)
96 // {
97 // throw new NotImplementedException();
98 // }
100 /// <summary>
101 /// Reads the cookie.
102 /// </summary>
103 /// <param name="name">The cookie name.</param>
104 /// <returns></returns>
105 public virtual string ReadCookie(string name)
107 HttpCookie cookie;
108 if (cookies.TryGetValue(name, out cookie))
110 return cookie.Value;
112 return null;
115 /// <summary>
116 /// Validates the input.
117 /// </summary>
118 public virtual void ValidateInput()
122 /// <summary>
123 /// Gets the Http headers.
124 /// </summary>
125 /// <value>The Http headers.</value>
126 public virtual NameValueCollection Headers
128 get { return headers; }
131 /// <summary>
132 /// Gets the <see cref="HttpPostedFile"/> per key.
133 /// </summary>
134 /// <value></value>
135 public virtual IDictionary Files
137 get { return files; }
140 /// <summary>
141 /// Gets a value indicating whether this requeest is from a local address.
142 /// </summary>
143 /// <value><c>true</c> if this instance is local; otherwise, <c>false</c>.</value>
144 public virtual bool IsLocal
146 get { return isLocal; }
147 set { isLocal = value; }
150 /// <summary>
151 /// Gets additional path information for
152 /// a resource with a URL extension.
153 /// </summary>
154 /// <value>The path info.</value>
155 public virtual string PathInfo
157 get { return pathInfo; }
158 set { pathInfo = value; }
161 /// <summary>
162 /// Gets the request type (GET, POST, etc)
163 /// </summary>
164 /// <value></value>
165 public string RequestType
167 get { return HttpMethod; }
170 /// <summary>
171 /// Gets the request URL.
172 /// </summary>
173 /// <value></value>
174 public string Url
176 get { return RawUrl; }
179 /// <summary>
180 /// Gets the raw URL.
181 /// </summary>
182 /// <value>The raw URL.</value>
183 public virtual string RawUrl
185 get { return rawUrl; }
186 set { rawUrl = value; }
189 /// <summary>
190 /// Gets the URI.
191 /// </summary>
192 /// <value>The URI.</value>
193 public virtual Uri Uri
195 get { return uri; }
196 set { uri = value; }
199 /// <summary>
200 /// Gets the HTTP method.
201 /// </summary>
202 /// <value>The HTTP method.</value>
203 public virtual string HttpMethod
205 get { return httpMethod; }
206 set { httpMethod = value; }
209 /// <summary>
210 /// Gets the file path.
211 /// </summary>
212 /// <value>The file path.</value>
213 public virtual string FilePath
215 get { return filePath; }
216 set { filePath = value; }
219 /// <summary>
220 /// Gets the param with the specified key.
221 /// </summary>
222 /// <value></value>
223 public virtual string this[string key]
225 get { return @params[key]; }
228 /// <summary>
229 /// Gets the params which accumulates headers, post, querystring and cookies.
230 /// </summary>
231 /// <value>The params.</value>
232 public virtual NameValueCollection Params
234 get { return @params; }
237 /// <summary>
238 /// Gets the query string.
239 /// </summary>
240 /// <value>The query string.</value>
241 public virtual NameValueCollection QueryString
243 get { return queryString; }
246 /// <summary>
247 /// Gets the form.
248 /// </summary>
249 /// <value>The form.</value>
250 public virtual NameValueCollection Form
252 get { return form; }
255 /// <summary>
256 /// Gets the user languages.
257 /// </summary>
258 /// <value>The user languages.</value>
259 public virtual string[] UserLanguages
261 get { return userLanguages; }
262 set { userLanguages = value; }
265 /// <summary>
266 /// Lazy initialized property with a hierarchical
267 /// representation of the flat data on <see cref="Controller.Params"/>
268 /// </summary>
269 /// <value></value>
270 public CompositeNode ParamsNode
272 get { return new TreeBuilder().BuildSourceNode(Params); }
275 /// <summary>
276 /// Lazy initialized property with a hierarchical
277 /// representation of the flat data on <see cref="IRequest.Form"/>
278 /// </summary>
279 /// <value></value>
280 public CompositeNode FormNode
282 get { return new TreeBuilder().BuildSourceNode(Form); }
285 /// <summary>
286 /// Lazy initialized property with a hierarchical
287 /// representation of the flat data on <see cref="IRequest.QueryString"/>
288 /// </summary>
289 /// <value>The query string node.</value>
290 public CompositeNode QueryStringNode
292 get { return new TreeBuilder().BuildSourceNode(QueryString); }
295 /// <summary>
296 /// Obtains the params node.
297 /// </summary>
298 /// <param name="from">From.</param>
299 /// <returns></returns>
300 public CompositeNode ObtainParamsNode(ParamStore from)
302 switch(from)
304 case ParamStore.Form:
305 return FormNode;
306 case ParamStore.Params:
307 return ParamsNode;
308 default:
309 return QueryStringNode;
313 /// <summary>
314 /// Gets the IP host address of the remote client.
315 /// </summary>
316 /// <value>The IP address of the remote client.</value>
317 public virtual string UserHostAddress
319 get { return userHostAddress; }
320 set { userHostAddress = value; }