| 1 | package SOAP::WSDL::Transport::Test; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Class::Std::Fast::Storable; | ||
| 5 | use SOAP::WSDL::Factory::Transport; | ||
| 6 | |||
| 7 | our $VERSION = 3.004; | ||
| 8 | |||
| 9 | SOAP::WSDL::Factory::Transport->register( http => __PACKAGE__ ); | ||
| 10 | SOAP::WSDL::Factory::Transport->register( https => __PACKAGE__ ); | ||
| 11 | |||
| 12 | my %code_of :ATTR(:name<code> :default<()>); | ||
| 13 | my %status_of :ATTR(:name<status> :default<()>); | ||
| 14 | my %message_of :ATTR(:name<message> :default<()>); | ||
| 15 | my %is_success_of :ATTR(:name<is_success> :default<()>); | ||
| 16 | my %base_dir_of :ATTR(:name<base_dir> :init_arg<base_dir> :default<.>); | ||
| 17 | |||
| 18 | # create methods normally inherited from SOAP::Client | ||
| 19 | SUBFACTORY: { | ||
| 20 | no strict qw(refs); | ||
| 21 | foreach my $method ( qw(code message status is_success) ) { | ||
| 22 | *{ $method } = *{ "get_$method" }; | ||
| 23 | } | ||
| 24 | } | ||
| 25 | |||
| 26 | sub send_receive { | ||
| 27 | my ($self, %parameters) = @_; | ||
| 28 | my ($envelope, $soap_action, $endpoint, $encoding, $content_type) = | ||
| 29 | @parameters{qw(envelope action endpoint encoding content_type)}; | ||
| 30 | |||
| 31 | my $filename = $soap_action; | ||
| 32 | $filename =~s{ \A(:?'|") }{}xms; | ||
| 33 | $filename =~s{ (:?'|")\z }{}xms; | ||
| 34 | $filename =~s{ \A [^:]+ : (:? /{2})? }{}xms; | ||
| 35 | |||
| 36 | $filename = join '/', $base_dir_of{ ${ $self } }, "$filename.xml"; | ||
| 37 | |||
| 38 | if (not -r $filename) { | ||
| 39 | warn "cannot access $filename"; | ||
| 40 | $self->set_code( 500 ); | ||
| 41 | $self->set_message( "Failed" ); | ||
| 42 | $self->set_is_success(0); | ||
| 43 | $self->set_status("500 Failed"); | ||
| 44 | return; | ||
| 45 | } | ||
| 46 | |||
| 47 | open my $fh, '<', $filename or die "cannot open $filename: $!"; | ||
| 48 | binmode $fh; | ||
| 49 | my $response = <$fh>; | ||
| 50 | close $fh or die "cannot close $filename: $!"; | ||
| 51 | |||
| 52 | $self->set_code( 200 ); | ||
| 53 | $self->set_message( "OK" ); | ||
| 54 | $self->set_is_success(1); | ||
| 55 | $self->set_status("200 OK"); | ||
| 56 | return $response; | ||
| 57 | } | ||
| 58 | |||
| 59 | 1; | ||
| 60 | |||
| 61 | =head1 NAME | ||
| 62 | |||
| 63 | SOAP::WSDL::Transport::Test - Test transport class for SOAP::WSDL | ||
| 64 | |||
| 65 | =head1 SYNOPSIS | ||
| 66 | |||
| 67 | use SOAP::WSDL::Client; | ||
| 68 | use SOAP::WSDL::Transport::Test; | ||
| 69 | |||
| 70 | my $soap = SOAP::WSDL::Client->new() | ||
| 71 | $soap->get_transport->set_base_dir('.'); | ||
| 72 | $soap->call('method', \%body, \%header); | ||
| 73 | |||
| 74 | =head1 DESCRIPTION | ||
| 75 | |||
| 76 | SOAP::WSDL::Transport::Test is a file-based test transport backend for | ||
| 77 | SOAP::WSDL. | ||
| 78 | |||
| 79 | When SOAP::WSDL::Transport::Test is used as transport backend, the response is | ||
| 80 | read from a XML file and the request message is discarded. This is | ||
| 81 | particularly useful for testing SOAP::WSDL plugins. | ||
| 82 | |||
| 83 | =head2 Filename resolution | ||
| 84 | |||
| 85 | SOAP::WSDL::Transport makes up the response XML file name from the SOAPAction | ||
| 86 | of the request. The following filename is used: | ||
| 87 | |||
| 88 | base_dir / soap_action .xml | ||
| 89 | |||
| 90 | The protocol scheme (e.g. http:) and two heading slashes (//) are stripped from | ||
| 91 | the soap_action. | ||
| 92 | |||
| 93 | base_dir defaults to '.' | ||
| 94 | |||
| 95 | Examples: | ||
| 96 | |||
| 97 | SOAPAction: http://somewhere.over.the.rainbow/webservice/webservice.asmx | ||
| 98 | Filename: ./somewhere.over.the.rainbow/webservice/webservice.asmx.xml | ||
| 99 | |||
| 100 | SOAPAction: uri:MyWickedService/test | ||
| 101 | Filename: ./MyWickedService/test.xml | ||
| 102 | |||
| 103 | |||
| 104 | =head1 METHODS | ||
| 105 | |||
| 106 | =head2 set_base_dir | ||
| 107 | |||
| 108 | Sets the base directory SOAP::WSDL::Transport::Test should look for response | ||
| 109 | files. | ||
| 110 | |||
| 111 | =head1 LICENSE AND COPYRIGHT | ||
| 112 | |||
| 113 | Copyright (c) 2007 Martin Kutter. All rights reserved. | ||
| 114 | |||
| 115 | This file is part of SOAP-WSDL. You may distribute/modify it under | ||
| 116 | the same terms as perl itself | ||
| 117 | |||
| 118 | =head1 AUTHOR | ||
| 119 | |||
| 120 | Martin Kutter E<lt>martin.kutter fen-net.deE<gt> | ||
| 121 | |||
| 122 | =head1 REPOSITORY INFORMATION | ||
| 123 | |||
| 124 | $Rev: 218 $ | ||
| 125 | $LastChangedBy: kutterma $ | ||
| 126 | $Id: HTTP.pm 218 2007-09-10 16:19:23Z kutterma $ | ||
| 127 | $HeadURL: https://soap-wsdl.svn.sourceforge.net/svnroot/soap-wsdl/SOAP-WSDL/trunk/lib/SOAP/WSDL/Transport/HTTP.pm $ | ||
| 128 | |||
| 129 | =cut |