2009-06-12 Bill Holmes <billholmes54@gmail.com>
[mono.git] / mcs / class / System.Web.DynamicData / Test / Common / FakeHttpWorkerRequest.cs
1 //
2 // System.Web.HttpResponseTest.cs - Unit tests for System.Web.HttpResponse
3 //
4 // Author:
5 //      Miguel de Icaza  <miguel@ximian.com>
6 //
7 // Copyright (C) 2005-2009 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System.Text;
30 using System.Web;
31 using System;
32 using System.Collections;
33 using System.Collections.Specialized;
34 using NUnit.Framework;
35
36 namespace MonoTests.Common
37 {
38
39     public class FakeHttpWorkerRequest : HttpWorkerRequest
40     {
41         string queryString;
42         public Hashtable KnownResponseHeaders;
43         public Hashtable UnknownResponseHeaders;
44         public int return_kind;
45
46         public FakeHttpWorkerRequest ()
47             : this (1)
48         {
49         }
50
51         public FakeHttpWorkerRequest (int re)
52         {
53             KnownResponseHeaders = CollectionsUtil.CreateCaseInsensitiveHashtable ();
54             UnknownResponseHeaders = CollectionsUtil.CreateCaseInsensitiveHashtable ();
55             return_kind = re;
56             queryString = String.Empty;
57         }
58
59         public override string GetUriPath ()
60         {
61             return "/fake";
62         }
63
64         public override string GetQueryString ()
65         {
66             return queryString;
67         }
68
69         public void SetQueryString (string queryString)
70         {
71             this.queryString = queryString;
72         }
73
74         public override string GetRawUrl ()
75         {
76             return "GetRawUrl";
77         }
78
79         public override string GetHttpVerbName ()
80         {
81             return "GET";
82         }
83
84         public override string GetHttpVersion ()
85         {
86             if (return_kind == 1)
87                 return "HTTP/1.0";
88             else
89                 return "HTTP/1.1";
90         }
91
92         public override string GetRemoteAddress ()
93         {
94             return "__GetRemoteAddress";
95         }
96
97         public override int GetRemotePort ()
98         {
99             return 1010;
100         }
101
102         public override string GetLocalAddress ()
103         {
104             return "GetLocalAddress";
105         }
106
107         public override string GetAppPath ()
108         {
109             return "AppPath";
110         }
111
112         public override int GetLocalPort ()
113         {
114             return 2020;
115         }
116
117         public bool status_sent;
118         public int status_code;
119         public string status_string;
120
121         public override void SendStatus (int s, string x)
122         {
123             status_sent = true;
124             status_code = s;
125             status_string = x;
126         }
127
128         void AddHeader (Hashtable table, string header_name, object header)
129         {
130             object o = table[header_name];
131             if (o == null)
132                 table.Add (header_name, header);
133             else {
134                 ArrayList al = o as ArrayList;
135                 if (al == null) {
136                     al = new ArrayList ();
137                     al.Add (o);
138                     table[header_name] = al;
139                 } else
140                     al = o as ArrayList;
141
142                 al.Add (header);
143             }
144         }
145
146         bool headers_sent;
147         public override void SendKnownResponseHeader (int x, string j)
148         {
149             string header_name = HttpWorkerRequest.GetKnownRequestHeaderName (x);
150             AddHeader (KnownResponseHeaders, header_name, new KnownResponseHeader (x, j));
151             headers_sent = true;
152         }
153
154         public override void SendUnknownResponseHeader (string a, string b)
155         {
156             AddHeader (UnknownResponseHeaders, a, new UnknownResponseHeader (a, b));
157             headers_sent = true;
158         }
159
160         bool data_sent;
161         public byte[] data;
162         public int data_len;
163         public int total = 0;
164
165         public override void SendResponseFromMemory (byte[] arr, int x)
166         {
167             data_sent = true;
168             data = new byte[x];
169             for (int i = 0; i < x; i++)
170                 data[i] = arr[i];
171             data_len = x;
172             total += data_len;
173         }
174
175         public override void SendResponseFromFile (string a, long b, long c)
176         {
177             data_sent = true;
178         }
179
180         public override void SendResponseFromFile (IntPtr a, long b, long c)
181         {
182             data_sent = true;
183         }
184
185         public override void FlushResponse (bool x)
186         {
187         }
188
189         public override void EndOfRequest ()
190         {
191         }
192
193         public override string GetKnownRequestHeader (int index)
194         {
195             return null;
196         }
197
198         public bool OutputProduced
199         {
200             get
201             {
202                 return headers_sent || data_sent;
203             }
204         }
205     }
206 }