-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathbase_spec.rb
258 lines (209 loc) · 6.21 KB
/
base_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
# frozen_string_literal: true
describe Grape::Middleware::Base do
subject { described_class.new(blank_app) }
let(:blank_app) { ->(_) { [200, {}, 'Hi there.'] } }
before do
# Keep it one object for testing.
allow(subject).to receive(:dup).and_return(subject)
end
it 'has the app as an accessor' do
expect(subject.app).to eq(blank_app)
end
it 'calls through to the app' do
expect(subject.call({})).to eq([200, {}, 'Hi there.'])
end
context 'callbacks' do
after { subject.call!({}) }
it 'calls #before' do
expect(subject).to receive(:before)
end
it 'calls #after' do
expect(subject).to receive(:after)
end
end
context 'callbacks on error' do
let(:blank_app) { ->(_) { raise StandardError } }
it 'calls #after' do
expect(subject).to receive(:after)
expect { subject.call({}) }.to raise_error(StandardError)
end
end
context 'after callback' do
before do
allow(subject).to receive(:after).and_return([200, {}, 'Hello from after callback'])
end
it 'overwrites application response' do
expect(subject.call!({}).last).to eq('Hello from after callback')
end
end
context 'after callback with errors' do
it 'does not overwrite the application response' do
expect(subject.call({})).to eq([200, {}, 'Hi there.'])
end
context 'with patched warnings' do
before do
@warnings = warnings = []
allow(subject).to receive(:warn) { |m| warnings << m }
allow(subject).to receive(:after).and_raise(StandardError)
end
it 'does show a warning' do
expect { subject.call({}) }.to raise_error(StandardError)
expect(@warnings).not_to be_empty
end
end
end
it 'is able to access the response' do
subject.call({})
expect(subject.response).to be_a(Rack::Response)
end
describe '#response' do
subject do
described_class.new(response)
end
before { subject.call({}) }
context 'when Array' do
let(:rack_response) { Rack::Response.new('test', 204, abc: 1) }
let(:response) { ->(_) { [204, { abc: 1 }, 'test'] } }
it 'status' do
expect(subject.response.status).to eq(204)
end
it 'body' do
expect(subject.response.body).to eq(['test'])
end
it 'header' do
expect(subject.response.headers).to have_key(:abc)
end
it 'returns the memoized Rack::Response instance' do
allow(Rack::Response).to receive(:new).and_return(rack_response)
expect(subject.response).to eq(rack_response)
end
end
context 'when Rack::Response' do
let(:rack_response) { Rack::Response.new('test', 204, abc: 1) }
let(:response) { ->(_) { rack_response } }
it 'status' do
expect(subject.response.status).to eq(204)
end
it 'body' do
expect(subject.response.body).to eq(['test'])
end
it 'header' do
expect(subject.response.headers).to have_key(:abc)
end
it 'returns the memoized Rack::Response instance' do
expect(subject.response).to eq(rack_response)
end
end
end
describe '#context' do
subject { described_class.new(blank_app) }
it 'allows access to response context' do
subject.call(Grape::Env::API_ENDPOINT => { header: 'some header' })
expect(subject.context).to eq(header: 'some header')
end
end
context 'options' do
it 'persists options passed at initialization' do
expect(described_class.new(blank_app, abc: true).options[:abc]).to be true
end
context 'defaults' do
let(:example_ware) do
Class.new(Grape::Middleware::Base) do
def default_options
{ monkey: true }
end
end
end
it 'persists the default options' do
expect(example_ware.new(blank_app).options[:monkey]).to be true
end
it 'overrides default options when provided' do
expect(example_ware.new(blank_app, monkey: false).options[:monkey]).to be false
end
end
end
context 'header' do
let(:example_ware) do
Class.new(Grape::Middleware::Base) do
def before
header 'X-Test-Before', 'Hi'
end
def after
header 'X-Test-After', 'Bye'
nil
end
end
end
let(:app) do
context = self
Rack::Builder.app do
use context.example_ware
run ->(_) { [200, {}, ['Yeah']] }
end
end
it 'is able to set a header' do
get '/'
expect(last_response.headers['X-Test-Before']).to eq('Hi')
expect(last_response.headers['X-Test-After']).to eq('Bye')
end
end
context 'header overwrite' do
let(:example_ware) do
Class.new(Grape::Middleware::Base) do
def before
header 'X-Test-Overwriting', 'Hi'
end
def after
header 'X-Test-Overwriting', 'Bye'
nil
end
end
end
let(:api) do
Class.new(Grape::API) do
get('/') do
header 'X-Test-Overwriting', 'Yeah'
'Hello'
end
end
end
let(:app) do
context = self
Rack::Builder.app do
use context.example_ware
run context.api.new
end
end
it 'overwrites header by after headers' do
get '/'
expect(last_response.headers['X-Test-Overwriting']).to eq('Bye')
end
end
describe 'query_params' do
let(:dummy_middleware) do
Class.new(Grape::Middleware::Base) do
def before
query_params
end
end
end
let(:app) do
context = self
Rack::Builder.app do
use context.dummy_middleware
run ->(_) { [200, {}, ['Yeah']] }
end
end
context 'when query params are conflicting' do
it 'raises an ConflictingTypes error' do
expect { get '/?x[y]=1&x[y]z=2' }.to raise_error(Grape::Exceptions::ConflictingTypes)
end
end
context 'when query params is over the specified limit' do
let(:query_params) { "foo#{'[a]' * Rack::Utils.param_depth_limit}=bar" }
it 'raises an ConflictingTypes error' do
expect { get "/?foo#{'[a]' * Rack::Utils.param_depth_limit}=bar" }.to raise_error(Grape::Exceptions::TooDeepParameters)
end
end
end
end