OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
OSHttpTests.cs
Go to the documentation of this file.
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]
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 { };
82  public event EventHandler<RequestEventArgs> RequestReceived = delegate { };
83 
84  public bool CanSend { get { return true; } }
85  public string RemoteEndPoint { get { return ""; } }
86  public string RemoteEndPointAddress { get { return ""; } }
87  public string RemoteEndPointPort { get { return ""; } }
88  }
89 
90  public class TestHttpRequest: IHttpRequest
91  {
92  private string _uriPath;
93  public bool BodyIsComplete
94  {
95  get { return true; }
96  }
97  public string[] AcceptTypes
98  {
99  get {return _acceptTypes; }
100  }
101  private string[] _acceptTypes;
102  public Stream Body
103  {
104  get { return _body; }
105  set { _body = value;}
106  }
107  private Stream _body;
108  public ConnectionType Connection
109  {
110  get { return _connection; }
111  set { _connection = value; }
112  }
113  private ConnectionType _connection;
114  public int ContentLength
115  {
116  get { return _contentLength; }
117  set { _contentLength = value; }
118  }
119  private int _contentLength;
120  public NameValueCollection Headers
121  {
122  get { return _headers; }
123  }
124  private NameValueCollection _headers = new NameValueCollection();
125  public string HttpVersion
126  {
127  get { return _httpVersion; }
128  set { _httpVersion = value; }
129  }
130  private string _httpVersion = null;
131  public string Method
132  {
133  get { return _method; }
134  set { _method = value; }
135  }
136  private string _method = null;
137  public HttpInput QueryString
138  {
139  get { return _queryString; }
140  }
141  private HttpInput _queryString = null;
142  public Uri Uri
143  {
144  get { return _uri; }
145  set { _uri = value; }
146  }
147  private Uri _uri = null;
148  public string[] UriParts
149  {
150  get { return _uri.Segments; }
151  }
152  public HttpParam Param
153  {
154  get { return null; }
155  }
156  public HttpForm Form
157  {
158  get { return null; }
159  }
160  public bool IsAjax
161  {
162  get { return false; }
163  }
164  public RequestCookies Cookies
165  {
166  get { return null; }
167  }
168 
169  public TestHttpRequest() {}
170 
171  public TestHttpRequest(string contentEncoding, string contentType, string userAgent,
172  string remoteAddr, string remotePort, string[] acceptTypes,
173  ConnectionType connectionType, int contentLength, Uri uri)
174  {
175  _headers["content-encoding"] = contentEncoding;
176  _headers["content-type"] = contentType;
177  _headers["user-agent"] = userAgent;
178  _headers["remote_addr"] = remoteAddr;
179  _headers["remote_port"] = remotePort;
180 
181  _acceptTypes = acceptTypes;
182  _connection = connectionType;
183  _contentLength = contentLength;
184  _uri = uri;
185  }
186 
187  public void DecodeBody(FormDecoderProvider providers) {}
188  public void SetCookies(RequestCookies cookies) {}
189  public void AddHeader(string name, string value)
190  {
191  _headers.Add(name, value);
192  }
193  public int AddToBody(byte[] bytes, int offset, int length)
194  {
195  return 0;
196  }
197  public void Clear() {}
198 
199  public object Clone()
200  {
201  TestHttpRequest clone = new TestHttpRequest();
202  clone._acceptTypes = _acceptTypes;
203  clone._connection = _connection;
204  clone._contentLength = _contentLength;
205  clone._uri = _uri;
206  clone._headers = new NameValueCollection(_headers);
207 
208  return clone;
209  }
210  public IHttpResponse CreateResponse(IHttpClientContext context)
211  {
212  return new HttpResponse(context, this);
213  }
218  public string UriPath
219  {
220  get { return _uriPath; }
221  set
222  {
223  _uriPath = value;
224 
225  }
226  }
227 
228  }
229 
230  public class TestHttpResponse: IHttpResponse
231  {
232  public Stream Body
233  {
234  get { return _body; }
235 
236  set { _body = value; }
237  }
238  private Stream _body;
239 
240  public string ProtocolVersion
241  {
242  get { return _protocolVersion; }
243  set { _protocolVersion = value; }
244  }
245  private string _protocolVersion;
246 
247  public bool Chunked
248  {
249  get { return _chunked; }
250 
251  set { _chunked = value; }
252  }
253  private bool _chunked;
254 
255  public ConnectionType Connection
256  {
257  get { return _connection; }
258 
259  set { _connection = value; }
260  }
261  private ConnectionType _connection;
262 
263  public Encoding Encoding
264  {
265  get { return _encoding; }
266 
267  set { _encoding = value; }
268  }
269  private Encoding _encoding;
270 
271  public int KeepAlive
272  {
273  get { return _keepAlive; }
274 
275  set { _keepAlive = value; }
276  }
277  private int _keepAlive;
278 
279  public HttpStatusCode Status
280  {
281  get { return _status; }
282 
283  set { _status = value; }
284  }
285  private HttpStatusCode _status;
286 
287  public string Reason
288  {
289  get { return _reason; }
290 
291  set { _reason = value; }
292  }
293  private string _reason;
294 
295  public long ContentLength
296  {
297  get { return _contentLength; }
298 
299  set { _contentLength = value; }
300  }
301  private long _contentLength;
302 
303  public string ContentType
304  {
305  get { return _contentType; }
306 
307  set { _contentType = value; }
308  }
309  private string _contentType;
310 
311  public bool HeadersSent
312  {
313  get { return _headersSent; }
314  }
315  private bool _headersSent;
316 
317  public bool Sent
318  {
319  get { return _sent; }
320  }
321  private bool _sent;
322 
323  public ResponseCookies Cookies
324  {
325  get { return _cookies; }
326  }
327  private ResponseCookies _cookies = null;
328 
330  {
331  _headersSent = false;
332  _sent = false;
333  }
334 
335  public void AddHeader(string name, string value) {}
336  public void Send()
337  {
338  if (!_headersSent) SendHeaders();
339  if (_sent) throw new InvalidOperationException("stuff already sent");
340  _sent = true;
341  }
342 
343  public void SendBody(byte[] buffer, int offset, int count)
344  {
345  if (!_headersSent) SendHeaders();
346  _sent = true;
347  }
348  public void SendBody(byte[] buffer)
349  {
350  if (!_headersSent) SendHeaders();
351  _sent = true;
352  }
353 
354  public void SendHeaders()
355  {
356  if (_headersSent) throw new InvalidOperationException("headers already sent");
357  _headersSent = true;
358  }
359 
360  public void Redirect(Uri uri) {}
361  public void Redirect(string url) {}
362  }
363 
364 
367 
369 
370  public IPEndPoint ipEP0;
371 
372  [TestFixtureSetUp]
373  public void Init()
374  {
375  TestHttpRequest threq0 = new TestHttpRequest("utf-8", "text/xml", "OpenSim Test Agent", "192.168.0.1", "4711",
376  new string[] {"text/xml"},
377  ConnectionType.KeepAlive, 4711,
378  new Uri("http://127.0.0.1/admin/inventory/Dr+Who/Tardis"));
379  threq0.Method = "GET";
380  threq0.HttpVersion = HttpHelper.HTTP10;
381 
382  TestHttpRequest threq1 = new TestHttpRequest("utf-8", "text/xml", "OpenSim Test Agent", "192.168.0.1", "4711",
383  new string[] {"text/xml"},
384  ConnectionType.KeepAlive, 4711,
385  new Uri("http://127.0.0.1/admin/inventory/Dr+Who/Tardis?a=0&b=1&c=2"));
386  threq1.Method = "POST";
387  threq1.HttpVersion = HttpHelper.HTTP11;
388  threq1.Headers["x-wuff"] = "wuffwuff";
389  threq1.Headers["www-authenticate"] = "go away";
390 
391  req0 = new OSHttpRequest(new TestHttpClientContext(false), threq0);
392  req1 = new OSHttpRequest(new TestHttpClientContext(false), threq1);
393 
394  rsp0 = new OSHttpResponse(new TestHttpResponse());
395 
396  ipEP0 = new IPEndPoint(IPAddress.Parse("192.168.0.1"), 4711);
397 
398  }
399 
400  [Test]
401  public void T000_OSHttpRequest()
402  {
403  Assert.That(req0.HttpMethod, Is.EqualTo("GET"));
404  Assert.That(req0.ContentType, Is.EqualTo("text/xml"));
405  Assert.That(req0.ContentLength, Is.EqualTo(4711));
406 
407  Assert.That(req1.HttpMethod, Is.EqualTo("POST"));
408  }
409 
410  [Test]
412  {
413  Assert.That(req1.Headers["x-wuff"], Is.EqualTo("wuffwuff"));
414  Assert.That(req1.Headers.Get("x-wuff"), Is.EqualTo("wuffwuff"));
415 
416  Assert.That(req1.Headers["www-authenticate"], Is.EqualTo("go away"));
417  Assert.That(req1.Headers.Get("www-authenticate"), Is.EqualTo("go away"));
418 
419  Assert.That(req0.RemoteIPEndPoint, Is.EqualTo(ipEP0));
420  }
421 
422  [Test]
424  {
425  Assert.That(req0.RawUrl, Is.EqualTo("/admin/inventory/Dr+Who/Tardis"));
426  Assert.That(req1.Url.ToString(), Is.EqualTo("http://127.0.0.1/admin/inventory/Dr+Who/Tardis?a=0&b=1&c=2"));
427  }
428 
429  [Test]
430  public void T100_OSHttpResponse()
431  {
432  rsp0.ContentType = "text/xml";
433  Assert.That(rsp0.ContentType, Is.EqualTo("text/xml"));
434  }
435  }
436 }
void Respond(string httpVersion, HttpStatusCode statusCode, string reason, string body)
Definition: OSHttpTests.cs:64
OSHttpResponse is the OpenSim representation of an HTTP response.
void Respond(string httpVersion, HttpStatusCode statusCode, string reason, string body, string contentType)
Definition: OSHttpTests.cs:69
void SendBody(byte[] buffer, int offset, int count)
Definition: OSHttpTests.cs:343
void Respond(string httpVersion, HttpStatusCode statusCode, string reason)
Definition: OSHttpTests.cs:65
IHttpResponse CreateResponse(IHttpClientContext context)
Definition: OSHttpTests.cs:210
Interactive OpenSim region server
Definition: OpenSim.cs:55
int AddToBody(byte[] bytes, int offset, int length)
Definition: OSHttpTests.cs:193
void Send(byte[] buffer, int offset, int size)
Definition: OSHttpTests.cs:68
TestHttpRequest(string contentEncoding, string contentType, string userAgent, string remoteAddr, string remotePort, string[] acceptTypes, ConnectionType connectionType, int contentLength, Uri uri)
Definition: OSHttpTests.cs:171