-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtestWebServices.m
executable file
·364 lines (324 loc) · 10.6 KB
/
testWebServices.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/**
Copyright (C) 2008 Free Software Foundation, Inc.
Written by: Richard Frith-Macdonald <[email protected]>
Date: September 2008
This file is part of the WebServices package.
This is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
$Date: 2007-09-14 13:54:55 +0100 (Fri, 14 Sep 2007) $ $Revision: 25485 $
*/
#import <Foundation/Foundation.h>
#import "GWSPrivate.h"
#import "WSSUsernameToken.h"
@interface SimpleDelegate : NSObject
{
WSSUsernameToken *_token;
}
- (void) setToken: (WSSUsernameToken*)token;
@end
@implementation SimpleDelegate
- (void) dealloc
{
[_token release];
[super dealloc];
}
- (void) setToken: (WSSUsernameToken*)token
{
if (_token != token)
{
[_token release];
_token = [token retain];
}
}
- (GWSElement*) webService: (GWSService*)service
willEncode: (GWSElement*)element
{
if (_token != nil)
{
if (element == nil || [[element name] isEqualToString: @"Header"])
{
element = [_token addToHeader: element];
}
}
return element;
}
@end
int
main()
{
NSAutoreleasePool *pool;
NSAutoreleasePool *inner;
NSUserDefaults *defs;
SimpleDelegate *del;
WSSUsernameToken *token;
GWSCoder *coder;
GWSService *service;
GWSDocument *document;
NSData *xml;
NSString *method;
NSMutableArray *order;
NSMutableDictionary *params;
NSMutableArray *norder;
NSMutableDictionary *nparams;
NSMutableDictionary *result;
id o;
BOOL coderDebug = YES;
BOOL serviceDebug = YES;
pool = [NSAutoreleasePool new];
defs = [NSUserDefaults standardUserDefaults];
[defs registerDefaults:
[NSDictionary dictionaryWithObjectsAndKeys:
@"80", @"Port",
nil]
];
o = [defs objectForKey: @"CoderDebug"];
if (nil != o)
{
coderDebug = [defs boolForKey: @"CoderDebug"];
}
o = [defs objectForKey: @"ServiceDebug"];
if (nil != o)
{
serviceDebug = [defs boolForKey: @"ServiceDebug"];
}
inner = [NSAutoreleasePool new];
document = [[GWSDocument alloc] initWithContentsOfFile: @"SMS.wsdl"];
params = [NSMutableDictionary dictionaryWithCapacity: 8];
order = [NSMutableArray arrayWithCapacity: 8];
[params setObject: @"hello" forKey: @"string1"];
[order addObject: @"string1"];
[params setObject: [NSDictionary dictionaryWithObjectsAndKeys:
@"obj1", @"key1",
nil] forKey: @"dict1"];
[order addObject: @"dict1"];
service = [document serviceWithName: @"SMSService" create: NO];
[service setDebug: serviceDebug];
result = [service invokeMethod: @"sendSMS"
parameters: params
order: order
timeout: 3];
NSLog(@"Invoke gives ... %@", result);
result = [service invokeMethod: @"sendSMS"
parameters: params
order: order
timeout: 3];
NSLog(@"Retry gives ... %@", result);
xml = [document data];
NSLog(@"Document:\n%*.*s", (int)[xml length], (int)[xml length], [xml bytes]);
[document release];
[inner release];
// [[NSRunLoop currentRunLoop] run];
inner = [NSAutoreleasePool new];
coder = [GWSXMLRPCCoder new];
[coder setDebug:coderDebug];
/* Test encoding and decoding of an XMLRPC request.
*/
params = [NSMutableDictionary dictionaryWithCapacity: 8];
order = [NSMutableArray arrayWithCapacity: 8];
[params setObject: @"hello" forKey: @"string1"];
[order addObject: @"string1"];
[params setObject: [NSDictionary dictionaryWithObjectsAndKeys:
@"obj1", @"key1",
nil] forKey: @"dict1"];
[order addObject: @"dict1"];
method = @"test";
fprintf(stdout, "Request encode/decode ");
xml = [coder buildRequest: method
parameters: params
order: order];
result = [coder parseMessage: xml];
norder = [result objectForKey: GWSOrderKey];
nparams = [result objectForKey: GWSParametersKey];
if (NO == [method isEqual: [result objectForKey: GWSMethodKey]])
{
fprintf(stdout, "FAIL\n");
NSLog(@"Method does not match ... %@", result);
}
else if ([order count] != [norder count])
{
fprintf(stdout, "FAIL\n");
NSLog(@"Counts of order array do not match ... %@", result);
}
else if ([params count] != [nparams count])
{
fprintf(stdout, "FAIL\n");
NSLog(@"Counts of params do not match ... %@", result);
}
else
{
unsigned c = [order count];
BOOL fail = NO;
while (c-- > 0)
{
id o = [params objectForKey: [order objectAtIndex: c]];
id n = [nparams objectForKey: [norder objectAtIndex: c]];
if (NO == [o isEqual: n])
{
if (NO == fail)
{
fprintf(stdout, "FAIL\n");
fail = YES;
}
NSLog(@"Parameter %u does not match", c);
}
}
if (YES == fail)
{
NSLog(@"Parameters do not match ... %@", result);
}
else
{
fprintf(stdout, "PASS\n");
}
}
[coder release];
[inner release];
inner = [NSAutoreleasePool new];
coder = [[GWSSOAPCoder new] autorelease];
[coder setDebug: coderDebug];
/* Test encoding and decoding of a very simple SOAP request.
*/
params = [NSMutableDictionary dictionaryWithCapacity: 8];
order = [NSMutableArray arrayWithCapacity: 8];
[params setObject: @"hello<" forKey: @"string1"];
[order addObject: @"string1"];
[params setObject: [NSArray arrayWithObjects: @"a1", @"a2", @"a3", nil]
forKey: @"array1"];
[order addObject: @"array1"];
method = @"test";
fprintf(stdout, "Request encode/decode ");
[(GWSSOAPCoder*)coder setOperationStyle: GWSSOAPBodyEncodingStyleRPC];
xml = [coder buildRequest: method
parameters: params
order: order];
result = [coder parseMessage: xml];
o = [[NSString alloc] initWithData: xml encoding: NSUTF8StringEncoding];
[result setObject: o forKey: @"EncodedDocument"];
[o release];
norder = [result objectForKey: GWSOrderKey];
nparams = [result objectForKey: GWSParametersKey];
if (NO == [method isEqual: [result objectForKey: GWSMethodKey]])
{
fprintf(stdout, "FAIL\n");
NSLog(@"Method does not match ... %@", result);
}
else if ([order count] != [norder count])
{
fprintf(stdout, "FAIL\n");
NSLog(@"Counts of order array do not match ... %@", result);
}
else if ([params count] != [nparams count])
{
fprintf(stdout, "FAIL\n");
NSLog(@"Counts of params do not match ... %@", result);
}
else
{
unsigned c = [order count];
BOOL fail = NO;
while (c-- > 0)
{
id o = [params objectForKey: [order objectAtIndex: c]];
id n = [nparams objectForKey: [norder objectAtIndex: c]];
if (NO == [o isEqual: n])
{
if (NO == fail)
{
fprintf(stdout, "FAIL\n");
fail = YES;
}
NSLog(@"Parameter %u does not match", c);
}
}
if (YES == fail)
{
NSLog(@"Parameters do not match ... %@", result);
}
else
{
fprintf(stdout, "PASS\n");
}
}
[inner release];
/* Now an example of encoding a complex SOAP message manually ..
*/
inner = [NSAutoreleasePool new];
/* First stage ... let's build a complex type with a specific
* order of the elements and a specific namespace.
*/
o = [NSDictionary dictionaryWithObjectsAndKeys:
@"urn://here.there/", GWSSOAPNamespaceURIKey,
[NSArray arrayWithObjects: @"username", @"password", nil], GWSOrderKey,
@"myname", @"username",
@"mypass", @"password",
nil];
/* Now we store that as a part called 'signin' and set the headers to
* use literal encoding.
*/
o = [NSDictionary dictionaryWithObjectsAndKeys:
o, @"signin",
GWSSOAPUseLiteral, GWSSOAPUseKey,
nil];
/* Now let's set that as a header for a little message containing a single
* value (called 'parameter') with 'use' encoded.
*/
o = [NSDictionary dictionaryWithObjectsAndKeys:
o, GWSSOAPMessageHeadersKey,
GWSSOAPUseEncoded, GWSSOAPUseKey,
@"an argument", @"parameter",
nil];
/* Now build and display the result.
*/
coder = [[GWSSOAPCoder new] autorelease];
[coder setDebug: coderDebug];
xml = [coder buildRequest: @"method"
parameters: o
order: nil];
fprintf(stdout, "\nENCODED ... %*.*s\n\n\n",
(int)[xml length], (int)[xml length], (char*)[xml bytes]);
result = [coder parseMessage: xml];
fprintf(stdout, "\nDECODED ... %s\n\n\n",
(char*)[[result description] UTF8String]);
[inner release];
inner = [NSAutoreleasePool new];
fprintf(stdout, "Expect this to produce an error ... it tries a request"
" on the local web server:\n");
service = [GWSService new];
[service setDebug: serviceDebug];
[service setURL: @"http://localhost/"];
coder = [[GWSSOAPCoder new] autorelease];
[coder setDebug: coderDebug];
[service setCoder: coder];
del = [SimpleDelegate new];
token = [[WSSUsernameToken alloc] initWithName: @"me" password: @"private"];
[del setToken: token];
[token release];
[service setDelegate: del];
params = [NSMutableDictionary dictionaryWithCapacity: 8];
order = [NSMutableArray arrayWithCapacity: 8];
[params setObject: @"hello<" forKey: @"string1"];
[order addObject: @"string1"];
[params setObject: [NSArray arrayWithObjects: @"a1", @"a2", @"a3", nil]
forKey: @"array1"];
[order addObject: @"array1"];
method = @"test";
result = [service invokeMethod: method
parameters: params
order: order
timeout: 30];
fprintf(stdout, "Result: %s\n", [[result description] UTF8String]);
[service release];
[del release];
[inner release];
[pool release];
return 0;
}