diff options
author | Leon Breedt <bitserf@gmail.com> | 2005-02-25 23:39:39 +0000 |
---|---|---|
committer | Leon Breedt <bitserf@gmail.com> | 2005-02-25 23:39:39 +0000 |
commit | 6f5a7b200443baf209d2f33c428ed4a4059782f7 (patch) | |
tree | 9c3942fe27be69c102873d9fdaa13f66dc12853d /actionwebservice/test/ws | |
parent | 10faf204b712763f05a2b3155a4fd9c5338f1fb2 (diff) | |
download | rails-6f5a7b200443baf209d2f33c428ed4a4059782f7.tar.gz rails-6f5a7b200443baf209d2f33c428ed4a4059782f7.tar.bz2 rails-6f5a7b200443baf209d2f33c428ed4a4059782f7.zip |
merged the changes for the upcoming 0.6.0:
seperate out protocol marshaling into a small 'ws' library in vendor, so that
AWS itself only does integration with ActionPack, and so we can keep protocol
specific code in AWS proper to a minimum. refactor unit tests to get 95%
code coverage (for a baseline).
be far more relaxed about the types given to us by the remote side, don't do
any poor man's type checking, just try to cast and marshal to the correct types if
possible, and if not, return what they gave us anyway. this should make interoperating
with fuzzy XML-RPC clients easier.
if exception reporting is turned on, do best-effort error responses, so that
we can avoid "Internal protocol error" with no details if there is a bug in
AWS itself.
also perform extensive cleanups on AWS proper.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@800 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionwebservice/test/ws')
-rw-r--r-- | actionwebservice/test/ws/abstract_encoding.rb | 68 | ||||
-rw-r--r-- | actionwebservice/test/ws/abstract_unit.rb | 14 | ||||
-rwxr-xr-x | actionwebservice/test/ws/gencov | 3 | ||||
-rwxr-xr-x | actionwebservice/test/ws/run | 5 | ||||
-rw-r--r-- | actionwebservice/test/ws/soap_marshaling_test.rb | 91 | ||||
-rw-r--r-- | actionwebservice/test/ws/soap_rpc_encoding_test.rb | 47 | ||||
-rw-r--r-- | actionwebservice/test/ws/types_test.rb | 41 | ||||
-rw-r--r-- | actionwebservice/test/ws/xmlrpc_encoding_test.rb | 34 |
8 files changed, 303 insertions, 0 deletions
diff --git a/actionwebservice/test/ws/abstract_encoding.rb b/actionwebservice/test/ws/abstract_encoding.rb new file mode 100644 index 0000000000..9a6aec44e0 --- /dev/null +++ b/actionwebservice/test/ws/abstract_encoding.rb @@ -0,0 +1,68 @@ +require File.dirname(__FILE__) + '/abstract_unit' + +module Nested + class StructClass + attr_accessor :name + attr_accessor :version + + def initialize + @name = 5 + @version = "1.0" + end + + def ==(other) + @name == other.name && @version == other.version + end + end +end + +module EncodingTest + def setup + @call_signature = [:int, :bool, :string, :float, [:time], Nested::StructClass] + @call_params = [1, true, "string", 5.0, [Time.now], Nested::StructClass.new] + @response_signature = [:string] + @response_param = "hello world" + test_setup + end + + def test_abstract + obj = WS::Encoding::AbstractEncoding.new + assert_raises(NotImplementedError) do + obj.encode_rpc_call(nil, nil) + end + assert_raises(NotImplementedError) do + obj.decode_rpc_call(nil) + end + assert_raises(NotImplementedError) do + obj.encode_rpc_response(nil, nil) + end + assert_raises(NotImplementedError) do + obj.decode_rpc_response(nil) + end + end + + def encode_rpc_call(method_name, signature, params) + params = params.dup + (0..(signature.length-1)).each do |i| + type_binding = @marshaler.register_type(signature[i]) + info = WS::ParamInfo.create(signature[i], i, type_binding) + params[i] = @marshaler.marshal(WS::Param.new(params[i], info)) + end + @encoder.encode_rpc_call(method_name, params) + end + + def decode_rpc_call(obj) + @encoder.decode_rpc_call(obj) + end + + def encode_rpc_response(method_name, signature, param) + type_binding = @marshaler.register_type(signature[0]) + info = WS::ParamInfo.create(signature[0], 0, type_binding) + param = @marshaler.marshal(WS::Param.new(param, info)) + @encoder.encode_rpc_response(method_name, param) + end + + def decode_rpc_response(obj) + @encoder.decode_rpc_response(obj) + end +end diff --git a/actionwebservice/test/ws/abstract_unit.rb b/actionwebservice/test/ws/abstract_unit.rb new file mode 100644 index 0000000000..f5015bea69 --- /dev/null +++ b/actionwebservice/test/ws/abstract_unit.rb @@ -0,0 +1,14 @@ +$:.unshift(File.dirname(File.dirname(__FILE__)) + '/../lib') +$:.unshift(File.dirname(File.dirname(__FILE__)) + '/../lib/action_web_service/vendor') +puts $:.inspect +require 'test/unit' +require 'ws' +begin + require 'active_record' +rescue LoadError + begin + require 'rubygems' + require_gem 'activerecord', '>= 1.6.0' + rescue LoadError + end +end diff --git a/actionwebservice/test/ws/gencov b/actionwebservice/test/ws/gencov new file mode 100755 index 0000000000..144233107a --- /dev/null +++ b/actionwebservice/test/ws/gencov @@ -0,0 +1,3 @@ +#!/bin/sh + +rcov -x '.*_test\.rb,rubygems,abstract_,/run' ./run diff --git a/actionwebservice/test/ws/run b/actionwebservice/test/ws/run new file mode 100755 index 0000000000..5c6f8b2bc2 --- /dev/null +++ b/actionwebservice/test/ws/run @@ -0,0 +1,5 @@ +#!/usr/bin/env ruby + +Dir[File.join(File.dirname(__FILE__), '*_test.rb')].each do |f| + require f +end diff --git a/actionwebservice/test/ws/soap_marshaling_test.rb b/actionwebservice/test/ws/soap_marshaling_test.rb new file mode 100644 index 0000000000..ee6413478d --- /dev/null +++ b/actionwebservice/test/ws/soap_marshaling_test.rb @@ -0,0 +1,91 @@ +require File.dirname(__FILE__) + '/abstract_unit' + +module Nested + class MyClass + attr_accessor :id + attr_accessor :name + + def initialize(id, name) + @id = id + @name = name + end + + def ==(other) + @id == other.id && @name == other.name + end + end +end + +class SoapMarshalingTest < Test::Unit::TestCase + def setup + @marshaler = WS::Marshaling::SoapMarshaler.new + end + + def test_abstract + marshaler = WS::Marshaling::AbstractMarshaler.new + assert_raises(NotImplementedError) do + marshaler.marshal(nil) + end + assert_raises(NotImplementedError) do + marshaler.unmarshal(nil) + end + assert_equal(nil, marshaler.register_type(nil)) + end + + def test_marshaling + info = WS::ParamInfo.create(Nested::MyClass) + param = WS::Param.new(Nested::MyClass.new(2, "name"), info) + new_param = @marshaler.unmarshal(@marshaler.marshal(param)) + assert(param == new_param) + end + + def test_exception_marshaling + info = WS::ParamInfo.create(RuntimeError) + param = WS::Param.new(RuntimeError.new("hello, world"), info) + new_param = @marshaler.unmarshal(@marshaler.marshal(param)) + assert_equal("hello, world", new_param.value.detail.cause.message) + end + + def test_registration + type_binding1 = @marshaler.register_type(:int) + type_binding2 = @marshaler.register_type(:int) + assert(type_binding1.equal?(type_binding2)) + end + + def test_active_record + if Object.const_defined?('ActiveRecord') + node_class = Class.new(ActiveRecord::Base) do + def initialize(*args) + super(*args) + @new_record = false + end + + class << self + def name + "Node" + end + + def columns(*args) + [ + ActiveRecord::ConnectionAdapters::Column.new('id', 0, 'int'), + ActiveRecord::ConnectionAdapters::Column.new('name', nil, 'string'), + ActiveRecord::ConnectionAdapters::Column.new('email', nil, 'string'), + ] + end + + def connection + self + end + end + end + info = WS::ParamInfo.create(node_class, 0, @marshaler.register_type(node_class)) + ar_obj = node_class.new('name' => 'hello', 'email' => 'test@test.com') + param = WS::Param.new(ar_obj, info) + obj = @marshaler.marshal(param) + param = @marshaler.unmarshal(obj) + new_ar_obj = param.value + assert_equal(ar_obj, new_ar_obj) + assert(!ar_obj.equal?(new_ar_obj)) + end + end +end diff --git a/actionwebservice/test/ws/soap_rpc_encoding_test.rb b/actionwebservice/test/ws/soap_rpc_encoding_test.rb new file mode 100644 index 0000000000..e5dcbfeb1b --- /dev/null +++ b/actionwebservice/test/ws/soap_rpc_encoding_test.rb @@ -0,0 +1,47 @@ +require File.dirname(__FILE__) + '/abstract_encoding' +require 'time' + +class SoapRpcEncodingTest < Test::Unit::TestCase + include EncodingTest + + def test_setup + @encoder = WS::Encoding::SoapRpcEncoding.new + @marshaler = WS::Marshaling::SoapMarshaler.new + end + + def test_call_encoding_and_decoding + obj = encode_rpc_call('DecodeMe', @call_signature, @call_params) + method_name, decoded_params = decode_rpc_call(obj) + params = decoded_params.map{|x| @marshaler.unmarshal(x).value} + assert_equal(method_name, 'DecodeMe') + assert_equal(@call_params[0..3], params[0..3]) + # XXX: DateTime not marshaled correctly yet + assert_equal(@call_params[5..-1], params[5..-1]) + end + + def test_response_encoding_and_decoding_simple + obj = encode_rpc_response('DecodeMe', @response_signature, @response_param) + method_name, return_value = decode_rpc_response(obj) + return_value = @marshaler.unmarshal(return_value).value + assert_equal('DecodeMe', method_name) + assert_equal(@response_param, return_value) + end + + def test_response_encoding_and_decoding_struct + struct = Nested::StructClass.new + obj = encode_rpc_response('DecodeMe', [Nested::StructClass], struct) + method_name, return_value = decode_rpc_response(obj) + return_value = @marshaler.unmarshal(return_value).value + assert_equal('DecodeMe', method_name) + assert_equal(struct, return_value) + end + + def test_response_encoding_and_decoding_array + struct = Nested::StructClass.new + obj = encode_rpc_response('DecodeMe', [[Nested::StructClass]], [struct]) + method_name, return_value = decode_rpc_response(obj) + return_value = @marshaler.unmarshal(return_value).value + assert_equal('DecodeMe', method_name) + assert_equal([struct], return_value) + end +end diff --git a/actionwebservice/test/ws/types_test.rb b/actionwebservice/test/ws/types_test.rb new file mode 100644 index 0000000000..649fdbad7c --- /dev/null +++ b/actionwebservice/test/ws/types_test.rb @@ -0,0 +1,41 @@ +require File.dirname(__FILE__) + '/abstract_unit' + +class TypesTest < Test::Unit::TestCase + include WS + + def setup + @caster = BaseTypeCaster.new + end + + def test_base_types + assert_equal(:int, BaseTypes.canonical_type_name(:integer)) + assert_equal(:int, BaseTypes.canonical_type_name(:fixnum)) + assert_equal(Integer, BaseTypes.type_name_to_class(:bignum)) + assert_equal(Date, BaseTypes.type_name_to_class(:date)) + assert_equal(Time, BaseTypes.type_name_to_class(:timestamp)) + assert_equal(TrueClass, BaseTypes.type_name_to_class(:bool)) + assert_equal(:int, BaseTypes.class_to_type_name(Bignum)) + assert_equal(:bool, BaseTypes.class_to_type_name(FalseClass)) + assert_equal(Integer, BaseTypes.canonical_type_class(Fixnum)) + assert_raises(TypeError) do + BaseTypes.canonical_type_name(:fake) + end + end + + def test_casting + assert_equal(5, @caster.cast("5", Fixnum)) + assert_equal('50.0', @caster.cast(50.0, String)) + assert_equal(true, @caster.cast('true', FalseClass)) + assert_equal(false, @caster.cast('false', TrueClass)) + assert_raises(TypeError) do + @caster.cast('yes', FalseClass) + end + assert_equal(3.14159, @caster.cast('3.14159', Float)) + now1 = Time.new + now2 = @caster.cast("#{now1}", Time) + assert_equal(now1.tv_sec, now2.tv_sec) + date1 = Date.parse('2004-01-01') + date2 = @caster.cast("#{date1}", Date) + assert_equal(date1, date2) + end +end diff --git a/actionwebservice/test/ws/xmlrpc_encoding_test.rb b/actionwebservice/test/ws/xmlrpc_encoding_test.rb new file mode 100644 index 0000000000..45aaa901bd --- /dev/null +++ b/actionwebservice/test/ws/xmlrpc_encoding_test.rb @@ -0,0 +1,34 @@ +require File.dirname(__FILE__) + '/abstract_encoding' +require 'time' + +class XmlRpcEncodingTest < Test::Unit::TestCase + include EncodingTest + + def test_setup + @encoder = WS::Encoding::XmlRpcEncoding.new + @marshaler = WS::Marshaling::XmlRpcMarshaler.new + end + + def test_typed_call_encoding_and_decoding + obj = encode_rpc_call('DecodeMe', @call_signature, @call_params) + method_name, params = decode_rpc_call(obj) + (0..(@call_signature.length-1)).each do |i| + params[i] = @marshaler.typed_unmarshal(params[i], @call_signature[i]).value + end + assert_equal(method_name, 'DecodeMe') + assert_equal(@call_params[0..3], params[0..3]) + assert_equal(@call_params[5..-1], params[5..-1]) + end + + def test_untyped_call_encoding_and_decoding + obj = encode_rpc_call('DecodeMe', @call_signature, @call_params) + method_name, params = decode_rpc_call(obj) + (0..(@call_signature.length-1)).each do |i| + params[i] = @marshaler.unmarshal(params[i]).value + end + assert_equal(method_name, 'DecodeMe') + assert_equal(@call_params[0..3], params[0..3]) + assert_equal(@call_params[5].name, params[5]['name']) + assert_equal(@call_params[5].version, params[5]['version']) + end +end |