aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/CoreModules/Scripting/HttpRequest/Tests
diff options
context:
space:
mode:
authorDavid Walter Seikel2016-11-03 21:44:39 +1000
committerDavid Walter Seikel2016-11-03 21:44:39 +1000
commit134f86e8d5c414409631b25b8c6f0ee45fbd8631 (patch)
tree216b89d3fb89acfb81be1e440c25c41ab09fa96d /OpenSim/Region/CoreModules/Scripting/HttpRequest/Tests
parentMore changing to production grid. Double oops. (diff)
downloadopensim-SC-134f86e8d5c414409631b25b8c6f0ee45fbd8631.zip
opensim-SC-134f86e8d5c414409631b25b8c6f0ee45fbd8631.tar.gz
opensim-SC-134f86e8d5c414409631b25b8c6f0ee45fbd8631.tar.bz2
opensim-SC-134f86e8d5c414409631b25b8c6f0ee45fbd8631.tar.xz
Initial update to OpenSim 0.8.2.1 source code.
Diffstat (limited to 'OpenSim/Region/CoreModules/Scripting/HttpRequest/Tests')
-rw-r--r--OpenSim/Region/CoreModules/Scripting/HttpRequest/Tests/ScriptsHttpRequestsTests.cs199
1 files changed, 199 insertions, 0 deletions
diff --git a/OpenSim/Region/CoreModules/Scripting/HttpRequest/Tests/ScriptsHttpRequestsTests.cs b/OpenSim/Region/CoreModules/Scripting/HttpRequest/Tests/ScriptsHttpRequestsTests.cs
new file mode 100644
index 0000000..d22487e
--- /dev/null
+++ b/OpenSim/Region/CoreModules/Scripting/HttpRequest/Tests/ScriptsHttpRequestsTests.cs
@@ -0,0 +1,199 @@
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
28using System;
29using System.Collections.Generic;
30using System.IO;
31using System.Net;
32using System.Reflection;
33using System.Runtime.Serialization;
34using System.Text;
35using System.Threading;
36using log4net.Config;
37using NUnit.Framework;
38using OpenMetaverse;
39using OpenMetaverse.Assets;
40using OpenSim.Framework;
41using OpenSim.Region.CoreModules.Scripting.HttpRequest;
42using OpenSim.Region.Framework.Scenes;
43using OpenSim.Tests.Common;
44
45namespace OpenSim.Region.CoreModules.Scripting.HttpRequest.Tests
46{
47 class TestWebRequestCreate : IWebRequestCreate
48 {
49 public TestWebRequest NextRequest { get; set; }
50
51 public WebRequest Create(Uri uri)
52 {
53// NextRequest.RequestUri = uri;
54
55 return NextRequest;
56
57// return new TestWebRequest(new SerializationInfo(typeof(TestWebRequest), new FormatterConverter()), new StreamingContext());
58 }
59 }
60
61 class TestWebRequest : WebRequest
62 {
63 public override string ContentType { get; set; }
64 public override string Method { get; set; }
65
66 public Func<IAsyncResult, WebResponse> OnEndGetResponse { get; set; }
67
68 public TestWebRequest() : base()
69 {
70// Console.WriteLine("created");
71 }
72
73// public TestWebRequest(SerializationInfo serializationInfo, StreamingContext streamingContext)
74// : base(serializationInfo, streamingContext)
75// {
76// Console.WriteLine("created");
77// }
78
79 public override IAsyncResult BeginGetResponse(AsyncCallback callback, object state)
80 {
81// Console.WriteLine("bish");
82 TestAsyncResult tasr = new TestAsyncResult();
83 callback(tasr);
84
85 return tasr;
86 }
87
88 public override WebResponse EndGetResponse(IAsyncResult asyncResult)
89 {
90// Console.WriteLine("bosh");
91 return OnEndGetResponse(asyncResult);
92 }
93 }
94
95 class TestHttpWebResponse : HttpWebResponse
96 {
97 public string Response { get; set; }
98
99#pragma warning disable 0618
100 public TestHttpWebResponse(SerializationInfo serializationInfo, StreamingContext streamingContext)
101 : base(serializationInfo, streamingContext) {}
102#pragma warning restore 0618
103
104 public override Stream GetResponseStream()
105 {
106 return new MemoryStream(Encoding.UTF8.GetBytes(Response));
107 }
108 }
109
110 class TestAsyncResult : IAsyncResult
111 {
112 WaitHandle m_wh = new ManualResetEvent(true);
113
114 object IAsyncResult.AsyncState
115 {
116 get {
117 throw new System.NotImplementedException ();
118 }
119 }
120
121 WaitHandle IAsyncResult.AsyncWaitHandle
122 {
123 get { return m_wh; }
124 }
125
126 bool IAsyncResult.CompletedSynchronously
127 {
128 get { return false; }
129 }
130
131 bool IAsyncResult.IsCompleted
132 {
133 get { return true; }
134 }
135 }
136
137 /// <summary>
138 /// Test script http request code.
139 /// </summary>
140 /// <remarks>
141 /// This class uses some very hacky workarounds in order to mock HttpWebResponse which are Mono dependent (though
142 /// alternative code can be written to make this work for Windows). However, the value of being able to
143 /// regression test this kind of code is very high.
144 /// </remarks>
145 [TestFixture]
146 public class ScriptsHttpRequestsTests : OpenSimTestCase
147 {
148 /// <summary>
149 /// Test what happens when we get a 404 response from a call.
150 /// </summary>
151// [Test]
152 public void Test404Response()
153 {
154 TestHelpers.InMethod();
155 TestHelpers.EnableLogging();
156
157 if (!Util.IsPlatformMono)
158 Assert.Ignore("Ignoring test since can only currently run on Mono");
159
160 string rawResponse = "boom";
161
162 TestWebRequestCreate twrc = new TestWebRequestCreate();
163
164 TestWebRequest twr = new TestWebRequest();
165 //twr.OnEndGetResponse += ar => new TestHttpWebResponse(null, new StreamingContext());
166 twr.OnEndGetResponse += ar =>
167 {
168 SerializationInfo si = new SerializationInfo(typeof(HttpWebResponse), new FormatterConverter());
169 StreamingContext sc = new StreamingContext();
170// WebHeaderCollection headers = new WebHeaderCollection();
171// si.AddValue("m_HttpResponseHeaders", headers);
172 si.AddValue("uri", new Uri("test://arrg"));
173// si.AddValue("m_Certificate", null);
174 si.AddValue("version", HttpVersion.Version11);
175 si.AddValue("statusCode", HttpStatusCode.NotFound);
176 si.AddValue("contentLength", 0);
177 si.AddValue("method", "GET");
178 si.AddValue("statusDescription", "Not Found");
179 si.AddValue("contentType", null);
180 si.AddValue("cookieCollection", new CookieCollection());
181
182 TestHttpWebResponse thwr = new TestHttpWebResponse(si, sc);
183 thwr.Response = rawResponse;
184
185 throw new WebException("no message", null, WebExceptionStatus.ProtocolError, thwr);
186 };
187
188 twrc.NextRequest = twr;
189
190 WebRequest.RegisterPrefix("test", twrc);
191 HttpRequestClass hr = new HttpRequestClass();
192 hr.Url = "test://something";
193 hr.SendRequest();
194
195 Assert.That(hr.Status, Is.EqualTo((int)HttpStatusCode.NotFound));
196 Assert.That(hr.ResponseBody, Is.EqualTo(rawResponse));
197 }
198 }
199} \ No newline at end of file