forked from ably/ably-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_paginated_result_spec.rb
380 lines (320 loc) · 10 KB
/
http_paginated_result_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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
require 'spec_helper'
require 'ostruct'
describe "Ably::Models::HttpPaginatedResponse: #HP1 -> #HP8" do
let(:paginated_result_class) { Ably::Models::HttpPaginatedResponse }
let(:headers) { Hash.new }
let(:client) do
instance_double('Ably::Rest::Client', logger: Ably::Models::NilLogger.new).tap do |client|
allow(client).to receive(:get).and_return(http_response)
end
end
let(:body) do
[
{ 'id' => 0 },
{ 'id' => 1 }
]
end
let(:status) { "200" }
let(:http_response) do
instance_double('Faraday::Response', {
body: body,
headers: headers,
status: status
})
end
let(:base_url) { 'http://rest.ably.io/channels/channel_name' }
let(:full_url) { "#{base_url}/whatever?param=exists" }
let(:paginated_result_options) { Hash.new }
let(:first_paged_request) { paginated_result_class.new(http_response, full_url, client, paginated_result_options) }
subject { first_paged_request }
context '#items' do
it 'returns correct length from body' do
expect(subject.items.length).to eql(body.length)
end
it 'is Enumerable' do
expect(subject.items).to be_kind_of(Enumerable)
end
it 'is iterable' do
expect(subject.items.map { |d| d }).to eql(body)
end
context '#each' do
it 'returns an enumerator' do
expect(subject.items.each).to be_a(Enumerator)
end
it 'yields each item' do
items = []
subject.items.each do |item|
items << item
end
expect(items).to eq(body)
end
end
it 'provides [] accessor method' do
expect(subject.items[0][:id]).to eql(body[0][:id])
expect(subject.items[1][:id]).to eql(body[1][:id])
expect(subject.items[2]).to be_nil
end
specify '#first gets the first item in page' do
expect(subject.items.first[:id]).to eql(body[0][:id])
end
specify '#last gets the last item in page' do
expect(subject.items.last[:id]).to eql(body[1][:id])
end
context 'with coercion', :api_private do
let(:paginated_result_options) { { coerce_into: 'OpenStruct' } }
it 'returns coerced objects' do
expect(subject.items.first).to be_a(OpenStruct)
expect(subject.items.first.id).to eql(body.first['id'])
end
end
end
context 'paged transformations', :api_private do
let(:headers) do
{
'link' => [
'<./history?index=1>; rel="next"'
].join(', ')
}
end
let(:paged_client) do
instance_double('Ably::Rest::Client', logger: Ably::Models::NilLogger.new).tap do |client|
allow(client).to receive(:get).and_return(http_response_page2)
end
end
let(:body_page2) do
[
{ id: 2 },
{ id: 3 }
]
end
let(:http_response_page2) do
instance_double('Faraday::Response', {
body: body_page2,
headers: headers
})
end
context 'with each block' do
subject do
paginated_result_class.new(http_response, full_url, paged_client, paginated_result_options) do |result|
result[:added_attribute_from_block] = "id:#{result[:id]}"
result
end
end
it 'calls the block for each result after retrieving the results' do
expect(subject.items[0][:added_attribute_from_block]).to eql("id:#{body[0][:id]}")
end
it 'calls the block for each result on second page after retrieving the results' do
page_1_first_id = subject.items[0][:id]
next_page = subject.next
expect(next_page.items[0][:added_attribute_from_block]).to eql("id:#{body_page2[0][:id]}")
expect(next_page.items[0][:id]).to_not eql(page_1_first_id)
end
end
if defined?(Ably::Realtime)
context 'with option async_blocking_operations: true' do
include RSpec::EventMachine
subject do
paginated_result_class.new(http_response, full_url, paged_client, async_blocking_operations: true)
end
context '#next' do
it 'returns a SafeDeferrable that catches exceptions in callbacks and logs them' do
run_reactor do
expect(subject.next).to be_a(Ably::Util::SafeDeferrable)
stop_reactor
end
end
it 'allows a success callback block to be added' do
run_reactor do
subject.next do |paginated_result|
expect(paginated_result).to be_a(Ably::Models::HttpPaginatedResponse)
stop_reactor
end
end
end
end
context '#first' do
it 'calls the errback callback when first page headers are missing' do
run_reactor do
subject.next do |paginated_result|
deferrable = subject.first
deferrable.errback do |error|
expect(error).to be_a(Ably::Exceptions::PageMissing)
stop_reactor
end
end
end
end
end
end
end
end
context 'with non paged http response' do
it 'is the last page' do
expect(subject).to be_last
end
it 'does not have next page' do
expect(subject).to_not have_next
end
it 'does not support pagination' do
expect(subject.supports_pagination?).to_not eql(true)
end
it 'returns nil when accessing next page' do
expect(subject.next).to be_nil
end
it 'returns nil when accessing first page' do
expect(subject.first).to be_nil
end
end
context 'with paged http response' do
let(:base_url) { 'http://rest.ably.io/channels/channel_name' }
let(:full_url) { "#{base_url}/messages" }
let(:headers) do
{
'link' => [
'<./history?index=0>; rel="first"',
'<./history?index=0>; rel="current"',
'<./history?index=1>; rel="next"'
].join(', ')
}
end
it 'has next page' do
expect(subject).to have_next
end
it 'is not the last page' do
expect(subject).to_not be_last
end
it 'supports pagination' do
expect(subject.supports_pagination?).to eql(true)
end
context 'accessing next page' do
let(:next_body) do
[ { id: 2 } ]
end
let(:next_headers) do
{
'link' => [
'<./history?index=0>; rel="first"',
'<./history?index=1>; rel="current"'
].join(', ')
}
end
let(:next_http_response) do
double('http_response', {
body: next_body,
headers: next_headers
})
end
let(:subject) { first_paged_request.next }
before do
expect(client).to receive(:get).with("#{base_url}/history?index=1").and_return(next_http_response).once
end
it 'returns another HttpPaginatedResponse' do
expect(subject).to be_a(paginated_result_class)
end
it 'retrieves the next page of results' do
expect(subject.items.length).to eql(next_body.length)
expect(subject.items[0][:id]).to eql(next_body[0][:id])
end
it 'does not have a next page' do
expect(subject).to_not have_next
end
it 'is the last page' do
expect(subject).to be_last
end
it 'returns nil when trying to access the last page when it is the last page' do
expect(subject).to be_last
expect(subject.next).to be_nil
end
context 'and then first page' do
before do
expect(client).to receive(:get).with("#{base_url}/history?index=0").and_return(http_response).once
end
subject { first_paged_request.next.first }
it 'returns a HttpPaginatedResponse' do
expect(subject).to be_a(paginated_result_class)
end
it 'retrieves the first page of results' do
expect(subject.items.length).to eql(body.length)
end
end
end
end
context 'response metadata' do
context 'successful response' do
let(:headers) { { 'Content-type' => 'application/json' } }
let(:status) { 200 }
specify '#success? is true' do
expect(subject).to be_succes
end
specify '#status_code reflects status code' do
expect(subject.status_code).to eql(200)
end
specify '#error_code to be empty' do
expect(subject.error_code).to be_nil
end
specify '#error_message to be empty' do
expect(subject.error_message).to be_nil
end
specify '#headers to be a hash' do
expect(subject.headers['Content-type']).to eql('application/json')
end
end
context 'failed response' do
let(:headers) { { 'X-Ably-Errormessage' => 'Fault', 'X-Ably-Errorcode' => '500' } }
let(:status) { 500 }
specify '#success? is false' do
expect(subject).to_not be_succes
end
specify '#status_code reflects status code' do
expect(subject.status_code).to eql(500)
end
specify '#error_code to be populated' do
expect(subject.error_code).to eql(500)
end
specify '#error_message to be populated' do
expect(subject.error_message).to eql('Fault')
end
specify '#headers to be present' do
expect(subject.headers['X-Ably-Errormessage']).to eql('Fault')
end
end
end
context '#items Array conversion and nil handling #HP3' do
context 'with Json Array' do
let(:body) do
[
{ 'id' => 0 },
{ 'id' => 1 }
]
end
it 'is an array' do
expect(subject.items.length).to eql(2)
end
end
context 'with Json Object' do
let(:body) do
{ 'id' => 0 }
end
it 'is an array' do
expect(subject.items.length).to eql(1)
expect(subject.items.first['id']).to eql(0)
end
end
context 'with empty response' do
let(:body) do
''
end
it 'is an array' do
expect(subject.items.length).to eql(0)
end
end
context 'with nil response' do
let(:body) do
nil
end
it 'is an array' do
expect(subject.items.length).to eql(0)
end
end
end
end