OSHttpTests.cs 15 KB

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