output WebException response stream
[mono.git] / mcs / class / System.Web / Test / mainsoft / NunitWeb / NunitWeb / MyHost.jvm.cs
1 using System;\r
2 using System.Text;\r
3 using System.Web;\r
4 using System.Web.UI;\r
5 using System.Web.UI.WebControls;\r
6 using System.Net;\r
7 using System.IO;\r
8 using System.Configuration;\r
9 using System.Web.Configuration;\r
10 using System.Collections;\r
11 using System.Runtime.Remoting.Messaging;\r
12 using System.Reflection;\r
13 using System.Threading;
14 using System.Runtime.Serialization;
15 using System.Runtime.Serialization.Formatters.Binary;\r
16 using System.Runtime.Serialization.Formatters.Soap;\r
17 using System.Collections.Specialized;\r
18 \r
19 namespace MonoTests.SystemWeb.Framework\r
20 {\r
21         internal class MyHost : MarshalByRefObject\r
22         {\r
23                 public const string INVOKER_HEADER = "NunitWebInvoker";\r
24                 public const string USER_HEADER = "NunitWebUserData";\r
25                 public const string EXCEPTION_HEADER = "NunitWebException";\r
26                 private const string CURRENT_WEBTEST = "NunitWebCurrentTest";\r
27                 public AppDomain AppDomain\r
28                 { get { return AppDomain.CurrentDomain; } }
29                 
30                 public static string Serialize (object o)
31                 {
32                         if (o == null)
33                                 return string.Empty;
34                         using (MemoryStream ms = new MemoryStream ()) {\r
35                                 SoapFormatter f = new SoapFormatter ();\r
36                                 f.Serialize (ms, o);\r
37                                 return HttpUtility.UrlEncode (ms.ToArray());\r
38                         }\r
39                 }
40
41                 public static object Deserialize (string s)
42                 {
43                         if (s == null || s == string.Empty)
44                                 return null;
45                         using (MemoryStream ms = new MemoryStream ()) {\r
46                                 byte [] ba = HttpUtility.UrlDecodeToBytes (s);\r
47                                 ms.Write (ba, 0, ba.Length);\r
48                                 ms.Position = 0;\r
49                                 SoapFormatter b = new SoapFormatter ();\r
50                                 try {\r
51                                         return b.Deserialize (ms);\r
52                                 }\r
53                                 catch (Exception e) {\r
54                                         throw;\r
55                                 }\r
56                         }\r
57                 }
58
59                 public WebTest Run (WebTest t)
60                 {\r
61                         NameValueCollection headers = new NameValueCollection ();\r
62                         headers.Add (INVOKER_HEADER, Serialize (t.Invoker));\r
63                         headers.Add (USER_HEADER, Serialize (t.UserData));\r
64                         WebRequest wr = t.Request.CreateWebRequest (\r
65 #if TARGET_JVM\r
66                                 new Uri ("http://localhost:8090/MainsoftWebApp20/"),\r
67 #else\r
68 new Uri ("http://localhost:59598/NunitWebTest/"),\r
69 #endif\r
70                                 headers);\r
71 \r
72 \r
73                         WebResponse response = null;\r
74                         try {\r
75                                 response = wr.GetResponse ();\r
76 \r
77                                 Exception e = (Exception) Deserialize (\r
78                                         response.Headers[EXCEPTION_HEADER]);\r
79                                 if (e != null)\r
80                                         RethrowException (e);\r
81 \r
82                                 t.UserData = Deserialize (response.Headers[USER_HEADER]);\r
83                                 t.Response = t.Request.ExtractResponse (response);\r
84                         }\r
85                         catch (WebException we) {\r
86                                 StreamReader sr = new StreamReader (we.Response.GetResponseStream ());\r
87                                 throw new WebException (we.Message + Environment.NewLine\r
88                                         + "Response:" + Environment.NewLine + sr.ReadToEnd (), we);\r
89                         }\r
90                         finally {\r
91                                 if (response != null)\r
92                                         response.Close ();\r
93                         }\r
94 \r
95                         return t;\r
96                 }\r
97 \r
98                 public void SendHeaders (WebTest t)\r
99                 {\r
100                         HttpContext.Current.Response.AppendHeader (USER_HEADER, Serialize(t.UserData));\r
101                 }\r
102 \r
103                 private static void RethrowException (Exception inner)\r
104                 {\r
105                         Exception outer;\r
106                         try { //Try create a similar exception and keep the inner intact\r
107                                 outer = (Exception) Activator.CreateInstance (inner.GetType (),\r
108                                         new object []{inner.ToString (), inner});\r
109                         }\r
110                         catch { //Failed to create a similar, fallback to the inner, ruining the call stack\r
111                                 throw inner;\r
112                         }\r
113                         throw outer;\r
114                 }\r
115 \r
116                 public static WebTest GetCurrentTest ()\r
117                 {\r
118                         WebTest wt = HttpContext.Current.Items[CURRENT_WEBTEST] as WebTest;\r
119                         if (wt != null)\r
120                                 return wt;\r
121                         wt = new WebTest ();\r
122                         wt.Invoker = (BaseInvoker) Deserialize (\r
123                                 HttpContext.Current.Request.Headers [INVOKER_HEADER]);\r
124                         if (wt.Invoker == null)\r
125                                 return null;\r
126                         wt.UserData = Deserialize (\r
127                                 HttpContext.Current.Request.Headers [USER_HEADER]);\r
128                         HttpContext.Current.Items[CURRENT_WEBTEST] = wt;\r
129                         return wt;\r
130                 }\r
131 \r
132                 public void RegisterException (Exception ex)\r
133                 {\r
134                         if (ex == null)\r
135                                 return;\r
136                         if (HttpContext.Current.Items[EXCEPTION_HEADER] != null)\r
137                                 return; //register only the first exception\r
138                         HttpContext.Current.Response.AddHeader (EXCEPTION_HEADER,\r
139                                 Serialize (ex));\r
140                         HttpContext.Current.Items[EXCEPTION_HEADER] = ex;\r
141                 }\r
142         }\r
143 }\r