File: 60xop.t

package info (click to toggle)
libxml-compile-soap-perl 3.28%2Bds-1
  • links: PTS, VCS
  • area: non-free
  • in suites: bookworm, forky, sid, trixie
  • size: 616 kB
  • sloc: perl: 4,407; makefile: 7
file content (168 lines) | stat: -rw-r--r-- 4,270 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env perl
# Test XOP

use warnings;
use strict;

use lib 'lib','t';
use TestTools;
use MIME::Base64;
use XML::LibXML;

use Data::Dumper;
$Data::Dumper::Indent = 1;

use XML::Compile::SOAP11::Client;
use XML::Compile::SOAP::Util qw/SOAP11ENV :xop10/;
use XML::Compile::Transport::SOAPHTTP;
use XML::Compile::Tester;
use XML::Compile::XOP;

use Test::More tests => 25;

my $soap11_env = SOAP11ENV;
my $xmime      = XMIME11;
my $xop10      = XOP10;

my $test_bin   = "Hello, World!\n";
my $test_enc   = encode_base64 $test_bin;

my $schema = <<_SCHEMA;
<schema targetNamespace="$TestNS"
  xmlns="$SchemaNS">

  <element name="top">
    <complexType>
      <sequence>
        <element name="first"  type="base64Binary" />
        <element name="second" type="base64Binary" />
      </sequence>
    </complexType>
  </element>

</schema>
_SCHEMA

#
# Create and interpret a message
#

my $soap = XML::Compile::SOAP11::Client->new;
ok(defined $soap, 'create client');

$soap->schemas->importDefinitions($schema);

my @msg_struct = ( body => [ request => "{$TestNS}top" ] );

my $sender   = $soap->compileMessage(SENDER => @msg_struct);
is(ref $sender, 'CODE', 'compiled a sender');

my $receiver = $soap->compileMessage(RECEIVER => @msg_struct);
is(ref $receiver, 'CODE', 'compiled a receiver');

#
# Message 1 is ok
#

# sender

my $msg1_soap = <<__MSG1;
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="$soap11_env">
  <SOAP-ENV:Body>
    <x0:top xmlns:x0="$TestNS">
      <first>$test_enc</first>
      <second>$test_enc</second>
    </x0:top>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
__MSG1

my $msg1_data = {request => { first => $test_bin, second => $test_bin }};
my $xml1a = $sender->($msg1_data);
isa_ok($xml1a, 'XML::LibXML::Node', 'produced XML');
compare_xml($xml1a, $msg1_soap);

my $hash1 = $receiver->($msg1_soap);
is(ref $hash1, 'HASH', 'produced HASH');

is_deeply($hash1, $msg1_data, "server parsed input");

###
### Now with a XOP::Include
###

my $xop = XML::Compile::XOP->new;
isa_ok($xop, 'XML::Compile::XOP');

my $first_xop = $xop->bytes($test_bin, cid => 'mycid2@home');
isa_ok($first_xop, 'XML::Compile::XOP::Include');
is($first_xop, $test_bin, 'test stringification');
cmp_ok(length $first_xop, '==', length $test_bin, "fallback");

my $msg2_data = {request => { first => $first_xop, second => $test_bin }};
my ($xml2, $mtom2) = $sender->($msg2_data);
isa_ok($xml2, 'XML::LibXML::Node', 'produced XML');
compare_xml($xml2, <<_XML2);
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="$soap11_env">
  <SOAP-ENV:Body>
    <x0:top xmlns:x0="$TestNS">
      <first xmlns:xmime="$xmime" xmime:contentType="application/octet-stream">
         <xop:Include xmlns:xop="$xop10" href="cid:mycid2\@home"/>
      </first>
      <second>$test_enc</second>
    </x0:top>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
_XML2

cmp_ok(scalar @$mtom2, '==', 1, 'one xop object');
isa_ok($mtom2->[0], 'XML::Compile::XOP::Include');

my $http2 = $mtom2->[0]->mimePart;
isa_ok($http2, 'HTTP::Message');
is($http2->as_string("\n"), <<_HTTP2);
Content-Type: application/octet-stream
Content-ID: <mycid2\@home>
Content-Transfer-Encoding: binary

Hello, World!
_HTTP2

sub transport_bounce_request($$)
{   my ($request, $trace) = @_;

    my $response = HTTP::Response->new(200 => 'OK', $request->headers->clone);
    $response->parts( map {$_->clone} $request->parts );
    $response;
}

my $transporter = XML::Compile::Transport::SOAPHTTP->new;
ok(defined $transporter, 'soaphttp');

my $call2   = $transporter->compileClient(hook => \&transport_bounce_request);
isa_ok($call2, 'CODE', 'transport hook');

my $trace2  = {};
my ($xmltext2, $back2) = $call2->($xml2, $trace2, $mtom2);
isa_ok($xmltext2, 'XML::LibXML::Node');

isa_ok($mtom2, 'ARRAY', 'returned XOPs');
cmp_ok(scalar @$mtom2, '==', 1);
is($mtom2->[0]->cid, 'mycid2@home');

my $hash2 = $receiver->($xmltext2, $back2);

isa_ok($hash2, 'HASH', 'produced HASH');
 
#warn Dumper $hash2;
my $exp2 = { request =>
  { first => XML::Compile::XOP::Include->new(bytes => \"Hello, World!\n"
     , type => 'application/octet-stream', cid => 'mycid2@home'
     , charset => undef)
  , second => "Hello, World!\n"
  }};

is_deeply($hash2, $exp2 , "server parsed input");