[r386]: branches / 0.71 / lib / SOAP / Test.pm Maximize Restore History

Download this file

Test.pm    408 lines (301 with data), 11.8 kB

  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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# ======================================================================
#
# Copyright (C) 2000-2001 Paul Kulchenko (paulclinger@yahoo.com)
# SOAP::Lite is free software; you can redistribute it
# and/or modify it under the same terms as Perl itself.
#
# $Id: Test.pm 386 2011-08-18 19:48:31Z kutterma $
#
# ======================================================================
package SOAP::Test;
use 5.006;
our $VERSION = 0.714;
our $TIMEOUT = 5;
# ======================================================================
package My::PingPong; # we'll use this package in our tests
sub new {
my $self = shift;
my $class = ref($self) || $self;
bless {_num=>shift} => $class;
}
sub next {
my $self = shift;
$self->{_num}++;
}
sub value {
my $self = shift;
$self->{_num};
}
# ======================================================================
package SOAP::Test::Server;
use strict;
use Test;
use SOAP::Lite;
sub run_for {
my $proxy = shift or die "Proxy/endpoint is not specified";
# ------------------------------------------------------
my $s = SOAP::Lite->uri('http://something/somewhere')->proxy($proxy)->on_fault(sub{});
eval { $s->transport->timeout($SOAP::Test::TIMEOUT) };
my $r = $s->test_connection;
unless (defined $r && defined $r->envelope) {
print "1..0 # Skip: ", $s->transport->status, "\n";
exit;
}
# ------------------------------------------------------
plan tests => 53;
eval q!use SOAP::Lite on_fault => sub{ref $_[1] ? $_[1] : new SOAP::SOM}; 1! or die;
print STDERR "Perl SOAP server test(s)...\n";
$s = SOAP::Lite
-> uri('urn:/My/Examples')
-> proxy($proxy);
ok($s->getStateName(1)->result eq 'Alabama');
ok($s->getStateNames(1,4,6,13)->result =~ /^Alabama\s+Arkansas\s+Colorado\s+Illinois\s*$/);
$r = $s->getStateList([1,2,3,4])->result;
ok(ref $r && $r->[0] eq 'Alabama');
$r = $s->getStateStruct({item1 => 1, item2 => 4})->result;
ok(ref $r && $r->{item2} eq 'Arkansas');
{
my $autoresult = $s->autoresult;
$s->autoresult(1);
ok($s->getStateName(1) eq 'Alabama');
$s->autoresult($autoresult);
}
print STDERR "Autobinding of output parameters test(s)...\n";
$s->uri('urn:/My/Parameters');
my $param1 = 10;
my $param2 = SOAP::Data->name('myparam' => 12);
my $result = $s->autobind($param1, $param2)->result;
ok($result == $param1 && $param2->value == 24);
print STDERR "Header manipulation test(s)...\n";
$a = $s->addheader(2, SOAP::Header->name(my => 123));
ok(ref $a->header && $a->header->{my} eq '123123');
ok($a->headers eq '123123');
print STDERR "Echo untyped data test(s)...\n";
$a = $s->echotwo(11, 12);
ok($a->result == 11);
print STDERR "mustUnderstand test(s)...\n";
$s->echo(SOAP::Header->name(somethingelse => 123)
->mustUnderstand(1));
ok($s->call->faultstring =~ /[Hh]eader has mustUnderstand attribute/);
if ($proxy =~ /^http/) {
ok($s->transport->status =~ /^500/);
} else {
skip('No Status checks for non http protocols on server side' => undef);
}
$s->echo(SOAP::Header->name(somethingelse => 123)
->mustUnderstand(1)
->actor('http://notme/'));
ok(!defined $s->call->fault);
print STDERR "dispatch_from test(s)...\n";
eval "use SOAP::Lite
uri => 'http://my.own.site.com/My/Examples',
dispatch_from => ['A', 'B'],
proxy => '$proxy',
; 1" or die;
eval { C->c };
ok($@ =~ /Can't locate object method "c"/);
eval { A->a };
ok(!$@ && SOAP::Lite->self->call->faultstring =~ /Failed to access class \(A\)/);
eval "use SOAP::Lite
dispatch_from => 'A',
uri => 'http://my.own.site.com/My/Examples',
proxy => '$proxy',
; 1" or die;
eval { A->a };
ok(!$@ && SOAP::Lite->self->call->faultstring =~ /Failed to access class \(A\)/);
print STDERR "Object autobinding and SOAP:: prefix test(s)...\n";
eval "use SOAP::Lite +autodispatch =>
uri => 'urn:', proxy => '$proxy'; 1" or die;
ok(SOAP::Lite->autodispatched);
eval { SOAP->new(1) };
ok($@ =~ /^URI is not specified/);
eval "use SOAP::Lite +autodispatch =>
uri => 'urn:/A/B', proxy => '$proxy'; 1" or die;
# should call My::PingPong, not A::B
my $p = My::PingPong->SOAP::new(10);
ok(ref $p && $p->SOAP::next+1 == $p->value);
# forget everything
SOAP::Lite->self(undef);
$s = SOAP::Lite
-> uri('urn:/My/PingPong')
-> proxy($proxy)
;
# should return object EXACTLY as after My::PingPong->SOAP::new(10)
$p = $s->SOAP::new(10);
ok(ref $p && $s->SOAP::next($p)+1 == $p->value);
print STDERR "VersionMismatch test(s)...\n";
{
local $SOAP::Constants::NS_ENV = 'http://schemas.xmlsoap.org/new/envelope/';
my $s = SOAP::Lite
-> uri('http://my.own.site.com/My/Examples')
-> proxy($proxy)
-> on_fault(sub{})
;
$r = $s->dosomething;
ok(ref $r && $r->faultcode =~ /:VersionMismatch/);
}
print STDERR "Objects-by-reference test(s)...\n";
eval "use SOAP::Lite +autodispatch =>
uri => 'urn:', proxy => '$proxy'; 1" or die;
print STDERR "Session iterator\n";
$r = My::SessionIterator->new(10);
if (!ref $r || exists $r->{id}) {
ok(ref $r && $r->next && $r->next == 11);
} else {
skip('No persistent objects (o-b-r) supported on server side' => undef);
}
print STDERR "Persistent iterator\n";
$r = My::PersistentIterator->new(10);
if (!ref $r || exists $r->{id}) {
my $first = ($r->next, $r->next) if ref $r;
$r = My::PersistentIterator->new(10);
ok(ref $r && $r->next && $r->next == $first+2);
} else {
skip('No persistent objects (o-b-r) supported on server side' => undef);
}
{ local $^W; # disable warnings about deprecated AUTOLOADing for nonmethods
print STDERR "Parameters-by-name test(s)...\n";
print STDERR "You can see warning about AUTOLOAD for non-method...\n" if $^W;
eval "use SOAP::Lite +autodispatch =>
uri => 'http://my.own.site.com/My/Parameters', proxy => '$proxy'; 1" or die;
my @parameters = (
SOAP::Data->name(b => 222),
SOAP::Data->name(c => 333),
SOAP::Data->name(a => 111)
);
# switch to 'main' package, because nonqualified methods should be there
ok(main::byname(@parameters) eq "a=111, b=222, c=333");
ok(main::bynameororder(@parameters) eq "a=111, b=222, c=333");
ok(main::bynameororder(111, 222, 333) eq "a=111, b=222, c=333");
print STDERR "Function call test(s)...\n";
print STDERR "You can see warning about AUTOLOAD for non-method...\n" if $^W;
ok(main::echo(11) == 11);
}
print STDERR "SOAPAction test(s)...\n";
if ($proxy =~ /^tcp:/) {
for (1..2) {skip('No SOAPAction checks for tcp: protocol on server side' => undef)}
} else {
my $s = SOAP::Lite
-> uri('http://my.own.site.com/My/Examples')
-> proxy($proxy)
-> on_action(sub{'""'})
;
ok($s->getStateName(1)->result eq 'Alabama');
$s->on_action(sub{'"wrong_SOAPAction_here"'});
ok($s->getStateName(1)->faultstring =~ /SOAPAction shall match/);
}
print STDERR "UTF8 test(s)...\n";
if (!eval "pack('U*', 0)") {
for (1) {skip('No UTF8 test. No support for pack("U*") modifier' => undef)}
} else {
$s = SOAP::Lite
-> uri('http://my.own.site.com/My/Parameters')
-> proxy($proxy);
my $latin1 = '������������';
my $utf8 = pack('U*', unpack('C*', $latin1));
my $result = $s->echo(SOAP::Data->type(string => $utf8))->result;
ok(pack('U*', unpack('C*', $result)) eq $utf8 # should work where XML::Parser marks resulting strings as UTF-8
|| join('', unpack('C*', $result)) eq join('', unpack('C*', $utf8)) # should work where it doesn't
);
}
{
my $on_fault_was_called = 0;
print STDERR "Die in server method test(s)...\n";
my $s = SOAP::Lite
-> uri('http://my.own.site.com/My/Parameters')
-> proxy($proxy)
-> on_fault(sub{$on_fault_was_called++;return})
;
ok($s->die_simply()->faultstring =~ /Something bad/);
ok($on_fault_was_called > 0);
my $detail = $s->die_with_object()->dataof(SOAP::SOM::faultdetail . '/[1]');
ok($on_fault_was_called > 1);
ok(ref $detail && $detail->name =~ /(^|:)something$/);
# get Fault as hash of subelements
my $fault = $s->die_with_fault()->fault;
ok($fault->{faultcode} =~ ':Server.Custom');
ok($fault->{faultstring} eq 'Died in server method');
ok(ref $fault->{detail}->{BadError} eq 'BadError');
ok($fault->{faultactor} eq 'http://www.soaplite.com/custom');
}
print STDERR "Method with attributes test(s)...\n";
$s = SOAP::Lite
-> uri('urn:/My/Examples')
-> proxy($proxy)
;
ok($s->call(SOAP::Data->name('getStateName')->attr({xmlns => 'urn:/My/Examples'}), 1)->result eq 'Alabama');
print STDERR "Call with empty uri test(s)...\n";
$s = SOAP::Lite
-> uri('')
-> proxy($proxy)
;
ok($s->getStateName(1)->faultstring =~ /Denied access to method \(getStateName\) in class \(main\)/);
ok($s->call('a:getStateName' => 1)->faultstring =~ /Denied access to method \(getStateName\) in class \(main\)/);
print STDERR "Number of parameters test(s)...\n";
$s = SOAP::Lite
-> uri('http://my.own.site.com/My/Parameters')
-> proxy($proxy)
;
{ my @all = $s->echo->paramsall; ok(@all == 0) }
{ my @all = $s->echo(1)->paramsall; ok(@all == 1) }
{ my @all = $s->echo((1) x 10)->paramsall; ok(@all == 10) }
print STDERR "Memory refresh test(s)...\n";
# Funny test.
# Let's forget about ALL settings we did before with 'use SOAP::Lite...'
SOAP::Lite->self(undef);
ok(!defined SOAP::Lite->self);
print STDERR "Call without uri test(s)...\n";
$s = SOAP::Lite
-> proxy($proxy)
;
ok($s->getStateName(1)->faultstring =~ /Denied access to method \(getStateName\) in class \(main\)/);
print STDERR "Different settings for method and namespace test(s)...\n";
ok($s->call(SOAP::Data
->name('getStateName')
->attr({xmlns => 'urn:/My/Examples'}), 1)->result eq 'Alabama');
ok($s->call(SOAP::Data
->name('a:getStateName')
->uri('urn:/My/Examples'), 1)->result eq 'Alabama');
ok($s->call(SOAP::Data
->name('getStateName')
->uri('urn:/My/Examples'), 1)->result eq 'Alabama');
ok($s->call(SOAP::Data
->name('a:getStateName')
->attr({'xmlns:a' => 'urn:/My/Examples'}), 1)->result eq 'Alabama');
eval { $s->call(SOAP::Data->name('a:getStateName')) };
ok($@ =~ /Can't find namespace for method \(a:getStateName\)/);
$s->serializer->namespaces->{'urn:/My/Examples'} = '';
ok($s->getStateName(1)->result eq 'Alabama');
eval "use SOAP::Lite
uri => 'urn:/My/Examples', proxy => '$proxy'; 1" or die;
print STDERR "Global settings test(s)...\n";
$s = new SOAP::Lite;
ok($s->getStateName(1)->result eq 'Alabama');
SOAP::Trace->import(transport =>
sub {$_[0]->content_type('something/wrong') if UNIVERSAL::isa($_[0] => 'HTTP::Request')}
);
if ($proxy =~ /^tcp:/) {
skip('No Content-Type checks for tcp: protocol on server side' => undef);
} else {
ok($s->getStateName(1)->faultstring =~ /Content-Type must be/);
}
}
# ======================================================================
1;
__END__
=head1 NAME
SOAP::Test - Test framework for SOAP::Lite
=head1 SYNOPSIS
use SOAP::Test;
SOAP::Test::Server::run_for('http://localhost/cgi-bin/soap.cgi');
=head1 DESCRIPTION
SOAP::Test provides simple framework for testing server implementations.
Specify your address (endpoint) and run provided tests against your server.
See t/1*.t for examples.
=head1 COPYRIGHT
Copyright (C) 2000-2001 Paul Kulchenko. All rights reserved.
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=head1 AUTHOR
Paul Kulchenko (paulclinger@yahoo.com)
=cut