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
;
23 using Castle
.Components
.Binder
;
26 /// Represents a mock implementation of <see cref="IRequest"/> for unit test purposes.
28 public class MockRequest
: IMockRequest
30 private NameValueCollection form
= new NameValueCollection();
31 private NameValueCollection headers
= new NameValueCollection();
32 private NameValueCollection queryString
= new NameValueCollection();
33 private NameValueCollection
@params = new NameValueCollection();
34 private string urlReferrer
;
35 private IDictionary
<string, HttpCookie
> cookies
;
36 private IDictionary files
= new Hashtable();
37 private bool isLocal
= true;
38 private string httpMethod
= "GET";
39 private string[] userLanguages
= new string[] {"en-ES", "pt-BR"}
;
40 private string rawUrl
= null;
41 private string filePath
= null;
42 private Uri uri
= null;
43 private string userHostAddress
= "127.0.0.1";
44 private string pathInfo
;
45 private string contentType
;
46 private Stream inputStream
= null;
49 /// Initializes a new instance of the <see cref="MockRequest"/> class.
51 /// <param name="cookies">The cookies.</param>
52 public MockRequest(IDictionary
<string, HttpCookie
> cookies
)
54 this.cookies
= cookies
;
58 /// Initializes a new instance of the <see cref="MockRequest"/> class.
60 /// <param name="httpMethod">The HTTP method.</param>
61 public MockRequest(string httpMethod
) : this()
63 this.httpMethod
= httpMethod
;
67 /// Initializes a new instance of the <see cref="MockRequest"/> class.
69 public MockRequest() : this(new Dictionary
<string, HttpCookie
>(StringComparer
.InvariantCultureIgnoreCase
))
74 /// Gets or sets the accept header.
76 /// <value>The accept header.</value>
77 public string AcceptHeader
79 get { return headers["Accept"]; }
80 set { headers["Accept"] = value; }
84 /// Gets the referring URL.
87 public string UrlReferrer
89 get { return urlReferrer; }
90 set { urlReferrer = value; }
94 // /// Reads the request data as a byte array.
96 // /// <param name="count">How many bytes.</param>
97 // /// <returns></returns>
98 // public virtual byte[] BinaryRead(int count)
100 // throw new NotImplementedException();
104 /// Reads the cookie.
106 /// <param name="name">The cookie name.</param>
107 /// <returns></returns>
108 public virtual string ReadCookie(string name
)
111 if (cookies
.TryGetValue(name
, out cookie
))
119 /// Validates the input.
121 public virtual void ValidateInput()
126 /// Gets the Http headers.
128 /// <value>The Http headers.</value>
129 public virtual NameValueCollection Headers
131 get { return headers; }
135 /// Gets the <see cref="HttpPostedFile"/> per key.
138 public virtual IDictionary Files
140 get { return files; }
144 /// Gets a value indicating whether this requeest is from a local address.
146 /// <value><c>true</c> if this instance is local; otherwise, <c>false</c>.</value>
147 public virtual bool IsLocal
149 get { return isLocal; }
150 set { isLocal = value; }
154 /// Gets additional path information for
155 /// a resource with a URL extension.
157 /// <value>The path info.</value>
158 public virtual string PathInfo
160 get { return pathInfo; }
161 set { pathInfo = value; }
165 /// Gets the request type (GET, POST, etc)
168 public string RequestType
170 get { return HttpMethod; }
174 /// Gets the request URL.
179 get { return RawUrl; }
183 /// Gets the raw URL.
185 /// <value>The raw URL.</value>
186 public virtual string RawUrl
188 get { return rawUrl; }
189 set { rawUrl = value; }
195 /// <value>The URI.</value>
196 public virtual Uri Uri
203 /// Gets the HTTP method.
205 /// <value>The HTTP method.</value>
206 public virtual string HttpMethod
208 get { return httpMethod; }
209 set { httpMethod = value; }
213 /// Gets the file path.
215 /// <value>The file path.</value>
216 public virtual string FilePath
218 get { return filePath; }
219 set { filePath = value; }
223 /// Gets the param with the specified key.
226 public virtual string this[string key
]
228 get { return @params[key]; }
232 /// Gets the params which accumulates headers, post, querystring and cookies.
234 /// <value>The params.</value>
235 public virtual NameValueCollection Params
237 get { return @params; }
241 /// Gets the query string.
243 /// <value>The query string.</value>
244 public virtual NameValueCollection QueryString
246 get { return queryString; }
252 /// <value>The form.</value>
253 public virtual NameValueCollection Form
259 /// Gets the user languages.
261 /// <value>The user languages.</value>
262 public virtual string[] UserLanguages
264 get { return userLanguages; }
265 set { userLanguages = value; }
269 /// Lazy initialized property with a hierarchical
270 /// representation of the flat data on <see cref="Controller.Params"/>
273 public CompositeNode ParamsNode
275 get { return new TreeBuilder().BuildSourceNode(Params); }
279 /// Lazy initialized property with a hierarchical
280 /// representation of the flat data on <see cref="IRequest.Form"/>
283 public CompositeNode FormNode
285 get { return new TreeBuilder().BuildSourceNode(Form); }
289 /// Lazy initialized property with a hierarchical
290 /// representation of the flat data on <see cref="IRequest.QueryString"/>
292 /// <value>The query string node.</value>
293 public CompositeNode QueryStringNode
295 get { return new TreeBuilder().BuildSourceNode(QueryString); }
299 /// Gets the contents of the incoming HTTP entity body.
301 public Stream InputStream
303 get { return inputStream; }
307 /// Gets or sets the MIME content type of the incoming request.
309 public string ContentType
311 get { return contentType; }
312 set { contentType = value; }
316 /// Obtains the params node.
318 /// <param name="from">From.</param>
319 /// <returns></returns>
320 public CompositeNode
ObtainParamsNode(ParamStore
from)
324 case ParamStore
.Form
:
326 case ParamStore
.Params
:
329 return QueryStringNode
;
334 /// Gets the IP host address of the remote client.
336 /// <value>The IP address of the remote client.</value>
337 public virtual string UserHostAddress
339 get { return userHostAddress; }
340 set { userHostAddress = value; }