2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / System.Web.DynamicData / Test / Common / MyHttpRequestWrapper.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Collections.Specialized;
4 using System.Linq;
5 using System.Reflection;
6 using System.Text;
7 using System.Web;
8
9 namespace MonoTests.Common
10 {
11         class MyHttpRequestWrapper : HttpRequestBase
12         {
13                 Dictionary<string, object> propertyValues = new Dictionary<string, object> ();
14
15                 public override string AppRelativeCurrentExecutionFilePath
16                 {
17                         get
18                         {
19                                 string value;
20                                 if (!GetProperty<string> ("AppRelativeCurrentExecutionFilePath", out value))
21                                         return base.AppRelativeCurrentExecutionFilePath;
22
23                                 return value;
24                         }
25                 }
26
27                 public override string PathInfo
28                 {
29                         get
30                         {
31                                 string value;
32                                 if (!GetProperty<string> ("PathInfo", out value))
33                                         return base.PathInfo;
34
35                                 return value;
36                         }
37                 }
38
39                 public override NameValueCollection QueryString
40                 {
41                         get
42                         {
43                                 NameValueCollection value;
44                                 if (!GetProperty<NameValueCollection> ("QueryString", out value))
45                                         return base.QueryString;
46
47                                 return value;
48                         }
49                 }
50
51                 bool GetProperty<T> (string name, out T value)
52                 {
53                         if (String.IsNullOrEmpty (name))
54                                 throw new ArgumentNullException ("name");
55
56                         value = default (T);
57                         object v;
58                         if (propertyValues.TryGetValue (name, out v)) {
59                                 if (v == null)
60                                         return true;
61                                 if (typeof (T).IsAssignableFrom (v.GetType ())) {
62                                         value = (T) v;
63                                         return true;
64                                 }
65
66                                 throw new InvalidOperationException ("Invalid value type. Expected '" + typeof (T) + "' and got '" + v.GetType () + "'");
67                         }
68
69                         return false;
70                 }
71
72                 public void SetProperty (string name, object value)
73                 {
74                         if (String.IsNullOrEmpty (name))
75                                 return;
76
77                         if (propertyValues.ContainsKey (name))
78                                 propertyValues[name] = value;
79                         else
80                                 propertyValues.Add (name, value);
81                 }
82         }
83 }