OSHttpTests.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. using System;
  28. using System.Collections.Specialized;
  29. using System.IO;
  30. using System.Net;
  31. using System.Net.Sockets;
  32. using System.Text;
  33. using HttpServer;
  34. using HttpServer.FormDecoders;
  35. using NUnit.Framework;
  36. using OpenSim.Framework.Servers.HttpServer;
  37. using OpenSim.Tests.Common;
  38. namespace OpenSim.Framework.Servers.Tests
  39. {
  40. /*
  41. [TestFixture]
  42. public class OSHttpTests : OpenSimTestCase
  43. {
  44. // we need an IHttpClientContext for our tests
  45. public class TestHttpClientContext: IHttpClientContext
  46. {
  47. private bool _secured;
  48. public bool IsSecured
  49. {
  50. get { return _secured; }
  51. }
  52. public bool Secured
  53. {
  54. get { return _secured; }
  55. }
  56. public TestHttpClientContext(bool secured)
  57. {
  58. _secured = secured;
  59. }
  60. public void Disconnect(SocketError error) {}
  61. public void Respond(string httpVersion, HttpStatusCode statusCode, string reason, string body) {}
  62. public void Respond(string httpVersion, HttpStatusCode statusCode, string reason) {}
  63. public void Respond(string body) {}
  64. public void Send(byte[] buffer) {}
  65. public void Send(byte[] buffer, int offset, int size) {}
  66. public void Respond(string httpVersion, HttpStatusCode statusCode, string reason, string body, string contentType) {}
  67. public void Close() { }
  68. public bool EndWhenDone { get { return false;} set { return;}}
  69. public HTTPNetworkContext GiveMeTheNetworkStreamIKnowWhatImDoing()
  70. {
  71. return new HTTPNetworkContext();
  72. }
  73. public event EventHandler<DisconnectedEventArgs> Disconnected = delegate { };
  74. /// <summary>
  75. /// A request have been received in the context.
  76. /// </summary>
  77. public event EventHandler<RequestEventArgs> RequestReceived = delegate { };
  78. public bool CanSend { get { return true; } }
  79. public string RemoteEndPoint { get { return ""; } }
  80. public string RemoteEndPointAddress { get { return ""; } }
  81. public string RemoteEndPointPort { get { return ""; } }
  82. }
  83. public class TestHttpRequest: IHttpRequest
  84. {
  85. private string _uriPath;
  86. public bool BodyIsComplete
  87. {
  88. get { return true; }
  89. }
  90. public string[] AcceptTypes
  91. {
  92. get {return _acceptTypes; }
  93. }
  94. private string[] _acceptTypes;
  95. public Stream Body
  96. {
  97. get { return _body; }
  98. set { _body = value;}
  99. }
  100. private Stream _body;
  101. public ConnectionType Connection
  102. {
  103. get { return _connection; }
  104. set { _connection = value; }
  105. }
  106. private ConnectionType _connection;
  107. public int ContentLength
  108. {
  109. get { return _contentLength; }
  110. set { _contentLength = value; }
  111. }
  112. private int _contentLength;
  113. public NameValueCollection Headers
  114. {
  115. get { return _headers; }
  116. }
  117. private NameValueCollection _headers = new NameValueCollection();
  118. public string HttpVersion
  119. {
  120. get { return _httpVersion; }
  121. set { _httpVersion = value; }
  122. }
  123. private string _httpVersion = null;
  124. public string Method
  125. {
  126. get { return _method; }
  127. set { _method = value; }
  128. }
  129. private string _method = null;
  130. public HttpInput QueryString
  131. {
  132. get { return _queryString; }
  133. }
  134. private HttpInput _queryString = null;
  135. public Uri Uri
  136. {
  137. get { return _uri; }
  138. set { _uri = value; }
  139. }
  140. private Uri _uri = null;
  141. public string[] UriParts
  142. {
  143. get { return _uri.Segments; }
  144. }
  145. public HttpParam Param
  146. {
  147. get { return null; }
  148. }
  149. public HttpForm Form
  150. {
  151. get { return null; }
  152. }
  153. public bool IsAjax
  154. {
  155. get { return false; }
  156. }
  157. public RequestCookies Cookies
  158. {
  159. get { return null; }
  160. }
  161. public TestHttpRequest() {}
  162. public TestHttpRequest(string contentEncoding, string contentType, string userAgent,
  163. string remoteAddr, string remotePort, string[] acceptTypes,
  164. ConnectionType connectionType, int contentLength, Uri uri)
  165. {
  166. _headers["content-encoding"] = contentEncoding;
  167. _headers["content-type"] = contentType;
  168. _headers["user-agent"] = userAgent;
  169. _headers["remote_addr"] = remoteAddr;
  170. _headers["remote_port"] = remotePort;
  171. _acceptTypes = acceptTypes;
  172. _connection = connectionType;
  173. _contentLength = contentLength;
  174. _uri = uri;
  175. }
  176. public void DecodeBody(FormDecoderProvider providers) {}
  177. public void SetCookies(RequestCookies cookies) {}
  178. public void AddHeader(string name, string value)
  179. {
  180. _headers.Add(name, value);
  181. }
  182. public int AddToBody(byte[] bytes, int offset, int length)
  183. {
  184. return 0;
  185. }
  186. public void Clear() {}
  187. public object Clone()
  188. {
  189. TestHttpRequest clone = new TestHttpRequest();
  190. clone._acceptTypes = _acceptTypes;
  191. clone._connection = _connection;
  192. clone._contentLength = _contentLength;
  193. clone._uri = _uri;
  194. clone._headers = new NameValueCollection(_headers);
  195. return clone;
  196. }
  197. public IHttpResponse CreateResponse(IHttpClientContext context)
  198. {
  199. return new HttpResponse(context, this);
  200. }
  201. /// <summary>
  202. /// Path and query (will be merged with the host header) and put in Uri
  203. /// </summary>
  204. /// <see cref="Uri"/>
  205. public string UriPath
  206. {
  207. get { return _uriPath; }
  208. set
  209. {
  210. _uriPath = value;
  211. }
  212. }
  213. }
  214. public class TestHttpResponse: IHttpResponse
  215. {
  216. public Stream Body
  217. {
  218. get { return _body; }
  219. set { _body = value; }
  220. }
  221. private Stream _body;
  222. public string ProtocolVersion
  223. {
  224. get { return _protocolVersion; }
  225. set { _protocolVersion = value; }
  226. }
  227. private string _protocolVersion;
  228. public bool Chunked
  229. {
  230. get { return _chunked; }
  231. set { _chunked = value; }
  232. }
  233. private bool _chunked;
  234. public ConnectionType Connection
  235. {
  236. get { return _connection; }
  237. set { _connection = value; }
  238. }
  239. private ConnectionType _connection;
  240. public Encoding Encoding
  241. {
  242. get { return _encoding; }
  243. set { _encoding = value; }
  244. }
  245. private Encoding _encoding;
  246. public int KeepAlive
  247. {
  248. get { return _keepAlive; }
  249. set { _keepAlive = value; }
  250. }
  251. private int _keepAlive;
  252. public HttpStatusCode Status
  253. {
  254. get { return _status; }
  255. set { _status = value; }
  256. }
  257. private HttpStatusCode _status;
  258. public string Reason
  259. {
  260. get { return _reason; }
  261. set { _reason = value; }
  262. }
  263. private string _reason;
  264. public long ContentLength
  265. {
  266. get { return _contentLength; }
  267. set { _contentLength = value; }
  268. }
  269. private long _contentLength;
  270. public string ContentType
  271. {
  272. get { return _contentType; }
  273. set { _contentType = value; }
  274. }
  275. private string _contentType;
  276. public bool HeadersSent
  277. {
  278. get { return _headersSent; }
  279. }
  280. private bool _headersSent;
  281. public bool Sent
  282. {
  283. get { return _sent; }
  284. }
  285. private bool _sent;
  286. public ResponseCookies Cookies
  287. {
  288. get { return _cookies; }
  289. }
  290. private ResponseCookies _cookies = null;
  291. public TestHttpResponse()
  292. {
  293. _headersSent = false;
  294. _sent = false;
  295. }
  296. public void AddHeader(string name, string value) {}
  297. public void Send()
  298. {
  299. if (!_headersSent) SendHeaders();
  300. if (_sent) throw new InvalidOperationException("stuff already sent");
  301. _sent = true;
  302. }
  303. public void SendBody(byte[] buffer, int offset, int count)
  304. {
  305. if (!_headersSent) SendHeaders();
  306. _sent = true;
  307. }
  308. public void SendBody(byte[] buffer)
  309. {
  310. if (!_headersSent) SendHeaders();
  311. _sent = true;
  312. }
  313. public void SendHeaders()
  314. {
  315. if (_headersSent) throw new InvalidOperationException("headers already sent");
  316. _headersSent = true;
  317. }
  318. public void Redirect(Uri uri) {}
  319. public void Redirect(string url) {}
  320. }
  321. public OSHttpRequest req0;
  322. public OSHttpRequest req1;
  323. public OSHttpResponse rsp0;
  324. public IPEndPoint ipEP0;
  325. [TestFixtureSetUp]
  326. public void Init()
  327. {
  328. TestHttpRequest threq0 = new TestHttpRequest("utf-8", "text/xml", "OpenSim Test Agent", "192.168.0.1", "4711",
  329. new string[] {"text/xml"},
  330. ConnectionType.KeepAlive, 4711,
  331. new Uri("http://127.0.0.1/admin/inventory/Dr+Who/Tardis"));
  332. threq0.Method = "GET";
  333. threq0.HttpVersion = HttpHelper.HTTP10;
  334. TestHttpRequest threq1 = new TestHttpRequest("utf-8", "text/xml", "OpenSim Test Agent", "192.168.0.1", "4711",
  335. new string[] {"text/xml"},
  336. ConnectionType.KeepAlive, 4711,
  337. new Uri("http://127.0.0.1/admin/inventory/Dr+Who/Tardis?a=0&b=1&c=2"));
  338. threq1.Method = "POST";
  339. threq1.HttpVersion = HttpHelper.HTTP11;
  340. threq1.Headers["x-wuff"] = "wuffwuff";
  341. threq1.Headers["www-authenticate"] = "go away";
  342. req0 = new OSHttpRequest(new TestHttpClientContext(false), threq0);
  343. req1 = new OSHttpRequest(new TestHttpClientContext(false), threq1);
  344. rsp0 = new OSHttpResponse(new TestHttpResponse());
  345. ipEP0 = new IPEndPoint(IPAddress.Parse("192.168.0.1"), 4711);
  346. }
  347. [Test]
  348. public void T000_OSHttpRequest()
  349. {
  350. Assert.That(req0.HttpMethod, Is.EqualTo("GET"));
  351. Assert.That(req0.ContentType, Is.EqualTo("text/xml"));
  352. Assert.That(req0.ContentLength, Is.EqualTo(4711));
  353. Assert.That(req1.HttpMethod, Is.EqualTo("POST"));
  354. }
  355. [Test]
  356. public void T001_OSHttpRequestHeaderAccess()
  357. {
  358. Assert.That(req1.Headers["x-wuff"], Is.EqualTo("wuffwuff"));
  359. Assert.That(req1.Headers.Get("x-wuff"), Is.EqualTo("wuffwuff"));
  360. Assert.That(req1.Headers["www-authenticate"], Is.EqualTo("go away"));
  361. Assert.That(req1.Headers.Get("www-authenticate"), Is.EqualTo("go away"));
  362. Assert.That(req0.RemoteIPEndPoint, Is.EqualTo(ipEP0));
  363. }
  364. [Test]
  365. public void T002_OSHttpRequestUriParsing()
  366. {
  367. Assert.That(req0.RawUrl, Is.EqualTo("/admin/inventory/Dr+Who/Tardis"));
  368. Assert.That(req1.Url.ToString(), Is.EqualTo("http://127.0.0.1/admin/inventory/Dr+Who/Tardis?a=0&b=1&c=2"));
  369. }
  370. [Test]
  371. public void T100_OSHttpResponse()
  372. {
  373. rsp0.ContentType = "text/xml";
  374. Assert.That(rsp0.ContentType, Is.EqualTo("text/xml"));
  375. }
  376. }
  377. */
  378. }