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
;
19 using System
.Collections
.Generic
;
20 using System
.Collections
.Specialized
;
22 using Castle
.Components
.Binder
;
25 /// Represents a mock implementation of <see cref="IRequest"/> for unit test purposes.
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
;
46 /// Initializes a new instance of the <see cref="MockRequest"/> class.
48 /// <param name="cookies">The cookies.</param>
49 public MockRequest(IDictionary
<string, HttpCookie
> cookies
)
51 this.cookies
= cookies
;
55 /// Initializes a new instance of the <see cref="MockRequest"/> class.
57 /// <param name="httpMethod">The HTTP method.</param>
58 public MockRequest(string httpMethod
) : this()
60 this.httpMethod
= httpMethod
;
64 /// Initializes a new instance of the <see cref="MockRequest"/> class.
66 public MockRequest() : this(new Dictionary
<string, HttpCookie
>(StringComparer
.InvariantCultureIgnoreCase
))
71 /// Gets or sets the accept header.
73 /// <value>The accept header.</value>
74 public string AcceptHeader
76 get { return headers["Accept"]; }
77 set { headers["Accept"] = value; }
81 /// Gets the referring URL.
84 public string UrlReferrer
86 get { return urlReferrer; }
87 set { urlReferrer = value; }
91 // /// Reads the request data as a byte array.
93 // /// <param name="count">How many bytes.</param>
94 // /// <returns></returns>
95 // public virtual byte[] BinaryRead(int count)
97 // throw new NotImplementedException();
101 /// Reads the cookie.
103 /// <param name="name">The cookie name.</param>
104 /// <returns></returns>
105 public virtual string ReadCookie(string name
)
108 if (cookies
.TryGetValue(name
, out cookie
))
116 /// Validates the input.
118 public virtual void ValidateInput()
123 /// Gets the Http headers.
125 /// <value>The Http headers.</value>
126 public virtual NameValueCollection Headers
128 get { return headers; }
132 /// Gets the <see cref="HttpPostedFile"/> per key.
135 public virtual IDictionary Files
137 get { return files; }
141 /// Gets a value indicating whether this requeest is from a local address.
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; }
151 /// Gets additional path information for
152 /// a resource with a URL extension.
154 /// <value>The path info.</value>
155 public virtual string PathInfo
157 get { return pathInfo; }
158 set { pathInfo = value; }
162 /// Gets the request type (GET, POST, etc)
165 public string RequestType
167 get { return HttpMethod; }
171 /// Gets the request URL.
176 get { return RawUrl; }
180 /// Gets the raw URL.
182 /// <value>The raw URL.</value>
183 public virtual string RawUrl
185 get { return rawUrl; }
186 set { rawUrl = value; }
192 /// <value>The URI.</value>
193 public virtual Uri Uri
200 /// Gets the HTTP method.
202 /// <value>The HTTP method.</value>
203 public virtual string HttpMethod
205 get { return httpMethod; }
206 set { httpMethod = value; }
210 /// Gets the file path.
212 /// <value>The file path.</value>
213 public virtual string FilePath
215 get { return filePath; }
216 set { filePath = value; }
220 /// Gets the param with the specified key.
223 public virtual string this[string key
]
225 get { return @params[key]; }
229 /// Gets the params which accumulates headers, post, querystring and cookies.
231 /// <value>The params.</value>
232 public virtual NameValueCollection Params
234 get { return @params; }
238 /// Gets the query string.
240 /// <value>The query string.</value>
241 public virtual NameValueCollection QueryString
243 get { return queryString; }
249 /// <value>The form.</value>
250 public virtual NameValueCollection Form
256 /// Gets the user languages.
258 /// <value>The user languages.</value>
259 public virtual string[] UserLanguages
261 get { return userLanguages; }
262 set { userLanguages = value; }
266 /// Lazy initialized property with a hierarchical
267 /// representation of the flat data on <see cref="Controller.Params"/>
270 public CompositeNode ParamsNode
272 get { return new TreeBuilder().BuildSourceNode(Params); }
276 /// Lazy initialized property with a hierarchical
277 /// representation of the flat data on <see cref="IRequest.Form"/>
280 public CompositeNode FormNode
282 get { return new TreeBuilder().BuildSourceNode(Form); }
286 /// Lazy initialized property with a hierarchical
287 /// representation of the flat data on <see cref="IRequest.QueryString"/>
289 /// <value>The query string node.</value>
290 public CompositeNode QueryStringNode
292 get { return new TreeBuilder().BuildSourceNode(QueryString); }
296 /// Obtains the params node.
298 /// <param name="from">From.</param>
299 /// <returns></returns>
300 public CompositeNode
ObtainParamsNode(ParamStore
from)
304 case ParamStore
.Form
:
306 case ParamStore
.Params
:
309 return QueryStringNode
;
314 /// Gets the IP host address of the remote client.
316 /// <value>The IP address of the remote client.</value>
317 public virtual string UserHostAddress
319 get { return userHostAddress; }
320 set { userHostAddress = value; }