diff options
-rw-r--r-- | OpenSim/Framework/Servers/Tests/OSHttpTests.cs | 249 |
1 files changed, 249 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..c6132f8 --- /dev/null +++ b/OpenSim/Framework/Servers/Tests/OSHttpTests.cs | |||
@@ -0,0 +1,249 @@ | |||
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 HttpServer; | ||
34 | using HttpServer.Exceptions; | ||
35 | using HttpServer.FormDecoders; | ||
36 | using NUnit.Framework; | ||
37 | using NUnit.Framework.SyntaxHelpers; | ||
38 | |||
39 | using OpenSim.Framework.Servers; | ||
40 | |||
41 | namespace OpenSim.Framework.Servers.Tests | ||
42 | { | ||
43 | [TestFixture] | ||
44 | public class OSHttpTests | ||
45 | { | ||
46 | // we need an IHttpClientContext for our tests | ||
47 | public class TestHttpClientContext: HttpServer.IHttpClientContext | ||
48 | { | ||
49 | private bool _secured; | ||
50 | public bool Secured | ||
51 | { | ||
52 | get { return _secured; } | ||
53 | } | ||
54 | |||
55 | public TestHttpClientContext(bool secured) | ||
56 | { | ||
57 | _secured = secured; | ||
58 | } | ||
59 | |||
60 | public void Disconnect(SocketError error) {} | ||
61 | public void Respond(string httpVersion, HttpStatusCode statusCode, string reason, string body) {} | ||
62 | public void Respond(string httpVersion, HttpStatusCode statusCode, string reason) {} | ||
63 | public void Respond(string body) {} | ||
64 | public void Send(byte[] buffer) {} | ||
65 | public void Send(byte[] buffer, int offset, int size) {} | ||
66 | } | ||
67 | |||
68 | public class TestHttpRequest: HttpServer.IHttpRequest | ||
69 | { | ||
70 | public bool BodyIsComplete | ||
71 | { | ||
72 | get { return true; } | ||
73 | } | ||
74 | public string[] AcceptTypes | ||
75 | { | ||
76 | get {return _acceptTypes; } | ||
77 | } | ||
78 | private string[] _acceptTypes; | ||
79 | public Stream Body | ||
80 | { | ||
81 | get { return _body; } | ||
82 | set { _body = value;} | ||
83 | } | ||
84 | private Stream _body; | ||
85 | public ConnectionType Connection | ||
86 | { | ||
87 | get { return _connection; } | ||
88 | set { _connection = value; } | ||
89 | } | ||
90 | private ConnectionType _connection; | ||
91 | public int ContentLength | ||
92 | { | ||
93 | get { return _contentLength; } | ||
94 | set { _contentLength = value; } | ||
95 | } | ||
96 | private int _contentLength; | ||
97 | public NameValueCollection Headers | ||
98 | { | ||
99 | get { return _headers; } | ||
100 | } | ||
101 | private NameValueCollection _headers = new NameValueCollection(); | ||
102 | public string HttpVersion | ||
103 | { | ||
104 | get { return _httpVersion; } | ||
105 | set { _httpVersion = value; } | ||
106 | } | ||
107 | private string _httpVersion = null; | ||
108 | public string Method | ||
109 | { | ||
110 | get { return _method; } | ||
111 | set { _method = value; } | ||
112 | } | ||
113 | private string _method = null; | ||
114 | public HttpInput QueryString | ||
115 | { | ||
116 | get { return _queryString; } | ||
117 | } | ||
118 | private HttpInput _queryString = null; | ||
119 | public Uri Uri | ||
120 | { | ||
121 | get { return _uri; } | ||
122 | set { _uri = value; } | ||
123 | } | ||
124 | private Uri _uri = null; | ||
125 | public string[] UriParts | ||
126 | { | ||
127 | get { return _uri.Segments; } | ||
128 | } | ||
129 | public HttpParam Param | ||
130 | { | ||
131 | get { return null; } | ||
132 | } | ||
133 | public HttpForm Form | ||
134 | { | ||
135 | get { return null; } | ||
136 | } | ||
137 | public bool IsAjax | ||
138 | { | ||
139 | get { return false; } | ||
140 | } | ||
141 | public RequestCookies Cookies | ||
142 | { | ||
143 | get { return null; } | ||
144 | } | ||
145 | |||
146 | public TestHttpRequest() {} | ||
147 | |||
148 | public TestHttpRequest(string contentEncoding, string contentType, string userAgent, | ||
149 | string remoteAddr, string remotePort, string[] acceptTypes, | ||
150 | ConnectionType connectionType, int contentLength, Uri uri) | ||
151 | { | ||
152 | _headers["content-encoding"] = contentEncoding; | ||
153 | _headers["content-type"] = contentType; | ||
154 | _headers["user-agent"] = userAgent; | ||
155 | _headers["remote_addr"] = remoteAddr; | ||
156 | _headers["remote_port"] = remotePort; | ||
157 | |||
158 | _acceptTypes = acceptTypes; | ||
159 | _connection = connectionType; | ||
160 | _contentLength = contentLength; | ||
161 | _uri = uri; | ||
162 | } | ||
163 | |||
164 | public void DecodeBody(FormDecoderProvider providers) {} | ||
165 | public void SetCookies(RequestCookies cookies) {} | ||
166 | public void AddHeader(string name, string value) | ||
167 | { | ||
168 | _headers.Add(name, value); | ||
169 | } | ||
170 | public int AddToBody(byte[] bytes, int offset, int length) | ||
171 | { | ||
172 | return 0; | ||
173 | } | ||
174 | public void Clear() {} | ||
175 | |||
176 | public object Clone() | ||
177 | { | ||
178 | TestHttpRequest clone = new TestHttpRequest(); | ||
179 | clone._acceptTypes = _acceptTypes; | ||
180 | clone._connection = _connection; | ||
181 | clone._contentLength = _contentLength; | ||
182 | clone._uri = _uri; | ||
183 | clone._headers = new NameValueCollection(_headers); | ||
184 | |||
185 | return clone; | ||
186 | } | ||
187 | } | ||
188 | |||
189 | |||
190 | public OSHttpRequest r0; | ||
191 | public OSHttpRequest r1; | ||
192 | |||
193 | public IPEndPoint ipEP0; | ||
194 | |||
195 | [TestFixtureSetUp] | ||
196 | public void Init() | ||
197 | { | ||
198 | TestHttpRequest thr0 = new TestHttpRequest("utf-8", "text/xml", "OpenSim Test Agent", "192.168.0.1", "4711", | ||
199 | new string[] {"text/xml"}, | ||
200 | ConnectionType.KeepAlive, 4711, | ||
201 | new Uri("http://127.0.0.1/admin/inventory/Dr+Who/Tardis")); | ||
202 | thr0.Method = "GET"; | ||
203 | thr0.HttpVersion = HttpHelper.HTTP10; | ||
204 | |||
205 | TestHttpRequest thr1 = new TestHttpRequest("utf-8", "text/xml", "OpenSim Test Agent", "192.168.0.1", "4711", | ||
206 | new string[] {"text/xml"}, | ||
207 | ConnectionType.KeepAlive, 4711, | ||
208 | new Uri("http://127.0.0.1/admin/inventory/Dr+Who/Tardis?a=0&b=1&c=2")); | ||
209 | thr1.Method = "POST"; | ||
210 | thr1.HttpVersion = HttpHelper.HTTP11; | ||
211 | thr1.Headers["x-wuff"] = "wuffwuff"; | ||
212 | thr1.Headers["www-authenticate"] = "go away"; | ||
213 | |||
214 | r0 = new OSHttpRequest(new TestHttpClientContext(false), thr0); | ||
215 | r1 = new OSHttpRequest(new TestHttpClientContext(false), thr1); | ||
216 | |||
217 | ipEP0 = new IPEndPoint(IPAddress.Parse("192.168.0.1"), 4711); | ||
218 | } | ||
219 | |||
220 | [Test] | ||
221 | public void T001_SimpleOSHttpRequest() | ||
222 | { | ||
223 | Assert.That(r0.HttpMethod, Is.EqualTo("GET")); | ||
224 | Assert.That(r0.ContentType, Is.EqualTo("text/xml")); | ||
225 | Assert.That(r0.ContentLength, Is.EqualTo(4711)); | ||
226 | |||
227 | Assert.That(r1.HttpMethod, Is.EqualTo("POST")); | ||
228 | } | ||
229 | |||
230 | [Test] | ||
231 | public void T002_HeaderAccess() | ||
232 | { | ||
233 | Assert.That(r1.Headers["x-wuff"], Is.EqualTo("wuffwuff")); | ||
234 | Assert.That(r1.Headers.Get("x-wuff"), Is.EqualTo("wuffwuff")); | ||
235 | |||
236 | Assert.That(r1.Headers["www-authenticate"], Is.EqualTo("go away")); | ||
237 | Assert.That(r1.Headers.Get("www-authenticate"), Is.EqualTo("go away")); | ||
238 | |||
239 | Assert.That(r0.RemoteIPEndPoint, Is.EqualTo(ipEP0)); | ||
240 | } | ||
241 | |||
242 | [Test] | ||
243 | public void T003_UriParsing() | ||
244 | { | ||
245 | Assert.That(r0.RawUrl, Is.EqualTo("/admin/inventory/Dr+Who/Tardis")); | ||
246 | Assert.That(r1.Url.ToString(), Is.EqualTo("http://127.0.0.1/admin/inventory/Dr+Who/Tardis?a=0&b=1&c=2")); | ||
247 | } | ||
248 | } | ||
249 | } | ||