add base64 signature type (thanks, Shugo Maeda!)

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1293 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Leon Breedt 2005-05-07 22:57:08 +00:00
parent 8a41ea4588
commit 47a7084b94
8 changed files with 72 additions and 10 deletions

@ -1,5 +1,7 @@
*SVN*
* Add support for a :base64 signature type #1272 [Shugo Maeda]
* Fix that boolean fields were not rendered correctly in scaffolding
* Fix that scaffolding was not working for :delegated dispatching

@ -63,6 +63,12 @@ def cast_base_type(value, signature_type) # :nodoc:
Integer(value)
when :string
value.to_s
when :base64
if value.is_a?(ActionWebService::Base64)
value
else
ActionWebService::Base64.new(value.to_s)
end
when :bool
return false if value.nil?
return value if value == true || value == false

@ -11,6 +11,7 @@ def initialize(type_namespace=nil)
@type_namespace = type_namespace || 'urn:ActionWebService'
@registry = SOAP::Mapping::Registry.new
@type2binding = {}
register_static_factories
end
def soap_to_ruby(obj)
@ -43,13 +44,7 @@ def register_type(type)
array_binding = nil
if type.array?
array_mapping = @registry.find_mapped_soap_class(Array) rescue nil
if (array_mapping && !array_mapping[1].is_a?(SoapTypedArrayFactory)) || array_mapping.nil?
@registry.set(Array,
SOAP::SOAPArray,
SoapTypedArrayFactory.new)
array_mapping = @registry.find_mapped_soap_class(Array)
end
qname = XSD::QName.new(@type_namespace, soap_type_name(type.element_type.type_class.name) + 'Array')
array_binding = SoapBinding.new(self, qname, type, array_mapping, type_binding)
end
@ -104,6 +99,21 @@ def soap_base_type_name(type)
def soap_type_name(type_name)
type_name.gsub(/::/, '..')
end
def register_static_factories
@registry.add(ActionWebService::Base64,
SOAP::SOAPBase64,
SoapBase64Factory.new,
nil)
mapping = @registry.find_mapped_soap_class(ActionWebService::Base64)
@type2binding[ActionWebService::Base64] =
SoapBinding.new(self, SOAP::SOAPBase64::Type,
ActionWebService::Base64, mapping)
@registry.add(Array,
SOAP::SOAPArray,
SoapTypedArrayFactory.new,
nil)
end
end
class SoapBinding
@ -130,6 +140,7 @@ def qualified_type_name(ns=nil)
else
ns = XSD::NS.new
ns.assign(XSD::Namespace, SOAP::XSDNamespaceTag)
ns.assign(SOAP::EncodingNamespace, "soapenc")
xsd_klass = mapping[0].ancestors.find{|c| c.const_defined?('Type')}
return ns.name(XSD::AnyTypeName) unless xsd_klass
ns.name(xsd_klass.const_get('Type'))
@ -192,6 +203,22 @@ def soap2obj(obj_class, node, info, map)
end
end
class SoapBase64Factory < SOAP::Mapping::Factory
def obj2soap(soap_class, obj, info, map)
unless obj.is_a?(ActionWebService::Base64)
return nil
end
return soap_class.new(obj)
end
def soap2obj(obj_class, node, info, map)
unless node.type == SOAP::SOAPBase64::Type
return false
end
return true, obj_class.new(node.string)
end
end
end
end
end

@ -67,6 +67,8 @@ def value_to_xmlrpc_wire_format(value, value_type)
struct[key.to_s] = member_value
end
struct
elsif value.is_a?(ActionWebService::Base64)
XMLRPC::Base64.new(value)
elsif value.is_a?(Exception) && !value.is_a?(XMLRPC::FaultException)
XMLRPC::FaultException.new(2, value.message)
else

