[r419]: trunk / lib / XMLRPC / Test.pm Maximize Restore History

Download this file

Test.pm    191 lines (135 with data), 4.9 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
# ======================================================================
#
# 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 419 2013-04-21 16:59:31Z kutterma $
#
# ======================================================================
package XMLRPC::Test;
use 5.004;
use vars qw($VERSION $TIMEOUT);
our $VERSION = 0.715;
$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 XMLRPC::Test::Server;
use strict;
use Test;
use XMLRPC::Lite;
sub run_for {
my $proxy = shift or die "Proxy/endpoint is not specified";
# ------------------------------------------------------
my $s = XMLRPC::Lite->proxy($proxy)->on_fault(sub{});
eval { $s->transport->timeout($XMLRPC::Test::TIMEOUT) };
my $r = $s->test_connection;
unless (defined $r && defined $r->envelope) {
print "1..0 # Skip: ", $s->transport->status, "\n";
exit;
}
# ------------------------------------------------------
plan tests => 17;
eval q!use XMLRPC::Lite on_fault => sub{ref $_[1] ? $_[1] : new XMLRPC::SOM}; 1! or die;
print "Perl XMLRPC server test(s)...\n";
$s = XMLRPC::Lite
-> proxy($proxy)
;
ok($s->call('My.Examples.getStateName', 1)->result eq 'Alabama');
ok($s->call('My.Examples.getStateNames', 1,4,6,13)->result =~ /^Alabama\s+Arkansas\s+Colorado\s+Illinois\s*$/);
$r = $s->call('My.Examples.getStateList', [1,2,3,4])->result;
ok(ref $r && $r->[0] eq 'Alabama');
$r = $s->call('My.Examples.getStateStruct', {item1 => 1, item2 => 4})->result;
ok(ref $r && $r->{item2} eq 'Arkansas');
print "dispatch_from test(s)...\n";
eval "use XMLRPC::Lite
dispatch_from => ['A', 'B'],
proxy => '$proxy',
; 1" or die;
eval { C->c };
ok($@ =~ /Can't locate object method "c"/);
print "Object autobinding and XMLRPC:: prefix test(s)...\n";
eval "use XMLRPC::Lite +autodispatch =>
proxy => '$proxy'; 1" or die;
ok(XMLRPC::Lite->autodispatched);
# forget everything
XMLRPC::Lite->self(undef);
{
my $on_fault_was_called = 0;
print "Die in server method test(s)...\n";
my $s = XMLRPC::Lite
-> proxy($proxy)
-> on_fault(sub{$on_fault_was_called++;return})
;
ok($s->call('My.Parameters.die_simply')->faultstring =~ /Something bad/);
ok($on_fault_was_called > 0);
# get Fault as hash of subelements
my $fault = $s->call('My.Parameters.die_with_fault');
ok($fault->faultcode =~ 'Server\.Custom');
ok($fault->faultstring eq 'Died in server method');
}
print "Number of parameters test(s)...\n";
$s = XMLRPC::Lite
-> proxy($proxy)
;
{ my @all = $s->call('My.Parameters.echo')->paramsall; ok(@all == 0) }
{ my @all = $s->call('My.Parameters.echo', 1)->paramsall; ok(@all == 1) }
{ my @all = $s->call('My.Parameters.echo', (1) x 10)->paramsall; ok(@all == 10) }
print "Memory refresh test(s)...\n";
# Funny test.
# Let's forget about ALL settings we did before with 'use XMLRPC::Lite...'
XMLRPC::Lite->self(undef);
ok(!defined XMLRPC::Lite->self);
eval "use XMLRPC::Lite
proxy => '$proxy'; 1" or die;
print "Global settings test(s)...\n";
$s = new XMLRPC::Lite;
ok($s->call('My.Examples.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->call('My.Examples.getStateName', 1)->faultstring =~ /Content-Type must be/);
}
# check status for fault messages
if ($proxy =~ /^http/) {
ok($s->transport->status =~ /^200/);
} else {
skip('No Status checks for non http protocols on server side' => undef);
}
}
# ======================================================================
1;
__END__
=head1 NAME
XMLRPC::Test - Test framework for XMLRPC::Lite
=head1 SYNOPSIS
use XMLRPC::Test;
XMLRPC::Test::Server::run_for('http://localhost/cgi-bin/XMLRPC.cgi');
=head1 DESCRIPTION
XMLRPC::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