summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornahi <nahi@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-12-05 13:26:26 +0000
committernahi <nahi@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-12-05 13:26:26 +0000
commit1ed39b739237bffd9c78e696c09462ce67017b95 (patch)
tree032499118c88146a86aede2d89fa707bb242edc7
parent3be09a4f4ffee94601051c37e5303b9546d816e1 (diff)
downloadruby-1ed39b739237bffd9c78e696c09462ce67017b95.tar.gz
* lib/soap/netHttpClient.rb: proxy support did not work. fixed.
* lib/soap/property.rb: add class methods for loading property from stream/file/propertyfile. propertyfile is a file which is located at somedir in $:. * lib/soap/soap.rb, lib/soap/wsdlDriver.rb, lib/soap/rpc/driver.rb, lib/wsdl/importer.rb: load property from propertyfile 'soap/property' e.g. /usr/local/lib/ruby/site_ruby/1.8/soap/property. * test/soap/test_property.rb, test/soap/test_streamhandler.rb: new file. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@5121 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog14
-rw-r--r--MANIFEST2
-rw-r--r--lib/soap/netHttpClient.rb19
-rw-r--r--lib/soap/property.rb42
-rw-r--r--lib/soap/rpc/driver.rb24
-rw-r--r--lib/soap/soap.rb1
-rw-r--r--lib/soap/wsdlDriver.rb26
-rw-r--r--lib/wsdl/importer.rb13
-rw-r--r--test/soap/test_property.rb345
-rw-r--r--test/soap/test_streamhandler.rb197
10 files changed, 645 insertions, 38 deletions
diff --git a/ChangeLog b/ChangeLog
index a2cb2ff8db..7ca33c8f8f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+Fri Dec 5 22:23:04 2003 NAKAMURA, Hiroshi <nahi@ruby-lang.org>
+
+ * lib/soap/netHttpClient.rb: proxy support did not work. fixed.
+
+ * lib/soap/property.rb: add class methods for loading property from
+ stream/file/propertyfile. propertyfile is a file which is located at
+ somedir in $:.
+
+ * lib/soap/soap.rb, lib/soap/wsdlDriver.rb, lib/soap/rpc/driver.rb,
+ lib/wsdl/importer.rb: load property from propertyfile 'soap/property'
+ e.g. /usr/local/lib/ruby/site_ruby/1.8/soap/property.
+
+ * test/soap/test_property.rb, test/soap/test_streamhandler.rb: new file.
+
Fri Dec 5 17:26:23 2003 Nobuyoshi Nakada <nobu@ruby-lang.org>
* eval.c (rb_exec_end_proc): maintain tmp_end_procs.
diff --git a/MANIFEST b/MANIFEST
index 41555142be..40c60ae64a 100644
--- a/MANIFEST
+++ b/MANIFEST
@@ -743,7 +743,9 @@ test/soap/marshal/test_digraph.rb
test/soap/marshal/test_marshal.rb
test/soap/marshal/test_struct.rb
test/soap/test_basetype.rb
+test/soap/test_property.rb
test/soap/test_soapelement.rb
+test/soap/test_streamhandler.rb
test/stringio/test_stringio.rb
test/strscan/test_stringscanner.rb
test/testunit/collector/test_dir.rb
diff --git a/lib/soap/netHttpClient.rb b/lib/soap/netHttpClient.rb
index 3eacdad69d..a0b3b7ca26 100644
--- a/lib/soap/netHttpClient.rb
+++ b/lib/soap/netHttpClient.rb
@@ -21,7 +21,7 @@ class NetHttpClient
false
end
- attr_accessor :proxy
+ attr_reader :proxy
attr_accessor :no_proxy
attr_accessor :debug_dev
attr_accessor :ssl_config # ignored for now.
@@ -34,21 +34,30 @@ class NetHttpClient
@session_manager = SessionManager.new
@no_proxy = nil
end
+
+ def proxy=(proxy_str)
+ if proxy_str.nil?
+ @proxy = nil
+ else
+ @proxy = URI.parse(proxy_str)
+ end
+ end
def set_basic_auth(uri, user_id, passwd)
- # ignored for now.
+ # net/http does not handle url.
+ @basic_auth = [user_id, passwd]
end
def set_cookie_store(filename)
- # ignored for now.
+ # ignored.
end
def reset(url)
- # ignored for now.
+ # no persistent connection. ignored.
end
def reset_all
- # ignored for now.
+ # no persistent connection. ignored.
end
def post(url, req_body, header = {})
diff --git a/lib/soap/property.rb b/lib/soap/property.rb
index 84fa876cae..b67ffa8361 100644
--- a/lib/soap/property.rb
+++ b/lib/soap/property.rb
@@ -12,6 +12,44 @@ module SOAP
class Property
include Enumerable
+ # Property file format:
+ # line separator is \r?\n. 1 line per a property.
+ # line which begins with '#' is comment line. empty line is ignored.
+ # key/value separator is ':', '=', or \s.
+ # '\' as escape character. but line separator cannot be escaped.
+ # \s at the head/tail of key/value are trimmed.
+ def self.load(stream)
+ prop = new
+ stream.each_with_index do |line, lineno|
+ line.sub!(/\r?\n\z/, '')
+ next if /^(#.*|)$/ =~ line
+ if /^\s*([^=:\s\\]+(?:\\.[^=:\s\\]*)*)\s*[=:\s]\s*(.*)$/ =~ line
+ key, value = $1, $2
+ key = eval("\"#{key}\"")
+ value = eval("\"#{value.strip}\"")
+ prop[key] = value
+ else
+ raise TypeError.new("property format error at line #{lineno + 1}: `#{line}'")
+ end
+ end
+ prop
+ end
+
+ def self.open(filename)
+ File.open(filename) do |f|
+ load(f)
+ end
+ end
+
+ def self.loadproperty(propname)
+ $:.each do |path|
+ if File.file?(file = File.join(path, propname))
+ return open(file)
+ end
+ end
+ nil
+ end
+
def initialize
@store = Hash.new
@hook = Hash.new
@@ -193,7 +231,7 @@ private
when Symbol
[name]
when String
- name.split(/\./)
+ name.scan(/[^.\\]+(?:\\.[^.\\])*/) # split with unescaped '.'
when Array
name
else
@@ -212,7 +250,7 @@ private
end
def to_key(name)
- name.to_s.downcase.intern
+ name.to_s.downcase
end
def generate_new_key
diff --git a/lib/soap/rpc/driver.rb b/lib/soap/rpc/driver.rb
index e6ca3f39d3..dd433ca33f 100644
--- a/lib/soap/rpc/driver.rb
+++ b/lib/soap/rpc/driver.rb
@@ -82,12 +82,6 @@ class Driver
@servant = Servant__.new(self, endpoint_url, namespace)
@servant.soapaction = soapaction
@proxy = @servant.proxy
- if env_httpproxy = ::SOAP::Env::HTTP_PROXY
- @servant.options["protocol.http.proxy"] = env_httpproxy
- end
- if env_no_proxy = ::SOAP::Env::NO_PROXY
- @servant.options["protocol.http.no_proxy"] = env_no_proxy
- end
end
def inspect
@@ -144,8 +138,7 @@ private
@mapping_registry = nil
@soapaction = nil
@wiredump_file_base = nil
- @options = ::SOAP::Property.new
- set_options
+ @options = setup_options
@streamhandler = HTTPPostStreamHandler.new(endpoint_url,
@options["protocol.http"] ||= ::SOAP::Property.new)
@proxy = Proxy.new(@streamhandler, @soapaction)
@@ -244,14 +237,21 @@ private
end
end
- def set_options
- @options.add_hook("protocol.mandatorycharset") do |key, value|
+ def setup_options
+ if opt = Property.loadproperty(::SOAP::PropertyName)
+ opt = opt["client"]
+ end
+ opt ||= Property.new
+ opt.add_hook("protocol.mandatorycharset") do |key, value|
@proxy.mandatorycharset = value
end
- @options.add_hook("protocol.wiredump_file_base") do |key, value|
+ opt.add_hook("protocol.wiredump_file_base") do |key, value|
@wiredump_file_base = value
end
- @options["protocol.http.charset"] = XSD::Charset.encoding_label
+ opt["protocol.http.charset"] ||= XSD::Charset.encoding_label
+ opt["protocol.http.proxy"] ||= Env::HTTP_PROXY
+ opt["protocol.http.no_proxy"] ||= Env::NO_PROXY
+ opt
end
end
end
diff --git a/lib/soap/soap.rb b/lib/soap/soap.rb
index b082823181..ea2b4db2e1 100644
--- a/lib/soap/soap.rb
+++ b/lib/soap/soap.rb
@@ -14,6 +14,7 @@ module SOAP
Version = '1.5.2'
+PropertyName = 'soap/property'
EnvelopeNamespace = 'http://schemas.xmlsoap.org/soap/envelope/'
EncodingNamespace = 'http://schemas.xmlsoap.org/soap/encoding/'
diff --git a/lib/soap/wsdlDriver.rb b/lib/soap/wsdlDriver.rb
index 3931c7c514..0961751474 100644
--- a/lib/soap/wsdlDriver.rb
+++ b/lib/soap/wsdlDriver.rb
@@ -132,12 +132,6 @@ class WSDLDriver
def initialize(wsdl, port, logdev)
@servant = Servant__.new(self, wsdl, port, logdev)
- if env_httpproxy = ::SOAP::Env::HTTP_PROXY
- @servant.options["protocol.http.proxy"] = env_httpproxy
- end
- if env_httpproxy = ::SOAP::Env::NO_PROXY
- @servant.options["protocol.http.no_proxy"] = env_httpproxy
- end
end
def inspect
@@ -171,8 +165,7 @@ class WSDLDriver
@port = port
@logdev = logdev
- @options = ::SOAP::Property.new
- set_options
+ @options = setup_options
@mapping_registry = nil # for rpc unmarshal
@wsdl_mapping_registry = nil # for rpc marshal
@default_encodingstyle = EncodingNamespace
@@ -189,7 +182,7 @@ class WSDLDriver
@doc_mapper = Mapper.new(@wsdl_elements, @wsdl_types)
endpoint_url = @port.soap_address.location
@streamhandler = HTTPPostStreamHandler.new(endpoint_url,
- @options["protocol.http"] ||= ::SOAP::Property.new)
+ @options["protocol.http"] ||= Property.new)
# Convert a map which key is QName, to a Hash which key is String.
@operations = {}
@port.inputoperation_map.each do |op_name, op_info|
@@ -402,14 +395,21 @@ class WSDLDriver
@logdev.add(sev, nil, self.class) { yield } if @logdev
end
- def set_options
- @options.add_hook("protocol.mandatorycharset") do |key, value|
+ def setup_options
+ if opt = Property.loadproperty(::SOAP::PropertyName)
+ opt = opt["client"]
+ end
+ opt ||= Property.new
+ opt.add_hook("protocol.mandatorycharset") do |key, value|
@mandatorycharset = value
end
- @options.add_hook("protocol.wiredump_file_base") do |key, value|
+ opt.add_hook("protocol.wiredump_file_base") do |key, value|
@wiredump_file_base = value
end
- @options["protocol.http.charset"] = XSD::Charset.encoding_label
+ opt["protocol.http.charset"] ||= XSD::Charset.encoding_label
+ opt["protocol.http.proxy"] ||= Env::HTTP_PROXY
+ opt["protocol.http.no_proxy"] ||= Env::NO_PROXY
+ opt
end
class Mapper
diff --git a/lib/wsdl/importer.rb b/lib/wsdl/importer.rb
index df354311a1..fac02b51a0 100644
--- a/lib/wsdl/importer.rb
+++ b/lib/wsdl/importer.rb
@@ -9,6 +9,7 @@
require 'wsdl/info'
require 'wsdl/parser'
require 'soap/soap'
+require 'soap/property'
module WSDL
@@ -29,15 +30,15 @@ class Importer
content = File.open(location).read
else
client = web_client.new(nil, "WSDL4R")
- if env_httpproxy = ::SOAP::Env::HTTP_PROXY
- client.proxy = env_httpproxy
- end
- if env_no_proxy = ::SOAP::Env::NO_PROXY
- client.no_proxy = env_no_proxy
+ if opt = ::SOAP::Property.loadproperty(::SOAP::PropertyName)
+ client.proxy = opt["client.protocol.http.proxy"]
+ client.no_proxy = opt["client.protocol.http.no_proxy"]
end
+ client.proxy ||= ::SOAP::Env::HTTP_PROXY
+ client.no_proxy ||= ::SOAP::Env::NO_PROXY
content = client.get_content(location)
end
- opt = {} # charset?
+ opt = {}
begin
WSDL::Parser.new(opt).parse(content)
rescue WSDL::Parser::ParseError => orgexcn
diff --git a/test/soap/test_property.rb b/test/soap/test_property.rb
new file mode 100644
index 0000000000..d7d6c2edf7
--- /dev/null
+++ b/test/soap/test_property.rb
@@ -0,0 +1,345 @@
+require 'test/unit'
+require 'soap/property'
+
+
+module SOAP
+
+
+class TestProperty < Test::Unit::TestCase
+ def setup
+ @prop = ::SOAP::Property.new
+ end
+
+ def teardown
+ # Nothing to do.
+ end
+
+ def test_s_load
+ propstr = <<__EOP__
+
+# comment1
+
+# comment2\r
+# comment2
+
+\r
+a.b.0 = 1
+a.b.1 = 2
+a.b.2 = 3
+client.protocol.http.proxy=http://myproxy:8080 \r
+client.protocol.http.no_proxy: intranet.example.com,local.example.com\r
+client.protocol.http.protocol_version = 1.0
+foo\\:bar\\=baz = qux
+foo\\\\.bar.baz=\tq\\\\ux\ttab
+ a\\ b = 1
+__EOP__
+ prop = Property.load(propstr)
+ assert_equal(["1", "2", "3"], prop["a.b"].values.sort)
+ assert_equal("intranet.example.com,local.example.com",
+ prop["client.protocol.http.no_proxy"])
+ assert_equal("http://myproxy:8080", prop["client.protocol.http.proxy"])
+ assert_equal("1.0", prop["client.protocol.http.protocol_version"])
+ assert_equal("q\\ux\ttab", prop['foo\.bar.baz'])
+ assert_equal("1", prop['a b'])
+ end
+
+ def test_initialize
+ prop = ::SOAP::Property.new
+ # store is empty
+ assert_nil(prop["a"])
+ # does hook work?
+ assert_equal(1, prop["a"] = 1)
+ end
+
+ def test_aref
+ # name_to_a
+ assert_nil(@prop[:foo])
+ assert_nil(@prop["foo"])
+ assert_nil(@prop[[:foo]])
+ assert_nil(@prop[["foo"]])
+ assert_raises(ArgumentError) do
+ @prop[1]
+ end
+ @prop[:foo] = :foo
+ assert_equal(:foo, @prop[:foo])
+ assert_equal(:foo, @prop["foo"])
+ assert_equal(:foo, @prop[[:foo]])
+ assert_equal(:foo, @prop[["foo"]])
+ end
+
+ def test_referent
+ # referent(1)
+ assert_nil(@prop["foo.foo"])
+ assert_nil(@prop[["foo", "foo"]])
+ assert_nil(@prop[["foo", :foo]])
+ @prop["foo.foo"] = :foo
+ assert_equal(:foo, @prop["foo.foo"])
+ assert_equal(:foo, @prop[["foo", "foo"]])
+ assert_equal(:foo, @prop[[:foo, "foo"]])
+ # referent(2)
+ @prop["bar.bar.bar"] = :bar
+ assert_equal(:bar, @prop["bar.bar.bar"])
+ assert_equal(:bar, @prop[["bar", "bar", "bar"]])
+ assert_equal(:bar, @prop[[:bar, "bar", :bar]])
+ end
+
+ def test_to_key_and_deref
+ @prop["foo.foo"] = :foo
+ assert_equal(:foo, @prop["fOo.FoO"])
+ assert_equal(:foo, @prop[[:fOO, :FOO]])
+ assert_equal(:foo, @prop[["FoO", :Foo]])
+ # deref_key negative test
+ assert_raises(ArgumentError) do
+ @prop["baz"] = 1
+ @prop["baz.qux"] = 2
+ end
+ end
+
+ def test_value_hook
+ tag = Object.new
+ tested = false
+ @prop.add_hook("FOO.BAR.BAZ") do |key, value|
+ assert_equal("foo.bar.baz", key)
+ assert_equal(tag, value)
+ tested = true
+ end
+ @prop["Foo.baR.baZ"] = tag
+ assert_equal(tag, @prop["foo.bar.baz"])
+ assert(tested)
+ @prop["foo.bar"] = 1 # unhook the above block
+ assert_equal(1, @prop["foo.bar"])
+ end
+
+ def test_key_hook
+ tag = Object.new
+ tested = 0
+ @prop.add_hook("foo") do |key, value|
+ assert_equal("foo.bar.baz.qux", key)
+ assert_equal(tag, value)
+ tested += 1
+ end
+ @prop.add_hook("foo.bar") do |key, value|
+ assert_equal("foo.bar.baz.qux", key)
+ assert_equal(tag, value)
+ tested += 1
+ end
+ @prop.add_hook("foo.bar.baz") do |key, value|
+ assert_equal("foo.bar.baz.qux", key)
+ assert_equal(tag, value)
+ tested += 1
+ end
+ @prop.add_hook("foo.bar.baz.qux") do |key, value|
+ assert_equal("foo.bar.baz.qux", key)
+ assert_equal(tag, value)
+ tested += 1
+ end
+ @prop["foo.bar.baz.qux"] = tag
+ assert_equal(tag, @prop["foo.bar.baz.qux"])
+ assert_equal(4, tested)
+ end
+
+ def test_keys
+ assert(@prop.keys.empty?)
+ @prop["foo"] = 1
+ @prop["bar"]
+ @prop["BAz"] = 2
+ assert_equal(2, @prop.keys.size)
+ assert(@prop.keys.member?("foo"))
+ assert(@prop.keys.member?("baz"))
+ #
+ assert_nil(@prop["a"])
+ @prop["a.a"] = 1
+ assert_instance_of(::SOAP::Property, @prop["a"])
+ @prop["a.b"] = 1
+ @prop["a.c"] = 1
+ assert_equal(3, @prop["a"].keys.size)
+ assert(@prop["a"].keys.member?("a"))
+ assert(@prop["a"].keys.member?("b"))
+ assert(@prop["a"].keys.member?("c"))
+ end
+
+ def test_lshift
+ assert(@prop.empty?)
+ @prop << 1
+ assert_equal([1], @prop.values)
+ assert_equal(1, @prop["0"])
+ @prop << 1
+ assert_equal([1, 1], @prop.values)
+ assert_equal(1, @prop["1"])
+ @prop << 1
+ assert_equal([1, 1, 1], @prop.values)
+ assert_equal(1, @prop["2"])
+ #
+ @prop["abc.def"] = o = SOAP::Property.new
+ tested = 0
+ o.add_hook do |k, v|
+ tested += 1
+ end
+ @prop["abc.def"] << 1
+ @prop["abc.def"] << 2
+ @prop["abc.def"] << 3
+ @prop["abc.def"] << 4
+ assert_equal(4, tested)
+ end
+
+ def test_lock_each
+ @prop["a.b.c.d.e"] = 1
+ @prop["a.b.d"] = branch = ::SOAP::Property.new
+ @prop["a.b.d.e.f"] = 2
+ @prop.lock
+ assert(@prop.locked?)
+ assert_instance_of(::SOAP::Property, @prop["a"])
+ assert_raises(TypeError) do
+ @prop["b"]
+ end
+ #
+ @prop["a"].lock
+ assert_raises(TypeError) do
+ @prop["a"]
+ end
+ assert_instance_of(::SOAP::Property, @prop["a.b"])
+ #
+ @prop["a.b"].lock
+ assert_raises(TypeError) do
+ @prop["a.b"]
+ end
+ assert_raises(TypeError) do
+ @prop["a"]
+ end
+ #
+ @prop["a.b.c.d"].lock
+ assert_instance_of(::SOAP::Property, @prop["a.b.c"])
+ assert_raises(TypeError) do
+ @prop["a.b.c.d"]
+ end
+ assert_instance_of(::SOAP::Property, @prop["a.b.d"])
+ #
+ branch["e"].lock
+ assert_instance_of(::SOAP::Property, @prop["a.b.d"])
+ assert_raises(TypeError) do
+ @prop["a.b.d.e"]
+ end
+ assert_raises(TypeError) do
+ branch["e"]
+ end
+ end
+
+ def test_lock_cascade
+ @prop["a.a"] = nil
+ @prop["a.b.c"] = 1
+ @prop["b"] = false
+ @prop.lock(true)
+ assert(@prop.locked?)
+ assert_equal(nil, @prop["a.a"])
+ assert_equal(1, @prop["a.b.c"])
+ assert_equal(false, @prop["b"])
+ assert_raises(TypeError) do
+ @prop["c"]
+ end
+ assert_raises(TypeError) do
+ @prop["c"] = 2
+ end
+ assert_raises(TypeError) do
+ @prop["a.b.R"]
+ end
+ assert_raises(TypeError) do
+ @prop.add_hook do
+ assert(false)
+ end
+ end
+ assert_raises(TypeError) do
+ @prop.add_hook("c") do
+ assert(false)
+ end
+ end
+ assert_raises(TypeError) do
+ @prop.add_hook("a.c") do
+ assert(false)
+ end
+ end
+ assert_nil(@prop["a.a"])
+ @prop["a.a"] = 2
+ assert_equal(2, @prop["a.a"])
+ #
+ @prop.unlock(true)
+ assert_nil(@prop["c"])
+ @prop["c"] = 2
+ assert_equal(2, @prop["c"])
+ @prop["a.d.a.a"] = :foo
+ assert_equal(:foo, @prop["a.d.a.a"])
+ tested = false
+ @prop.add_hook("a.c") do |name, value|
+ assert(true)
+ tested = true
+ end
+ @prop["a.c"] = 3
+ assert(tested)
+ end
+
+ def test_hook_then_lock
+ tested = false
+ @prop.add_hook("a.b.c") do |name, value|
+ assert_equal("a.b.c", name)
+ tested = true
+ end
+ @prop["a.b"].lock
+ assert(!tested)
+ @prop["a.b.c"] = 5
+ assert(tested)
+ assert_equal(5, @prop["a.b.c"])
+ assert_raises(TypeError) do
+ @prop["a.b.d"] = 5
+ end
+ end
+
+ def test_lock_unlock_return
+ assert_equal(@prop, @prop.lock)
+ assert_equal(@prop, @prop.unlock)
+ end
+
+ def test_lock_split
+ @prop["a.b.c"] = 1
+ assert_instance_of(::SOAP::Property, @prop["a.b"])
+ @prop["a.b.d"] = branch = ::SOAP::Property.new
+ @prop["a.b.d.e"] = 2
+ assert_equal(branch, @prop["a.b.d"])
+ assert_equal(branch, @prop[:a][:b][:d])
+ @prop.lock(true)
+ # split error 1
+ assert_raises(TypeError) do
+ @prop["a.b"]
+ end
+ # split error 2
+ assert_raises(TypeError) do
+ @prop["a"]
+ end
+ @prop["a.b.c"] = 2
+ assert_equal(2, @prop["a.b.c"])
+ # replace error
+ assert_raises(TypeError) do
+ @prop["a.b.c"] = ::SOAP::Property.new
+ end
+ # override error
+ assert_raises(TypeError) do
+ @prop["a.b"] = 1
+ end
+ #
+ assert_raises(TypeError) do
+ @prop["a.b.d"] << 1
+ end
+ assert_raises(TypeError) do
+ branch << 1
+ end
+ branch.unlock(true)
+ branch << 1
+ branch << 2
+ branch << 3
+ assert_equal(2, @prop["a.b.d.e"])
+ assert_equal(1, @prop["a.b.d.1"])
+ assert_equal(2, @prop["a.b.d.2"])
+ assert_equal(3, @prop["a.b.d.3"])
+ end
+end
+
+
+end
diff --git a/test/soap/test_streamhandler.rb b/test/soap/test_streamhandler.rb
new file mode 100644
index 0000000000..3713e9e172
--- /dev/null
+++ b/test/soap/test_streamhandler.rb
@@ -0,0 +1,197 @@
+require 'test/unit'
+require 'soap/rpc/driver'
+require 'webrick'
+require 'webrick/httpproxy'
+require 'logger'
+
+
+module SOAP
+
+
+class TestStreamHandler < Test::Unit::TestCase
+ Port = 17171
+ ProxyPort = 17172
+
+ def setup
+ @logger = Logger.new(STDERR)
+ @logger.level = Logger::Severity::ERROR
+ @url = "http://localhost:#{Port}/"
+ @proxyurl = "http://localhost:#{ProxyPort}/"
+ @server = @proxyserver = @client = nil
+ @server_thread = @proxyserver_thread = nil
+ setup_server
+ setup_client
+ end
+
+ def teardown
+ teardown_client
+ teardown_proxyserver if @proxyserver
+ teardown_server
+ end
+
+ def setup_server
+ @server = WEBrick::HTTPServer.new(
+ :BindAddress => "0.0.0.0",
+ :Logger => @logger,
+ :Port => Port,
+ :AccessLog => [],
+ :DocumentRoot => File.dirname(File.expand_path(__FILE__))
+ )
+ @server.mount(
+ '/',
+ WEBrick::HTTPServlet::ProcHandler.new(method(:do_server_proc).to_proc)
+ )
+ @server_thread = start_server_thread(@server)
+ end
+
+ def setup_proxyserver
+ @proxyserver = WEBrick::HTTPProxyServer.new(
+ :BindAddress => "0.0.0.0",
+ :Logger => @logger,
+ :Port => ProxyPort,
+ :AccessLog => []
+ )
+ @proxyserver_thread = start_server_thread(@proxyserver)
+ end
+
+ def setup_client
+ @client = SOAP::RPC::Driver.new(@url, '')
+ @client.add_method("do_server_proc")
+ end
+
+ def teardown_server
+ @server.shutdown
+ @server_thread.kill
+ @server_thread.join
+ end
+
+ def teardown_proxyserver
+ @proxyserver.shutdown
+ @proxyserver_thread.kill
+ @proxyserver_thread.join
+ end
+
+ def teardown_client
+ @client.reset_stream
+ end
+
+ def start_server_thread(server)
+ 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
+ t
+ end
+
+ def do_server_proc(req, res)
+ res['content-type'] = 'text/xml'
+ res.body = <<__EOX__
+<?xml version="1.0" encoding="utf-8" ?>
+<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+ <env:Body>
+ <n1:do_server_proc xmlns:n1="urn:foo" env:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
+ <return xsi:nil="true"/>
+ </n1:do_server_proc>
+ </env:Body>
+</env:Envelope>
+__EOX__
+ end
+
+ def parse_req_header(str)
+ if ::SOAP::StreamHandler::Client.to_s == 'SOAP::NetHttpClient'
+ str = eval(str.split(/\r?\n/)[4][3..-1])
+ end
+ parse_req_header_http_access2(str)
+ end
+
+ def parse_req_header_http_access2(str)
+ headerp = false
+ headers = {}
+ req = nil
+ str.split(/(?:\r?\n)/).each do |line|
+ if headerp and /^$/ =~line
+ headerp = false
+ break
+ end
+ if headerp
+ k, v = line.scan(/^([^:]+):\s*(.*)$/)[0]
+ headers[k.downcase] = v
+ end
+ if /^POST/ =~ line
+ req = line
+ headerp = true
+ end
+ end
+ return req, headers
+ end
+
+ def test_normal
+ str = ""
+ @client.wiredump_dev = str
+ assert_nil(@client.do_server_proc)
+ r, h = parse_req_header(str)
+ assert_match(%r"POST / HTTP/1.", r)
+ assert(/^text\/xml;/ =~ h["content-type"])
+ end
+
+ def test_basic_auth
+ unless Object.const_defined?('HTTPAccess2')
+ STDERR.puts("basic_auth is not supported")
+ return
+ end
+ str = ""
+ @client.wiredump_dev = str
+ @client.options["protocol.http.basic_auth"] << [@url, "foo", "bar"]
+ assert_nil(@client.do_server_proc)
+ r, h = parse_req_header(str)
+ assert_equal("Basic Zm9vOmJhcg==", h["authorization"])
+ end
+
+ def test_proxy
+ if Object.const_defined?('HTTPAccess2')
+ backup = HTTPAccess2::Client::NO_PROXY_HOSTS.dup
+ HTTPAccess2::Client::NO_PROXY_HOSTS.clear
+ else
+ backup = SOAP::NetHttpClient::NO_PROXY_HOSTS.dup
+ SOAP::NetHttpClient::NO_PROXY_HOSTS.clear
+ end
+ setup_proxyserver
+ str = ""
+ @client.wiredump_dev = str
+ @client.options["protocol.http.proxy"] = @proxyurl
+ assert_nil(@client.do_server_proc)
+ r, h = parse_req_header(str)
+ assert_match(%r"POST http://localhost:17171/ HTTP/1.", r)
+ ensure
+ if Object.const_defined?('HTTPAccess2')
+ HTTPAccess2::Client::NO_PROXY_HOSTS.replace(backup)
+ else
+ SOAP::NetHttpClient::NO_PROXY_HOSTS.replace(backup)
+ end
+ end
+
+ def test_charset
+ str = ""
+ @client.wiredump_dev = str
+ @client.options["protocol.http.charset"] = "iso-8859-8"
+ assert_nil(@client.do_server_proc)
+ r, h = parse_req_header(str)
+ assert_equal("text/xml; charset=iso-8859-8", h["content-type"])
+ #
+ str.replace("")
+ @client.options["protocol.http.charset"] = "iso-8859-3"
+ assert_nil(@client.do_server_proc)
+ r, h = parse_req_header(str)
+ assert_equal("text/xml; charset=iso-8859-3", h["content-type"])
+ end
+end
+
+
+end