Skip to content

Commit 5ff8293

Browse files
committed
chore: add unit tests for sanitizer for #2098
1 parent b6f8abc commit 5ff8293

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

packages/runtime/src/helpers/edge.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ const sanitizeName = (name: string) => `next_${name.replace(/\W/g, '_')}`
9696
/**
9797
* Convert the images path to strip the origin (until domain-level Edge functions are supported)
9898
*/
99-
const sanitizeEdgePath = (imagesPath: string) => new URL(imagesPath, process.env.URL || 'http://n').pathname
99+
export const sanitizeEdgePath = (imagesPath: string) => new URL(imagesPath, process.env.URL || 'http://n').pathname
100100

101101
// Slightly different spacing in different versions!
102102
const IMPORT_UNSUPPORTED = [

test/helpers/matchers.spec.ts

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getEdgeFunctionPatternForPage } from '../../packages/runtime/src/helpers/edge'
1+
import { getEdgeFunctionPatternForPage, sanitizeEdgePath } from '../../packages/runtime/src/helpers/edge'
22
import { makeLocaleOptional, stripLookahead } from '../../packages/runtime/src/helpers/matchers'
33

44
const makeDataPath = (path: string) => `/_next/data/build-id${path === '/' ? '/index' : path}.json`
@@ -183,3 +183,24 @@ describe('the edge function matcher helpers', () => {
183183
expect('/edge/1/').toMatch(new RegExp(regex))
184184
})
185185
})
186+
187+
describe('the images path is sanitized', () => {
188+
it('passes through a path', () => {
189+
expect(sanitizeEdgePath('/_next/image')).toBe('/_next/image')
190+
})
191+
192+
it('strips domains', () => {
193+
expect(sanitizeEdgePath('http://example.com/_next/image')).toBe('/_next/image')
194+
expect(sanitizeEdgePath('https://example.com/_next/image')).toBe('/_next/image')
195+
})
196+
197+
it('strips domains with globs', () => {
198+
expect(sanitizeEdgePath('http://example.com/_next/image/*')).toBe('/_next/image/*')
199+
expect(sanitizeEdgePath('https://example.com/_next/image/*')).toBe('/_next/image/*')
200+
})
201+
202+
it('strips domains with tokens', () => {
203+
expect(sanitizeEdgePath('http://example.com/_next/image/:slug/')).toBe('/_next/image/:slug/')
204+
expect(sanitizeEdgePath('https://example.com/_next/image/:slug/')).toBe('/_next/image/:slug/')
205+
})
206+
})

0 commit comments

Comments
 (0)