diff options
Diffstat (limited to 'OpenSim/Region/CoreModules/Scripting/HttpRequest/Tests/ScriptsHttpRequestsTests.cs')
-rw-r--r-- | OpenSim/Region/CoreModules/Scripting/HttpRequest/Tests/ScriptsHttpRequestsTests.cs | 199 |
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 | |||
28 | using System; | ||
29 | using System.Collections.Generic; | ||
30 | using System.IO; | ||
31 | using System.Net; | ||
32 | using System.Reflection; | ||
33 | using System.Runtime.Serialization; | ||
34 | using System.Text; | ||
35 | using System.Threading; | ||
36 | using log4net.Config; | ||
37 | using NUnit.Framework; | ||
38 | using OpenMetaverse; | ||
39 | using OpenMetaverse.Assets; | ||
40 | using OpenSim.Framework; | ||
41 | using OpenSim.Region.CoreModules.Scripting.HttpRequest; | ||
42 | using OpenSim.Region.Framework.Scenes; | ||
43 | using OpenSim.Tests.Common; | ||
44 | |||
45 | namespace 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 | ||