1#!/bin/env perl 
2
3BEGIN {
4  unless(grep /blib/, @INC) {
5    chdir 't' if -d 't';
6    unshift @INC, '../lib' if -d '../lib';
7  }
8}
9
10use strict;
11use Test;
12
13BEGIN {
14  use SOAP::Lite;
15  eval { SOAP::Packager::MIME->new->initialize_parser; 1 };
16  if ($@) {
17    $@ =~ s/ at .+//; 
18    print "1..0 # Skip: $@"; exit;
19  }
20}
21
22BEGIN { plan tests => 15 }
23
24my($a, $soap, $d, $s, $r, $serialized, $deserialized);
25
26{ # check attachment deserialization
27  $soap = SOAP::Lite->new();
28  $soap->init_context();
29
30  ##############################################################################
31  print "Attachment deserialization (Content-ID) test(s)...\n";
32  $a = $soap->deserializer->deserialize(<<'EOX');
33Content-Type: Multipart/Related; boundary=MIME_boundary; type="text/xml"; start="<claim061400a.xml@claiming-it.com>"
34SOAPAction: http://schemas.risky-stuff.com/Auto-Claim
35Content-Description: This is the optional message description.
36
37--MIME_boundary
38Content-Type: text/xml; charset=UTF-8
39Content-Transfer-Encoding: 8bit
40Content-ID: <claim061400a.xml@claiming-it.com>
41
42<?xml version='1.0' ?>
43<soap:Envelope
44    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
45  <soap:Body>
46    <claim:insurance_claim_auto
47          id="insurance_claim_document_id"
48          xmlns:claim="http://schemas.risky-stuff.com/Auto-Claim">
49      <theSignedForm href="cid:claim061400a.tiff@claiming-it.com" />
50      <theCrashPhoto href="cid:claim061400a.jpeg@claiming-it.com" />
51      <somexml href="cid:claim061400a.somexml@claiming-it.com" />
52      <realxml href="cid:claim061400a.realxml@claiming-it.com" />
53      <!-- ... more claim details go here... -->
54    </claim:insurance_claim_auto>
55  </soap:Body>
56</soap:Envelope>
57
58--MIME_boundary
59Content-Type: image/tiff
60Content-Transfer-Encoding: base64
61Content-ID: <claim061400a.tiff@claiming-it.com>
62
63AAECAyAgIAQFBg==
64--MIME_boundary
65Content-Type: image/jpeg
66Content-Transfer-Encoding: binary
67Content-ID: <claim061400a.jpeg@claiming-it.com>
68
69...Raw JPEG image..
70--MIME_boundary
71Content-Type: text/plain
72Content-Transfer-Encoding: binary
73Content-ID: <claim061400a.somexml@claiming-it.com>
74
75<a><b>c</b></a>
76--MIME_boundary
77Content-Type: text/xml
78Content-Transfer-Encoding: binary
79Content-ID: <claim061400a.realxml@claiming-it.com>
80
81<a><b>c</b></a>
82--MIME_boundary--
83
84EOX
85
86  ok(ref $a);
87  ok(ref $a && ref $a->valueof('//insurance_claim_auto') && 
88                   $a->valueof('//insurance_claim_auto')->{theCrashPhoto} =~ /JPEG/);
89  ok(ref $a && $a->valueof('//theCrashPhoto') =~ /Raw JPEG image/);
90  ok(ref $a && $a->valueof('//theSignedForm') eq "\0\1\2\3   \4\5\6");
91  ok(ref $a && $a->valueof('//somexml') =~ m!<a><b>c</b></a>!);
92  ok(ref $a && $a->valueof('//realxml')->{b} eq 'c');
93
94  ##############################################################################
95  print "Attachment deserialization (Content-ID and Content-Location) test(s)...\n";
96  $a = $soap->deserializer->deserialize(<<'EOX');
97MIME-Version: 1.0
98Content-Type: Multipart/Related; boundary=MIME_boundary; type="text/xml"; start="<http://claiming-it.com/claim061400a.xml>"
99Content-Description: This is the optional message description.
100
101--MIME_boundary
102Content-Type: text/xml; charset=UTF-8
103Content-Transfer-Encoding: 8bit
104Content-ID: <http://claiming-it.com/claim061400a.xml>
105Content-Location: http://claiming-it.com/claim061400a.xml
106
107<?xml version='1.0' ?>
108<soap:Envelope
109    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
110  <soap:Body>
111    <claim:insurance_claim_auto
112          id="insurance_claim_document_id"
113          xmlns:claim="http://schemas.risky-stuff.com/Auto-Claim">
114      <theSignedForm href="http://claiming-it.com/claim061400a.tiff" />
115    </claim:insurance_claim_auto>
116  </soap:Body>
117</soap:Envelope>
118
119--MIME_boundary
120Content-Type: image/tiff
121Content-Transfer-Encoding: binary
122Content-ID: <http://claiming-it.com/claim061400a.tiff>
123Content-Location: http://claiming-it.com/claim061400a.tiff
124
125...binary TIFF image...
126--MIME_boundary--
127
128EOX
129
130  ok(ref $a);
131  ok(ref $a && ref $a->valueof('//insurance_claim_auto') && 
132                   $a->valueof('//insurance_claim_auto')->{theSignedForm} =~ /TIFF/);
133  ok(ref $a && $a->valueof('//theSignedForm') =~ /binary TIFF image/);
134
135  ##############################################################################
136  print "Attachment deserialization (relative Content-Location) test(s)...\n";
137  # TODO - this unit test breaks - it does not seem to be picking up a base location
138  #        from outer Content-Location
139  eval { $a = $soap->deserializer->deserialize(<<'EOX'); };
140MIME-Version: 1.0
141Content-Type: Multipart/Related; boundary=MIME_boundary; type="text/xml"; start="<http://claiming-it.com/claim061400a.xml>"
142Content-Description: This is the optional message description.
143Content-Location: http://claiming-it.com/
144
145--MIME_boundary
146Content-Type: text/xml; charset=UTF-8
147Content-Transfer-Encoding: 8bit
148Content-ID: <http://claiming-it.com/claim061400a.xml>
149Content-Location: claim061400a.xml
150
151<?xml version='1.0' ?>
152<soap:Envelope
153    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
154  <soap:Body>
155    <claim:insurance_claim_auto
156          id="insurance_claim_document_id"
157          xmlns:claim="http://schemas.risky-stuff.com/Auto-Claim">
158      <theSignedForm href="cid:claim061400a.tiff" />
159    </claim:insurance_claim_auto>
160  </soap:Body>
161</soap:Envelope>
162
163--MIME_boundary
164Content-Type: image/tiff
165Content-Transfer-Encoding: binary
166Content-Id: <claim061400a.tiff>
167Content-Location: claim061400a.tiff
168
169...binary TIFF image...
170--MIME_boundary--
171
172EOX
173  ok(ref $a);
174  ok(ref $a && ref $a->valueof('//insurance_claim_auto') && 
175                   $a->valueof('//insurance_claim_auto')->{theSignedForm} =~ /TIFF/);
176  ok(ref $a && $a->valueof('//theSignedForm') =~ /binary TIFF image/);
177
178  ##############################################################################
179  print "Attachment deserialization (no default Content-Location) test(s)...\n";
180  $a = $soap->deserializer->deserialize(<<'EOX');
181MIME-Version: 1.0
182Content-Type: Multipart/Related; boundary=MIME_boundary; type="text/xml"; start="<b6f4ccrt@15.4.9.92/s445>"
183Content-Description: This is the optional message description.
184
185--MIME_boundary
186Content-Type: text/xml; charset=UTF-8
187Content-Transfer-Encoding: 8bit
188Content-ID: <b6f4ccrt@15.4.9.92/s445>
189Content-Location: claim061400a.xml
190
191<?xml version='1.0' ?>
192<soap:Envelope
193    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
194  <soap:Body>
195    <claim:insurance_claim_auto
196          id="insurance_claim_document_id"
197          xmlns:claim="http://schemas.risky-stuff.com/Auto-Claim">
198      <theSignedForm href="cid:a34ccrt@15.4.9.92/s445" />
199    </claim:insurance_claim_auto>
200  </soap:Body>
201</soap:Envelope>
202
203--MIME_boundary
204Content-Type: image/tiff
205Content-Transfer-Encoding: binary
206Content-ID: <a34ccrt@15.4.9.92/s445>
207Content-Location: the_signed_form.tiff
208
209...binary TIFF image...
210--MIME_boundary-
211
212EOX
213#      <theSignedForm href="cid:the_signed_form.tiff"/>
214
215  ok(ref $a);
216  ok(ref $a && ref $a->valueof('//insurance_claim_auto') && 
217                   $a->valueof('//insurance_claim_auto')->{theSignedForm} =~ /TIFF/);
218  ok(ref $a && $a->valueof('//theSignedForm') =~ /binary TIFF image/);
219}
220