wcc-api 0.3.1 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,266 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
- require 'wcc/api/rest_client'
5
-
6
- RSpec.describe WCC::API::RestClient do
7
- describe 'initialize' do
8
- after do
9
- described_class::ADAPTERS = {
10
- typhoeus: ['typhoeus', '~> 1.0'],
11
- http: ['http', '> 1.0', '< 3.0']
12
- }.freeze
13
- end
14
-
15
- it 'fails to load when no adapter gem found' do
16
- expect do
17
- described_class::ADAPTERS = {
18
- asdf: ['asdf', '~> 1.0']
19
- }.freeze
20
-
21
- described_class.new(
22
- api_url: 'https://cdn.contentful.com'
23
- )
24
- end.to raise_error(ArgumentError)
25
- end
26
-
27
- it 'fails to load when gem is wrong version' do
28
- expect do
29
- described_class::ADAPTERS = {
30
- http: ['http', '< 1.0']
31
- }.freeze
32
-
33
- described_class.new(
34
- api_url: 'https://cdn.contentful.com'
35
- )
36
- end.to raise_error(ArgumentError)
37
- end
38
-
39
- it 'loads proc as adapter' do
40
- described_class::ADAPTERS = {}.freeze
41
- resp = double(body: 'test body', code: 200)
42
-
43
- # act
44
- client = described_class.new(
45
- api_url: 'https://cdn.contentful.com',
46
- adapter: proc { resp }
47
- )
48
- resp = client.get('http://asdf.com')
49
-
50
- # assert
51
- expect(resp.raw_body).to eq('test body')
52
- end
53
-
54
- it 'fails to load when adapter is not invokeable' do
55
- described_class::ADAPTERS = {}.freeze
56
-
57
- expect do
58
- described_class::ADAPTERS = {
59
- http: ['http', '< 1.0']
60
- }.freeze
61
-
62
- described_class.new(
63
- api_url: 'https://cdn.contentful.com',
64
- adapter: :whoopsie
65
- )
66
- end.to raise_error(ArgumentError)
67
- end
68
- end
69
-
70
- described_class::ADAPTERS.keys.each do |adapter|
71
- context "with #{adapter} adapter" do
72
- subject(:client) do
73
- described_class.new(
74
- api_url: 'https://cdn.contentful.com/spaces/1234',
75
- adapter: adapter
76
- )
77
- end
78
-
79
- let(:entries) do
80
- ::JSON.parse(load_fixture('contentful/entries.json'))
81
- end
82
-
83
- describe 'get' do
84
- it 'gets entries with query params' do
85
- stub_request(:get, 'https://cdn.contentful.com/spaces/1234/entries?limit=2')
86
- .to_return(body: load_fixture('contentful/entries.json'))
87
-
88
- # act
89
- resp = client.get('entries', limit: 2)
90
-
91
- # assert
92
- resp.assert_ok!
93
- expect(resp.code).to eq(200)
94
- expect(resp.to_json['items'].map { |i| i.dig('sys', 'id') }).to eq(
95
- %w[6xJzDTX2HCo0u4QKIuGCOu 5yozzvgItUSYu4eI8yQ0ee]
96
- )
97
- end
98
-
99
- it 'can query entries with query param' do
100
- stub_request(:get, 'https://cdn.contentful.com/spaces/1234/entries')
101
- .with(query: {
102
- 'content_type' => 'menuButton',
103
- 'fields.text' => 'Ministries'
104
- })
105
- .to_return(body: load_fixture('contentful/entries.json'))
106
-
107
- # act
108
- resp = client.get('entries',
109
- content_type: 'menuButton',
110
- 'fields.text' => 'Ministries')
111
-
112
- # assert
113
- resp.assert_ok!
114
- expect(resp.code).to eq(200)
115
- expect(resp.to_json['items'].map { |i| i.dig('sys', 'id') }).to eq(
116
- %w[6xJzDTX2HCo0u4QKIuGCOu 5yozzvgItUSYu4eI8yQ0ee]
117
- )
118
- end
119
-
120
- it 'follows redirects' do
121
- stub_request(:get, 'http://jtj.watermark.org/api')
122
- .to_return(status: 301, headers: { 'Location' => 'https://jtj.watermark.org/api' })
123
- stub_request(:get, 'https://jtj.watermark.org/api')
124
- .to_return(body: '{ "links": { "entries": "/entries" } }')
125
-
126
- client = described_class.new(
127
- api_url: 'http://jtj.watermark.org/'
128
- )
129
-
130
- # act
131
- resp = client.get('/api')
132
-
133
- # assert
134
- resp.assert_ok!
135
- expect(resp.to_json['links']).to_not be_nil
136
- end
137
-
138
- it 'paginates directly when block given' do
139
- page1 = entries.merge('total' => 7)
140
- page2 = entries.merge('total' => 7, 'skip' => 2)
141
- page3 = entries.merge('total' => 7, 'skip' => 4)
142
- page4 = entries.merge('total' => 7, 'skip' => 6)
143
- stub_request(:get, 'https://cdn.contentful.com/spaces/1234/entries')
144
- .with(query: { 'limit' => 2 })
145
- .to_return(body: page1.to_json)
146
- stub_request(:get, 'https://cdn.contentful.com/spaces/1234/entries')
147
- .with(query: { 'limit' => 2, 'skip' => 2 })
148
- .to_return(body: page2.to_json)
149
- stub_request(:get, 'https://cdn.contentful.com/spaces/1234/entries')
150
- .with(query: { 'limit' => 2, 'skip' => 4 })
151
- .to_return(body: page3.to_json)
152
- stub_request(:get, 'https://cdn.contentful.com/spaces/1234/entries')
153
- .with(query: { 'limit' => 2, 'skip' => 6 })
154
- .to_return(body: page4.to_json)
155
-
156
- # act
157
- resp = client.get('entries', limit: 2)
158
-
159
- # assert
160
- resp.assert_ok!
161
- num_pages = 0
162
- resp.each_page do |page|
163
- expect(page.to_json['items'].length).to be <= 2
164
- num_pages += 1
165
- end
166
- expect(num_pages).to eq(4)
167
- end
168
-
169
- it 'does lazy pagination' do
170
- page1 = entries.merge('total' => 7)
171
- page2 = entries.merge('total' => 7, 'skip' => 2)
172
- page3 = entries.merge('total' => 7, 'skip' => 4)
173
- page4 = entries.merge('total' => 7, 'skip' => 6, 'items' => [entries['items'][0]])
174
- stub_request(:get, 'https://cdn.contentful.com/spaces/1234/entries')
175
- .with(query: { 'limit' => 2 })
176
- .to_return(body: page1.to_json)
177
- stub_request(:get, 'https://cdn.contentful.com/spaces/1234/entries')
178
- .with(query: { 'limit' => 2, 'skip' => 2 })
179
- .to_return(body: page2.to_json)
180
- stub_request(:get, 'https://cdn.contentful.com/spaces/1234/entries')
181
- .with(query: { 'limit' => 2, 'skip' => 4 })
182
- .to_return(body: page3.to_json)
183
- stub_request(:get, 'https://cdn.contentful.com/spaces/1234/entries')
184
- .with(query: { 'limit' => 2, 'skip' => 6 })
185
- .to_return(body: page4.to_json)
186
-
187
- # act
188
- resp = client.get('entries', limit: 2)
189
-
190
- # assert
191
- resp.assert_ok!
192
- pages = resp.each_page
193
- expect(pages).to be_a(Enumerator::Lazy)
194
- pages =
195
- pages.map do |page|
196
- expect(page.to_json['items'].length).to be <= 2
197
- page.to_json['items']
198
- end
199
- pages = pages.force
200
- expect(pages.length).to eq(4)
201
- expect(pages.flatten.map { |c| c.dig('sys', 'id') })
202
- .to eq(%w[
203
- 6xJzDTX2HCo0u4QKIuGCOu
204
- 5yozzvgItUSYu4eI8yQ0ee
205
- 6xJzDTX2HCo0u4QKIuGCOu
206
- 5yozzvgItUSYu4eI8yQ0ee
207
- 6xJzDTX2HCo0u4QKIuGCOu
208
- 5yozzvgItUSYu4eI8yQ0ee
209
- 6xJzDTX2HCo0u4QKIuGCOu
210
- ])
211
- end
212
-
213
- it 'does not paginate if only the first page is taken' do
214
- stub_request(:get, 'https://cdn.contentful.com/spaces/1234/entries')
215
- .with(query: { 'limit' => 2 })
216
- .to_return(body: load_fixture('contentful/entries.json'))
217
-
218
- stub_request(:get, 'https://cdn.contentful.com/spaces/1234/entries')
219
- .with(query: { 'limit' => 2, 'skip' => 2 })
220
- .to_raise(StandardError.new('Should not execute request for second page'))
221
-
222
- # act
223
- resp = client.get('entries', limit: 2)
224
-
225
- # assert
226
- resp.assert_ok!
227
- items = resp.items.take(2)
228
- expect(items.map { |c| c.dig('sys', 'id') }.force)
229
- .to eq(%w[
230
- 6xJzDTX2HCo0u4QKIuGCOu
231
- 5yozzvgItUSYu4eI8yQ0ee
232
- ])
233
- end
234
-
235
- it 'memoizes pages' do
236
- page1 = entries.merge('total' => 4)
237
- page2 = entries.merge('total' => 4, 'skip' => 2)
238
- stub_request(:get, 'https://cdn.contentful.com/spaces/1234/entries')
239
- .with(query: { 'limit' => 2 })
240
- .to_return(body: page1.to_json)
241
- .times(1)
242
- stub_request(:get, 'https://cdn.contentful.com/spaces/1234/entries')
243
- .with(query: { 'limit' => 2, 'skip' => 2 })
244
- .to_return(body: page2.to_json)
245
- .times(1)
246
-
247
- # act
248
- resp = client.get('entries', limit: 2)
249
-
250
- # assert
251
- resp.assert_ok!
252
- # first pagination
253
- expect(resp.items.count).to eq(4)
254
- # should be memoized
255
- expect(resp.items.map { |c| c.dig('sys', 'id') }.force)
256
- .to eq(%w[
257
- 6xJzDTX2HCo0u4QKIuGCOu
258
- 5yozzvgItUSYu4eI8yQ0ee
259
- 6xJzDTX2HCo0u4QKIuGCOu
260
- 5yozzvgItUSYu4eI8yQ0ee
261
- ])
262
- end
263
- end
264
- end
265
- end
266
- end