diff options
Diffstat (limited to 'OpenSim/Framework/Servers/Tests')
-rw-r--r-- | OpenSim/Framework/Servers/Tests/OSHttpTests.cs | 394 |
1 files changed, 394 insertions, 0 deletions
diff --git a/OpenSim/Framework/Servers/Tests/OSHttpTests.cs b/OpenSim/Framework/Servers/Tests/OSHttpTests.cs new file mode 100644 index 0000000..ee8ab09 --- /dev/null +++ b/OpenSim/Framework/Servers/Tests/OSHttpTests.cs | |||
@@ -0,0 +1,394 @@ | |||
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 OpenSim 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.Specialized; | ||
30 | using System.IO; | ||
31 | using System.Net; | ||
32 | using System.Net.Sockets; | ||
33 | using System.Text; | ||
34 | using HttpServer; | ||
35 | using HttpServer.FormDecoders; | ||
36 | using NUnit.Framework; | ||
37 | using NUnit.Framework.SyntaxHelpers; | ||
38 | using OpenSim.Framework.Servers.HttpServer; | ||
39 | |||
40 | namespace OpenSim.Framework.Servers.Tests | ||
41 | { | ||
42 | [TestFixture] | ||
43 | public class OSHttpTests | ||
44 | { | ||
45 | // we need an IHttpClientContext for our tests | ||
46 | public class TestHttpClientContext: IHttpClientContext | ||
47 | { | ||
48 | private bool _secured; | ||
49 | public bool Secured | ||
50 | { | ||
51 | get { return _secured; } | ||
52 | } | ||
53 | |||
54 | public TestHttpClientContext(bool secured) | ||
55 | { | ||
56 | _secured = secured; | ||
57 | } | ||
58 | |||
59 | public void Disconnect(SocketError error) {} | ||
60 | public void Respond(string httpVersion, HttpStatusCode statusCode, string reason, string body) {} | ||
61 | public void Respond(string httpVersion, HttpStatusCode statusCode, string reason) {} | ||
62 | public void Respond(string body) {} | ||
63 | public void Send(byte[] buffer) {} | ||
64 | public void Send(byte[] buffer, int offset, int size) {} | ||
65 | } | ||
66 | |||
67 | public class TestHttpRequest: IHttpRequest | ||
68 | { | ||
69 | public bool BodyIsComplete | ||
70 | { | ||
71 | get { return true; } | ||
72 | } | ||
73 | public string[] AcceptTypes | ||
74 | { | ||
75 | get {return _acceptTypes; } | ||
76 | } | ||
77 | private string[] _acceptTypes; | ||
78 | public Stream Body | ||
79 | { | ||
80 | get { return _body; } | ||
81 | set { _body = value;} | ||
82 | } | ||
83 | private Stream _body; | ||
84 | public ConnectionType Connection | ||
85 | { | ||
86 | get { return _connection; } | ||
87 | set { _connection = value; } | ||
88 | } | ||
89 | private ConnectionType _connection; | ||
90 | public int ContentLength | ||
91 | { | ||
92 | get { return _contentLength; } | ||
93 | set { _contentLength = value; } | ||
94 | } | ||
95 | private int _contentLength; | ||
96 | public NameValueCollection Headers | ||
97 | { | ||
98 | get { return _headers; } | ||
99 | } | ||
100 | private NameValueCollection _headers = new NameValueCollection(); | ||
101 | public string HttpVersion | ||
102 | { | ||
103 | get { return _httpVersion; } | ||
104 | set { _httpVersion = value; } | ||
105 | } | ||
106 | private string _httpVersion = null; | ||
107 | public string Method | ||
108 | { | ||
109 | get { return _method; } | ||
110 | set { _method = value; } | ||
111 | } | ||
112 | private string _method = null; | ||
113 | public HttpInput QueryString | ||
114 | { | ||
115 | get { return _queryString; } | ||
116 | } | ||
117 | private HttpInput _queryString = null; | ||
118 | public Uri Uri | ||
119 | { | ||
120 | get { return _uri; } | ||
121 | set { _uri = value; } | ||
122 | } | ||
123 | private Uri _uri = null; | ||
124 | public string[] UriParts | ||
125 | { | ||
126 | get { return _uri.Segments; } | ||
127 | } | ||
128 | public HttpParam Param | ||
129 | { | ||
130 | get { return null; } | ||
131 | } | ||
132 | public HttpForm Form | ||
133 | { | ||
134 | get { return null; } | ||
135 | } | ||
136 | public bool IsAjax | ||
137 | { | ||
138 | get { return false; } | ||
139 | } | ||
140 | public RequestCookies Cookies | ||
141 | { | ||
142 | get { return null; } | ||
143 | } | ||
144 | |||
145 | public TestHttpRequest() {} | ||
146 | |||
147 | public TestHttpRequest(string contentEncoding, string contentType, string userAgent, | ||
148 | string remoteAddr, string remotePort, string[] acceptTypes, | ||
149 | ConnectionType connectionType, int contentLength, Uri uri) | ||
150 | { | ||
151 | _headers["content-encoding"] = contentEncoding; | ||
152 | _headers["content-type"] = contentType; | ||
153 | _headers["user-agent"] = userAgent; | ||
154 | _headers["remote_addr"] = remoteAddr; | ||
155 | _headers["remote_port"] = remotePort; | ||
156 | |||
157 | _acceptTypes = acceptTypes; | ||
158 | _connection = connectionType; | ||
159 | _contentLength = contentLength; | ||
160 | _uri = uri; | ||
161 | } | ||
162 | |||
163 | public void DecodeBody(FormDecoderProvider providers) {} | ||
164 | public void SetCookies(RequestCookies cookies) {} | ||
165 | public void AddHeader(string name, string value) | ||
166 | { | ||
167 | _headers.Add(name, value); | ||
168 | } | ||
169 | public int AddToBody(byte[] bytes, int offset, int length) | ||
170 | { | ||
171 | return 0; | ||
172 | } | ||
173 | public void Clear() {} | ||
174 | |||
175 | public object Clone() | ||
176 | { | ||
177 | TestHttpRequest clone = new TestHttpRequest(); | ||
178 | clone._acceptTypes = _acceptTypes; | ||
179 | clone._connection = _connection; | ||
180 | clone._contentLength = _contentLength; | ||
181 | clone._uri = _uri; | ||
182 | clone._headers = new NameValueCollection(_headers); | ||
183 | |||
184 | return clone; | ||
185 | } | ||
186 | } | ||
187 | |||
188 | public class TestHttpResponse: IHttpResponse | ||
189 | { | ||
190 | public Stream Body | ||
191 | { | ||
192 | get { return _body; } | ||
193 | |||
194 | set { _body = value; } | ||
195 | } | ||
196 | private Stream _body; | ||
197 | |||
198 | public string ProtocolVersion | ||
199 | { | ||
200 | get { return _protocolVersion; } | ||
201 | set { _protocolVersion = value; } | ||
202 | } | ||
203 | private string _protocolVersion; | ||
204 | |||
205 | public bool Chunked | ||
206 | { | ||
207 | get { return _chunked; } | ||
208 | |||
209 | set { _chunked = value; } | ||
210 | } | ||
211 | private bool _chunked; | ||
212 | |||
213 | public ConnectionType Connection | ||
214 | { | ||
215 | get { return _connection; } | ||
216 | |||
217 | set { _connection = value; } | ||
218 | } | ||
219 | private ConnectionType _connection; | ||
220 | |||
221 | public Encoding Encoding | ||
222 | { | ||
223 | get { return _encoding; } | ||
224 | |||
225 | set { _encoding = value; } | ||
226 | } | ||
227 | private Encoding _encoding; | ||
228 | |||
229 | public int KeepAlive | ||
230 | { | ||
231 | get { return _keepAlive; } | ||
232 | |||
233 | set { _keepAlive = value; } | ||
234 | } | ||
235 | private int _keepAlive; | ||
236 | |||
237 | public HttpStatusCode Status | ||
238 | { | ||
239 | get { return _status; } | ||
240 | |||
241 | set { _status = value; } | ||
242 | } | ||
243 | private HttpStatusCode _status; | ||
244 | |||
245 | public string Reason | ||
246 | { | ||
247 | get { return _reason; } | ||
248 | |||
249 | set { _reason = value; } | ||
250 | } | ||
251 | private string _reason; | ||
252 | |||
253 | public long ContentLength | ||
254 | { | ||
255 | get { return _contentLength; } | ||
256 | |||
257 | set { _contentLength = value; } | ||
258 | } | ||
259 | private long _contentLength; | ||
260 | |||
261 | public string ContentType | ||
262 | { | ||
263 | get { return _contentType; } | ||
264 | |||
265 | set { _contentType = value; } | ||
266 | } | ||
267 | private string _contentType; | ||
268 | |||
269 | public bool HeadersSent | ||
270 | { | ||
271 | get { return _headersSent; } | ||
272 | } | ||
273 | private bool _headersSent; | ||
274 | |||
275 | public bool Sent | ||
276 | { | ||
277 | get { return _sent; } | ||
278 | } | ||
279 | private bool _sent; | ||
280 | |||
281 | public ResponseCookies Cookies | ||
282 | { | ||
283 | get { return _cookies; } | ||
284 | } | ||
285 | private ResponseCookies _cookies = null; | ||
286 | |||
287 | public TestHttpResponse() | ||
288 | { | ||
289 | _headersSent = false; | ||
290 | _sent = false; | ||
291 | } | ||
292 | |||
293 | public void AddHeader(string name, string value) {} | ||
294 | public void Send() | ||
295 | { | ||
296 | if (!_headersSent) SendHeaders(); | ||
297 | if (_sent) throw new InvalidOperationException("stuff already sent"); | ||
298 | _sent = true; | ||
299 | } | ||
300 | |||
301 | public void SendBody(byte[] buffer, int offset, int count) | ||
302 | { | ||
303 | if (!_headersSent) SendHeaders(); | ||
304 | _sent = true; | ||
305 | } | ||
306 | public void SendBody(byte[] buffer) | ||
307 | { | ||
308 | if (!_headersSent) SendHeaders(); | ||
309 | _sent = true; | ||
310 | } | ||
311 | |||
312 | public void SendHeaders() | ||
313 | { | ||
314 | if (_headersSent) throw new InvalidOperationException("headers already sent"); | ||
315 | _headersSent = true; | ||
316 | } | ||
317 | |||
318 | public void Redirect(Uri uri) {} | ||
319 | public void Redirect(string url) {} | ||
320 | } | ||
321 | |||
322 | |||
323 | public OSHttpRequest req0; | ||
324 | public OSHttpRequest req1; | ||
325 | |||
326 | public OSHttpResponse rsp0; | ||
327 | |||
328 | public IPEndPoint ipEP0; | ||
329 | |||
330 | [TestFixtureSetUp] | ||
331 | public void Init() | ||
332 | { | ||
333 | TestHttpRequest threq0 = new TestHttpRequest("utf-8", "text/xml", "OpenSim Test Agent", "192.168.0.1", "4711", | ||
334 | new string[] {"text/xml"}, | ||
335 | ConnectionType.KeepAlive, 4711, | ||
336 | new Uri("http://127.0.0.1/admin/inventory/Dr+Who/Tardis")); | ||
337 | threq0.Method = "GET"; | ||
338 | threq0.HttpVersion = HttpHelper.HTTP10; | ||
339 | |||
340 | TestHttpRequest threq1 = new TestHttpRequest("utf-8", "text/xml", "OpenSim Test Agent", "192.168.0.1", "4711", | ||
341 | new string[] {"text/xml"}, | ||
342 | ConnectionType.KeepAlive, 4711, | ||
343 | new Uri("http://127.0.0.1/admin/inventory/Dr+Who/Tardis?a=0&b=1&c=2")); | ||
344 | threq1.Method = "POST"; | ||
345 | threq1.HttpVersion = HttpHelper.HTTP11; | ||
346 | threq1.Headers["x-wuff"] = "wuffwuff"; | ||
347 | threq1.Headers["www-authenticate"] = "go away"; | ||
348 | |||
349 | req0 = new OSHttpRequest(new TestHttpClientContext(false), threq0); | ||
350 | req1 = new OSHttpRequest(new TestHttpClientContext(false), threq1); | ||
351 | |||
352 | rsp0 = new OSHttpResponse(new TestHttpResponse()); | ||
353 | |||
354 | ipEP0 = new IPEndPoint(IPAddress.Parse("192.168.0.1"), 4711); | ||
355 | |||
356 | } | ||
357 | |||
358 | [Test] | ||
359 | public void T000_OSHttpRequest() | ||
360 | { | ||
361 | Assert.That(req0.HttpMethod, Is.EqualTo("GET")); | ||
362 | Assert.That(req0.ContentType, Is.EqualTo("text/xml")); | ||
363 | Assert.That(req0.ContentLength, Is.EqualTo(4711)); | ||
364 | |||
365 | Assert.That(req1.HttpMethod, Is.EqualTo("POST")); | ||
366 | } | ||
367 | |||
368 | [Test] | ||
369 | public void T001_OSHttpRequestHeaderAccess() | ||
370 | { | ||
371 | Assert.That(req1.Headers["x-wuff"], Is.EqualTo("wuffwuff")); | ||
372 | Assert.That(req1.Headers.Get("x-wuff"), Is.EqualTo("wuffwuff")); | ||
373 | |||
374 | Assert.That(req1.Headers["www-authenticate"], Is.EqualTo("go away")); | ||
375 | Assert.That(req1.Headers.Get("www-authenticate"), Is.EqualTo("go away")); | ||
376 | |||
377 | Assert.That(req0.RemoteIPEndPoint, Is.EqualTo(ipEP0)); | ||
378 | } | ||
379 | |||
380 | [Test] | ||
381 | public void T002_OSHttpRequestUriParsing() | ||
382 | { | ||
383 | Assert.That(req0.RawUrl, Is.EqualTo("/admin/inventory/Dr+Who/Tardis")); | ||
384 | Assert.That(req1.Url.ToString(), Is.EqualTo("http://127.0.0.1/admin/inventory/Dr+Who/Tardis?a=0&b=1&c=2")); | ||
385 | } | ||
386 | |||
387 | [Test] | ||
388 | public void T100_OSHttpResponse() | ||
389 | { | ||
390 | rsp0.ContentType = "text/xml"; | ||
391 | Assert.That(rsp0.ContentType, Is.EqualTo("text/xml")); | ||
392 | } | ||
393 | } | ||
394 | } | ||