Fixing an issue with output parameters that are of type IntPtr
[castle.git] / MonoRail / Castle.MonoRail.Framework / Test / MockRequest.cs
blob24625e6cf13aac4daaadd791650bd324b0a3a158
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.IO;
22 using System.Web;
23 using Castle.Components.Binder;
25 /// <summary>
26 /// Represents a mock implementation of <see cref="IRequest"/> for unit test purposes.
27 /// </summary>
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;
48 /// <summary>
49 /// Initializes a new instance of the <see cref="MockRequest"/> class.
50 /// </summary>
51 /// <param name="cookies">The cookies.</param>
52 public MockRequest(IDictionary<string, HttpCookie> cookies)
54 this.cookies = cookies;
57 /// <summary>
58 /// Initializes a new instance of the <see cref="MockRequest"/> class.
59 /// </summary>
60 /// <param name="httpMethod">The HTTP method.</param>
61 public MockRequest(string httpMethod) : this()
63 this.httpMethod = httpMethod;
66 /// <summary>
67 /// Initializes a new instance of the <see cref="MockRequest"/> class.
68 /// </summary>
69 public MockRequest() : this(new Dictionary<string, HttpCookie>(StringComparer.InvariantCultureIgnoreCase))
73 /// <summary>
74 /// Gets or sets the accept header.
75 /// </summary>
76 /// <value>The accept header.</value>
77 public string AcceptHeader
79 get { return headers["Accept"]; }
80 set { headers["Accept"] = value; }
83 /// <summary>
84 /// Gets the referring URL.
85 /// </summary>
86 /// <value></value>
87 public string UrlReferrer
89 get { return urlReferrer; }
90 set { urlReferrer = value; }
93 // /// <summary>
94 // /// Reads the request data as a byte array.
95 // /// </summary>
96 // /// <param name="count">How many bytes.</param>
97 // /// <returns></returns>
98 // public virtual byte[] BinaryRead(int count)
99 // {
100 // throw new NotImplementedException();
101 // }
103 /// <summary>
104 /// Reads the cookie.
105 /// </summary>
106 /// <param name="name">The cookie name.</param>
107 /// <returns></returns>
108 public virtual string ReadCookie(string name)
110 HttpCookie cookie;
111 if (cookies.TryGetValue(name, out cookie))
113 return cookie.Value;
115 return null;
118 /// <summary>
119 /// Validates the input.
120 /// </summary>
121 public virtual void ValidateInput()
125 /// <summary>
126 /// Gets the Http headers.
127 /// </summary>
128 /// <value>The Http headers.</value>
129 public virtual NameValueCollection Headers
131 get { return headers; }
134 /// <summary>
135 /// Gets the <see cref="HttpPostedFile"/> per key.
136 /// </summary>
137 /// <value></value>
138 public virtual IDictionary Files
140 get { return files; }
143 /// <summary>
144 /// Gets a value indicating whether this requeest is from a local address.
145 /// </summary>
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; }
153 /// <summary>
154 /// Gets additional path information for
155 /// a resource with a URL extension.
156 /// </summary>
157 /// <value>The path info.</value>
158 public virtual string PathInfo
160 get { return pathInfo; }
161 set { pathInfo = value; }
164 /// <summary>
165 /// Gets the request type (GET, POST, etc)
166 /// </summary>
167 /// <value></value>
168 public string RequestType
170 get { return HttpMethod; }
173 /// <summary>
174 /// Gets the request URL.
175 /// </summary>
176 /// <value></value>
177 public string Url
179 get { return RawUrl; }
182 /// <summary>
183 /// Gets the raw URL.
184 /// </summary>
185 /// <value>The raw URL.</value>
186 public virtual string RawUrl
188 get { return rawUrl; }
189 set { rawUrl = value; }
192 /// <summary>
193 /// Gets the URI.
194 /// </summary>
195 /// <value>The URI.</value>
196 public virtual Uri Uri
198 get { return uri; }
199 set { uri = value; }
202 /// <summary>
203 /// Gets the HTTP method.
204 /// </summary>
205 /// <value>The HTTP method.</value>
206 public virtual string HttpMethod
208 get { return httpMethod; }
209 set { httpMethod = value; }
212 /// <summary>
213 /// Gets the file path.
214 /// </summary>
215 /// <value>The file path.</value>
216 public virtual string FilePath
218 get { return filePath; }
219 set { filePath = value; }
222 /// <summary>
223 /// Gets the param with the specified key.
224 /// </summary>
225 /// <value></value>
226 public virtual string this[string key]
228 get { return @params[key]; }
231 /// <summary>
232 /// Gets the params which accumulates headers, post, querystring and cookies.
233 /// </summary>
234 /// <value>The params.</value>
235 public virtual NameValueCollection Params
237 get { return @params; }
240 /// <summary>
241 /// Gets the query string.
242 /// </summary>
243 /// <value>The query string.</value>
244 public virtual NameValueCollection QueryString
246 get { return queryString; }
249 /// <summary>
250 /// Gets the form.
251 /// </summary>
252 /// <value>The form.</value>
253 public virtual NameValueCollection Form
255 get { return form; }
258 /// <summary>
259 /// Gets the user languages.
260 /// </summary>
261 /// <value>The user languages.</value>
262 public virtual string[] UserLanguages
264 get { return userLanguages; }
265 set { userLanguages = value; }
268 /// <summary>
269 /// Lazy initialized property with a hierarchical
270 /// representation of the flat data on <see cref="Controller.Params"/>
271 /// </summary>
272 /// <value></value>
273 public CompositeNode ParamsNode
275 get { return new TreeBuilder().BuildSourceNode(Params); }
278 /// <summary>
279 /// Lazy initialized property with a hierarchical
280 /// representation of the flat data on <see cref="IRequest.Form"/>
281 /// </summary>
282 /// <value></value>
283 public CompositeNode FormNode
285 get { return new TreeBuilder().BuildSourceNode(Form); }
288 /// <summary>
289 /// Lazy initialized property with a hierarchical
290 /// representation of the flat data on <see cref="IRequest.QueryString"/>
291 /// </summary>
292 /// <value>The query string node.</value>
293 public CompositeNode QueryStringNode
295 get { return new TreeBuilder().BuildSourceNode(QueryString); }
298 /// <summary>
299 /// Gets the contents of the incoming HTTP entity body.
300 /// </summary>
301 public Stream InputStream
303 get { return inputStream; }
306 /// <summary>
307 /// Gets or sets the MIME content type of the incoming request.
308 /// </summary>
309 public string ContentType
311 get { return contentType; }
312 set { contentType = value; }
315 /// <summary>
316 /// Obtains the params node.
317 /// </summary>
318 /// <param name="from">From.</param>
319 /// <returns></returns>
320 public CompositeNode ObtainParamsNode(ParamStore from)
322 switch(from)
324 case ParamStore.Form:
325 return FormNode;
326 case ParamStore.Params:
327 return ParamsNode;
328 default:
329 return QueryStringNode;
333 /// <summary>
334 /// Gets the IP host address of the remote client.
335 /// </summary>
336 /// <value>The IP address of the remote client.</value>
337 public virtual string UserHostAddress
339 get { return userHostAddress; }
340 set { userHostAddress = value; }