forked from ably/ably-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base64_spec.rb
182 lines (139 loc) · 5.01 KB
/
base64_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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
require 'spec_helper'
require 'base64'
require 'msgpack'
require 'ably/models/message_encoders/base64'
describe Ably::Models::MessageEncoders::Base64 do
let(:decoded_data) { random_str(32) }
let(:base64_data) { Base64.encode64(decoded_data) }
let(:binary_data) { MessagePack.pack(decoded_data) }
let(:base64_binary_data) { Base64.encode64(binary_data) }
let(:client) { instance_double('Ably::Realtime::Client') }
subject { Ably::Models::MessageEncoders::Base64.new(client) }
context '#decode' do
before do
subject.decode message, {}
end
context 'message with base64 payload' do
let(:message) { { data: base64_data, encoding: 'base64' } }
it 'decodes base64' do
expect(message[:data]).to eql(decoded_data)
end
it 'strips the encoding' do
expect(message[:encoding]).to be_nil
end
end
context 'message with base64 payload before other payloads' do
let(:message) { { data: base64_data, encoding: 'utf-8/base64' } }
it 'decodes base64' do
expect(message[:data]).to eql(decoded_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: decoded_data, encoding: 'utf-8' } }
it 'leaves the message data intact' do
expect(message[:data]).to eql(decoded_data)
end
it 'leaves the encoding intact' do
expect(message[:encoding]).to eql('utf-8')
end
end
end
context '#encode' do
context 'over binary transport' do
subject { Ably::Models::MessageEncoders::Base64.new(client, binary_protocol: true) }
before do
subject.encode message, {}
end
context 'message with binary payload' do
let(:message) { { data: binary_data, encoding: nil } }
it 'leaves the message data intact as Base64 encoding is not necessary' do
expect(message[:data]).to eql(binary_data)
end
it 'leaves the encoding intact' do
expect(message[:encoding]).to eql(nil)
end
end
context 'already encoded message with binary payload' do
let(:message) { { data: binary_data, encoding: 'cipher' } }
it 'leaves the message data intact as Base64 encoding is not necessary' do
expect(message[:data]).to eql(binary_data)
end
it 'leaves the encoding intact' do
expect(message[:encoding]).to eql('cipher')
end
end
context 'message with UTF-8 payload' do
let(:message) { { data: decoded_data, encoding: nil } }
it 'leaves the data intact' do
expect(message[:data]).to eql(decoded_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 empty binary string payload' do
let(:message) { { data: ''.encode(Encoding::ASCII_8BIT), encoding: nil } }
it 'leaves the message data intact' do
expect(message[:data]).to eql('')
end
it 'leaves the encoding intact' do
expect(message[:encoding]).to be_nil
end
end
end
context 'over text transport' do
before do
allow(client).to receive(:protocol_binary?).and_return(false)
subject.encode message, {}
end
context 'message with binary payload' do
let(:message) { { data: binary_data, encoding: nil } }
it 'encodes binary data as base64' do
expect(message[:data]).to eql(base64_binary_data)
end
it 'adds the encoding' do
expect(message[:encoding]).to eql('base64')
end
end
context 'already encoded message with binary payload' do
let(:message) { { data: binary_data, encoding: 'cipher' } }
it 'encodes binary data as base64' do
expect(message[:data]).to eql(base64_binary_data)
end
it 'adds the encoding' do
expect(message[:encoding]).to eql('cipher/base64')
end
end
context 'message with UTF-8 payload' do
let(:message) { { data: decoded_data, encoding: nil } }
it 'leaves the data intact' do
expect(message[:data]).to eql(decoded_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
end
end
end