Welcome to the "trac"-ing site of soap4r!
[soap4r] [httpclient] [openpgp4u] [pkcs1] [logger] [csv] [vtr]

root/trunk/test/wsdl/document/test_rpc.rb

Revision 1963, 11.2 kB (checked in by nahi, 1 year ago)
  • update tests. do not crash at teardown when setup failed. closes #396.
Line 
1 require 'test/unit'
2 require 'wsdl/parser'
3 require 'wsdl/soap/wsdl2ruby'
4 require 'soap/rpc/standaloneServer'
5 require 'soap/wsdlDriver'
6 require File.join(File.dirname(File.expand_path(__FILE__)), '..', '..', 'testutil.rb')
7
8
9 module WSDL; module Document
10
11
12 class TestRPC < Test::Unit::TestCase
13   class Server < ::SOAP::RPC::StandaloneServer
14     Namespace = 'urn:docrpc'
15
16     def on_init
17       add_document_method(
18         self,
19         Namespace + ':echo',
20         'echo',
21         XSD::QName.new(Namespace, 'echo'),
22         XSD::QName.new(Namespace, 'echo_response')
23       )
24       add_document_method(
25         self,
26         Namespace + ':return_nil',
27         'return_nil',
28         nil,
29         XSD::QName.new(Namespace, 'echo_response')
30       )
31       add_document_method(
32         self,
33         Namespace + ':return_empty',
34         'return_empty',
35         nil,
36         XSD::QName.new(Namespace, 'echo_response')
37       )
38       self.literal_mapping_registry = EchoMappingRegistry::LiteralRegistry
39     end
40  
41     def echo(arg)
42       if arg.is_a?(Echoele)
43         # swap args
44         tmp = arg.struct1
45         arg.struct1 = arg.struct_2
46         arg.struct_2 = tmp
47         arg
48       else
49         # swap args
50         tmp = arg["struct1"]
51         arg["struct1"] = arg["struct-2"]
52         arg["struct-2"] = tmp
53         arg
54       end
55     end
56
57     def return_nil
58       e = Echoele.new
59       e.struct1 = Echo_struct.new(nil, nil)
60       e.struct_2 = Echo_struct.new(nil, nil)
61       e.long = nil
62       e
63     end
64
65     def return_empty
66       e = Echoele.new
67       e.struct1 = Echo_struct.new("", nil)
68       e.struct_2 = Echo_struct.new("", nil)
69       e.long = 0
70       e
71     end
72   end
73
74   DIR = File.dirname(File.expand_path(__FILE__))
75
76   Port = 17171
77
78   def setup
79     setup_classdef
80     setup_server
81     @client = nil
82   end
83
84   def teardown
85     teardown_server if @server
86     File.unlink(pathname('echo.rb')) unless $DEBUG
87     File.unlink(pathname('echoMappingRegistry.rb')) unless $DEBUG
88     File.unlink(pathname('echoDriver.rb')) unless $DEBUG
89     @client.reset_stream if @client
90   end
91
92   def setup_server
93     @server = Server.new('Test', "urn:rpc", '0.0.0.0', Port)
94     @server.level = Logger::Severity::ERROR
95     @server_thread = TestUtil.start_server_thread(@server)
96   end
97
98   def setup_classdef
99     gen = WSDL::SOAP::WSDL2Ruby.new
100     gen.location = pathname("document.wsdl")
101     gen.basedir = DIR
102     gen.logger.level = Logger::FATAL
103     gen.opt['classdef'] = nil
104     gen.opt['mapping_registry'] = nil
105     gen.opt['driver'] = nil
106     gen.opt['module_path'] = self.class.to_s.sub(/::[^:]+$/, '')
107     gen.opt['force'] = true
108     gen.run
109     TestUtil.require(DIR, 'echoDriver.rb', 'echoMappingRegistry.rb', 'echo.rb')
110   end
111
112   def teardown_server
113     @server.shutdown
114     @server_thread.kill
115     @server_thread.join
116   end
117
118   def pathname(filename)
119     File.join(DIR, filename)
120   end
121
122   def test_wsdl
123     wsdl = File.join(DIR, 'document.wsdl')
124     @client = ::SOAP::WSDLDriverFactory.new(wsdl).create_rpc_driver
125     @client.endpoint_url = "http://localhost:#{Port}/"
126     @client.wiredump_dev = STDOUT if $DEBUG
127     @client.literal_mapping_registry = EchoMappingRegistry::LiteralRegistry
128     do_test_with_stub(@client)
129   end
130
131   def test_driver_stub
132     @client = ::WSDL::Document::Docrpc_porttype.new
133     @client.endpoint_url = "http://localhost:#{Port}/"
134     @client.wiredump_dev = STDOUT if $DEBUG
135     do_test_with_stub(@client)
136   end
137
138   def test_nil_attribute
139     @client = ::WSDL::Document::Docrpc_porttype.new
140     @client.endpoint_url = "http://localhost:#{Port}/"
141     @client.wiredump_dev = STDOUT if $DEBUG
142     struct1 = Echo_struct.new("mystring1", now1 = Time.now)
143     struct1.xmlattr_m_attr = nil
144     struct2 = Echo_struct.new("mystr<>ing2", now2 = Time.now)
145     struct2.xmlattr_m_attr = ''
146     echo = Echoele.new(struct1, struct2, 105759347)
147     echo.xmlattr_attr_string = ''
148     echo.xmlattr_attr_int = nil
149     ret = @client.echo(echo)
150     # struct1 and struct2 are swapped
151     assert_equal('', ret.struct1.xmlattr_m_attr)
152     assert_equal(nil, ret.struct_2.xmlattr_m_attr)
153     assert_equal('', ret.xmlattr_attr_string)
154     assert_equal(nil, ret.xmlattr_attr_int)
155     assert_equal(105759347, ret.long)
156   end
157
158   def do_test_with_stub(client)
159     struct1 = Echo_struct.new("mystring1", now1 = Time.now)
160     struct1.xmlattr_m_attr = 'myattr1'
161     struct2 = Echo_struct.new("mystr<>ing2", now2 = Time.now)
162     struct2.xmlattr_m_attr = 'myattr2'
163     echo = Echoele.new(struct1, struct2, 105759347)
164     echo.xmlattr_attr_string = 'attr_str<>ing'
165     echo.xmlattr_attr_int = 5
166     ret = client.echo(echo)
167
168     # struct#m_datetime in a response is a DateTime even though
169     # struct#m_datetime in a request is a Time.
170     timeformat = "%Y-%m-%dT%H:%M:%S"
171     assert_equal("mystr<>ing2", ret.struct1.m_string)
172     assert_equal(now2.strftime(timeformat),
173       date2time(ret.struct1.m_datetime).strftime(timeformat))
174     assert_equal("mystring1", ret.struct_2.m_string)
175     assert_equal(now1.strftime(timeformat),
176       date2time(ret.struct_2.m_datetime).strftime(timeformat))
177     assert_equal("attr_str<>ing", ret.xmlattr_attr_string)
178     assert_equal(5, ret.xmlattr_attr_int)
179     assert_equal(105759347, ret.long)
180   end
181
182   def test_wsdl_with_map
183     wsdl = File.join(DIR, 'document.wsdl')
184     @client = ::SOAP::WSDLDriverFactory.new(wsdl).create_rpc_driver
185     @client.endpoint_url = "http://localhost:#{Port}/"
186     @client.wiredump_dev = STDOUT if $DEBUG
187
188     struct1 = {
189       :m_string => "mystring1",
190       :m_datetime => (now1 = Time.now),
191       :xmlattr_m_attr => "myattr1"
192     }
193     struct2 = {
194       "m_string" => "mystr<>ing2",
195       "m_datetime" => now2 = (Time.now),
196       "xmlattr_m_attr" => "myattr2"
197     }
198     echo = {
199       :struct1 => struct1,
200       "struct-2" => struct2,
201       :xmlattr_attr_string => 'attr_str<>ing',
202       "xmlattr_attr-int" => 5
203     }
204     ret = @client.echo(echo)
205     #
206     now1str = XSD::XSDDateTime.new(now1).to_s
207     now2str = XSD::XSDDateTime.new(now2).to_s
208     assert_equal("mystr<>ing2", ret.struct1.m_string)
209     assert_equal(now2str, ret.struct1.m_datetime)
210     assert_equal("mystring1", ret.struct_2.m_string)
211     assert_equal(now1str, ret.struct_2.m_datetime)
212     assert_equal("attr_str<>ing", ret.xmlattr_attr_string)
213     assert_equal("5", ret.xmlattr_attr_int)
214   end
215
216   def date2time(date)
217     if date.respond_to?(:to_time)
218       date.to_time
219     else
220       d = date.new_offset(0)
221       d.instance_eval {
222         Time.utc(year, mon, mday, hour, min, sec,
223           (sec_fraction * 86400000000).to_i)
224       }.getlocal
225     end
226   end
227
228   include ::SOAP
229   def test_naive
230     @client = ::SOAP::RPC::Driver.new("http://localhost:#{Port}/")
231     @client.add_document_method('echo', 'urn:docrpc:echo',
232       XSD::QName.new('urn:docrpc', 'echoele'),
233       XSD::QName.new('urn:docrpc', 'echo_response'))
234     @client.literal_mapping_registry = EchoMappingRegistry::LiteralRegistry
235     @client.wiredump_dev = STDOUT if $DEBUG
236
237     echo = SOAPElement.new('foo')
238     echo.extraattr['attr_string'] = 'attr_str<>ing'
239     echo.extraattr['attr-int'] = 5
240     echo.add(struct1 = SOAPElement.new('struct1'))
241     struct1.add(SOAPElement.new('m_string', 'mystring1'))
242     struct1.add(SOAPElement.new('m_datetime', '2005-03-17T19:47:31+01:00'))
243     struct1.extraattr['m_attr'] = 'myattr1'
244     echo.add(struct2 = SOAPElement.new('struct-2'))
245     struct2.add(SOAPElement.new('m_string', 'mystring2'))
246     struct2.add(SOAPElement.new('m_datetime', '2005-03-17T19:47:32+02:00'))
247     struct2.extraattr['m_attr'] = 'myattr2'
248     ret = @client.echo(echo)
249     timeformat = "%Y-%m-%dT%H:%M:%S"
250     assert_equal('mystring2', ret.struct1.m_string)
251     assert_equal('2005-03-17T19:47:32',
252       ret.struct1.m_datetime.strftime(timeformat))
253     assert_equal("mystring1", ret.struct_2.m_string)
254     assert_equal('2005-03-17T19:47:31',
255       ret.struct_2.m_datetime.strftime(timeformat))
256     assert_equal('attr_str<>ing', ret.xmlattr_attr_string)
257     assert_equal(5, ret.xmlattr_attr_int)
258
259     echo = {'struct1' => {'m_string' => 'mystring1', 'm_datetime' => '2005-03-17T19:47:31+01:00'},
260           'struct_2' => {'m_string' => 'mystring2', 'm_datetime' => '2005-03-17T19:47:32+02:00'}}
261     ret = @client.echo(echo)
262     timeformat = "%Y-%m-%dT%H:%M:%S"
263     assert_equal('mystring2', ret.struct1.m_string)
264     assert_equal('2005-03-17T19:47:32',
265       ret.struct1.m_datetime.strftime(timeformat))
266     assert_equal("mystring1", ret.struct_2.m_string)
267     assert_equal('2005-03-17T19:47:31',
268       ret.struct_2.m_datetime.strftime(timeformat))
269   end
270
271   def test_to_xml
272     @client = ::SOAP::RPC::Driver.new("http://localhost:#{Port}/")
273     @client.add_document_method('echo', 'urn:docrpc:echo',
274       XSD::QName.new('urn:docrpc', 'echoele'),
275       XSD::QName.new('urn:docrpc', 'echo_response'))
276     @client.literal_mapping_registry = EchoMappingRegistry::LiteralRegistry
277     @client.wiredump_dev = STDOUT if $DEBUG
278
279     require 'rexml/document'
280     echo = REXML::Document.new(<<__XML__.chomp)
281 <foo attr-int="5" attr_string="attr_string">
282   <struct1 m_attr="myattr1">
283     <m_string>mystring1</m_string>
284     <m_datetime>2005-03-17T19:47:31+01:00</m_datetime>
285   </struct1>
286   <struct-2 m_attr="myattr2">
287     <m_string>mystring2</m_string>
288     <m_datetime>2005-03-17T19:47:32+02:00</m_datetime>
289   </struct-2>
290 </foo>
291 __XML__
292     ret = @client.echo(echo)
293     timeformat = "%Y-%m-%dT%H:%M:%S"
294     assert_equal('mystring2', ret.struct1.m_string)
295     assert_equal('2005-03-17T19:47:32',
296       ret.struct1.m_datetime.strftime(timeformat))
297     assert_equal("mystring1", ret.struct_2.m_string)
298     assert_equal('2005-03-17T19:47:31',
299       ret.struct_2.m_datetime.strftime(timeformat))
300     assert_equal('attr_string', ret.xmlattr_attr_string)
301     assert_equal(5, ret.xmlattr_attr_int)
302     #
303     echoele = REXML::Document.new(<<__XML__.chomp)
304 <n1:echoele xmlns:n1="urn:docrpc">
305   <struct-2>
306     <m_datetime>2005-03-17T19:47:32+02:00</m_datetime>
307     <m_string>mystring2</m_string>
308   </struct-2>
309   <struct1>
310     <m_datetime>2005-03-17T19:47:31+01:00</m_datetime>
311     <m_string>mystring1</m_string>
312   </struct1>
313 </n1:echoele>
314 __XML__
315     ret = @client.echo(echoele)
316     timeformat = "%Y-%m-%dT%H:%M:%S"
317     assert_equal('mystring2', ret.struct1.m_string)
318     assert_equal('2005-03-17T19:47:32',
319       ret.struct1.m_datetime.strftime(timeformat))
320     assert_equal("mystring1", ret.struct_2.m_string)
321     assert_equal('2005-03-17T19:47:31',
322       ret.struct_2.m_datetime.strftime(timeformat))
323   end
324
325   def test_nil
326     @client = ::SOAP::RPC::Driver.new("http://localhost:#{Port}/")
327     @client.add_document_method('return_nil', 'urn:docrpc:return_nil',
328       nil,
329       XSD::QName.new('urn:docrpc', 'return_nil'))
330     @client.literal_mapping_registry = EchoMappingRegistry::LiteralRegistry
331     @client.wiredump_dev = STDOUT if $DEBUG
332
333     ret = @client.return_nil
334     assert_nil(ret.struct1.m_string)
335     assert_nil(ret.struct_2.m_string)
336     assert_nil(ret.long)
337   end
338
339   def test_empty
340     @client = ::SOAP::RPC::Driver.new("http://localhost:#{Port}/")
341     @client.add_document_method('return_empty', 'urn:docrpc:return_empty',
342       nil,
343       XSD::QName.new('urn:docrpc', 'return_empty'))
344     @client.literal_mapping_registry = EchoMappingRegistry::LiteralRegistry
345     @client.wiredump_dev = STDOUT if $DEBUG
346
347     ret = @client.return_empty
348     assert_equal("", ret.struct1.m_string)
349     assert_equal("", ret.struct_2.m_string)
350     assert_equal(0, ret.long)
351   end
352 end
353
354
355 end; end
Note: See TracBrowser for help on using the browser.