ScriptsHttpRequestsTests.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. public TestHttpWebResponse(SerializationInfo serializationInfo, StreamingContext streamingContext)
  86. : base(serializationInfo, streamingContext) {}
  87. public override Stream GetResponseStream()
  88. {
  89. return new MemoryStream(Encoding.UTF8.GetBytes(Response));
  90. }
  91. }
  92. class TestAsyncResult : IAsyncResult
  93. {
  94. WaitHandle m_wh = new ManualResetEvent(true);
  95. object IAsyncResult.AsyncState
  96. {
  97. get {
  98. throw new System.NotImplementedException ();
  99. }
  100. }
  101. WaitHandle IAsyncResult.AsyncWaitHandle
  102. {
  103. get { return m_wh; }
  104. }
  105. bool IAsyncResult.CompletedSynchronously
  106. {
  107. get { return false; }
  108. }
  109. bool IAsyncResult.IsCompleted
  110. {
  111. get { return true; }
  112. }
  113. }
  114. /// <summary>
  115. /// Test script http request code.
  116. /// </summary>
  117. /// <remarks>
  118. /// This class uses some very hacky workarounds in order to mock HttpWebResponse which are Mono dependent (though
  119. /// alternative code can be written to make this work for Windows). However, the value of being able to
  120. /// regression test this kind of code is very high.
  121. /// </remarks>
  122. [TestFixture]
  123. public class ScriptsHttpRequestsTests : OpenSimTestCase
  124. {
  125. /// <summary>
  126. /// Test what happens when we get a 404 response from a call.
  127. /// </summary>
  128. [Test]
  129. public void Test404Response()
  130. {
  131. TestHelpers.InMethod();
  132. // TestHelpers.EnableLogging();
  133. if (!Util.IsPlatformMono)
  134. Assert.Ignore("Ignoring test since can only currently run on Mono");
  135. string rawResponse = "boom";
  136. TestWebRequestCreate twrc = new TestWebRequestCreate();
  137. TestWebRequest twr = new TestWebRequest();
  138. //twr.OnEndGetResponse += ar => new TestHttpWebResponse(null, new StreamingContext());
  139. twr.OnEndGetResponse += ar =>
  140. {
  141. SerializationInfo si = new SerializationInfo(typeof(HttpWebResponse), new FormatterConverter());
  142. StreamingContext sc = new StreamingContext();
  143. // WebHeaderCollection headers = new WebHeaderCollection();
  144. // si.AddValue("m_HttpResponseHeaders", headers);
  145. si.AddValue("uri", new Uri("test://arrg"));
  146. // si.AddValue("m_Certificate", null);
  147. si.AddValue("version", HttpVersion.Version11);
  148. si.AddValue("statusCode", HttpStatusCode.NotFound);
  149. si.AddValue("contentLength", 0);
  150. si.AddValue("method", "GET");
  151. si.AddValue("statusDescription", "Not Found");
  152. si.AddValue("contentType", null);
  153. si.AddValue("cookieCollection", new CookieCollection());
  154. TestHttpWebResponse thwr = new TestHttpWebResponse(si, sc);
  155. thwr.Response = rawResponse;
  156. throw new WebException("no message", null, WebExceptionStatus.ProtocolError, thwr);
  157. };
  158. twrc.NextRequest = twr;
  159. WebRequest.RegisterPrefix("test", twrc);
  160. HttpRequestClass hr = new HttpRequestClass();
  161. hr.Url = "test://something";
  162. hr.SendRequest();
  163. Assert.That(hr.Status, Is.EqualTo((int)HttpStatusCode.NotFound));
  164. Assert.That(hr.ResponseBody, Is.EqualTo(rawResponse));
  165. }
  166. }
  167. }