| 1 |
require 'test/unit' |
|---|
| 2 |
require 'soap/rpc/httpserver' |
|---|
| 3 |
require 'soap/wsdlDriver' |
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
module WSDL |
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
class TestMap < Test::Unit::TestCase |
|---|
| 10 |
Port = 17171 |
|---|
| 11 |
DIR = File.dirname(File.expand_path(__FILE__)) |
|---|
| 12 |
|
|---|
| 13 |
class Server < ::SOAP::RPC::HTTPServer |
|---|
| 14 |
def on_init |
|---|
| 15 |
add_method(self, 'map') |
|---|
| 16 |
add_method(self, 'map2', 'arg') |
|---|
| 17 |
end |
|---|
| 18 |
|
|---|
| 19 |
def map |
|---|
| 20 |
{1 => "a", 2 => "b"} |
|---|
| 21 |
end |
|---|
| 22 |
|
|---|
| 23 |
def map2(arg) |
|---|
| 24 |
arg |
|---|
| 25 |
end |
|---|
| 26 |
end |
|---|
| 27 |
|
|---|
| 28 |
def setup |
|---|
| 29 |
setup_server |
|---|
| 30 |
setup_client |
|---|
| 31 |
end |
|---|
| 32 |
|
|---|
| 33 |
def setup_server |
|---|
| 34 |
@server = Server.new( |
|---|
| 35 |
:BindAddress => "0.0.0.0", |
|---|
| 36 |
:Port => Port, |
|---|
| 37 |
:AccessLog => [], |
|---|
| 38 |
:SOAPDefaultNamespace => "urn:map" |
|---|
| 39 |
) |
|---|
| 40 |
@server.level = Logger::Severity::ERROR |
|---|
| 41 |
@t = Thread.new { |
|---|
| 42 |
Thread.current.abort_on_exception = true |
|---|
| 43 |
@server.start |
|---|
| 44 |
} |
|---|
| 45 |
end |
|---|
| 46 |
|
|---|
| 47 |
def setup_client |
|---|
| 48 |
wsdl = File.join(DIR, 'map.wsdl') |
|---|
| 49 |
@client = ::SOAP::WSDLDriverFactory.new(wsdl).create_rpc_driver |
|---|
| 50 |
@client.endpoint_url = "http://localhost:#{Port}/" |
|---|
| 51 |
@client.generate_explicit_type = true |
|---|
| 52 |
@client.wiredump_dev = STDOUT if $DEBUG |
|---|
| 53 |
end |
|---|
| 54 |
|
|---|
| 55 |
def teardown |
|---|
| 56 |
teardown_server if @server |
|---|
| 57 |
teardown_client if @client |
|---|
| 58 |
end |
|---|
| 59 |
|
|---|
| 60 |
def teardown_server |
|---|
| 61 |
@server.shutdown |
|---|
| 62 |
@t.kill |
|---|
| 63 |
@t.join |
|---|
| 64 |
end |
|---|
| 65 |
|
|---|
| 66 |
def teardown_client |
|---|
| 67 |
@client.reset_stream |
|---|
| 68 |
end |
|---|
| 69 |
|
|---|
| 70 |
def test_by_wsdl |
|---|
| 71 |
dir = File.dirname(File.expand_path(__FILE__)) |
|---|
| 72 |
wsdlfile = File.join(dir, 'map.wsdl') |
|---|
| 73 |
xml = File.open(File.join(dir, 'map.xml')) { |f| f.read } |
|---|
| 74 |
wsdl = WSDL::Importer.import(wsdlfile) |
|---|
| 75 |
service = wsdl.services[0] |
|---|
| 76 |
port = service.ports[0] |
|---|
| 77 |
wsdl_types = wsdl.collect_complextypes |
|---|
| 78 |
rpc_decode_typemap = wsdl_types + wsdl.soap_rpc_complextypes(port.find_binding) |
|---|
| 79 |
opt = {} |
|---|
| 80 |
opt[:default_encodingstyle] = ::SOAP::EncodingNamespace |
|---|
| 81 |
opt[:decode_typemap] = rpc_decode_typemap |
|---|
| 82 |
header, body = ::SOAP::Processor.unmarshal(xml, opt) |
|---|
| 83 |
map = ::SOAP::Mapping.soap2obj(body.response) |
|---|
| 84 |
assert_equal(["a1"], map["a"]["a1"]) |
|---|
| 85 |
assert_equal(["a2"], map["a"]["a2"]) |
|---|
| 86 |
assert_equal(["b1"], map["b"]["b1"]) |
|---|
| 87 |
assert_equal(["b2"], map["b"]["b2"]) |
|---|
| 88 |
end |
|---|
| 89 |
|
|---|
| 90 |
def test_wsdldriver |
|---|
| 91 |
assert_equal({1 => "a", 2 => "b"}, @client.map) |
|---|
| 92 |
assert_equal({1 => 2}, @client.map2({1 => 2})) |
|---|
| 93 |
assert_equal({1 => {2 => 3}}, @client.map2({1 => {2 => 3}})) |
|---|
| 94 |
assert_equal({["a", 2] => {2 => 3}}, @client.map2({["a", 2] => {2 => 3}})) |
|---|
| 95 |
end |
|---|
| 96 |
end |
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
end |
|---|