r19055: JSON-RPC is working! It passes the small qooxdoo JSON-RPC test suite
[samba.git] / jsonrpc / qooxdoo / test.esp
1 <%
2 /*
3  * Copyright:
4  *   (C) 2006 by Derrell Lipman
5  *       All rights reserved
6  *
7  * License:
8  *   LGPL 2.1: http://creativecommons.org/licenses/LGPL/2.1/
9  */
10
11 /*
12  * This is the standard qooxdoo test class.  There are tests for each of the
13  * primitive types here, along with standard named tests "echo", "sink" and
14  * "sleep".
15  */
16
17 /**
18  * Echo the (one and only) parameter.
19  *
20  * @param params
21  *   An array containing the parameters to this method
22  *
23  * @param error
24  *   An object of class JsonRpcError.
25  *
26  * @return
27  *   Success: The object containing the result of the method;
28  *   Failure: null
29  */
30 function _echo(params, error)
31 {
32     if (params.length != 1)
33     {
34         error.setError(JsonRpcError_ParameterMismatch,
35                        "Expected 1 parameter; got " + params.length);
36         return error;
37     }
38     return "Client said: [" + params[0] + "]";
39 }
40 jsonrpc.method.echo = _echo;
41
42 /**
43  * Sink all data and never return.
44  *
45  * @param params
46  *   An array containing the parameters to this method (none expected)
47  *
48  * @param error
49  *   An object of class JsonRpcError.
50  *
51  * @return
52  *   "Never"
53  */
54 function _sink(params, error)
55 {
56     /* We're never supposed to return.  Just sleep for a very long time. */
57     sleep(240);
58 }
59 jsonrpc.method.sink = _sink;
60
61 /**
62  * Sleep for the number of seconds specified by the parameter.
63  *
64  * @param params
65  *   An array containing the parameters to this method (one expected)
66  *
67  * @param error
68  *   An object of class JsonRpcError.
69  *
70  * @return
71  *   Success: The object containing the result of the method;
72  *   Failure: null
73  */
74 function _sleep(params, error)
75 {
76     if (params.length != 1)
77     {
78         error.setError(JsonRpcError_ParameterMismatch,
79                        "Expected 1 parameter; got " + params.length);
80         return error;
81     }
82     
83     sleep(params[0]);
84     return params[0];
85 }
86 jsonrpc.method.sleep = _sleep;
87
88 /*************************************************************************/
89
90 /*
91  * The remainder of the functions test each individual primitive type, and
92  * test echoing arbitrary types.  Hopefully the name is self-explanatory.
93  */
94
95 function _getInteger(params, error)
96 {
97     return 1;
98 }
99 jsonrpc.method.getInteger = _getInteger;
100
101 function _getFloat(params, error)
102 {
103     return 1/3;
104 }
105 jsonrpc.method.getFloat = _getFloat;
106
107 function _getString(params, error)
108 {
109     return "Hello world";
110 }
111 jsonrpc.method.getString = _getString;
112
113 function _getBadString(params, error)
114 {
115     return "<!DOCTYPE HTML \"-//IETF//DTD HTML 2.0//EN\">";
116 }
117 jsonrpc.method.getBadString = _getBadString;
118
119 function _getArrayInteger(params, error)
120 {
121     return new Array(1, 2, 3, 4);
122 }
123 jsonrpc.method.getArrayInteger = _getArrayInteger;
124
125 function _getArrayString(params, error)
126 {
127     return new Array("one", "two", "three", "four");
128 }
129 jsonrpc.method.getArrayString = _getArrayString;
130
131 function _getObject(params, error)
132 {
133     o = new Object(); // some arbitrary object
134     o.something = 23;
135     o.garbage = 'lkasjdff;lajsdfkl;sadf';
136     return o;    
137 }
138 jsonrpc.method.getObject = _getObject;
139
140 function _getTrue(params, error)
141 {
142     return true;
143 }
144 jsonrpc.method.getTrue = _getTrue;
145
146 function _getFalse(params, error)
147 {
148     return false;
149 }
150 jsonrpc.method.getFalse = _getFalse;
151
152 function _getNull(params, error)
153 {
154     return null;
155 }
156 jsonrpc.method.getNull = _getNull;
157
158 function _isInteger(params, error)
159 {
160     var type = nativeTypeOf(params[0]);
161     return type == "integer" || type == "integer64";
162 }
163 jsonrpc.method.isInteger = _isInteger;
164
165 function _isFloat(params, error)
166 {
167     return nativeTypeOf(params[0]) == "float";
168 }
169 jsonrpc.method.isFloat = _isFloat;
170
171 function _isString(params, error)
172 {
173     return nativeTypeOf(params[0]) == "string";
174 }
175 jsonrpc.method.isString = _isString;
176
177 function _isBoolean(params, error)
178 {
179     return nativeTypeOf(params[0]) == "boolean";
180 }
181 jsonrpc.method.isBoolean = _isBoolean;
182
183 function _isArray(params, error)
184 {
185     return nativeTypeOf(params[0]) == "object" && params.length != undefined;
186 }
187 jsonrpc.method.isArray = _isArray;
188
189 function _isObject(params, error)
190 {
191     return nativeTypeOf(params[0]) == "object";
192 }
193 jsonrpc.method.isObject = _isObject;
194
195 function _isNull(params, error)
196 {
197     return nativeTypeOf(params[0]) == "null";
198 }
199 jsonrpc.method.isNull = _isNull;
200
201 function _getParams(params, error)
202 {
203     return params;
204 }       
205 jsonrpc.method.getParams = _getParams;
206
207 function _getParam(params, error)
208 {
209     return params[0];
210 }       
211 jsonrpc.method.getParam = _getParam;
212
213 function _getCurrentTimestamp()
214 {
215     now = gettimeofday();
216     obj = new Object();
217     obj.now = now.sec;
218     obj.json = JSON_Date.create(now);
219     return obj;
220 }
221 jsonrpc.method.getCurrentTimestamp = _getCurrentTimestamp;
222
223 function _getError(params, error)
224 {
225     error.setError(23, "This is an application-provided error");
226     return error;
227 }       
228 jsonrpc.method.getError = _getError;
229
230 %>