hessian2 2.0.5 → 2.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +10 -10
- data/README.md +203 -199
- data/lib/hessian2.rb +14 -14
- data/lib/hessian2/client.rb +57 -57
- data/lib/hessian2/constants.rb +164 -164
- data/lib/hessian2/fault.rb +3 -3
- data/lib/hessian2/handler.rb +18 -18
- data/lib/hessian2/hessian_client.rb +3 -3
- data/lib/hessian2/parser.rb +619 -619
- data/lib/hessian2/type_wrapper.rb +49 -49
- data/lib/hessian2/version.rb +3 -3
- data/lib/hessian2/writer.rb +506 -504
- data/spec/binary_spec.rb +51 -51
- data/spec/boolean_spec.rb +26 -26
- data/spec/class_wrapper_spec.rb +52 -52
- data/spec/create_monkeys.rb +14 -14
- data/spec/date_spec.rb +45 -45
- data/spec/double_spec.rb +78 -78
- data/spec/int_spec.rb +54 -54
- data/spec/list_spec.rb +66 -66
- data/spec/long_spec.rb +68 -68
- data/spec/map_spec.rb +36 -36
- data/spec/null_spec.rb +17 -17
- data/spec/object_spec.rb +78 -78
- data/spec/ref_spec.rb +43 -43
- data/spec/spec_helper.rb +23 -23
- data/spec/string_spec.rb +61 -61
- data/spec/struct_wrapper_spec.rb +47 -47
- data/spec/type_wrapper_spec.rb +102 -102
- data/test/app.rb +24 -24
- data/test/async/em_http_asleep.rb +25 -25
- data/test/async/em_http_sleep.rb +25 -25
- data/test/async/monkey.asleep.rb +27 -27
- data/test/async/mysql2_aquery.rb +37 -37
- data/test/fault/monkey.undefined_method.rb +5 -5
- data/test/fault/monkey.wrong_arguments.rb +5 -5
- data/test/fiber_concurrency/em_http_asleep.rb +17 -17
- data/test/fiber_concurrency/em_http_sleep.rb +17 -17
- data/test/fiber_concurrency/monkey.asleep.fiber_aware.rb +18 -18
- data/test/fiber_concurrency/mysql2_query.rb +29 -29
- data/test/fiber_concurrency/net_http_asleep.rb +19 -19
- data/test/fiber_concurrency/net_http_sleep.rb +19 -19
- data/test/fibered_rainbows/Gemfile +15 -15
- data/test/fibered_rainbows/config.ru +11 -11
- data/test/fibered_rainbows/rainbows.rb +13 -13
- data/test/monkey_service.rb +16 -16
- data/test/prepare.rb +7 -7
- data/test/thread_concurrency/active_record_execute.rb +29 -29
- data/test/thread_concurrency/monkey.asleep.rb +22 -22
- data/test/thread_concurrency/net_http_asleep.rb +24 -24
- data/test/thread_concurrency/net_http_sleep.rb +24 -24
- data/test/threaded_rainbows/Gemfile +13 -13
- data/test/threaded_rainbows/config.ru +9 -9
- data/test/threaded_rainbows/rainbows.rb +13 -13
- metadata +46 -4
data/spec/null_spec.rb
CHANGED
@@ -1,17 +1,17 @@
|
|
1
|
-
require File.expand_path('../spec_helper', __FILE__)
|
2
|
-
|
3
|
-
module Hessian2
|
4
|
-
describe Writer do
|
5
|
-
context "when null" do
|
6
|
-
|
7
|
-
it "should write null ('N') ::= 'N'" do
|
8
|
-
val = nil
|
9
|
-
bin = Hessian2.write(val)
|
10
|
-
|
11
|
-
expect(bin).to eq('N')
|
12
|
-
expect(Hessian2.parse(bin)).to eq(val)
|
13
|
-
end
|
14
|
-
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
1
|
+
require File.expand_path('../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
module Hessian2
|
4
|
+
describe Writer do
|
5
|
+
context "when null" do
|
6
|
+
|
7
|
+
it "should write null ('N') ::= 'N'" do
|
8
|
+
val = nil
|
9
|
+
bin = Hessian2.write(val)
|
10
|
+
|
11
|
+
expect(bin).to eq('N')
|
12
|
+
expect(Hessian2.parse(bin)).to eq(val)
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/spec/object_spec.rb
CHANGED
@@ -1,78 +1,78 @@
|
|
1
|
-
require File.expand_path('../spec_helper', __FILE__)
|
2
|
-
|
3
|
-
module Hessian2
|
4
|
-
describe Writer do
|
5
|
-
context "when object" do
|
6
|
-
hash = { id: nil, born_at: Time.new(2009, 5, 8), name: '大鸡', price: 99.99 }
|
7
|
-
|
8
|
-
it "should write object type definition ('C') ::= 'C' string int string* and object with direct type ::= [x60-x6f] value*" do
|
9
|
-
[ 'Monkey', 'AnotherMonkey' ].each do |klass|
|
10
|
-
bin = Hessian2.write(Kernel.const_get(klass).new(hash))
|
11
|
-
|
12
|
-
bytes = bin.each_byte
|
13
|
-
expect([ bytes.next ].pack('C')).to eq('C')
|
14
|
-
expect(Hessian2.parse_string(bytes)).to eq(klass)
|
15
|
-
expect(Hessian2.parse_int(bytes)).to eq(4)
|
16
|
-
4.times{ Hessian2.parse_string(bytes) }
|
17
|
-
expect(bytes.next - 0x60).to eq(0)
|
18
|
-
_monkey = Hessian2.parse(bin)
|
19
|
-
expect([ _monkey.born_at, _monkey.name, _monkey.price ]).to eq([ hash[:born_at], hash[:name], hash[:price] ])
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
|
24
|
-
it "should write object instance ('O') ::= 'O' int value*" do
|
25
|
-
arr = []
|
26
|
-
17.times do |i|
|
27
|
-
arr << Hessian2::ClassWrapper.new("com.sun.java.Monkey#{i}", hash)
|
28
|
-
end
|
29
|
-
bin = Hessian2.write(arr)
|
30
|
-
|
31
|
-
bytes = bin.each_byte
|
32
|
-
|
33
|
-
# skip x58 int
|
34
|
-
bytes.next
|
35
|
-
Hessian2.parse_int(bytes)
|
36
|
-
|
37
|
-
# skip top 16
|
38
|
-
16.times do
|
39
|
-
bytes.next
|
40
|
-
Hessian2.parse_string(bytes)
|
41
|
-
Hessian2.parse_int(bytes)
|
42
|
-
4.times{ Hessian2.parse_string(bytes) }
|
43
|
-
bytes.next
|
44
|
-
4.times{ Hessian2.parse_bytes(bytes) }
|
45
|
-
end
|
46
|
-
|
47
|
-
# skip 17th class definition
|
48
|
-
bytes.next
|
49
|
-
Hessian2.parse_string(bytes)
|
50
|
-
Hessian2.parse_int(bytes)
|
51
|
-
4.times{ Hessian2.parse_string(bytes) }
|
52
|
-
|
53
|
-
expect([ bytes.next ].pack('C')).to eq('O')
|
54
|
-
expect(Hessian2.parse_int(bytes)).to eq(16)
|
55
|
-
|
56
|
-
_monkeys = Hessian2.parse(bin)
|
57
|
-
expect(_monkeys.size).to eq(17)
|
58
|
-
_monkeys.each do |_monkey|
|
59
|
-
expect([ _monkey.born_at, _monkey.name, _monkey.price ]).to eq([ hash[:born_at], hash[:name], hash[:price] ])
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
it "should write ActiveRecord::Relation" do
|
64
|
-
val = Monkey.where(true).limit(2)
|
65
|
-
bin = Hessian2.write(val)
|
66
|
-
|
67
|
-
bytes = bin.each_byte
|
68
|
-
expect(bytes.next - 0x78).to eq(val.size)
|
69
|
-
|
70
|
-
born_at = Time.new(1989, 5, 8)
|
71
|
-
Hessian2.parse(bin).each_with_index do |_monkey, i|
|
72
|
-
expect([ _monkey.id, _monkey.name, _monkey.price, _monkey.born_at ]).to eq([ i + 1, "#{i}号猴", 0.25 * i, born_at + 86400 * i ])
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
1
|
+
require File.expand_path('../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
module Hessian2
|
4
|
+
describe Writer do
|
5
|
+
context "when object" do
|
6
|
+
hash = { id: nil, born_at: Time.new(2009, 5, 8), name: '大鸡', price: 99.99 }
|
7
|
+
|
8
|
+
it "should write object type definition ('C') ::= 'C' string int string* and object with direct type ::= [x60-x6f] value*" do
|
9
|
+
[ 'Monkey', 'AnotherMonkey' ].each do |klass|
|
10
|
+
bin = Hessian2.write(Kernel.const_get(klass).new(hash))
|
11
|
+
|
12
|
+
bytes = bin.each_byte
|
13
|
+
expect([ bytes.next ].pack('C')).to eq('C')
|
14
|
+
expect(Hessian2.parse_string(bytes)).to eq(klass)
|
15
|
+
expect(Hessian2.parse_int(bytes)).to eq(4)
|
16
|
+
4.times{ Hessian2.parse_string(bytes) }
|
17
|
+
expect(bytes.next - 0x60).to eq(0)
|
18
|
+
_monkey = Hessian2.parse(bin)
|
19
|
+
expect([ _monkey.born_at, _monkey.name, _monkey.price ]).to eq([ hash[:born_at], hash[:name], hash[:price] ])
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
it "should write object instance ('O') ::= 'O' int value*" do
|
25
|
+
arr = []
|
26
|
+
17.times do |i|
|
27
|
+
arr << Hessian2::ClassWrapper.new("com.sun.java.Monkey#{i}", hash)
|
28
|
+
end
|
29
|
+
bin = Hessian2.write(arr)
|
30
|
+
|
31
|
+
bytes = bin.each_byte
|
32
|
+
|
33
|
+
# skip x58 int
|
34
|
+
bytes.next
|
35
|
+
Hessian2.parse_int(bytes)
|
36
|
+
|
37
|
+
# skip top 16
|
38
|
+
16.times do
|
39
|
+
bytes.next
|
40
|
+
Hessian2.parse_string(bytes)
|
41
|
+
Hessian2.parse_int(bytes)
|
42
|
+
4.times{ Hessian2.parse_string(bytes) }
|
43
|
+
bytes.next
|
44
|
+
4.times{ Hessian2.parse_bytes(bytes) }
|
45
|
+
end
|
46
|
+
|
47
|
+
# skip 17th class definition
|
48
|
+
bytes.next
|
49
|
+
Hessian2.parse_string(bytes)
|
50
|
+
Hessian2.parse_int(bytes)
|
51
|
+
4.times{ Hessian2.parse_string(bytes) }
|
52
|
+
|
53
|
+
expect([ bytes.next ].pack('C')).to eq('O')
|
54
|
+
expect(Hessian2.parse_int(bytes)).to eq(16)
|
55
|
+
|
56
|
+
_monkeys = Hessian2.parse(bin)
|
57
|
+
expect(_monkeys.size).to eq(17)
|
58
|
+
_monkeys.each do |_monkey|
|
59
|
+
expect([ _monkey.born_at, _monkey.name, _monkey.price ]).to eq([ hash[:born_at], hash[:name], hash[:price] ])
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should write ActiveRecord::Relation" do
|
64
|
+
val = Monkey.where(true).limit(2)
|
65
|
+
bin = Hessian2.write(val)
|
66
|
+
|
67
|
+
bytes = bin.each_byte
|
68
|
+
expect(bytes.next - 0x78).to eq(val.size)
|
69
|
+
|
70
|
+
born_at = Time.new(1989, 5, 8)
|
71
|
+
Hessian2.parse(bin).each_with_index do |_monkey, i|
|
72
|
+
expect([ _monkey.id, _monkey.name, _monkey.price, _monkey.born_at ]).to eq([ i + 1, "#{i}号猴", 0.25 * i, born_at + 86400 * i ])
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
data/spec/ref_spec.rb
CHANGED
@@ -1,43 +1,43 @@
|
|
1
|
-
require File.expand_path('../spec_helper', __FILE__)
|
2
|
-
|
3
|
-
module Hessian2
|
4
|
-
describe Writer do
|
5
|
-
context "when ref" do
|
6
|
-
hash1 = { id: nil, born_at: Time.new(2009, 5, 8), name: '大鸡', price: 99.99 }
|
7
|
-
|
8
|
-
it "should write reference to map/list/object - integer ('Q') ::= x51 int" do
|
9
|
-
monkey1 = Monkey.new(hash1)
|
10
|
-
monkey2 = Hessian2::ClassWrapper.new("com.sun.java.Monkey", hash1)
|
11
|
-
monkey3 = Hessian2::TypeWrapper.new("com.sun.java.Monkey", hash1)
|
12
|
-
monkeys1 = [ monkey1, monkey2, monkey3 ]
|
13
|
-
monkeys2 = Hessian2::ClassWrapper.new("[com.sun.java.Monkey", monkeys1)
|
14
|
-
monkeys3 = Hessian2::TypeWrapper.new("[com.sun.java.Monkey", monkeys1)
|
15
|
-
|
16
|
-
arr = [ hash1, monkey1, monkey2, monkey3, monkeys1, monkeys2, monkeys3 ] * 2
|
17
|
-
|
18
|
-
bin = Hessian2.write(arr)
|
19
|
-
|
20
|
-
_hash1, _monkey1, _monkey2, _monkey3, _monkeys1, _monkeys2, _monkeys3, \
|
21
|
-
_hash1r, _monkey1r, _monkey2r, _monkey3r, _monkeys1r, _monkeys2r, _monkeys3r = Hessian2.parse(bin, nil, symbolize_keys: true)
|
22
|
-
|
23
|
-
[ _hash1, _hash1r, _monkey3, _monkey3r ].each do |_hash|
|
24
|
-
expect([ _hash[:born_at], _hash[:name], _hash[:price] ]).to eq([ hash1[:born_at], hash1[:name], hash1[:price] ])
|
25
|
-
end
|
26
|
-
|
27
|
-
[ _monkey1, _monkey2, _monkey1r, _monkey2r ].each do |_monkey|
|
28
|
-
expect([ _monkey.born_at, _monkey.name, _monkey.price ]).to eq([ hash1[:born_at], hash1[:name], hash1[:price] ])
|
29
|
-
end
|
30
|
-
|
31
|
-
[ _monkeys1, _monkeys2, _monkeys3, _monkeys1r, _monkeys2r, _monkeys3r ].each do |_monkeys|
|
32
|
-
[ _monkeys[0], _monkeys[1] ].each do |_monkey|
|
33
|
-
expect([ _monkey.born_at, _monkey.name, _monkey.price ]).to eq([ hash1[:born_at], hash1[:name], hash1[:price] ])
|
34
|
-
end
|
35
|
-
last = _monkeys.last
|
36
|
-
expect([ last[:born_at], last[:name], last[:price] ]).to eq([ hash1[:born_at], hash1[:name], hash1[:price] ])
|
37
|
-
end
|
38
|
-
|
39
|
-
end
|
40
|
-
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
1
|
+
require File.expand_path('../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
module Hessian2
|
4
|
+
describe Writer do
|
5
|
+
context "when ref" do
|
6
|
+
hash1 = { id: nil, born_at: Time.new(2009, 5, 8), name: '大鸡', price: 99.99 }
|
7
|
+
|
8
|
+
it "should write reference to map/list/object - integer ('Q') ::= x51 int" do
|
9
|
+
monkey1 = Monkey.new(hash1)
|
10
|
+
monkey2 = Hessian2::ClassWrapper.new("com.sun.java.Monkey", hash1)
|
11
|
+
monkey3 = Hessian2::TypeWrapper.new("com.sun.java.Monkey", hash1)
|
12
|
+
monkeys1 = [ monkey1, monkey2, monkey3 ]
|
13
|
+
monkeys2 = Hessian2::ClassWrapper.new("[com.sun.java.Monkey", monkeys1)
|
14
|
+
monkeys3 = Hessian2::TypeWrapper.new("[com.sun.java.Monkey", monkeys1)
|
15
|
+
|
16
|
+
arr = [ hash1, monkey1, monkey2, monkey3, monkeys1, monkeys2, monkeys3 ] * 2
|
17
|
+
|
18
|
+
bin = Hessian2.write(arr)
|
19
|
+
|
20
|
+
_hash1, _monkey1, _monkey2, _monkey3, _monkeys1, _monkeys2, _monkeys3, \
|
21
|
+
_hash1r, _monkey1r, _monkey2r, _monkey3r, _monkeys1r, _monkeys2r, _monkeys3r = Hessian2.parse(bin, nil, symbolize_keys: true)
|
22
|
+
|
23
|
+
[ _hash1, _hash1r, _monkey3, _monkey3r ].each do |_hash|
|
24
|
+
expect([ _hash[:born_at], _hash[:name], _hash[:price] ]).to eq([ hash1[:born_at], hash1[:name], hash1[:price] ])
|
25
|
+
end
|
26
|
+
|
27
|
+
[ _monkey1, _monkey2, _monkey1r, _monkey2r ].each do |_monkey|
|
28
|
+
expect([ _monkey.born_at, _monkey.name, _monkey.price ]).to eq([ hash1[:born_at], hash1[:name], hash1[:price] ])
|
29
|
+
end
|
30
|
+
|
31
|
+
[ _monkeys1, _monkeys2, _monkeys3, _monkeys1r, _monkeys2r, _monkeys3r ].each do |_monkeys|
|
32
|
+
[ _monkeys[0], _monkeys[1] ].each do |_monkey|
|
33
|
+
expect([ _monkey.born_at, _monkey.name, _monkey.price ]).to eq([ hash1[:born_at], hash1[:name], hash1[:price] ])
|
34
|
+
end
|
35
|
+
last = _monkeys.last
|
36
|
+
expect([ last[:born_at], last[:name], last[:price] ]).to eq([ hash1[:born_at], hash1[:name], hash1[:price] ])
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,23 +1,23 @@
|
|
1
|
-
lib_path = File.expand_path('../../lib', __FILE__)
|
2
|
-
$:.unshift(lib_path)
|
3
|
-
|
4
|
-
require 'hessian2'
|
5
|
-
|
6
|
-
require 'active_record'
|
7
|
-
require 'mysql2'
|
8
|
-
|
9
|
-
MonkeyStruct = Struct.new(:born_at, :id, :name, :price)
|
10
|
-
|
11
|
-
options = YAML.load_file(File.expand_path('../database.yml', __FILE__))
|
12
|
-
ActiveRecord::Base.establish_connection(options)
|
13
|
-
ActiveRecord::Base.default_timezone = :local
|
14
|
-
|
15
|
-
class Monkey < ActiveRecord::Base; end
|
16
|
-
|
17
|
-
class AnotherMonkey
|
18
|
-
attr_accessor :born_at, :id, :name, :price
|
19
|
-
|
20
|
-
def initialize(attrs = {})
|
21
|
-
attrs.each { |k, v| send("#{k}=", v) if respond_to?("#{k}=") }
|
22
|
-
end
|
23
|
-
end
|
1
|
+
lib_path = File.expand_path('../../lib', __FILE__)
|
2
|
+
$:.unshift(lib_path)
|
3
|
+
|
4
|
+
require 'hessian2'
|
5
|
+
|
6
|
+
require 'active_record'
|
7
|
+
require 'mysql2'
|
8
|
+
|
9
|
+
MonkeyStruct = Struct.new(:born_at, :id, :name, :price)
|
10
|
+
|
11
|
+
options = YAML.load_file(File.expand_path('../database.yml', __FILE__))
|
12
|
+
ActiveRecord::Base.establish_connection(options)
|
13
|
+
ActiveRecord::Base.default_timezone = :local
|
14
|
+
|
15
|
+
class Monkey < ActiveRecord::Base; end
|
16
|
+
|
17
|
+
class AnotherMonkey
|
18
|
+
attr_accessor :born_at, :id, :name, :price
|
19
|
+
|
20
|
+
def initialize(attrs = {})
|
21
|
+
attrs.each { |k, v| send("#{k}=", v) if respond_to?("#{k}=") }
|
22
|
+
end
|
23
|
+
end
|
data/spec/string_spec.rb
CHANGED
@@ -1,61 +1,61 @@
|
|
1
|
-
require File.expand_path('../spec_helper', __FILE__)
|
2
|
-
|
3
|
-
module Hessian2
|
4
|
-
describe Writer do
|
5
|
-
context "when string" do
|
6
|
-
|
7
|
-
it "should write utf-8 string length 0-31 ::= [x00-x1f] <utf8-data>" do
|
8
|
-
(0..31).each do |len|
|
9
|
-
val = '啦' * len
|
10
|
-
bin = Hessian2.write(val)
|
11
|
-
|
12
|
-
expect(bin[0].unpack('C').first).to eq(val.size)
|
13
|
-
expect(bin[1..-1]).to eq(val.b)
|
14
|
-
expect(Hessian2.parse(bin)).to eq(val)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
|
19
|
-
it "should write utf-8 string length 0-1023 ::= [x30-x33] b0 <utf8-data>" do
|
20
|
-
[ 33, 256, 512, 1023 ].each do |len|
|
21
|
-
val = '啦' * len
|
22
|
-
bin = Hessian2.write(val)
|
23
|
-
|
24
|
-
b1, b0 = bin[0, 2].unpack('CC')
|
25
|
-
expect(256 * (b1 - 0x30) + b0).to eq(val.size)
|
26
|
-
expect(bin[2..-1]).to eq(val.b)
|
27
|
-
expect(Hessian2.parse(bin)).to eq(val)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
it "should write utf-8 string final chunk ('S') ::= S b1 b0 <utf8-data>" do
|
32
|
-
val = '啦' * 0x400
|
33
|
-
bin = Hessian2.write(val)
|
34
|
-
|
35
|
-
expect(bin[0]).to eq('S')
|
36
|
-
expect(bin[1, 2].unpack('n').first).to eq(0x400)
|
37
|
-
expect(Hessian2.parse(bin)).to eq(val)
|
38
|
-
end
|
39
|
-
|
40
|
-
|
41
|
-
it "should write utf-8 string non-final chunk ('R') ::= x52 b1 b0 <utf8-data>" do
|
42
|
-
val = '啦' * 0x10400
|
43
|
-
bin = Hessian2.write(val)
|
44
|
-
|
45
|
-
chunks = val.size / 0x8000
|
46
|
-
i = 0
|
47
|
-
|
48
|
-
chunks.times do |c|
|
49
|
-
expect(bin[i]).to eq('R')
|
50
|
-
expect(bin[i + 1, 2].unpack('n').first).to eq(0x8000)
|
51
|
-
i += (3 + val[c * 0x8000, 0x8000].bytesize)
|
52
|
-
end
|
53
|
-
|
54
|
-
expect(bin[i]).to eq('S')
|
55
|
-
expect(bin[i + 1, 2].unpack('n').first).to eq(val.size - 0x8000 * chunks)
|
56
|
-
expect(Hessian2.parse(bin)).to eq(val)
|
57
|
-
end
|
58
|
-
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
1
|
+
require File.expand_path('../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
module Hessian2
|
4
|
+
describe Writer do
|
5
|
+
context "when string" do
|
6
|
+
|
7
|
+
it "should write utf-8 string length 0-31 ::= [x00-x1f] <utf8-data>" do
|
8
|
+
(0..31).each do |len|
|
9
|
+
val = '啦' * len
|
10
|
+
bin = Hessian2.write(val)
|
11
|
+
|
12
|
+
expect(bin[0].unpack('C').first).to eq(val.size)
|
13
|
+
expect(bin[1..-1]).to eq(val.b)
|
14
|
+
expect(Hessian2.parse(bin)).to eq(val)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
it "should write utf-8 string length 0-1023 ::= [x30-x33] b0 <utf8-data>" do
|
20
|
+
[ 33, 256, 512, 1023 ].each do |len|
|
21
|
+
val = '啦' * len
|
22
|
+
bin = Hessian2.write(val)
|
23
|
+
|
24
|
+
b1, b0 = bin[0, 2].unpack('CC')
|
25
|
+
expect(256 * (b1 - 0x30) + b0).to eq(val.size)
|
26
|
+
expect(bin[2..-1]).to eq(val.b)
|
27
|
+
expect(Hessian2.parse(bin)).to eq(val)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should write utf-8 string final chunk ('S') ::= S b1 b0 <utf8-data>" do
|
32
|
+
val = '啦' * 0x400
|
33
|
+
bin = Hessian2.write(val)
|
34
|
+
|
35
|
+
expect(bin[0]).to eq('S')
|
36
|
+
expect(bin[1, 2].unpack('n').first).to eq(0x400)
|
37
|
+
expect(Hessian2.parse(bin)).to eq(val)
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
it "should write utf-8 string non-final chunk ('R') ::= x52 b1 b0 <utf8-data>" do
|
42
|
+
val = '啦' * 0x10400
|
43
|
+
bin = Hessian2.write(val)
|
44
|
+
|
45
|
+
chunks = val.size / 0x8000
|
46
|
+
i = 0
|
47
|
+
|
48
|
+
chunks.times do |c|
|
49
|
+
expect(bin[i]).to eq('R')
|
50
|
+
expect(bin[i + 1, 2].unpack('n').first).to eq(0x8000)
|
51
|
+
i += (3 + val[c * 0x8000, 0x8000].bytesize)
|
52
|
+
end
|
53
|
+
|
54
|
+
expect(bin[i]).to eq('S')
|
55
|
+
expect(bin[i + 1, 2].unpack('n').first).to eq(val.size - 0x8000 * chunks)
|
56
|
+
expect(Hessian2.parse(bin)).to eq(val)
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/spec/struct_wrapper_spec.rb
CHANGED
@@ -1,47 +1,47 @@
|
|
1
|
-
require File.expand_path('../spec_helper', __FILE__)
|
2
|
-
|
3
|
-
module Hessian2
|
4
|
-
describe StructWrapper do
|
5
|
-
hash = { id: nil, born_at: Time.new(2009, 5, 8), name: '大鸡', price: 99.99 }
|
6
|
-
|
7
|
-
# it "should raise error" do
|
8
|
-
# expect(lambda{ Hessian2::StructWrapper.new(MonkeyStruct) }).to raise_error
|
9
|
-
# end
|
10
|
-
|
11
|
-
|
12
|
-
# it "should wrap nil" do
|
13
|
-
# bin = Hessian2.write(Hessian2::StructWrapper.new(MonkeyStruct, nil))
|
14
|
-
|
15
|
-
# _monkey = Hessian2.parse(bin, MonkeyStruct)
|
16
|
-
# expect(_monkey).to eq(nil)
|
17
|
-
# end
|
18
|
-
|
19
|
-
|
20
|
-
# it "should wrap hash, monkey, another monkey" do
|
21
|
-
# [ MonkeyStruct, 'MonkeyStruct' ].each do |klass|
|
22
|
-
# [ hash, Monkey.new(hash), AnotherMonkey.new(hash) ].each do |val|
|
23
|
-
# bin = Hessian2.write(Hessian2::StructWrapper.new(klass, val))
|
24
|
-
|
25
|
-
# _monkey = Hessian2.parse(bin, klass)
|
26
|
-
# expect([ _monkey.born_at, _monkey.name, _monkey.price ]).to eq([ hash[:born_at], hash[:name], hash[:price] ])
|
27
|
-
# end
|
28
|
-
# end
|
29
|
-
# end
|
30
|
-
|
31
|
-
|
32
|
-
it "should wrap array" do
|
33
|
-
[ [MonkeyStruct], '[MonkeyStruct' ].each do |klass|
|
34
|
-
arr = [ nil, hash, Monkey.new(hash), AnotherMonkey.new(hash) ]
|
35
|
-
bin = Hessian2.write(Hessian2::StructWrapper.new(klass, arr))
|
36
|
-
|
37
|
-
_monkey1, _monkey2, _monkey3, _monkey4 = Hessian2.parse(bin, klass, symbolize_keys: true)
|
38
|
-
expect(_monkey1).to eq(nil)
|
39
|
-
expect([ _monkey2[:born_at], _monkey2[:name], _monkey2[:price] ]).to eq([ hash[:born_at], hash[:name], hash[:price] ])
|
40
|
-
[ _monkey3, _monkey4 ].each do |_monkey|
|
41
|
-
expect([ _monkey.born_at, _monkey.name, _monkey.price ]).to eq([ hash[:born_at], hash[:name], hash[:price] ])
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
end
|
47
|
-
end
|
1
|
+
require File.expand_path('../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
module Hessian2
|
4
|
+
describe StructWrapper do
|
5
|
+
hash = { id: nil, born_at: Time.new(2009, 5, 8), name: '大鸡', price: 99.99 }
|
6
|
+
|
7
|
+
# it "should raise error" do
|
8
|
+
# expect(lambda{ Hessian2::StructWrapper.new(MonkeyStruct) }).to raise_error
|
9
|
+
# end
|
10
|
+
|
11
|
+
|
12
|
+
# it "should wrap nil" do
|
13
|
+
# bin = Hessian2.write(Hessian2::StructWrapper.new(MonkeyStruct, nil))
|
14
|
+
|
15
|
+
# _monkey = Hessian2.parse(bin, MonkeyStruct)
|
16
|
+
# expect(_monkey).to eq(nil)
|
17
|
+
# end
|
18
|
+
|
19
|
+
|
20
|
+
# it "should wrap hash, monkey, another monkey" do
|
21
|
+
# [ MonkeyStruct, 'MonkeyStruct' ].each do |klass|
|
22
|
+
# [ hash, Monkey.new(hash), AnotherMonkey.new(hash) ].each do |val|
|
23
|
+
# bin = Hessian2.write(Hessian2::StructWrapper.new(klass, val))
|
24
|
+
|
25
|
+
# _monkey = Hessian2.parse(bin, klass)
|
26
|
+
# expect([ _monkey.born_at, _monkey.name, _monkey.price ]).to eq([ hash[:born_at], hash[:name], hash[:price] ])
|
27
|
+
# end
|
28
|
+
# end
|
29
|
+
# end
|
30
|
+
|
31
|
+
|
32
|
+
it "should wrap array" do
|
33
|
+
[ [MonkeyStruct], '[MonkeyStruct' ].each do |klass|
|
34
|
+
arr = [ nil, hash, Monkey.new(hash), AnotherMonkey.new(hash) ]
|
35
|
+
bin = Hessian2.write(Hessian2::StructWrapper.new(klass, arr))
|
36
|
+
|
37
|
+
_monkey1, _monkey2, _monkey3, _monkey4 = Hessian2.parse(bin, klass, symbolize_keys: true)
|
38
|
+
expect(_monkey1).to eq(nil)
|
39
|
+
expect([ _monkey2[:born_at], _monkey2[:name], _monkey2[:price] ]).to eq([ hash[:born_at], hash[:name], hash[:price] ])
|
40
|
+
[ _monkey3, _monkey4 ].each do |_monkey|
|
41
|
+
expect([ _monkey.born_at, _monkey.name, _monkey.price ]).to eq([ hash[:born_at], hash[:name], hash[:price] ])
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|