-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathchannel_spec.rb
173 lines (136 loc) · 4.71 KB
/
channel_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
# encoding: utf-8
require 'spec_helper'
describe Ably::Rest::Channel do
let(:client) do
instance_double(
'Ably::Rest::Client',
encoders: [],
post: instance_double('Faraday::Response', status: 201),
idempotent_rest_publishing: false, max_message_size: max_message_size
)
end
let(:channel_name) { 'unique' }
let(:max_message_size) { nil }
subject { Ably::Rest::Channel.new(client, channel_name) }
describe '#initializer' do
let(:channel_name) { random_str.encode(encoding) }
context 'as UTF_8 string' do
let(:encoding) { Encoding::UTF_8 }
it 'is permitted' do
expect(subject.name).to eql(channel_name)
end
it 'remains as UTF-8' do
expect(subject.name.encoding).to eql(encoding)
end
end
context 'as frozen UTF_8 string' do
let(:channel_name) { 'unique'.freeze }
let(:encoding) { Encoding::UTF_8 }
it 'is permitted' do
expect(subject.name).to eql(channel_name)
end
it 'remains as UTF-8' do
expect(subject.name.encoding).to eql(encoding)
end
end
context 'as SHIFT_JIS string' do
let(:encoding) { Encoding::SHIFT_JIS }
it 'gets converted to UTF-8' do
expect(subject.name.encoding).to eql(Encoding::UTF_8)
end
it 'is compatible with original encoding' do
expect(subject.name.encode(encoding)).to eql(channel_name)
end
end
context 'as ASCII_8BIT string' do
let(:encoding) { Encoding::ASCII_8BIT }
it 'gets converted to UTF-8' do
expect(subject.name.encoding).to eql(Encoding::UTF_8)
end
it 'is compatible with original encoding' do
expect(subject.name.encode(encoding)).to eql(channel_name)
end
end
context 'as Integer' do
let(:channel_name) { 1 }
it 'raises an argument error' do
expect { subject }.to raise_error ArgumentError, /must be a String/
end
end
context 'as Nil' do
let(:channel_name) { nil }
it 'raises an argument error' do
expect { subject }.to raise_error ArgumentError, /must be a String/
end
end
end
describe '#publish name argument' do
let(:encoded_value) { random_str.encode(encoding) }
context 'as UTF_8 string' do
let(:encoding) { Encoding::UTF_8 }
it 'is permitted' do
expect(subject.publish(encoded_value, 'data')).to eql(true)
end
end
context 'as frozen UTF_8 string' do
let(:encoded_value) { 'unique'.freeze }
let(:encoding) { Encoding::UTF_8 }
it 'is permitted' do
expect(subject.publish(encoded_value, 'data')).to eql(true)
end
end
context 'as SHIFT_JIS string' do
let(:encoding) { Encoding::SHIFT_JIS }
it 'is permitted' do
expect(subject.publish(encoded_value, 'data')).to eql(true)
end
end
context 'as ASCII_8BIT string' do
let(:encoding) { Encoding::ASCII_8BIT }
it 'is permitted' do
expect(subject.publish(encoded_value, 'data')).to eql(true)
end
end
context 'as Integer' do
let(:encoded_value) { 1 }
it 'raises an argument error' do
expect { subject.publish(encoded_value, 'data') }.to raise_error ArgumentError, /must be a String/
end
end
context 'max message size exceeded' do
context 'when max_message_size is nil' do
context 'and a message size is 65537 bytes' do
it 'should raise Ably::Exceptions::MaxMessageSizeExceeded' do
expect { subject.publish('x' * 65537, 'data') }.to raise_error Ably::Exceptions::MaxMessageSizeExceeded
end
end
end
context 'when max_message_size is 65536 bytes' do
let(:max_message_size) { 65536 }
context 'and a message size is 65537 bytes' do
it 'should raise Ably::Exceptions::MaxMessageSizeExceeded' do
expect { subject.publish('x' * 65537, 'data') }.to raise_error Ably::Exceptions::MaxMessageSizeExceeded
end
end
context 'and a message size is 10 bytes' do
it 'should send a message' do
expect(subject.publish('x' * 10, 'data')).to eq(true)
end
end
end
context 'when max_message_size is 10 bytes' do
let(:max_message_size) { 10 }
context 'and a message size is 11 bytes' do
it 'should raise Ably::Exceptions::MaxMessageSizeExceeded' do
expect { subject.publish('x' * 11, 'data') }.to raise_error Ably::Exceptions::MaxMessageSizeExceeded
end
end
context 'and a message size is 2 bytes' do
it 'should send a message' do
expect(subject.publish('x' * 2, 'data')).to eq(true)
end
end
end
end
end
end