|
1 | 1 | /* eslint-disable turbo/no-undeclared-env-vars */ |
2 | 2 | import { beforeEach, describe, expect, it } from 'vitest'; |
3 | 3 |
|
4 | | -import { CLERK_NETLIFY_CACHE_BUST_PARAM, handleNetlifyCacheInDevInstance } from '../netlifyCacheHandler'; |
| 4 | +import { CLERK_NETLIFY_CACHE_BUST_PARAM, handleNetlifyCacheHeaders } from '../netlifyCacheHandler'; |
5 | 5 |
|
6 | | -const mockPublishableKey = 'pk_test_YW55LW9mZabcZS1wYWdlX3BhZ2VfcG9pbnRlci1pZF90ZXN0XzE'; |
| 6 | +const mockDevPublishableKey = 'pk_test_YW55LW9mZabcZS1wYWdlX3BhZ2VfcG9pbnRlci1pZF90ZXN0XzE'; |
| 7 | +const mockProdPublishableKey = 'pk_live_YW55LW9mZabcZS1wYWdlX3BhZ2VfcG9pbnRlci1pZF90ZXN0XzE'; |
7 | 8 |
|
8 | | -describe('handleNetlifyCacheInDevInstance', () => { |
| 9 | +describe('handleNetlifyCacheHeaders', () => { |
9 | 10 | beforeEach(() => { |
10 | 11 | delete process.env.URL; |
11 | 12 | delete process.env.NETLIFY; |
| 13 | + delete process.env.NETLIFY_FUNCTIONS_TOKEN; |
12 | 14 | }); |
13 | 15 |
|
14 | | - it('should add cache bust parameter when on Netlify and in development', () => { |
15 | | - process.env.NETLIFY = 'true'; |
16 | | - process.env.URL = 'https://example.netlify.app'; |
| 16 | + describe('Netlify-Vary header', () => { |
| 17 | + it('should set Netlify-Vary header when on Netlify', () => { |
| 18 | + process.env.NETLIFY = 'true'; |
17 | 19 |
|
18 | | - const requestStateHeaders = new Headers({ |
19 | | - Location: 'https://example.netlify.app', |
| 20 | + const headers = new Headers(); |
| 21 | + handleNetlifyCacheHeaders({ headers, publishableKey: mockProdPublishableKey }); |
| 22 | + |
| 23 | + expect(headers.get('Netlify-Vary')).toBe('cookie=__client_uat,cookie=__session'); |
20 | 24 | }); |
21 | | - const locationHeader = requestStateHeaders.get('Location') || ''; |
22 | 25 |
|
23 | | - handleNetlifyCacheInDevInstance({ |
24 | | - locationHeader, |
25 | | - requestStateHeaders, |
26 | | - publishableKey: mockPublishableKey, |
| 26 | + it('should not set Netlify-Vary header when not on Netlify', () => { |
| 27 | + const headers = new Headers(); |
| 28 | + handleNetlifyCacheHeaders({ headers, publishableKey: mockProdPublishableKey }); |
| 29 | + |
| 30 | + expect(headers.get('Netlify-Vary')).toBeNull(); |
27 | 31 | }); |
28 | 32 |
|
29 | | - const locationUrl = new URL(requestStateHeaders.get('Location') || ''); |
30 | | - expect(locationUrl.searchParams.has(CLERK_NETLIFY_CACHE_BUST_PARAM)).toBe(true); |
31 | | - }); |
| 33 | + it('should detect Netlify via NETLIFY_FUNCTIONS_TOKEN', () => { |
| 34 | + process.env.NETLIFY_FUNCTIONS_TOKEN = 'some-token'; |
32 | 35 |
|
33 | | - it('should not modify the Location header if it has the handshake param', () => { |
34 | | - process.env.URL = 'https://example.netlify.app'; |
35 | | - process.env.NETLIFY = 'true'; |
| 36 | + const headers = new Headers(); |
| 37 | + handleNetlifyCacheHeaders({ headers, publishableKey: mockProdPublishableKey }); |
36 | 38 |
|
37 | | - const requestStateHeaders = new Headers({ |
38 | | - Location: 'https://example.netlify.app/redirect?__clerk_handshake=', |
| 39 | + expect(headers.get('Netlify-Vary')).toBe('cookie=__client_uat,cookie=__session'); |
39 | 40 | }); |
40 | | - const locationHeader = requestStateHeaders.get('Location') || ''; |
41 | 41 |
|
42 | | - handleNetlifyCacheInDevInstance({ |
43 | | - locationHeader, |
44 | | - requestStateHeaders, |
45 | | - publishableKey: mockPublishableKey, |
46 | | - }); |
| 42 | + it('should detect Netlify via URL ending with netlify.app', () => { |
| 43 | + process.env.URL = 'https://example.netlify.app'; |
47 | 44 |
|
48 | | - expect(requestStateHeaders.get('Location')).toBe(locationHeader); |
| 45 | + const headers = new Headers(); |
| 46 | + handleNetlifyCacheHeaders({ headers, publishableKey: mockProdPublishableKey }); |
| 47 | + |
| 48 | + expect(headers.get('Netlify-Vary')).toBe('cookie=__client_uat,cookie=__session'); |
| 49 | + }); |
49 | 50 | }); |
50 | 51 |
|
51 | | - it('should not modify the Location header if not on Netlify', () => { |
52 | | - const requestStateHeaders = new Headers({ |
53 | | - Location: 'https://example.netlify.app', |
| 52 | + describe('cache bust parameter (dev instances)', () => { |
| 53 | + it('should add cache bust parameter when on Netlify and in development with Location header', () => { |
| 54 | + process.env.NETLIFY = 'true'; |
| 55 | + |
| 56 | + const headers = new Headers({ Location: 'https://example.netlify.app' }); |
| 57 | + handleNetlifyCacheHeaders({ headers, publishableKey: mockDevPublishableKey }); |
| 58 | + |
| 59 | + const locationUrl = new URL(headers.get('Location') || ''); |
| 60 | + expect(locationUrl.searchParams.has(CLERK_NETLIFY_CACHE_BUST_PARAM)).toBe(true); |
54 | 61 | }); |
55 | | - const locationHeader = requestStateHeaders.get('Location') || ''; |
56 | 62 |
|
57 | | - handleNetlifyCacheInDevInstance({ |
58 | | - locationHeader, |
59 | | - requestStateHeaders, |
60 | | - publishableKey: mockPublishableKey, |
| 63 | + it('should not add cache bust parameter for production instances', () => { |
| 64 | + process.env.NETLIFY = 'true'; |
| 65 | + |
| 66 | + const headers = new Headers({ Location: 'https://example.netlify.app' }); |
| 67 | + handleNetlifyCacheHeaders({ headers, publishableKey: mockProdPublishableKey }); |
| 68 | + |
| 69 | + const locationUrl = new URL(headers.get('Location') || ''); |
| 70 | + expect(locationUrl.searchParams.has(CLERK_NETLIFY_CACHE_BUST_PARAM)).toBe(false); |
61 | 71 | }); |
62 | 72 |
|
63 | | - expect(requestStateHeaders.get('Location')).toBe('https://example.netlify.app'); |
64 | | - }); |
| 73 | + it('should not modify the Location header if it has the handshake param', () => { |
| 74 | + process.env.NETLIFY = 'true'; |
65 | 75 |
|
66 | | - it('should ignore the URL environment variable if it is not a string', () => { |
67 | | - // @ts-expect-error - Random object |
68 | | - process.env.URL = {}; |
69 | | - process.env.NETLIFY = 'true'; |
| 76 | + const locationValue = 'https://example.netlify.app/redirect?__clerk_handshake='; |
| 77 | + const headers = new Headers({ Location: locationValue }); |
| 78 | + handleNetlifyCacheHeaders({ headers, publishableKey: mockDevPublishableKey }); |
70 | 79 |
|
71 | | - const requestStateHeaders = new Headers({ |
72 | | - Location: 'https://example.netlify.app', |
| 80 | + expect(headers.get('Location')).toBe(locationValue); |
73 | 81 | }); |
74 | | - const locationHeader = requestStateHeaders.get('Location') || ''; |
75 | 82 |
|
76 | | - handleNetlifyCacheInDevInstance({ |
77 | | - locationHeader, |
78 | | - requestStateHeaders, |
79 | | - publishableKey: mockPublishableKey, |
| 83 | + it('should not modify the Location header if not on Netlify', () => { |
| 84 | + const headers = new Headers({ Location: 'https://example.com' }); |
| 85 | + handleNetlifyCacheHeaders({ headers, publishableKey: mockDevPublishableKey }); |
| 86 | + |
| 87 | + expect(headers.get('Location')).toBe('https://example.com'); |
80 | 88 | }); |
81 | 89 |
|
82 | | - const locationUrl = new URL(requestStateHeaders.get('Location') || ''); |
83 | | - expect(locationUrl.searchParams.has(CLERK_NETLIFY_CACHE_BUST_PARAM)).toBe(true); |
| 90 | + it('should ignore the URL environment variable if it is not a string', () => { |
| 91 | + // @ts-expect-error - Random object |
| 92 | + process.env.URL = {}; |
| 93 | + process.env.NETLIFY = 'true'; |
| 94 | + |
| 95 | + const headers = new Headers({ Location: 'https://example.netlify.app' }); |
| 96 | + handleNetlifyCacheHeaders({ headers, publishableKey: mockDevPublishableKey }); |
| 97 | + |
| 98 | + const locationUrl = new URL(headers.get('Location') || ''); |
| 99 | + expect(locationUrl.searchParams.has(CLERK_NETLIFY_CACHE_BUST_PARAM)).toBe(true); |
| 100 | + }); |
84 | 101 | }); |
85 | 102 | }); |
0 commit comments