@ -40,8 +40,10 @@ def canonical_type_name(name)
case name
when :int, :integer, :fixnum, :bignum
:int
when :string, :base64
when :string
:string
when :base64
:base64
when :bool, :boolean
:bool
when :float, :double
@ -73,6 +75,8 @@ def class_to_type_name(klass)
:int
elsif klass == String
:string
elsif klass == Base64
:base64
elsif klass == TrueClass || klass == FalseClass
:bool
elsif derived_from?(Float, klass) || derived_from?(Precision, klass) || derived_from?(Numeric, klass)
@ -94,6 +98,8 @@ def type_name_to_class(name)
Integer
when :string
String
when :base64
Base64
when :bool
TrueClass
when :float
@ -197,4 +203,7 @@ def structured?
true
end
end
class Base64 < String
end
end

@ -56,6 +56,8 @@ class DirectAPI < ActionWebService::API::Base
api_method :hash_struct_return, :returns => [[Person]]
api_method :thrower
api_method :void
api_method :hex, :expects => [:base64], :returns => [:string]
api_method :unhex, :expects => [:string], :returns => [:base64]
end
class VirtualAPI < ActionWebService::API::Base
@ -216,6 +218,14 @@ def void
@void_called = @method_params
end
def hex(s)
return s.unpack("H*")[0]
end
def unhex(s)
return [s].pack("H*")
end
protected
def alwaysfail
@before_filter_called = true
@ -248,6 +258,8 @@ def test_direct_dispatching
result = do_method_call(@direct_controller, 'BaseStructReturn')
assert(result[0].is_a?(DispatcherTest::Person))
assert(result[1].is_a?(DispatcherTest::Person))
assert_equal("cafe", do_method_call(@direct_controller, 'Hex', "\xca\xfe"))
assert_equal("\xca\xfe", do_method_call(@direct_controller, 'Unhex', "cafe"))
end
def test_direct_entrypoint

@ -7,7 +7,7 @@ class API < ActionWebService::API::Base
api_method :expects, :expects => [:int, :bool]
api_method :returns, :returns => [:int, [:string]]
api_method :named_signature, :expects => [{:appkey=>:int}, {:publish=>:bool}]
api_method :string_types, :expects => ['int', 'string', 'bool']
api_method :string_types, :expects => ['int', 'string', 'bool', 'base64']
api_method :class_types, :expects => [TrueClass, Bignum, String]
end
end
@ -45,7 +45,7 @@ def test_signature_canonicalization
assert_equal([Integer, [String]], API.api_methods[:returns].returns.map{|x| x.array?? [x.element_type.type_class] : x.type_class})
assert_equal([[:appkey, Integer], [:publish, TrueClass]], API.api_methods[:named_signature].expects.map{|x| [x.name, x.type_class]})
assert_equal(nil, API.api_methods[:named_signature].returns)
assert_equal([Integer, String, TrueClass], API.api_methods[:string_types].expects.map{|x| x.type_class})
assert_equal([Integer, String, TrueClass, ActionWebService::Base64], API.api_methods[:string_types].expects.map{|x| x.type_class})
assert_equal(nil, API.api_methods[:string_types].returns)
assert_equal([TrueClass, Integer, String], API.api_methods[:class_types].expects.map{|x| x.type_class})
assert_equal(nil, API.api_methods[:class_types].returns)

@ -4,6 +4,7 @@ module CastingTest
class API < ActionWebService::API::Base
api_method :int, :expects => [:int]
api_method :str, :expects => [:string]
api_method :base64, :expects => [:base64]
api_method :bool, :expects => [:bool]
api_method :float, :expects => [:float]
api_method :time, :expects => [:time]
@ -22,6 +23,9 @@ class TC_Casting < Test::Unit::TestCase
def test_base_type_casting_valid
assert_equal 10000, cast_expects(:int, '10000')[0]
assert_equal '10000', cast_expects(:str, 10000)[0]
base64 = cast_expects(:base64, 10000)[0]
assert_equal '10000', base64
assert_instance_of ActionWebService::Base64, base64
[1, '1', 'true', 'y', 'yes'].each do |val|
assert_equal true, cast_expects(:bool, val)[0]
end