ScriptsHttpRequestsTests.cs 7.1 KB

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