OSHttpTests.cs 13 KB

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