-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathencoders_spec.rb
185 lines (151 loc) · 6.16 KB
/
encoders_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
183
184
185
# encoding: utf-8
require 'spec_helper'
require 'base64'
describe Ably::Models::MessageEncoders do
let(:default_client_options) { { key: api_key, environment: environment } }
let(:client) { Ably::Rest::Client.new(default_client_options.merge(protocol: protocol)) }
let(:channel_options) { {} }
let(:channel) { client.channel('test', channel_options) }
let(:response) { instance_double('Faraday::Response', status: 201) }
let(:cipher_params) { { key: Ably::Util::Crypto.generate_random_key(128), algorithm: 'aes', mode: 'cbc', key_length: 128 } }
let(:crypto) { Ably::Util::Crypto.new(cipher_params) }
let(:utf_8_data) { random_str.encode(Encoding::UTF_8) }
let(:binary_data) { MessagePack.pack(random_str).encode(Encoding::ASCII_8BIT) }
let(:json_data) { { 'some_id' => random_str } }
after do
channel.publish 'event', published_data
end
def on_publish
expect(client).to receive(:post) do |url, message|
yield(message['encoding'], message['data'])
end.and_return(response)
end
def decrypted(payload, options = {})
payload = Base64.decode64(payload) if options[:base64]
crypto.decrypt(payload)
end
context 'with binary transport protocol' do
let(:protocol) { :msgpack }
context 'without encryption' do
context 'with UTF-8 data' do
let(:published_data) { utf_8_data }
it 'does not apply any encoding' do
on_publish do |encoding, encoded_data|
expect(encoding).to be_nil
expect(encoded_data).to eql(published_data)
end
end
end
context 'with binary data' do
let(:published_data) { binary_data }
it 'does not apply any encoding' do
on_publish do |encoding, encoded_data|
expect(encoding).to be_nil
expect(encoded_data).to eql(published_data)
end
end
end
context 'with JSON data' do
let(:published_data) { json_data }
it 'stringifies the JSON and sets the encoding attribute to "json"' do
on_publish do |encoding, encoded_data|
expect(encoding).to eql('json')
expect(encoded_data).to eql(JSON.dump(published_data))
end
end
end
end
context 'with encryption' do
let(:channel_options) { { cipher: cipher_params } }
context 'with UTF-8 data' do
let(:published_data) { utf_8_data }
it 'applies utf-8 and cipher encoding and sets the encoding attribute to "utf-8/cipher+aes-128-cbc"' do
on_publish do |encoding, encoded_data|
expect(encoding).to eql('utf-8/cipher+aes-128-cbc')
expect(decrypted(encoded_data)).to eql(published_data)
end
end
end
context 'with binary data' do
let(:published_data) { binary_data }
it 'applies cipher encoding and sets the encoding attribute to "cipher+aes-128-cbc"' do
on_publish do |encoding, encoded_data|
expect(encoding).to eql('cipher+aes-128-cbc')
expect(decrypted(encoded_data)).to eql(published_data)
end
end
end
context 'with JSON data' do
let(:published_data) { json_data }
it 'applies json, utf-8 and cipher encoding and sets the encoding attribute to "json/utf-8/cipher+aes-128-cbc"' do
on_publish do |encoding, encoded_data|
expect(encoding).to eql('json/utf-8/cipher+aes-128-cbc')
expect(decrypted(encoded_data)).to eql(JSON.dump(published_data))
end
end
end
end
end
context 'with text transport protocol' do
let(:protocol) { :json }
context 'without encryption' do
context 'with UTF-8 data' do
let(:published_data) { utf_8_data }
it 'does not apply any encoding' do
on_publish do |encoding, encoded_data|
expect(encoding).to be_nil
expect(encoded_data).to eql(published_data)
end
end
end
context 'with binary data' do
let(:published_data) { binary_data }
it 'applies a base64 encoding and sets the encoding attribute to "base64"' do
on_publish do |encoding, encoded_data|
expect(encoding).to eql('base64')
expect(Base64.decode64(encoded_data)).to eql(published_data)
end
end
end
context 'with JSON data' do
let(:published_data) { json_data }
it 'stringifies the JSON and sets the encoding attribute to "json"' do
on_publish do |encoding, encoded_data|
expect(encoding).to eql('json')
expect(encoded_data).to eql(JSON.dump(published_data))
end
end
end
end
context 'with encryption' do
let(:channel_options) { { cipher: cipher_params } }
context 'with UTF-8 data' do
let(:published_data) { utf_8_data }
it 'applies utf-8, cipher and base64 encodings and sets the encoding attribute to "utf-8/cipher+aes-128-cbc/base64"' do
on_publish do |encoding, encoded_data|
expect(encoding).to eql('utf-8/cipher+aes-128-cbc/base64')
expect(decrypted(encoded_data, base64: true)).to eql(published_data)
end
end
end
context 'with binary data' do
let(:published_data) { binary_data }
it 'applies cipher and base64 encoding and sets the encoding attribute to "cipher+aes-128-cbc/base64"' do
on_publish do |encoding, encoded_data|
expect(encoding).to eql('cipher+aes-128-cbc/base64')
expect(decrypted(encoded_data, base64: true)).to eql(published_data)
end
end
end
context 'with JSON data' do
let(:published_data) { json_data }
it 'applies json, utf-8, cipher and base64 encoding and sets the encoding attribute to "json/utf-8/cipher+aes-128-cbc/base64"' do
on_publish do |encoding, encoded_data|
expect(encoding).to eql('json/utf-8/cipher+aes-128-cbc/base64')
expect(decrypted(encoded_data, base64: true)).to eql(JSON.dump(published_data))
end
end
end
end
end
end