OSHttpTests.cs 14 KB

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