Repository navigation
Expand file tree
/
Copy pathmiddleware.test.ts
More file actions
705 lines (526 loc) · 23.6 KB
/
Copy pathmiddleware.test.ts
File metadata and controls
705 lines (526 loc) · 23.6 KB
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
// =============================================================================
// Tests for middleware.ts
// Run with: npm test
// =============================================================================
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { NextRequest, NextResponse } from 'next/server';
import { resetIpGeoRuntimeState, type IpGeoResponse } from './ipgeolocation-edge.js';
import { evaluateIpGeolocation, middleware, proxy, withIpGeolocation } from './middleware.js';
// -----------------------------------------------------------------------------
// Helpers
// -----------------------------------------------------------------------------
const ENV_KEYS = [
'IPGEOLOCATION_API_KEY',
'IPGEO_ALLOWED_COUNTRIES',
'IPGEO_ALLOW_UNKNOWN_COUNTRY',
'IPGEO_BLOCKED_COUNTRIES',
'IPGEO_BLOCK_BOT',
'IPGEO_BLOCK_INCLUDE_REASON',
'IPGEO_BLOCK_MESSAGE',
'IPGEO_BLOCK_MODE',
'IPGEO_BLOCK_PATH',
'IPGEO_BLOCK_STATUS',
'IPGEO_BLOCK_TOR',
'IPGEO_BLOCK_VPN',
'IPGEO_BYPASS_IPS',
'IPGEO_BYPASS_PATHS',
'IPGEO_BYPASS_TOKEN',
'IPGEO_CACHE_TTL_MS',
'IPGEO_COUNTRY_REDIRECTS',
'IPGEO_ENABLED',
'IPGEO_FAIL_CLOSED',
'IPGEO_HEADER_PREFIX',
'IPGEO_LOG_LEVEL',
'IPGEO_REDIRECT_PRESERVE_PATH',
'IPGEO_REDIRECT_RESPECT_EXISTING',
'IPGEO_REDIRECT_SKIP_COOKIE',
'IPGEO_REDIRECT_STATUS',
'IPGEO_REQUIRE_SECURITY',
'IPGEO_RETRIES',
'IPGEO_SET_RESPONSE_HEADERS',
'IPGEO_THREAT_SCORE_BLOCK_THRESHOLD'
];
function clearEnv(): void {
for (const key of ENV_KEYS) delete process.env[key];
}
function makeRequest(
url: string,
init: { ip?: string; headers?: Record<string, string>; method?: string } = {}
): NextRequest {
const headers = new Headers(init.headers ?? {});
if (init.ip !== undefined) headers.set('x-forwarded-for', init.ip);
const requestInit: { headers: Headers; method?: string } = { headers };
if (init.method) requestInit.method = init.method;
return new NextRequest(url, requestInit);
}
function geoFor(countryCode: string, security?: IpGeoResponse['security']): IpGeoResponse {
const response: IpGeoResponse = {
ip: '8.8.8.8',
location: {
country_code2: countryCode,
country_name: countryCode === 'SE' ? 'Sweden' : 'United States',
city: 'Stockholm',
latitude: '59.40510',
longitude: '17.95510'
},
asn: { as_number: 'AS1257', organization: 'Tele2 Sverige AB' },
time_zone: { name: 'Europe/Stockholm' }
};
if (security) response.security = security;
return response;
}
function mockApi(body: IpGeoResponse | null, status = 200): ReturnType<typeof vi.fn> {
const fetchMock = vi.fn().mockImplementation(
async () =>
new Response(body === null ? 'error' : JSON.stringify(body), {
status,
headers: { 'content-type': 'application/json' }
})
);
vi.stubGlobal('fetch', fetchMock);
return fetchMock;
}
/** Reads a request header that the middleware forwarded to the application. */
function forwardedHeader(response: NextResponse, name: string): string | null {
return response.headers.get(`x-middleware-request-${name}`);
}
beforeEach(() => {
clearEnv();
process.env.IPGEO_LOG_LEVEL = 'silent';
process.env.IPGEOLOCATION_API_KEY = 'test-key';
process.env.IPGEO_RETRIES = '0';
resetIpGeoRuntimeState();
});
afterEach(() => {
vi.restoreAllMocks();
vi.unstubAllGlobals();
clearEnv();
});
// -----------------------------------------------------------------------------
// Pass through and configuration guards
// -----------------------------------------------------------------------------
describe('middleware guards', () => {
it('does nothing when no API key is set', async () => {
delete process.env.IPGEOLOCATION_API_KEY;
const fetchMock = mockApi(geoFor('SE'));
const response = await middleware(makeRequest('https://example.com/', { ip: '8.8.8.8' }));
expect(response.status).toBe(200);
expect(fetchMock).not.toHaveBeenCalled();
});
it('does nothing when IPGEO_ENABLED is false', async () => {
process.env.IPGEO_ENABLED = 'false';
const fetchMock = mockApi(geoFor('SE'));
await middleware(makeRequest('https://example.com/', { ip: '8.8.8.8' }));
expect(fetchMock).not.toHaveBeenCalled();
});
it('skips the block page so it cannot loop', async () => {
process.env.IPGEO_BLOCKED_COUNTRIES = 'SE';
const fetchMock = mockApi(geoFor('SE'));
const response = await middleware(
makeRequest('https://example.com/blocked?reason=tor', { ip: '8.8.8.8' })
);
expect(response.status).toBe(200);
expect(fetchMock).not.toHaveBeenCalled();
});
it('does not treat a path that merely starts with the block path as the block page', async () => {
process.env.IPGEO_BLOCKED_COUNTRIES = 'SE';
mockApi(geoFor('SE'));
const response = await middleware(
makeRequest('https://example.com/blockedlist', { ip: '8.8.8.8' })
);
expect(response.status).toBe(307);
});
it('skips paths listed in IPGEO_BYPASS_PATHS', async () => {
process.env.IPGEO_BYPASS_PATHS = '/health,/api/webhooks';
process.env.IPGEO_BLOCKED_COUNTRIES = 'SE';
const fetchMock = mockApi(geoFor('SE'));
const response = await middleware(
makeRequest('https://example.com/api/webhooks/stripe', { ip: '8.8.8.8' })
);
expect(response.status).toBe(200);
expect(fetchMock).not.toHaveBeenCalled();
});
it('skips a request that carries the bypass token', async () => {
process.env.IPGEO_BYPASS_TOKEN = 'uptime-robot';
process.env.IPGEO_BLOCKED_COUNTRIES = 'SE';
const fetchMock = mockApi(geoFor('SE'));
const response = await middleware(
makeRequest('https://example.com/', {
ip: '8.8.8.8',
headers: { 'x-ipgeo-bypass-token': 'uptime-robot' }
})
);
expect(response.status).toBe(200);
expect(fetchMock).not.toHaveBeenCalled();
});
it('skips IPs listed in IPGEO_BYPASS_IPS', async () => {
process.env.IPGEO_BYPASS_IPS = '8.8.8.8';
process.env.IPGEO_BLOCKED_COUNTRIES = 'SE';
const fetchMock = mockApi(geoFor('SE'));
const response = await middleware(makeRequest('https://example.com/', { ip: '8.8.8.8' }));
expect(response.status).toBe(200);
expect(fetchMock).not.toHaveBeenCalled();
});
it('passes a local request through without calling the API', async () => {
const fetchMock = mockApi(geoFor('SE'));
const response = await middleware(makeRequest('https://example.com/', { ip: '127.0.0.1' }));
expect(response.status).toBe(200);
expect(fetchMock).not.toHaveBeenCalled();
});
it('passes through when no IP header is present', async () => {
const fetchMock = mockApi(geoFor('SE'));
const response = await middleware(makeRequest('https://example.com/'));
expect(response.status).toBe(200);
expect(fetchMock).not.toHaveBeenCalled();
});
});
// -----------------------------------------------------------------------------
// Header hardening
// -----------------------------------------------------------------------------
describe('header hardening', () => {
it('removes client supplied geo headers before the request reaches the app', async () => {
mockApi(geoFor('SE'));
const response = await middleware(
makeRequest('https://example.com/', {
ip: '8.8.8.8',
headers: { 'x-ipgeo-country': 'US', 'x-ipgeo-is-vpn': 'false' }
})
);
expect(forwardedHeader(response, 'x-ipgeo-country')).toBe('SE');
expect(forwardedHeader(response, 'x-ipgeo-is-vpn')).toBeNull();
});
it('removes client supplied geo headers even when no lookup runs', async () => {
delete process.env.IPGEOLOCATION_API_KEY;
const evaluation = await evaluateIpGeolocation(
makeRequest('https://example.com/', {
ip: '8.8.8.8',
headers: { 'x-ipgeo-country': 'US' }
})
);
expect(evaluation.requestHeaders.get('x-ipgeo-country')).toBeNull();
});
it('forwards the geo headers on a normal request', async () => {
mockApi(geoFor('SE'));
const response = await middleware(makeRequest('https://example.com/', { ip: '8.8.8.8' }));
expect(forwardedHeader(response, 'x-ipgeo-country')).toBe('SE');
expect(forwardedHeader(response, 'x-ipgeo-city')).toBe('Stockholm');
expect(forwardedHeader(response, 'x-ipgeo-timezone')).toBe('Europe/Stockholm');
expect(forwardedHeader(response, 'x-ipgeo-asn-organization')).toBe('Tele2 Sverige AB');
});
it('honours a custom header prefix', async () => {
process.env.IPGEO_HEADER_PREFIX = 'x-acme-geo';
mockApi(geoFor('SE'));
const response = await middleware(makeRequest('https://example.com/', { ip: '8.8.8.8' }));
expect(forwardedHeader(response, 'x-acme-geo-country')).toBe('SE');
});
it('adds the geo headers to the response when IPGEO_SET_RESPONSE_HEADERS is on', async () => {
process.env.IPGEO_SET_RESPONSE_HEADERS = 'true';
mockApi(geoFor('SE'));
const response = await middleware(makeRequest('https://example.com/', { ip: '8.8.8.8' }));
expect(response.headers.get('x-ipgeo-country')).toBe('SE');
});
});
// -----------------------------------------------------------------------------
// Country rules
// -----------------------------------------------------------------------------
describe('country rules', () => {
it('blocks a country on the block list', async () => {
process.env.IPGEO_BLOCKED_COUNTRIES = 'se,ru';
mockApi(geoFor('SE'));
const response = await middleware(makeRequest('https://example.com/', { ip: '8.8.8.8' }));
expect(response.status).toBe(307);
expect(response.headers.get('location')).toBe('https://example.com/blocked?reason=country');
expect(response.headers.get('x-ipgeo-block-reason')).toBe('country');
expect(response.headers.get('cache-control')).toBe('no-store');
});
it('allows a country that is not on the block list', async () => {
process.env.IPGEO_BLOCKED_COUNTRIES = 'RU';
mockApi(geoFor('SE'));
const response = await middleware(makeRequest('https://example.com/', { ip: '8.8.8.8' }));
expect(response.status).toBe(200);
});
it('blocks a country that is not on the allow list', async () => {
process.env.IPGEO_ALLOWED_COUNTRIES = 'US,CA';
mockApi(geoFor('SE'));
const response = await middleware(makeRequest('https://example.com/', { ip: '8.8.8.8' }));
expect(response.status).toBe(307);
});
it('allows a country on the allow list', async () => {
process.env.IPGEO_ALLOWED_COUNTRIES = 'SE';
mockApi(geoFor('SE'));
const response = await middleware(makeRequest('https://example.com/', { ip: '8.8.8.8' }));
expect(response.status).toBe(200);
});
it('blocks an unknown country when an allow list is configured', async () => {
process.env.IPGEO_ALLOWED_COUNTRIES = 'US';
mockApi({ ip: '8.8.8.8', location: {} });
const response = await middleware(makeRequest('https://example.com/', { ip: '8.8.8.8' }));
expect(response.status).toBe(307);
});
it('allows an unknown country when IPGEO_ALLOW_UNKNOWN_COUNTRY is on', async () => {
process.env.IPGEO_ALLOWED_COUNTRIES = 'US';
process.env.IPGEO_ALLOW_UNKNOWN_COUNTRY = 'true';
mockApi({ ip: '8.8.8.8', location: {} });
const response = await middleware(makeRequest('https://example.com/', { ip: '8.8.8.8' }));
expect(response.status).toBe(200);
});
it('leaves the reason out when IPGEO_BLOCK_INCLUDE_REASON is off', async () => {
process.env.IPGEO_BLOCKED_COUNTRIES = 'SE';
process.env.IPGEO_BLOCK_INCLUDE_REASON = 'false';
mockApi(geoFor('SE'));
const response = await middleware(makeRequest('https://example.com/', { ip: '8.8.8.8' }));
expect(response.headers.get('location')).toBe('https://example.com/blocked');
});
});
// -----------------------------------------------------------------------------
// Block modes
// -----------------------------------------------------------------------------
describe('block modes', () => {
it('answers 403 in deny mode without needing a block page', async () => {
process.env.IPGEO_BLOCK_MODE = 'deny';
process.env.IPGEO_BLOCKED_COUNTRIES = 'SE';
mockApi(geoFor('SE'));
const response = await middleware(makeRequest('https://example.com/', { ip: '8.8.8.8' }));
expect(response.status).toBe(403);
expect(await response.text()).toBe('Access to this site is restricted.');
});
it('uses a custom status and message in deny mode', async () => {
process.env.IPGEO_BLOCK_MODE = 'deny';
process.env.IPGEO_BLOCK_STATUS = '451';
process.env.IPGEO_BLOCK_MESSAGE = 'Not available in your region.';
process.env.IPGEO_BLOCKED_COUNTRIES = 'SE';
mockApi(geoFor('SE'));
const response = await middleware(makeRequest('https://example.com/', { ip: '8.8.8.8' }));
expect(response.status).toBe(451);
expect(await response.text()).toBe('Not available in your region.');
});
it('keeps the visitor URL in rewrite mode', async () => {
process.env.IPGEO_BLOCK_MODE = 'rewrite';
process.env.IPGEO_BLOCKED_COUNTRIES = 'SE';
mockApi(geoFor('SE'));
const response = await middleware(makeRequest('https://example.com/pricing', { ip: '8.8.8.8' }));
expect(response.status).toBe(200);
expect(response.headers.get('x-middleware-rewrite')).toBe(
'https://example.com/blocked?reason=country'
);
});
it('honours a custom block path', async () => {
process.env.IPGEO_BLOCK_PATH = '/access-denied';
process.env.IPGEO_BLOCKED_COUNTRIES = 'SE';
mockApi(geoFor('SE'));
const response = await middleware(makeRequest('https://example.com/', { ip: '8.8.8.8' }));
expect(response.headers.get('location')).toBe(
'https://example.com/access-denied?reason=country'
);
});
});
// -----------------------------------------------------------------------------
// Security rules
// -----------------------------------------------------------------------------
describe('security rules', () => {
it('does not request the security module when no security rule is on', async () => {
const fetchMock = mockApi(geoFor('SE'));
await middleware(makeRequest('https://example.com/', { ip: '8.8.8.8' }));
expect(String(fetchMock.mock.calls[0]?.[0])).not.toContain('include=');
});
it('requests the security module when a security rule is on', async () => {
process.env.IPGEO_BLOCK_VPN = 'true';
const fetchMock = mockApi(geoFor('SE', { is_vpn: false }));
await middleware(makeRequest('https://example.com/', { ip: '8.8.8.8' }));
expect(String(fetchMock.mock.calls[0]?.[0])).toContain('include=security');
});
it('blocks a VPN with the matching reason', async () => {
process.env.IPGEO_BLOCK_VPN = 'true';
mockApi(geoFor('SE', { is_vpn: true }));
const response = await middleware(makeRequest('https://example.com/', { ip: '8.8.8.8' }));
expect(response.status).toBe(307);
expect(response.headers.get('location')).toBe('https://example.com/blocked?reason=vpn');
});
it('blocks on the threat score threshold', async () => {
process.env.IPGEO_THREAT_SCORE_BLOCK_THRESHOLD = '75';
mockApi(geoFor('SE', { threat_score: 80 }));
const response = await middleware(makeRequest('https://example.com/', { ip: '8.8.8.8' }));
expect(response.headers.get('location')).toBe('https://example.com/blocked?reason=threat_score');
});
it('passes the request through when security data is missing', async () => {
process.env.IPGEO_BLOCK_VPN = 'true';
mockApi(geoFor('SE'));
const response = await middleware(makeRequest('https://example.com/', { ip: '8.8.8.8' }));
expect(response.status).toBe(200);
});
it('blocks when security data is missing and IPGEO_REQUIRE_SECURITY is on', async () => {
process.env.IPGEO_BLOCK_VPN = 'true';
process.env.IPGEO_REQUIRE_SECURITY = 'true';
mockApi(geoFor('SE'));
const response = await middleware(makeRequest('https://example.com/', { ip: '8.8.8.8' }));
expect(response.headers.get('location')).toBe(
'https://example.com/blocked?reason=security_unavailable'
);
});
});
// -----------------------------------------------------------------------------
// Country redirects
// -----------------------------------------------------------------------------
describe('country redirects', () => {
it('redirects to the mapped path', async () => {
process.env.IPGEO_COUNTRY_REDIRECTS = '{"SE":"/se"}';
mockApi(geoFor('SE'));
const response = await middleware(makeRequest('https://example.com/', { ip: '8.8.8.8' }));
expect(response.status).toBe(307);
expect(response.headers.get('location')).toBe('https://example.com/se');
});
it('keeps the path and the query string', async () => {
process.env.IPGEO_COUNTRY_REDIRECTS = '{"SE":"/se"}';
mockApi(geoFor('SE'));
const response = await middleware(
makeRequest('https://example.com/pricing?ref=ad', { ip: '8.8.8.8' })
);
expect(response.headers.get('location')).toBe('https://example.com/se/pricing?ref=ad');
});
it('drops the path when IPGEO_REDIRECT_PRESERVE_PATH is off', async () => {
process.env.IPGEO_COUNTRY_REDIRECTS = '{"SE":"/se"}';
process.env.IPGEO_REDIRECT_PRESERVE_PATH = 'false';
mockApi(geoFor('SE'));
const response = await middleware(
makeRequest('https://example.com/pricing?ref=ad', { ip: '8.8.8.8' })
);
expect(response.headers.get('location')).toBe('https://example.com/se');
});
it('does not redirect a visitor who is already on the target path', async () => {
process.env.IPGEO_COUNTRY_REDIRECTS = '{"SE":"/se"}';
mockApi(geoFor('SE'));
const response = await middleware(makeRequest('https://example.com/se', { ip: '8.8.8.8' }));
expect(response.status).toBe(200);
});
it('does not loop when the configured target has a trailing slash', async () => {
process.env.IPGEO_COUNTRY_REDIRECTS = '{"SE":"/se/"}';
mockApi(geoFor('SE'));
const first = await middleware(makeRequest('https://example.com/', { ip: '8.8.8.8' }));
expect(first.headers.get('location')).toBe('https://example.com/se');
const second = await middleware(makeRequest('https://example.com/se', { ip: '8.8.8.8' }));
expect(second.status).toBe(200);
});
it('leaves a visitor alone on another mapped locale', async () => {
process.env.IPGEO_COUNTRY_REDIRECTS = '{"SE":"/se","US":"/us"}';
mockApi(geoFor('SE'));
const response = await middleware(makeRequest('https://example.com/us', { ip: '8.8.8.8' }));
expect(response.status).toBe(200);
});
it('redirects across locales when IPGEO_REDIRECT_RESPECT_EXISTING is off', async () => {
process.env.IPGEO_COUNTRY_REDIRECTS = '{"SE":"/se","US":"/us"}';
process.env.IPGEO_REDIRECT_RESPECT_EXISTING = 'false';
mockApi(geoFor('SE'));
const response = await middleware(makeRequest('https://example.com/us', { ip: '8.8.8.8' }));
expect(response.status).toBe(307);
});
it('does not redirect a POST request', async () => {
process.env.IPGEO_COUNTRY_REDIRECTS = '{"SE":"/se"}';
mockApi(geoFor('SE'));
const response = await middleware(
makeRequest('https://example.com/checkout', { ip: '8.8.8.8', method: 'POST' })
);
expect(response.status).toBe(200);
});
it('skips the redirect when the opt out cookie is present', async () => {
process.env.IPGEO_COUNTRY_REDIRECTS = '{"SE":"/se"}';
process.env.IPGEO_REDIRECT_SKIP_COOKIE = 'ipgeo_no_redirect';
mockApi(geoFor('SE'));
const response = await middleware(
makeRequest('https://example.com/', {
ip: '8.8.8.8',
headers: { cookie: 'ipgeo_no_redirect=1' }
})
);
expect(response.status).toBe(200);
});
it('uses the configured redirect status', async () => {
process.env.IPGEO_COUNTRY_REDIRECTS = '{"SE":"/se"}';
process.env.IPGEO_REDIRECT_STATUS = '302';
mockApi(geoFor('SE'));
const response = await middleware(makeRequest('https://example.com/', { ip: '8.8.8.8' }));
expect(response.status).toBe(302);
});
it('ignores an off site redirect target', async () => {
process.env.IPGEO_COUNTRY_REDIRECTS = '{"SE":"https://evil.example"}';
mockApi(geoFor('SE'));
const response = await middleware(makeRequest('https://example.com/', { ip: '8.8.8.8' }));
expect(response.status).toBe(200);
});
});
// -----------------------------------------------------------------------------
// Failure handling
// -----------------------------------------------------------------------------
describe('failure handling', () => {
it('passes the request through when the lookup fails', async () => {
mockApi(null, 500);
const response = await middleware(makeRequest('https://example.com/', { ip: '8.8.8.8' }));
expect(response.status).toBe(200);
});
it('blocks when the lookup fails and IPGEO_FAIL_CLOSED is on', async () => {
process.env.IPGEO_FAIL_CLOSED = 'true';
mockApi(null, 500);
const response = await middleware(makeRequest('https://example.com/', { ip: '8.8.8.8' }));
expect(response.status).toBe(307);
expect(response.headers.get('location')).toBe(
'https://example.com/blocked?reason=lookup_failed'
);
});
it('does not block a local request in fail closed mode', async () => {
process.env.IPGEO_FAIL_CLOSED = 'true';
mockApi(geoFor('SE'));
const response = await middleware(makeRequest('https://example.com/', { ip: '10.0.0.4' }));
expect(response.status).toBe(200);
});
it('passes the request through when fetch throws', async () => {
vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new Error('network down')));
const response = await middleware(makeRequest('https://example.com/', { ip: '8.8.8.8' }));
expect(response.status).toBe(200);
});
});
// -----------------------------------------------------------------------------
// Composition
// -----------------------------------------------------------------------------
describe('composition', () => {
it('exposes the evaluation so your own middleware can forward the headers', async () => {
mockApi(geoFor('SE'));
const evaluation = await evaluateIpGeolocation(
makeRequest('https://example.com/', { ip: '8.8.8.8' })
);
expect(evaluation.action).toBe('pass');
expect(evaluation.response).toBeNull();
expect(evaluation.country).toBe('SE');
expect(evaluation.requestHeaders.get('x-ipgeo-country')).toBe('SE');
});
it('keeps the geo headers when the wrapped handler passes the request on', async () => {
mockApi(geoFor('SE'));
const composed = withIpGeolocation(async (_request, geo) =>
NextResponse.next({ request: { headers: geo.requestHeaders } })
);
const response = await composed(makeRequest('https://example.com/', { ip: '8.8.8.8' }));
expect(forwardedHeader(response as NextResponse, 'x-ipgeo-country')).toBe('SE');
});
it('does not call the wrapped handler when the request is blocked', async () => {
process.env.IPGEO_BLOCKED_COUNTRIES = 'SE';
mockApi(geoFor('SE'));
const handler = vi.fn().mockResolvedValue(NextResponse.next());
const composed = withIpGeolocation(handler);
const response = await composed(makeRequest('https://example.com/', { ip: '8.8.8.8' }));
expect(handler).not.toHaveBeenCalled();
expect(response.status).toBe(307);
});
it('accepts configuration overrides in code', async () => {
mockApi(geoFor('SE'));
const evaluation = await evaluateIpGeolocation(
makeRequest('https://example.com/', { ip: '8.8.8.8' }),
{ blockedCountries: new Set(['SE']) }
);
expect(evaluation.action).toBe('block');
expect(evaluation.reason).toBe('country');
});
it('exports proxy as an alias for Next.js 16', async () => {
mockApi(geoFor('SE'));
const response = await proxy(makeRequest('https://example.com/', { ip: '8.8.8.8' }));
expect(response.status).toBe(200);
expect(forwardedHeader(response, 'x-ipgeo-country')).toBe('SE');
});
});