-
Notifications
You must be signed in to change notification settings - Fork 8
/
middleware_spec.rb
376 lines (292 loc) · 10.7 KB
/
middleware_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
require 'spec_helper'
require 'tempfile'
require 'logger'
describe GeoRedirect::Middleware do
include GeoRedirect::Support
include Rack::Test::Methods
let(:config) { YAML.load_file(fixture_path('config.yml')) }
describe '#load_config' do
it 'reads a config file' do
mock_app
expect(@app.config).to eq(config)
end
it 'errors on not-found config file' do
mock_app config: nonexisting_file_path
log_should_include('ERROR')
log_should_include('Could not load GeoRedirect config YML file')
end
it 'errors on a mal-formatted config file' do
mock_app config: fixture_path('config.bad.yml')
log_should_include('ERROR')
log_should_include('Could not load GeoRedirect config YML file')
end
end
describe '#load_db' do
it 'reads a db file' do
mock_app
expect(@app.db).to be_a(GeoIP)
end
it 'errors on not-found db file' do
mock_app db: nonexisting_file_path
log_should_include('ERROR')
log_should_include('Could not load GeoIP database file.')
end
# this example is disabled, as it seems that
# GeoIP does not let me know if a db file is proper.
# it "errors on mal-formatted db file" do
# pending "GeoIP does not raise on bad files"
# mock_app :db => fixture_path("config.yml")
# log_should_include("ERROR")
# log_should_include("Could not load GeoIP database file.")
# end
end
describe '#log' do
context 'with valid logfile path' do
before { mock_app }
it 'initiates a log file' do
expect(@app.instance_variable_get(:"@logger")).to be_kind_of Logger
end
it 'prints to log file' do
message = 'Testing GeoRedirect logger'
@app.send(:log, [message])
log_should_include(message)
end
end
it 'ignores invalid logfile path' do
mock_app logfile: '/no_such_file'
expect(@app.instance_variable_get(:"@logger")).to be_nil
end
it 'ignores empty logfile option' do
mock_app logfile: nil
expect(@app.instance_variable_get(:"@logger")).to be_nil
end
end
describe '#host_by_country' do
before { mock_app }
subject { @app.host_by_country(country) }
context 'when country is valid' do
let(:country) { 'US' }
it { is_expected.to eq(:us) }
end
context 'when country is invalid' do
let(:country) { 'WHATEVER' }
it { is_expected.to eq(:default) }
end
end
describe 'host_by_hostname' do
before { mock_app }
subject { @app.host_by_hostname(hostname) }
context 'when hostname is valid' do
let(:hostname) { 'biz.waze.co.il' }
it { is_expected.to eq(:il) }
end
context 'when hostname is invalid' do
let(:hostname) { 'something.else.org' }
it { is_expected.to eq(:default) }
end
end
describe 'redirect logic' do
subject(:session) { last_request.env['rack.session'] }
subject(:response) { last_response }
let(:app_options) { {} }
let(:request_ip) { '5.5.5.5' }
let(:request_path) { '/' }
let(:request_args) { {} }
let(:request_session) { nil }
let(:country) { { country_code: 5, country_code2: country_code } }
let(:env) do
{
'REMOTE_ADDR' => request_ip,
'HTTP_HOST' => 'biz.waze.co.il',
'rack.session' => {
'geo_redirect' => request_session,
'geo_redirect.country' => (country_code if request_session)
}
}
end
before do
mock_app(app_options)
allow(@app.db).to receive(:country).with(request_ip).and_return(country)
get request_path, request_args, env
end
matcher :redirect_to do |expected|
match do |response|
url_scheme = last_request.env['rack.url_scheme']
url = "#{url_scheme}://#{config[expected][:host]}"
response.body.include?('Moved Permanently') &&
response.status == 301 &&
response.headers['Location'].start_with?(url)
end
end
matcher :not_redirect do
match do |response|
response.body.include?('Hello world!') &&
response.ok?
end
end
matcher :remember do |host|
match do
session && session['geo_redirect'] == host
end
end
matcher :remember_country do |country|
match do
session && session['geo_redirect.country'] == country
end
end
context 'without session memory' do
context 'for a foreign source' do
let(:country_code) { 'US' }
it { is_expected.to redirect_to :us }
it { is_expected.to remember :us }
it { is_expected.to remember_country 'US' }
end
context 'for a local source' do
let(:country_code) { 'IL' }
it { is_expected.to not_redirect }
it { is_expected.to remember :il }
it { is_expected.to remember_country 'IL' }
end
context 'for an unknown source' do
let(:country_code) { 'SOMEWHERE OVER THE RAINBOW' }
it { is_expected.to redirect_to :default }
it { is_expected.to remember :default }
it { is_expected.to remember_country 'SOMEWHERE OVER THE RAINBOW' }
end
end
context 'with valid session memory' do
let(:request_session) { :default }
let(:country_code) { 'US' }
it { is_expected.to redirect_to :default }
it { is_expected.to remember :default }
it { is_expected.to remember_country 'US' }
end
context 'with invalid session memory' do
let(:request_session) { 'foo' }
let(:country_code) { 'US' }
it 'removes invalid session data' do
expect(session['geo_redirect']).not_to eq('foo')
end
it { is_expected.to redirect_to :us }
it { is_expected.to remember :us }
it { is_expected.to remember_country 'US' }
end
context 'with forced redirect flag' do
let(:country_code) { 'US' }
let(:request_args) { { redirect: 1 } }
it { is_expected.to redirect_to :il }
it 'rewrites the flag out' do
expect(response.headers['Location']).not_to include('redirect=1')
end
it { is_expected.to remember :il }
it { is_expected.to remember_country nil }
end
context 'with skip flag' do
let(:country_code) { 'US' }
let(:request_args) { { skip_geo: true } }
it { is_expected.to not_redirect }
it { is_expected.to remember nil }
it { is_expected.to remember_country nil }
context 'with remember_when_skipping option set to true' do
let(:app_options) { { remember_when_skipping: true } }
let(:country_code) { 'IL' }
let(:request_args) { { skip_geo: true } }
it { is_expected.to not_redirect }
it { is_expected.to remember :il }
it { is_expected.to remember_country nil }
end
context 'with remember_when_skipping option set to false' do
let(:app_options) { { remember_when_skipping: false } }
let(:country_code) { 'IL' }
let(:request_args) { { skip_geo: true } }
it { is_expected.to not_redirect }
it { is_expected.to remember nil }
it { is_expected.to remember_country nil }
end
end
context 'with no recognizable IP' do
let(:country_code) { nil }
it { is_expected.to not_redirect }
it { is_expected.to remember nil }
it { is_expected.to remember_country nil }
end
context 'with skip_if block' do
let(:country_code) { 'US' }
context 'when returns true' do
let(:app_options) { { skip_if: ->(_req) { true } } }
it { is_expected.to not_redirect }
it { is_expected.to remember nil }
it { is_expected.to remember_country nil }
end
context 'when returns false' do
let(:app_options) { { skip_if: ->(_req) { false } } }
it { is_expected.to redirect_to :us }
it { is_expected.to remember :us }
it { is_expected.to remember_country 'US' }
end
end
describe 'include/exclude logic' do
let(:country_code) { 'US' }
shared_examples :skips_redirect do
it { is_expected.to not_redirect }
it { is_expected.to remember nil }
it { is_expected.to remember_country nil }
end
shared_examples :does_not_skip_redirect do
it { is_expected.to redirect_to :us }
it { is_expected.to remember :us }
it { is_expected.to remember_country 'US' }
end
context 'with an include option' do
let(:app_options) { { include: include_value } }
context 'when include is an array of paths' do
let(:include_value) { %w[/include_me /include_me/too] }
context 'when request URL matches one of the included paths' do
let(:request_path) { '/include_me?query_param=value' }
it_behaves_like :does_not_skip_redirect
end
context 'when request URL does not match any of the included paths' do
let(:request_path) { '/dont_include_me?query_param=value' }
it_behaves_like :skips_redirect
end
end
context 'when include is a single path' do
let(:include_value) { '/include_me' }
context 'when request URL matches one of the included paths' do
let(:request_path) { '/include_me?query_param=value' }
it_behaves_like :does_not_skip_redirect
end
context 'when request URL does not match any of the included paths' do
let(:request_path) { '/dont_include_me?query_param=value' }
it_behaves_like :skips_redirect
end
end
end
context 'with an exclude option' do
let(:app_options) { { exclude: exclude_value } }
context 'when exclude is an array of paths' do
let(:exclude_value) { %w[/exclude_me /exclude_me/too] }
context 'when request URL matches one of the excluded paths' do
let(:request_path) { '/exclude_me?query_param=value' }
it_behaves_like :skips_redirect
end
context 'when request URL does not match any of the excluded paths' do
let(:request_path) { '/dont_exclude_me?query_param=value' }
it_behaves_like :does_not_skip_redirect
end
end
context 'when exclude is a single path' do
let(:exclude_value) { '/exclude_me' }
context 'when request URL matches the excluded path' do
let(:request_path) { '/exclude_me?query_param=value' }
it_behaves_like :skips_redirect
end
context 'when request URL does not match any of the excluded paths' do
let(:request_path) { '/dont_exclude_me?query_param=value' }
it_behaves_like :does_not_skip_redirect
end
end
end
end
end
end