OSHttpTests.cs 14 KB

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