diff options
author | Dr Scofield | 2008-07-25 10:02:21 +0000 |
---|---|---|
committer | Dr Scofield | 2008-07-25 10:02:21 +0000 |
commit | 50bc51b64216e3b0003bc89edbebcc9914cbf90d (patch) | |
tree | 05a515add1cb71a18780ee88a3486800da6fb4ee | |
parent | From: awebb (diff) | |
download | opensim-SC-50bc51b64216e3b0003bc89edbebcc9914cbf90d.zip opensim-SC-50bc51b64216e3b0003bc89edbebcc9914cbf90d.tar.gz opensim-SC-50bc51b64216e3b0003bc89edbebcc9914cbf90d.tar.bz2 opensim-SC-50bc51b64216e3b0003bc89edbebcc9914cbf90d.tar.xz |
From: awebb
test code for REST handlers.
-rw-r--r-- | OpenSim/ApplicationPlugins/Rest/Inventory/RestTestServices.cs | 236 | ||||
-rw-r--r-- | OpenSim/ApplicationPlugins/Rest/Inventory/tests/ITest.cs | 48 |
2 files changed, 284 insertions, 0 deletions
diff --git a/OpenSim/ApplicationPlugins/Rest/Inventory/RestTestServices.cs b/OpenSim/ApplicationPlugins/Rest/Inventory/RestTestServices.cs new file mode 100644 index 0000000..270a8c7 --- /dev/null +++ b/OpenSim/ApplicationPlugins/Rest/Inventory/RestTestServices.cs | |||
@@ -0,0 +1,236 @@ | |||
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 | |||
29 | using libsecondlife; | ||
30 | using Nini.Config; | ||
31 | using System; | ||
32 | using System.IO; | ||
33 | using System.Collections.Generic; | ||
34 | using System.Reflection; | ||
35 | using System.Threading; | ||
36 | using OpenSim.Framework; | ||
37 | using OpenSim.Framework.Servers; | ||
38 | using OpenSim.Framework.Communications; | ||
39 | using OpenSim.Framework.Communications.Cache; | ||
40 | |||
41 | namespace OpenSim.ApplicationPlugins.Rest.Inventory | ||
42 | { | ||
43 | |||
44 | public class RestTestServices : IRest | ||
45 | { | ||
46 | |||
47 | private bool enabled = false; | ||
48 | private string qPrefix = "test"; | ||
49 | |||
50 | // A simple constructor is used to handle any once-only | ||
51 | // initialization of working classes. | ||
52 | |||
53 | public RestTestServices() | ||
54 | { | ||
55 | |||
56 | Rest.Log.InfoFormat("{0} Test services initializing", MsgId); | ||
57 | Rest.Log.InfoFormat("{0} Using REST Implementation Version {1}", MsgId, Rest.Version); | ||
58 | |||
59 | // If a relative path was specified, make it absolute by adding | ||
60 | // the standard prefix, e.g. /admin | ||
61 | |||
62 | if (!qPrefix.StartsWith(Rest.UrlPathSeparator)) | ||
63 | { | ||
64 | qPrefix = Rest.Prefix + Rest.UrlPathSeparator + qPrefix; | ||
65 | } | ||
66 | |||
67 | // Load test cases | ||
68 | |||
69 | loadTests(); | ||
70 | foreach ( ITest test in tests ) | ||
71 | { | ||
72 | test.Initialize(); | ||
73 | } | ||
74 | |||
75 | // Register interface | ||
76 | |||
77 | Rest.Plugin.AddPathHandler(DoTests,qPrefix,Allocate); | ||
78 | |||
79 | // Activate | ||
80 | |||
81 | enabled = true; | ||
82 | |||
83 | Rest.Log.InfoFormat("{0} Test services initialization complete", MsgId); | ||
84 | |||
85 | } | ||
86 | |||
87 | // Post-construction, pre-enabled initialization opportunity | ||
88 | // Not currently exploited. | ||
89 | |||
90 | public void Initialize() | ||
91 | { | ||
92 | } | ||
93 | |||
94 | // Called by the plug-in to halt REST processing. Local processing is | ||
95 | // disabled, and control blocks until all current processing has | ||
96 | // completed. No new processing will be started | ||
97 | |||
98 | public void Close() | ||
99 | { | ||
100 | enabled = false; | ||
101 | foreach ( ITest test in tests ) | ||
102 | { | ||
103 | test.Close(); | ||
104 | } | ||
105 | Rest.Log.InfoFormat("{0} Test services closing down", MsgId); | ||
106 | } | ||
107 | |||
108 | // Properties | ||
109 | |||
110 | internal string MsgId | ||
111 | { | ||
112 | get { return Rest.MsgId; } | ||
113 | } | ||
114 | |||
115 | #region Interface | ||
116 | |||
117 | private RequestData Allocate(OSHttpRequest request, OSHttpResponse response) | ||
118 | { | ||
119 | return new RequestData(request, response, qPrefix); | ||
120 | } | ||
121 | |||
122 | // Inventory Handler | ||
123 | |||
124 | private void DoTests(RequestData rdata) | ||
125 | { | ||
126 | |||
127 | if (!enabled) return; | ||
128 | |||
129 | // Now that we know this is a serious attempt to | ||
130 | // access inventory data, we should find out who | ||
131 | // is asking, and make sure they are authorized | ||
132 | // to do so. We need to validate the caller's | ||
133 | // identity before revealing anything about the | ||
134 | // status quo. Authenticate throws an exception | ||
135 | // via Fail if no identity information is present. | ||
136 | // | ||
137 | // With the present HTTP server we can't use the | ||
138 | // builtin authentication mechanisms because they | ||
139 | // would be enforced for all in-bound requests. | ||
140 | // Instead we look at the headers ourselves and | ||
141 | // handle authentication directly. | ||
142 | |||
143 | try | ||
144 | { | ||
145 | if (!rdata.IsAuthenticated) | ||
146 | { | ||
147 | rdata.Fail(Rest.HttpStatusCodeNotAuthorized, Rest.HttpStatusDescNotAuthorized); | ||
148 | } | ||
149 | } | ||
150 | catch (RestException e) | ||
151 | { | ||
152 | if (e.statusCode == Rest.HttpStatusCodeNotAuthorized) | ||
153 | { | ||
154 | Rest.Log.WarnFormat("{0} User not authenticated", MsgId); | ||
155 | Rest.Log.DebugFormat("{0} Authorization header: {1}", MsgId, rdata.request.Headers.Get("Authorization")); | ||
156 | } | ||
157 | else | ||
158 | { | ||
159 | Rest.Log.ErrorFormat("{0} User authentication failed", MsgId); | ||
160 | Rest.Log.DebugFormat("{0} Authorization header: {1}", MsgId, rdata.request.Headers.Get("Authorization")); | ||
161 | } | ||
162 | throw (e); | ||
163 | } | ||
164 | |||
165 | // Check that a test was specified | ||
166 | |||
167 | if (rdata.parameters.Length < 1) | ||
168 | { | ||
169 | Rest.Log.DebugFormat("{0} Insufficient parameters", MsgId); | ||
170 | rdata.Fail(Rest.HttpStatusCodeBadRequest, Rest.HttpStatusDescBadRequest); | ||
171 | } | ||
172 | |||
173 | // Select the test | ||
174 | |||
175 | foreach (ITest test in tests) | ||
176 | { | ||
177 | if (!rdata.handled) | ||
178 | test.Execute(rdata); | ||
179 | } | ||
180 | |||
181 | } | ||
182 | |||
183 | #endregion Interface | ||
184 | |||
185 | private static bool testsLoaded = false; | ||
186 | private static List<Type> classes = new List<Type>(); | ||
187 | private static List<ITest> tests = new List<ITest>(); | ||
188 | private static Type[] parms = new Type[1]; | ||
189 | private static Object[] args = new Object[1]; | ||
190 | |||
191 | static RestTestServices() | ||
192 | { | ||
193 | Module[] mods = Assembly.GetExecutingAssembly().GetModules(); | ||
194 | foreach (Module m in mods) | ||
195 | { | ||
196 | Type[] types = m.GetTypes(); | ||
197 | foreach (Type t in types) | ||
198 | { | ||
199 | if (t.GetInterface("ITest") != null) | ||
200 | { | ||
201 | classes.Add(t); | ||
202 | } | ||
203 | } | ||
204 | } | ||
205 | } | ||
206 | |||
207 | /// <summary> | ||
208 | /// This routine loads all of the handlers discovered during | ||
209 | /// instance initialization. Each handler is responsible for | ||
210 | /// registering itself with this handler. | ||
211 | /// I was not able to make this code work in a constructor. | ||
212 | /// </summary> | ||
213 | private void loadTests() | ||
214 | { | ||
215 | lock(tests) | ||
216 | { | ||
217 | if (!testsLoaded) | ||
218 | { | ||
219 | parms[0] = this.GetType(); | ||
220 | args[0] = this; | ||
221 | |||
222 | ConstructorInfo ci; | ||
223 | Object ht; | ||
224 | |||
225 | foreach (Type t in classes) | ||
226 | { | ||
227 | ci = t.GetConstructor(parms); | ||
228 | ht = ci.Invoke(args); | ||
229 | tests.Add((ITest)ht); | ||
230 | } | ||
231 | testsLoaded = true; | ||
232 | } | ||
233 | } | ||
234 | } | ||
235 | } | ||
236 | } | ||
diff --git a/OpenSim/ApplicationPlugins/Rest/Inventory/tests/ITest.cs b/OpenSim/ApplicationPlugins/Rest/Inventory/tests/ITest.cs new file mode 100644 index 0000000..a3f1d14 --- /dev/null +++ b/OpenSim/ApplicationPlugins/Rest/Inventory/tests/ITest.cs | |||
@@ -0,0 +1,48 @@ | |||
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 | |||
29 | using System; | ||
30 | |||
31 | namespace OpenSim.ApplicationPlugins.Rest.Inventory | ||
32 | { | ||
33 | |||
34 | /// <summary> | ||
35 | /// This interface represents the boundary between the general purpose | ||
36 | /// REST plugin handling, and the functionally specific handlers. The | ||
37 | /// handler knows only to initialzie and terminate all such handlers | ||
38 | /// that it finds. | ||
39 | /// </summary> | ||
40 | |||
41 | internal interface ITest | ||
42 | { | ||
43 | void Initialize(); | ||
44 | void Execute(RequestData rdata); | ||
45 | void Close(); | ||
46 | } | ||
47 | |||
48 | } | ||