opensim – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 eva 1 /*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSimulator Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27  
28 using System;
29 using System.Collections.Specialized;
30 using System.IO;
31 using System.Net;
32 using System.Net.Sockets;
33 using System.Text;
34 using HttpServer;
35 using HttpServer.FormDecoders;
36 using NUnit.Framework;
37 using OpenSim.Framework.Servers.HttpServer;
38 using OpenSim.Tests.Common;
39  
40 namespace OpenSim.Framework.Servers.Tests
41 {
42 [TestFixture]
43 public class OSHttpTests : OpenSimTestCase
44 {
45 // we need an IHttpClientContext for our tests
46 public class TestHttpClientContext: IHttpClientContext
47 {
48 private bool _secured;
49 public bool IsSecured
50 {
51 get { return _secured; }
52 }
53 public bool Secured
54 {
55 get { return _secured; }
56 }
57  
58 public TestHttpClientContext(bool secured)
59 {
60 _secured = secured;
61 }
62  
63 public void Disconnect(SocketError error) {}
64 public void Respond(string httpVersion, HttpStatusCode statusCode, string reason, string body) {}
65 public void Respond(string httpVersion, HttpStatusCode statusCode, string reason) {}
66 public void Respond(string body) {}
67 public void Send(byte[] buffer) {}
68 public void Send(byte[] buffer, int offset, int size) {}
69 public void Respond(string httpVersion, HttpStatusCode statusCode, string reason, string body, string contentType) {}
70 public void Close() { }
71 public bool EndWhenDone { get { return false;} set { return;}}
72  
73 public HTTPNetworkContext GiveMeTheNetworkStreamIKnowWhatImDoing()
74 {
75 return new HTTPNetworkContext();
76 }
77  
78 public event EventHandler<DisconnectedEventArgs> Disconnected = delegate { };
79 /// <summary>
80 /// A request have been received in the context.
81 /// </summary>
82 public event EventHandler<RequestEventArgs> RequestReceived = delegate { };
83  
84 }
85  
86 public class TestHttpRequest: IHttpRequest
87 {
88 private string _uriPath;
89 public bool BodyIsComplete
90 {
91 get { return true; }
92 }
93 public string[] AcceptTypes
94 {
95 get {return _acceptTypes; }
96 }
97 private string[] _acceptTypes;
98 public Stream Body
99 {
100 get { return _body; }
101 set { _body = value;}
102 }
103 private Stream _body;
104 public ConnectionType Connection
105 {
106 get { return _connection; }
107 set { _connection = value; }
108 }
109 private ConnectionType _connection;
110 public int ContentLength
111 {
112 get { return _contentLength; }
113 set { _contentLength = value; }
114 }
115 private int _contentLength;
116 public NameValueCollection Headers
117 {
118 get { return _headers; }
119 }
120 private NameValueCollection _headers = new NameValueCollection();
121 public string HttpVersion
122 {
123 get { return _httpVersion; }
124 set { _httpVersion = value; }
125 }
126 private string _httpVersion = null;
127 public string Method
128 {
129 get { return _method; }
130 set { _method = value; }
131 }
132 private string _method = null;
133 public HttpInput QueryString
134 {
135 get { return _queryString; }
136 }
137 private HttpInput _queryString = null;
138 public Uri Uri
139 {
140 get { return _uri; }
141 set { _uri = value; }
142 }
143 private Uri _uri = null;
144 public string[] UriParts
145 {
146 get { return _uri.Segments; }
147 }
148 public HttpParam Param
149 {
150 get { return null; }
151 }
152 public HttpForm Form
153 {
154 get { return null; }
155 }
156 public bool IsAjax
157 {
158 get { return false; }
159 }
160 public RequestCookies Cookies
161 {
162 get { return null; }
163 }
164  
165 public TestHttpRequest() {}
166  
167 public TestHttpRequest(string contentEncoding, string contentType, string userAgent,
168 string remoteAddr, string remotePort, string[] acceptTypes,
169 ConnectionType connectionType, int contentLength, Uri uri)
170 {
171 _headers["content-encoding"] = contentEncoding;
172 _headers["content-type"] = contentType;
173 _headers["user-agent"] = userAgent;
174 _headers["remote_addr"] = remoteAddr;
175 _headers["remote_port"] = remotePort;
176  
177 _acceptTypes = acceptTypes;
178 _connection = connectionType;
179 _contentLength = contentLength;
180 _uri = uri;
181 }
182  
183 public void DecodeBody(FormDecoderProvider providers) {}
184 public void SetCookies(RequestCookies cookies) {}
185 public void AddHeader(string name, string value)
186 {
187 _headers.Add(name, value);
188 }
189 public int AddToBody(byte[] bytes, int offset, int length)
190 {
191 return 0;
192 }
193 public void Clear() {}
194  
195 public object Clone()
196 {
197 TestHttpRequest clone = new TestHttpRequest();
198 clone._acceptTypes = _acceptTypes;
199 clone._connection = _connection;
200 clone._contentLength = _contentLength;
201 clone._uri = _uri;
202 clone._headers = new NameValueCollection(_headers);
203  
204 return clone;
205 }
206 public IHttpResponse CreateResponse(IHttpClientContext context)
207 {
208 return new HttpResponse(context, this);
209 }
210 /// <summary>
211 /// Path and query (will be merged with the host header) and put in Uri
212 /// </summary>
213 /// <see cref="Uri"/>
214 public string UriPath
215 {
216 get { return _uriPath; }
217 set
218 {
219 _uriPath = value;
220  
221 }
222 }
223  
224 }
225  
226 public class TestHttpResponse: IHttpResponse
227 {
228 public Stream Body
229 {
230 get { return _body; }
231  
232 set { _body = value; }
233 }
234 private Stream _body;
235  
236 public string ProtocolVersion
237 {
238 get { return _protocolVersion; }
239 set { _protocolVersion = value; }
240 }
241 private string _protocolVersion;
242  
243 public bool Chunked
244 {
245 get { return _chunked; }
246  
247 set { _chunked = value; }
248 }
249 private bool _chunked;
250  
251 public ConnectionType Connection
252 {
253 get { return _connection; }
254  
255 set { _connection = value; }
256 }
257 private ConnectionType _connection;
258  
259 public Encoding Encoding
260 {
261 get { return _encoding; }
262  
263 set { _encoding = value; }
264 }
265 private Encoding _encoding;
266  
267 public int KeepAlive
268 {
269 get { return _keepAlive; }
270  
271 set { _keepAlive = value; }
272 }
273 private int _keepAlive;
274  
275 public HttpStatusCode Status
276 {
277 get { return _status; }
278  
279 set { _status = value; }
280 }
281 private HttpStatusCode _status;
282  
283 public string Reason
284 {
285 get { return _reason; }
286  
287 set { _reason = value; }
288 }
289 private string _reason;
290  
291 public long ContentLength
292 {
293 get { return _contentLength; }
294  
295 set { _contentLength = value; }
296 }
297 private long _contentLength;
298  
299 public string ContentType
300 {
301 get { return _contentType; }
302  
303 set { _contentType = value; }
304 }
305 private string _contentType;
306  
307 public bool HeadersSent
308 {
309 get { return _headersSent; }
310 }
311 private bool _headersSent;
312  
313 public bool Sent
314 {
315 get { return _sent; }
316 }
317 private bool _sent;
318  
319 public ResponseCookies Cookies
320 {
321 get { return _cookies; }
322 }
323 private ResponseCookies _cookies = null;
324  
325 public TestHttpResponse()
326 {
327 _headersSent = false;
328 _sent = false;
329 }
330  
331 public void AddHeader(string name, string value) {}
332 public void Send()
333 {
334 if (!_headersSent) SendHeaders();
335 if (_sent) throw new InvalidOperationException("stuff already sent");
336 _sent = true;
337 }
338  
339 public void SendBody(byte[] buffer, int offset, int count)
340 {
341 if (!_headersSent) SendHeaders();
342 _sent = true;
343 }
344 public void SendBody(byte[] buffer)
345 {
346 if (!_headersSent) SendHeaders();
347 _sent = true;
348 }
349  
350 public void SendHeaders()
351 {
352 if (_headersSent) throw new InvalidOperationException("headers already sent");
353 _headersSent = true;
354 }
355  
356 public void Redirect(Uri uri) {}
357 public void Redirect(string url) {}
358 }
359  
360  
361 public OSHttpRequest req0;
362 public OSHttpRequest req1;
363  
364 public OSHttpResponse rsp0;
365  
366 public IPEndPoint ipEP0;
367  
368 [TestFixtureSetUp]
369 public void Init()
370 {
371 TestHttpRequest threq0 = new TestHttpRequest("utf-8", "text/xml", "OpenSim Test Agent", "192.168.0.1", "4711",
372 new string[] {"text/xml"},
373 ConnectionType.KeepAlive, 4711,
374 new Uri("http://127.0.0.1/admin/inventory/Dr+Who/Tardis"));
375 threq0.Method = "GET";
376 threq0.HttpVersion = HttpHelper.HTTP10;
377  
378 TestHttpRequest threq1 = new TestHttpRequest("utf-8", "text/xml", "OpenSim Test Agent", "192.168.0.1", "4711",
379 new string[] {"text/xml"},
380 ConnectionType.KeepAlive, 4711,
381 new Uri("http://127.0.0.1/admin/inventory/Dr+Who/Tardis?a=0&b=1&c=2"));
382 threq1.Method = "POST";
383 threq1.HttpVersion = HttpHelper.HTTP11;
384 threq1.Headers["x-wuff"] = "wuffwuff";
385 threq1.Headers["www-authenticate"] = "go away";
386  
387 req0 = new OSHttpRequest(new TestHttpClientContext(false), threq0);
388 req1 = new OSHttpRequest(new TestHttpClientContext(false), threq1);
389  
390 rsp0 = new OSHttpResponse(new TestHttpResponse());
391  
392 ipEP0 = new IPEndPoint(IPAddress.Parse("192.168.0.1"), 4711);
393  
394 }
395  
396 [Test]
397 public void T000_OSHttpRequest()
398 {
399 Assert.That(req0.HttpMethod, Is.EqualTo("GET"));
400 Assert.That(req0.ContentType, Is.EqualTo("text/xml"));
401 Assert.That(req0.ContentLength, Is.EqualTo(4711));
402  
403 Assert.That(req1.HttpMethod, Is.EqualTo("POST"));
404 }
405  
406 [Test]
407 public void T001_OSHttpRequestHeaderAccess()
408 {
409 Assert.That(req1.Headers["x-wuff"], Is.EqualTo("wuffwuff"));
410 Assert.That(req1.Headers.Get("x-wuff"), Is.EqualTo("wuffwuff"));
411  
412 Assert.That(req1.Headers["www-authenticate"], Is.EqualTo("go away"));
413 Assert.That(req1.Headers.Get("www-authenticate"), Is.EqualTo("go away"));
414  
415 Assert.That(req0.RemoteIPEndPoint, Is.EqualTo(ipEP0));
416 }
417  
418 [Test]
419 public void T002_OSHttpRequestUriParsing()
420 {
421 Assert.That(req0.RawUrl, Is.EqualTo("/admin/inventory/Dr+Who/Tardis"));
422 Assert.That(req1.Url.ToString(), Is.EqualTo("http://127.0.0.1/admin/inventory/Dr+Who/Tardis?a=0&b=1&c=2"));
423 }
424  
425 [Test]
426 public void T100_OSHttpResponse()
427 {
428 rsp0.ContentType = "text/xml";
429 Assert.That(rsp0.ContentType, Is.EqualTo("text/xml"));
430 }
431 }
432 }