summaryrefslogtreecommitdiff
path: root/test/wsdl/map/test_map.rb
blob: bee3a75892ce5aed6f4fb750fa5ce7c849532b17 (plain)
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
require 'test/unit'
require 'soap/rpc/httpserver'
require 'soap/wsdlDriver'


module WSDL


class TestMap < Test::Unit::TestCase
  Port = 17171
  DIR = File.dirname(File.expand_path(__FILE__))

  class Server < ::SOAP::RPC::HTTPServer
    def on_init
      add_method(self, 'map')
      add_method(self, 'map2', 'arg')
    end

    def map
      {1 => "a", 2 => "b"}
    end

    def map2(arg)
      arg
    end
  end

  def setup
    setup_server
    setup_client
  end

  def setup_server
    @server = Server.new(
      :Port => Port,
      :AccessLog => [],
      :SOAPDefaultNamespace => "urn:map"
    )
    @server.level = Logger::Severity::ERROR
    @t = Thread.new {
      Thread.current.abort_on_exception = true
      @server.start
    }
    while @server.status != :Running
      sleep 0.1
      unless @t.alive?
	@t.join
	raise
      end
    end
  end

  def setup_client
    wsdl = File.join(DIR, 'map.wsdl')
    @client = ::SOAP::WSDLDriverFactory.new(wsdl).create_driver
    @client.endpoint_url = "http://localhost:#{Port}/"
    @client.generate_explicit_type = true
  end

  def teardown
    teardown_server
    teardown_client
  end

  def teardown_server
    @server.shutdown
    @t.kill
    @t.join
  end

  def teardown_client
    @client.reset_stream
  end

  def test_by_wsdl
    dir = File.dirname(File.expand_path(__FILE__))
    wsdlfile = File.join(dir, 'map.wsdl')
    xml = File.open(File.join(dir, 'map.xml')) { |f| f.read }
    wsdl = WSDL::Importer.import(wsdlfile)
    service = wsdl.services[0]
    port = service.ports[0]
    wsdl_types = wsdl.collect_complextypes
    rpc_decode_typemap = wsdl_types + wsdl.soap_rpc_complextypes(port.find_binding)
    opt = {}
    opt[:default_encodingstyle] = ::SOAP::EncodingNamespace
    opt[:decode_typemap] = rpc_decode_typemap
    header, body = ::SOAP::Processor.unmarshal(xml, opt)
    map = ::SOAP::Mapping.soap2obj(body.response)
    assert_equal(["a1"], map["a"]["a1"])
    assert_equal(["a2"], map["a"]["a2"])
    assert_equal(["b1"], map["b"]["b1"])
    assert_equal(["b2"], map["b"]["b2"])
  end

  def test_wsdldriver
    assert_equal({1 => "a", 2 => "b"}, @client.map)
    assert_equal({1 => 2}, @client.map2({1 => 2}))
    assert_equal({1 => {2 => 3}}, @client.map2({1 => {2 => 3}}))
    assert_equal({["a", 2] => {2 => 3}}, @client.map2({["a", 2] => {2 => 3}}))
  end
end


end