forked from ably/ably-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_spec.rb
159 lines (120 loc) · 4.15 KB
/
json_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
require 'spec_helper'
require 'json'
require 'ably/models/message_encoders/json'
describe Ably::Models::MessageEncoders::Json do
let(:hash_data) { { 'key' => 'value', 'key2' => 123 } }
let(:hash_string_data) { JSON.dump(hash_data) }
let(:array_data) { ['value', 123] }
let(:array_string_data) { JSON.dump(array_data) }
let(:client) { instance_double('Ably::Realtime::Client') }
subject { Ably::Models::MessageEncoders::Json.new(client) }
context '#decode' do
before do
subject.decode message, {}
end
context 'message with json payload' do
let(:message) { { data: hash_string_data, encoding: 'json' } }
it 'decodes json' do
expect(message[:data]).to eq(hash_data)
end
it 'strips the encoding' do
expect(message[:encoding]).to be_nil
end
end
context 'message with json payload in camelCase' do
let(:message) { { data: '{"keyId":"test"}', encoding: 'json' } }
it 'decodes json' do
expect(message[:data]).to eq({ 'keyId' => 'test' })
end
it 'strips the encoding' do
expect(message[:encoding]).to be_nil
end
end
context 'message with json payload before other payloads' do
let(:message) { { data: hash_string_data, encoding: 'utf-8/json' } }
it 'decodes json' do
expect(message[:data]).to eql(hash_data)
end
it 'strips the encoding' do
expect(message[:encoding]).to eql('utf-8')
end
end
context 'message with another payload' do
let(:message) { { data: hash_string_data, encoding: 'utf-8' } }
it 'leaves the message data intact' do
expect(message[:data]).to eql(hash_string_data)
end
it 'leaves the encoding intact' do
expect(message[:encoding]).to eql('utf-8')
end
end
end
context '#encode' do
before do
subject.encode message, {}
end
context 'message with hash payload' do
let(:message) { { data: hash_data, encoding: nil } }
it 'encodes hash payload data as json' do
expect(message[:data]).to eql(hash_string_data)
end
it 'adds the encoding' do
expect(message[:encoding]).to eql('json')
end
end
context 'message with hash payload and underscore case keys' do
let(:message) { { data: { key_id: 'test' }, encoding: nil } }
it 'encodes hash payload data as json and leaves underscore case in tact' do
expect(message[:data]).to eql('{"key_id":"test"}')
end
it 'adds the encoding' do
expect(message[:encoding]).to eql('json')
end
end
context 'already encoded message with hash payload' do
let(:message) { { data: hash_data, encoding: 'utf-8' } }
it 'encodes hash payload data as json' do
expect(message[:data]).to eql(hash_string_data)
end
it 'adds the encoding' do
expect(message[:encoding]).to eql('utf-8/json')
end
end
context 'message with Array payload' do
let(:message) { { data: array_data, encoding: nil } }
it 'encodes Array payload data as json' do
expect(message[:data]).to eql(array_string_data)
end
it 'adds the encoding' do
expect(message[:encoding]).to eql('json')
end
end
context 'message with UTF-8 payload' do
let(:message) { { data: hash_string_data, encoding: nil } }
it 'leaves the message data intact' do
expect(message[:data]).to eql(hash_string_data)
end
it 'leaves the encoding intact' do
expect(message[:encoding]).to be_nil
end
end
context 'message with nil payload' do
let(:message) { { data: nil, encoding: nil } }
it 'leaves the message data intact' do
expect(message[:data]).to be_nil
end
it 'leaves the encoding intact' do
expect(message[:encoding]).to be_nil
end
end
context 'message with no data payload' do
let(:message) { { encoding: nil } }
it 'leaves the message data intact' do
expect(message[:data]).to be_nil
end
it 'leaves the encoding intact' do
expect(message[:encoding]).to be_nil
end
end
end
end