|
| 1 | +import { describe, expect, it } from 'bun:test'; |
| 2 | +import { GitBookAPIError } from '@gitbook/api'; |
| 3 | +import { extractCacheControl } from './errors'; |
| 4 | + |
| 5 | +describe('extractCacheControl', () => { |
| 6 | + it('should return undefined when error has no response', () => { |
| 7 | + const error = { message: 'Test error' } as GitBookAPIError; |
| 8 | + const result = extractCacheControl(error); |
| 9 | + expect(result).toBeUndefined(); |
| 10 | + }); |
| 11 | + |
| 12 | + it('should return undefined when response has no cache-control header', () => { |
| 13 | + const error = new GitBookAPIError('Test error', new Response(null, {})); |
| 14 | + |
| 15 | + const result = extractCacheControl(error); |
| 16 | + expect(result).toBeUndefined(); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should parse max-age from cache-control header', () => { |
| 20 | + const error = new GitBookAPIError( |
| 21 | + 'Test error', |
| 22 | + new Response(null, { |
| 23 | + headers: { |
| 24 | + 'cache-control': 'max-age=3600', |
| 25 | + }, |
| 26 | + }) |
| 27 | + ); |
| 28 | + |
| 29 | + const result = extractCacheControl(error); |
| 30 | + expect(result).toEqual({ |
| 31 | + maxAge: 3600, |
| 32 | + staleWhileRevalidate: undefined, |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should parse stale-while-revalidate from cache-control header', () => { |
| 37 | + const error = new GitBookAPIError( |
| 38 | + 'Test error', |
| 39 | + new Response(null, { |
| 40 | + headers: { |
| 41 | + 'cache-control': 'stale-while-revalidate=86400', |
| 42 | + }, |
| 43 | + }) |
| 44 | + ); |
| 45 | + |
| 46 | + const result = extractCacheControl(error); |
| 47 | + expect(result).toEqual({ |
| 48 | + maxAge: undefined, |
| 49 | + staleWhileRevalidate: 86400, |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should parse both max-age and stale-while-revalidate', () => { |
| 54 | + const error = new GitBookAPIError( |
| 55 | + 'Test error', |
| 56 | + new Response(null, { |
| 57 | + headers: { |
| 58 | + 'cache-control': 'max-age=3600, stale-while-revalidate=86400', |
| 59 | + }, |
| 60 | + }) |
| 61 | + ); |
| 62 | + |
| 63 | + const result = extractCacheControl(error); |
| 64 | + expect(result).toEqual({ |
| 65 | + maxAge: 3600, |
| 66 | + staleWhileRevalidate: 86400, |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should return undefined for maxAge when it is 0', () => { |
| 71 | + const error = new GitBookAPIError( |
| 72 | + 'Test error', |
| 73 | + new Response(null, { |
| 74 | + headers: { |
| 75 | + 'cache-control': 'max-age=0, stale-while-revalidate=86400', |
| 76 | + }, |
| 77 | + }) |
| 78 | + ); |
| 79 | + |
| 80 | + const result = extractCacheControl(error); |
| 81 | + expect(result).toEqual({ |
| 82 | + maxAge: undefined, |
| 83 | + staleWhileRevalidate: 86400, |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should handle complex cache-control header with multiple directives', () => { |
| 88 | + const error = new GitBookAPIError( |
| 89 | + 'Test error', |
| 90 | + new Response(null, { |
| 91 | + headers: { |
| 92 | + 'cache-control': |
| 93 | + 'public, max-age=3600, must-revalidate, stale-while-revalidate=86400', |
| 94 | + }, |
| 95 | + }) |
| 96 | + ); |
| 97 | + |
| 98 | + const result = extractCacheControl(error); |
| 99 | + expect(result).toEqual({ |
| 100 | + maxAge: 3600, |
| 101 | + staleWhileRevalidate: 86400, |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should return undefined when cache-control has no parseable values', () => { |
| 106 | + const error = new GitBookAPIError( |
| 107 | + 'Test error', |
| 108 | + new Response(null, { |
| 109 | + headers: { |
| 110 | + 'cache-control': 'no-cache, no-store', |
| 111 | + }, |
| 112 | + }) |
| 113 | + ); |
| 114 | + |
| 115 | + const result = extractCacheControl(error); |
| 116 | + expect(result).toEqual({ |
| 117 | + maxAge: undefined, |
| 118 | + staleWhileRevalidate: undefined, |
| 119 | + }); |
| 120 | + }); |
| 121 | +}); |
0 commit comments