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
|
#!/usr/bin/env perl
# Test Literal RPC
# Example contributed by Daniel Ruoso
use warnings;
use strict;
use lib 'lib','t';
use TestTools;
use Test::Deep qw/cmp_deeply/;
#use Log::Report mode => 3; # debugging
use Data::Dumper;
$Data::Dumper::Indent = 1;
use XML::Compile::WSDL11;
use XML::Compile::Tester;
use Test::More tests => 14;
use XML::LibXML;
use XML::Compile::SOAP::Util ':soap11';
use XML::Compile::SOAP11;
use XML::Compile::Transport::SOAPHTTP;
my $NS = 'http://example.com/hello';
my $soapenv = SOAP11ENV;
my $schema = <<_SCHEMA;
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions
xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:s="http://www.w3.org/2001/XMLSchema"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:hello="$NS"
xmlns="$NS"
targetNamespace="http://example.com/hello">
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="http://example.com/hello">
<s:element minOccurs="0" maxOccurs="1" name="who" type="s:string"/>
<s:element minOccurs="0" maxOccurs="1" name="greeting" type="s:string"/>
</s:schema>
</wsdl:types>
<wsdl:message name="AskGreeting">
<wsdl:part name="who" element="hello:who"/>
<wsdl:part name="greeting" element="hello:greeting"/>
</wsdl:message>
<wsdl:message name="GiveGreeting">
<wsdl:part name="greeting" element="hello:greeting"/>
</wsdl:message>
<wsdl:portType name="Greeting">
<wsdl:operation name="Greet">
<wsdl:input message="hello:AskGreeting"/>
<wsdl:output message="hello:GiveGreeting"/>
</wsdl:operation>
<wsdl:operation name="Shout">
<wsdl:input message="hello:AskGreeting"/>
<wsdl:output message="hello:GiveGreeting"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="Greeting" type="hello:Greeting">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"/>
<wsdl:operation name="Greet">
<wsdl:input>
<soap:body use="literal" namespace="$NS" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" namespace="$NS" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="Shout">
<wsdl:input>
<soap:body use="literal" namespace="$NS" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" namespace="$NS" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="GreetService">
<wsdl:port name="Greet" binding="hello:Greeting">
<soap:address location="http://localhost:3000/rpcliteral/"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
_SCHEMA
### HELPER
my ($server_expects, $server_answers);
sub fake_server($$)
{ my ($request, $trace) = @_;
my $content = $request->decoded_content;
compare_xml($content, $server_expects, 'fake server received');
HTTP::Response->new(200, 'answer manually created'
, [ 'Content-Type' => 'text/xml' ], $server_answers);
}
#
# Create and interpret a message
#
my $soap = XML::Compile::SOAP11::Client->new;
isa_ok($soap, 'XML::Compile::SOAP11::Client');
isa_ok($soap, 'XML::Compile::SOAP11');
my $wsdl = XML::Compile::WSDL11->new($schema);
ok(defined $wsdl, "created object");
isa_ok($wsdl, 'XML::Compile::WSDL11');
#
# Element part
#
ok(1, "** using element");
my $eop = $wsdl->operation('Greet');
isa_ok($eop, 'XML::Compile::SOAP11::Operation');
is($eop->name, 'Greet');
is($eop->style, 'rpc');
my $er = $eop->compileClient(transport_hook => \&fake_server);
isa_ok($er, 'CODE');
$server_expects = <<_EXPECTS;
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="$soapenv">
<SOAP-ENV:Body>
<hello:Greet xmlns:hello="$NS" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<hello:who xmlns:hello="$NS">World</hello:who>
<hello:greeting xmlns:hello="$NS">Hello</hello:greeting>
</hello:Greet>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
_EXPECTS
$server_answers = <<_ANSWER;
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="$soapenv">
<SOAP-ENV:Body>
<hello:GreetResponse xmlns:hello="$NS">
<hello:greeting>Hello, World!</hello:greeting>
</hello:GreetResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
_ANSWER
my %data = ( who => 'World', greeting => 'Hello' );
my $ea = $er->(\%data);
ok(defined $ea, 'got element answer');
my $g = $ea->{GreetResponse};
isa_ok($g, 'HASH');
cmp_ok(keys %$g, '==', 1);
is($g->{greeting}, 'Hello, World!');
|