Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

382 lines
11 KiB

  1. # ======================================================================
  2. #
  3. # Copyright (C) 2000-2001 Paul Kulchenko ([email protected])
  4. # SOAP::Lite is free software; you can redistribute it
  5. # and/or modify it under the same terms as Perl itself.
  6. #
  7. # $Id: SOAP::Test.pm,v 0.51 2001/07/18 15:15:14 $
  8. #
  9. # ======================================================================
  10. package SOAP::Test;
  11. use 5.004;
  12. use vars qw($VERSION $TIMEOUT);
  13. $VERSION = '0.51';
  14. $TIMEOUT = 5;
  15. # ======================================================================
  16. package My::PingPong; # we'll use this package in our tests
  17. sub new {
  18. my $self = shift;
  19. my $class = ref($self) || $self;
  20. bless {_num=>shift} => $class;
  21. }
  22. sub next {
  23. my $self = shift;
  24. $self->{_num}++;
  25. }
  26. sub value {
  27. my $self = shift;
  28. $self->{_num};
  29. }
  30. # ======================================================================
  31. package SOAP::Test::Server;
  32. use strict;
  33. use Test;
  34. use SOAP::Lite;
  35. sub run_for {
  36. my $proxy = shift or die "Proxy/endpoint is not specified";
  37. # ------------------------------------------------------
  38. my $s = SOAP::Lite->uri('http://something/somewhere')->proxy($proxy)->on_fault(sub{});
  39. eval { $s->transport->timeout($SOAP::Test::TIMEOUT) };
  40. my $r = $s->test_connection;
  41. unless (defined $r && defined $r->envelope) {
  42. print "1..0 # Skip: ", $s->transport->status, "\n";
  43. exit;
  44. }
  45. # ------------------------------------------------------
  46. plan tests => 49;
  47. eval q!use SOAP::Lite on_fault => sub{ref $_[1] ? $_[1] : new SOAP::SOM}; 1! or die;
  48. print "Perl SOAP server test(s)...\n";
  49. $s = SOAP::Lite
  50. -> uri('urn:/My/Examples')
  51. -> proxy($proxy)
  52. ;
  53. ok($s->getStateName(1)->result eq 'Alabama');
  54. ok($s->getStateNames(1,4,6,13)->result =~ /^Alabama\s+Arkansas\s+Colorado\s+Illinois\s*$/);
  55. $r = $s->getStateList([1,2,3,4])->result;
  56. ok(ref $r && $r->[0] eq 'Alabama');
  57. $r = $s->getStateStruct({item1 => 1, item2 => 4})->result;
  58. ok(ref $r && $r->{item2} eq 'Arkansas');
  59. {
  60. my $autoresult = $s->autoresult;
  61. $s->autoresult(1);
  62. ok($s->getStateName(1) eq 'Alabama');
  63. $s->autoresult($autoresult);
  64. }
  65. print "Autobinding of output parameters test(s)...\n";
  66. $s->uri('urn:/My/Parameters');
  67. my $param1 = 10;
  68. my $param2 = SOAP::Data->name('myparam' => 12);
  69. my $result = $s->autobind($param1, $param2)->result;
  70. ok($result == $param1 && $param2->value == 24);
  71. print "Header manipulation test(s)...\n";
  72. $a = $s->addheader(2, SOAP::Header->name(my => 123));
  73. ok(ref $a->header && $a->header->{my} eq '123123');
  74. ok($a->headers eq '123123');
  75. print "Echo untyped data test(s)...\n";
  76. $a = $s->echotwo(11, 12);
  77. ok($a->result == 11);
  78. print "mustUnderstand test(s)...\n";
  79. $s->echo(SOAP::Header->name(somethingelse => 123)
  80. ->mustUnderstand(1));
  81. ok($s->call->faultstring =~ /Header has mustUnderstand attribute/);
  82. $s->echo(SOAP::Header->name(somethingelse => 123)
  83. ->mustUnderstand(1)
  84. ->actor('http://notme/'));
  85. ok(!defined $s->call->fault);
  86. print "dispatch_from test(s)...\n";
  87. eval "use SOAP::Lite
  88. uri => 'http://my.own.site.com/My/Examples',
  89. dispatch_from => ['A', 'B'],
  90. proxy => '$proxy',
  91. ; 1" or die;
  92. eval { C->c };
  93. ok($@ =~ /Can't locate object method "c"/);
  94. eval { A->a };
  95. ok(!$@ && SOAP::Lite->self->call->faultstring =~ /Failed to access class \(A\)/);
  96. eval "use SOAP::Lite
  97. dispatch_from => 'A',
  98. uri => 'http://my.own.site.com/My/Examples',
  99. proxy => '$proxy',
  100. ; 1" or die;
  101. eval { A->a };
  102. ok(!$@ && SOAP::Lite->self->call->faultstring =~ /Failed to access class \(A\)/);
  103. print "Object autobinding and SOAP:: prefix test(s)...\n";
  104. eval "use SOAP::Lite +autodispatch =>
  105. uri => 'urn:', proxy => '$proxy'; 1" or die;
  106. ok(SOAP::Lite->autodispatched);
  107. eval { SOAP->new(1) };
  108. ok($@ =~ /^URI is not specified/);
  109. eval "use SOAP::Lite +autodispatch =>
  110. uri => 'urn:/A/B', proxy => '$proxy'; 1" or die;
  111. # should call My::PingPong, not A::B
  112. my $p = My::PingPong->SOAP::new(10);
  113. ok(ref $p && $p->SOAP::next+1 == $p->value);
  114. # forget everything
  115. SOAP::Lite->self(undef);
  116. $s = SOAP::Lite
  117. -> uri('urn:/My/PingPong')
  118. -> proxy($proxy)
  119. ;
  120. # should return object EXACTLY as after My::PingPong->SOAP::new(10)
  121. $p = $s->SOAP::new(10);
  122. ok(ref $p && $s->SOAP::next($p)+1 == $p->value);
  123. print "VersionMismatch test(s)...\n";
  124. {
  125. local $SOAP::Constants::NS_ENV = 'http://schemas.xmlsoap.org/new/envelope/';
  126. my $s = SOAP::Lite
  127. -> uri('http://my.own.site.com/My/Examples')
  128. -> proxy($proxy)
  129. -> on_fault(sub{})
  130. ;
  131. $r = $s->dosomething;
  132. ok(ref $r && $r->faultcode =~ /:VersionMismatch/);
  133. }
  134. print "Objects-by-reference test(s)...\n";
  135. eval "use SOAP::Lite +autodispatch =>
  136. uri => 'urn:', proxy => '$proxy'; 1" or die;
  137. print "Session iterator\n";
  138. $r = My::SessionIterator->new(10);
  139. if (!ref $r || exists $r->{id}) {
  140. ok(ref $r && $r->next && $r->next == 11);
  141. } else {
  142. skip('No persistent objects (o-b-r) supported on server side' => undef);
  143. }
  144. print "Persistent iterator\n";
  145. $r = My::PersistentIterator->new(10);
  146. if (!ref $r || exists $r->{id}) {
  147. my $first = ($r->next, $r->next) if ref $r;
  148. $r = My::PersistentIterator->new(10);
  149. ok(ref $r && $r->next && $r->next == $first+2);
  150. } else {
  151. skip('No persistent objects (o-b-r) supported on server side' => undef);
  152. }
  153. { local $^W; # disable warnings about deprecated AUTOLOADing for nonmethods
  154. print "Parameters-by-name test(s)...\n";
  155. print "You can see warning about AUTOLOAD for non-method...\n" if $^W;
  156. eval "use SOAP::Lite +autodispatch =>
  157. uri => 'http://my.own.site.com/My/Parameters', proxy => '$proxy'; 1" or die;
  158. my @parameters = (
  159. SOAP::Data->name(b => 222),
  160. SOAP::Data->name(c => 333),
  161. SOAP::Data->name(a => 111)
  162. );
  163. # switch to 'main' package, because nonqualified methods should be there
  164. ok(main::byname(@parameters) eq "a=111, b=222, c=333");
  165. print "Function call test(s)...\n";
  166. print "You can see warning about AUTOLOAD for non-method...\n" if $^W;
  167. ok(main::echo(11) == 11);
  168. }
  169. print "SOAPAction test(s)...\n";
  170. if ($proxy =~ /^tcp:/) {
  171. for (1..2) {skip('No SOAPAction checks for tcp: protocol on server side' => undef)}
  172. } else {
  173. my $s = SOAP::Lite
  174. -> uri('http://my.own.site.com/My/Examples')
  175. -> proxy($proxy)
  176. -> on_action(sub{'""'})
  177. ;
  178. ok($s->getStateName(1)->result eq 'Alabama');
  179. $s->on_action(sub{'"wrong_SOAPAction_here"'});
  180. ok($s->getStateName(1)->faultstring =~ /SOAPAction shall match/);
  181. }
  182. {
  183. my $on_fault_was_called = 0;
  184. print "Die in server method test(s)...\n";
  185. my $s = SOAP::Lite
  186. -> uri('http://my.own.site.com/My/Parameters')
  187. -> proxy($proxy)
  188. -> on_fault(sub{$on_fault_was_called++;return})
  189. ;
  190. ok($s->die_simply()->faultstring =~ /Something bad/);
  191. ok($on_fault_was_called > 0);
  192. my $detail = $s->die_with_object()->dataof(SOAP::SOM::faultdetail . '/[1]');
  193. ok($on_fault_was_called > 1);
  194. ok(ref $detail && $detail->name =~ /(^|:)something$/);
  195. # get Fault as hash of subelements
  196. my $fault = $s->die_with_fault()->fault;
  197. ok($fault->{faultcode} =~ ':Server.Custom');
  198. ok($fault->{faultstring} eq 'Died in server method');
  199. ok(ref $fault->{detail}->{BadError} eq 'BadError');
  200. ok($fault->{faultactor} eq 'http://www.soaplite.com/custom');
  201. }
  202. print "Method with attributes test(s)...\n";
  203. $s = SOAP::Lite
  204. -> uri('urn:/My/Examples')
  205. -> proxy($proxy)
  206. ;
  207. ok($s->call(SOAP::Data->name('getStateName')->attr({xmlns => 'urn:/My/Examples'}), 1)->result eq 'Alabama');
  208. print "Call with empty uri test(s)...\n";
  209. $s = SOAP::Lite
  210. -> uri('')
  211. -> proxy($proxy)
  212. ;
  213. ok($s->getStateName(1)->faultstring =~ /Denied access to method \(getStateName\) in class \(main\)/);
  214. ok($s->call('a:getStateName' => 1)->faultstring =~ /Denied access to method \(getStateName\) in class \(main\)/);
  215. print "Number of parameters test(s)...\n";
  216. $s = SOAP::Lite
  217. -> uri('http://my.own.site.com/My/Parameters')
  218. -> proxy($proxy)
  219. ;
  220. { my @all = $s->echo->paramsall; ok(@all == 0) }
  221. { my @all = $s->echo(1)->paramsall; ok(@all == 1) }
  222. { my @all = $s->echo((1) x 10)->paramsall; ok(@all == 10) }
  223. print "Memory refresh test(s)...\n";
  224. # Funny test.
  225. # Let's forget about ALL settings we did before with 'use SOAP::Lite...'
  226. SOAP::Lite->self(undef);
  227. ok(!defined SOAP::Lite->self);
  228. print "Call without uri test(s)...\n";
  229. $s = SOAP::Lite
  230. -> proxy($proxy)
  231. ;
  232. ok($s->getStateName(1)->faultstring =~ /Denied access to method \(getStateName\) in class \(main\)/);
  233. print "Different settings for method and namespace test(s)...\n";
  234. ok($s->call(SOAP::Data
  235. ->name('getStateName')
  236. ->attr({xmlns => 'urn:/My/Examples'}), 1)->result eq 'Alabama');
  237. ok($s->call(SOAP::Data
  238. ->name('a:getStateName')
  239. ->attr({'xmlns:~' => 'urn:/My/Examples'}), 1)->result eq 'Alabama');
  240. ok($s->call(SOAP::Data
  241. ->name('getStateName')
  242. ->uri('urn:/My/Examples'), 1)->result eq 'Alabama');
  243. ok($s->call(SOAP::Data
  244. ->name('a:getStateName')
  245. ->attr({'xmlns:a' => 'urn:/My/Examples'}), 1)->result eq 'Alabama');
  246. eval { $s->call(SOAP::Data->name('a:getStateName')) };
  247. ok($@ =~ /Can't find namespace for method \(a:getStateName\)/);
  248. $s->serializer->namespaces->{'urn:/My/Examples'} = '';
  249. ok($s->getStateName(1)->result eq 'Alabama');
  250. eval "use SOAP::Lite
  251. uri => 'urn:/My/Examples', proxy => '$proxy'; 1" or die;
  252. print "Global settings test(s)...\n";
  253. $s = new SOAP::Lite;
  254. ok($s->getStateName(1)->result eq 'Alabama');
  255. SOAP::Trace->import(transport =>
  256. sub {$_[0]->content_type('something/wrong') if UNIVERSAL::isa($_[0] => 'HTTP::Request')}
  257. );
  258. if ($proxy =~ /^tcp:/) {
  259. skip('No Content-Type checks for tcp: protocol on server side' => undef);
  260. } else {
  261. ok($s->getStateName(1)->faultstring =~ /Content-Type must be/);
  262. }
  263. }
  264. # ======================================================================
  265. 1;
  266. __END__
  267. =head1 NAME
  268. SOAP::Test - Test framework for SOAP::Lite
  269. =head1 SYNOPSIS
  270. use SOAP::Test;
  271. SOAP::Test::Server::run_for('http://localhost/cgi-bin/soap.cgi');
  272. =head1 DESCRIPTION
  273. SOAP::Test provides simple framework for testing server implementations.
  274. Specify your address (endpoint) and run provided tests against your server.
  275. See t/1*.t for examples.
  276. =head1 COPYRIGHT
  277. Copyright (C) 2000-2001 Paul Kulchenko. All rights reserved.
  278. This library is free software; you can redistribute it and/or modify
  279. it under the same terms as Perl itself.
  280. =head1 AUTHOR
  281. Paul Kulchenko (paulclinger@yahoo.com)
  282. =cut