ScriptsHttpRequestsTests.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.Generic;
  29. using System.IO;
  30. using System.Net;
  31. using System.Reflection;
  32. using System.Runtime.Serialization;
  33. using System.Text;
  34. using System.Threading;
  35. using log4net.Config;
  36. using NUnit.Framework;
  37. using OpenMetaverse;
  38. using OpenMetaverse.Assets;
  39. using OpenSim.Framework;
  40. using OpenSim.Region.CoreModules.Scripting.HttpRequest;
  41. using OpenSim.Region.Framework.Scenes;
  42. using OpenSim.Tests.Common;
  43. using OpenSim.Tests.Common.Mock;
  44. namespace OpenSim.Region.CoreModules.Scripting.HttpRequest.Tests
  45. {
  46. class TestWebRequestCreate : IWebRequestCreate
  47. {
  48. public TestWebRequest NextRequest { get; set; }
  49. public WebRequest Create(Uri uri)
  50. {
  51. // NextRequest.RequestUri = uri;
  52. return NextRequest;
  53. // return new TestWebRequest(new SerializationInfo(typeof(TestWebRequest), new FormatterConverter()), new StreamingContext());
  54. }
  55. }
  56. class TestWebRequest : WebRequest
  57. {
  58. public override string ContentType { get; set; }
  59. public override string Method { get; set; }
  60. public Func<IAsyncResult, WebResponse> OnEndGetResponse { get; set; }
  61. public TestWebRequest() : base()
  62. {
  63. // Console.WriteLine("created");
  64. }
  65. // public TestWebRequest(SerializationInfo serializationInfo, StreamingContext streamingContext)
  66. // : base(serializationInfo, streamingContext)
  67. // {
  68. // Console.WriteLine("created");
  69. // }
  70. public override IAsyncResult BeginGetResponse(AsyncCallback callback, object state)
  71. {
  72. // Console.WriteLine("bish");
  73. TestAsyncResult tasr = new TestAsyncResult();
  74. callback(tasr);
  75. return tasr;
  76. }
  77. public override WebResponse EndGetResponse(IAsyncResult asyncResult)
  78. {
  79. // Console.WriteLine("bosh");
  80. return OnEndGetResponse(asyncResult);
  81. }
  82. }
  83. class TestHttpWebResponse : HttpWebResponse
  84. {
  85. public string Response { get; set; }
  86. public TestHttpWebResponse(SerializationInfo serializationInfo, StreamingContext streamingContext)
  87. : base(serializationInfo, streamingContext) {}
  88. public override Stream GetResponseStream()
  89. {
  90. return new MemoryStream(Encoding.UTF8.GetBytes(Response));
  91. }
  92. }
  93. class TestAsyncResult : IAsyncResult
  94. {
  95. WaitHandle m_wh = new ManualResetEvent(true);
  96. object IAsyncResult.AsyncState
  97. {
  98. get {
  99. throw new System.NotImplementedException ();
  100. }
  101. }
  102. WaitHandle IAsyncResult.AsyncWaitHandle
  103. {
  104. get { return m_wh; }
  105. }
  106. bool IAsyncResult.CompletedSynchronously
  107. {
  108. get { return false; }
  109. }
  110. bool IAsyncResult.IsCompleted
  111. {
  112. get { return true; }
  113. }
  114. }
  115. /// <summary>
  116. /// Test script http request code.
  117. /// </summary>
  118. /// <remarks>
  119. /// This class uses some very hacky workarounds in order to mock HttpWebResponse which are Mono dependent (though
  120. /// alternative code can be written to make this work for Windows). However, the value of being able to
  121. /// regression test this kind of code is very high.
  122. /// </remarks>
  123. [TestFixture]
  124. public class ScriptsHttpRequestsTests : OpenSimTestCase
  125. {
  126. /// <summary>
  127. /// Test what happens when we get a 404 response from a call.
  128. /// </summary>
  129. [Test]
  130. public void Test404Response()
  131. {
  132. TestHelpers.InMethod();
  133. // TestHelpers.EnableLogging();
  134. if (!Util.IsPlatformMono)
  135. Assert.Ignore("Ignoring test since can only currently run on Mono");
  136. string rawResponse = "boom";
  137. TestWebRequestCreate twrc = new TestWebRequestCreate();
  138. TestWebRequest twr = new TestWebRequest();
  139. //twr.OnEndGetResponse += ar => new TestHttpWebResponse(null, new StreamingContext());
  140. twr.OnEndGetResponse += ar =>
  141. {
  142. SerializationInfo si = new SerializationInfo(typeof(HttpWebResponse), new FormatterConverter());
  143. StreamingContext sc = new StreamingContext();
  144. // WebHeaderCollection headers = new WebHeaderCollection();
  145. // si.AddValue("m_HttpResponseHeaders", headers);
  146. si.AddValue("uri", new Uri("test://arrg"));
  147. // si.AddValue("m_Certificate", null);
  148. si.AddValue("version", HttpVersion.Version11);
  149. si.AddValue("statusCode", HttpStatusCode.NotFound);
  150. si.AddValue("contentLength", 0);
  151. si.AddValue("method", "GET");
  152. si.AddValue("statusDescription", "Not Found");
  153. si.AddValue("contentType", null);
  154. si.AddValue("cookieCollection", new CookieCollection());
  155. TestHttpWebResponse thwr = new TestHttpWebResponse(si, sc);
  156. thwr.Response = rawResponse;
  157. throw new WebException("no message", null, WebExceptionStatus.ProtocolError, thwr);
  158. };
  159. twrc.NextRequest = twr;
  160. WebRequest.RegisterPrefix("test", twrc);
  161. HttpRequestClass hr = new HttpRequestClass();
  162. hr.Url = "test://something";
  163. hr.SendRequest();
  164. Assert.That(hr.Status, Is.EqualTo((int)HttpStatusCode.NotFound));
  165. Assert.That(hr.ResponseBody, Is.EqualTo(rawResponse));
  166. }
  167. }
  168. }