OSHttpTests.cs 13 KB

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