Files
next.js/test/unit/image-optimizer/match-remote-pattern-with-url.test.ts
Arian Tron 61f56f997c
Some checks failed
Test examples / Test Examples (20) (push) Has been cancelled
Test examples / Test Examples (22) (push) Has been cancelled
Lock Threads / action (push) Has been cancelled
Trigger Release / start (push) Has been cancelled
Stale issue handler / stale (push) Has been cancelled
Update Font Data / create-pull-request (push) Has been cancelled
build-and-deploy / deploy-target (push) Has been cancelled
build-and-deploy / build (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / build-wasm (nodejs) (push) Has been cancelled
build-and-deploy / build-wasm (web) (push) Has been cancelled
build-and-deploy / Deploy preview tarball (push) Has been cancelled
build-and-deploy / Potentially publish release (push) Has been cancelled
build-and-deploy / publish-turbopack-npm-packages (push) Has been cancelled
build-and-deploy / Deploy examples (push) Has been cancelled
build-and-deploy / thank you, build (push) Has been cancelled
build-and-deploy / Upload Turbopack Bytesize metrics to Datadog (push) Has been cancelled
Rspack Next.js development integration tests / Rspack integration tests (push) Has been cancelled
Rspack Next.js production integration tests / Rspack integration tests (push) Has been cancelled
Turbopack Next.js development integration tests / Next.js integration tests (push) Has been cancelled
Turbopack Next.js production integration tests / Next.js integration tests (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack development test manifest (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack production test manifest (push) Has been cancelled
Upload bundler test manifests to areweturboyet.com / Upload test results (push) Has been cancelled
Update React / create-pull-request (push) Has been cancelled
test-e2e-project-reset-cron / reset-test-project (push) Has been cancelled
Notify about the top 15 issues/PRs/feature requests (most reacted) in the last 90 days / run (push) Has been cancelled
first commit
2026-03-10 19:37:31 +03:30

319 lines
17 KiB
TypeScript

/* eslint-env jest */
import { matchRemotePattern as m } from 'next/dist/shared/lib/match-remote-pattern'
describe('matchRemotePattern with URL', () => {
it('should match literal protocol, hostname, no port, no search', () => {
const p = new URL('https://example.com/**')
expect(m(p, new URL('https://example.com'))).toBe(true)
expect(m(p, new URL('https://example.com.uk'))).toBe(false)
expect(m(p, new URL('https://sub.example.com'))).toBe(false)
expect(m(p, new URL('https://com'))).toBe(false)
expect(m(p, new URL('https://example.com/path/to/file'))).toBe(true)
expect(m(p, new URL('https://example.com/path/to/file?q=1'))).toBe(false)
expect(m(p, new URL('http://example.com/path/to/file'))).toBe(false)
expect(m(p, new URL('ftp://example.com/path/to/file'))).toBe(false)
expect(m(p, new URL('https://example.com:81/path/to/file'))).toBe(false)
expect(m(p, new URL('https://example.com:81/path/to/file?q=1'))).toBe(false)
expect(m(p, new URL('http://example.com:81/path/to/file'))).toBe(false)
})
it('should match literal protocol, hostname, port 42', () => {
const p = new URL('https://example.com:42/**')
expect(m(p, new URL('https://example.com:42'))).toBe(true)
expect(m(p, new URL('https://example.com.uk:42'))).toBe(false)
expect(m(p, new URL('https://sub.example.com:42'))).toBe(false)
expect(m(p, new URL('https://com:42'))).toBe(false)
expect(m(p, new URL('https://example.com:42/path/to/file'))).toBe(true)
expect(m(p, new URL('https://example.com:42/path/to/file?q=1'))).toBe(false)
expect(m(p, new URL('http://example.com:42/path/to/file'))).toBe(false)
expect(m(p, new URL('ftp://example.com:42/path/to/file'))).toBe(false)
expect(m(p, new URL('https://example.com'))).toBe(false)
expect(m(p, new URL('https://example.com.uk'))).toBe(false)
expect(m(p, new URL('https://sub.example.com'))).toBe(false)
expect(m(p, new URL('https://com'))).toBe(false)
expect(m(p, new URL('https://example.com/path/to/file'))).toBe(false)
expect(m(p, new URL('https://example.com/path/to/file?q=1'))).toBe(false)
expect(m(p, new URL('http://example.com/path/to/file'))).toBe(false)
expect(m(p, new URL('ftp://example.com/path/to/file'))).toBe(false)
expect(m(p, new URL('https://example.com:81'))).toBe(false)
expect(m(p, new URL('https://example.com:81/path/to/file'))).toBe(false)
expect(m(p, new URL('https://example.com:81/path/to/file?q=1'))).toBe(false)
})
it('should match literal protocol, hostname, port, pathname', () => {
const p = new URL('https://example.com:42/path/to/file')
expect(m(p, new URL('https://example.com:42'))).toBe(false)
expect(m(p, new URL('https://example.com.uk:42'))).toBe(false)
expect(m(p, new URL('https://sub.example.com:42'))).toBe(false)
expect(m(p, new URL('https://example.com:42/path'))).toBe(false)
expect(m(p, new URL('https://example.com:42/path/to'))).toBe(false)
expect(m(p, new URL('https://example.com:42/file'))).toBe(false)
expect(m(p, new URL('https://example.com:42/path/to/file'))).toBe(true)
expect(m(p, new URL('https://example.com:42/path/to/file?q=1'))).toBe(false)
expect(m(p, new URL('http://example.com:42/path/to/file'))).toBe(false)
expect(m(p, new URL('ftp://example.com:42/path/to/file'))).toBe(false)
expect(m(p, new URL('https://example.com'))).toBe(false)
expect(m(p, new URL('https://example.com.uk'))).toBe(false)
expect(m(p, new URL('https://sub.example.com'))).toBe(false)
expect(m(p, new URL('https://example.com/path'))).toBe(false)
expect(m(p, new URL('https://example.com/path/to'))).toBe(false)
expect(m(p, new URL('https://example.com/path/to/file'))).toBe(false)
expect(m(p, new URL('https://example.com/path/to/file?q=1'))).toBe(false)
expect(m(p, new URL('http://example.com/path/to/file'))).toBe(false)
expect(m(p, new URL('ftp://example.com/path/to/file'))).toBe(false)
expect(m(p, new URL('https://example.com:81/path/to/file'))).toBe(false)
expect(m(p, new URL('https://example.com:81/path/to/file?q=1'))).toBe(false)
})
it('should match literal protocol, hostname, port, pathname, search', () => {
const p = new URL(
'https://example.com:42/path/to/file?q=1&a=two&s=!@$^&-_+/()[]{};:~'
)
expect(m(p, new URL('https://example.com:42'))).toBe(false)
expect(m(p, new URL('https://example.com.uk:42'))).toBe(false)
expect(m(p, new URL('https://sub.example.com:42'))).toBe(false)
expect(m(p, new URL('https://example.com:42/path'))).toBe(false)
expect(m(p, new URL('https://example.com:42/path/to'))).toBe(false)
expect(m(p, new URL('https://example.com:42/file'))).toBe(false)
expect(m(p, new URL('https://example.com:42/path/to/file'))).toBe(false)
expect(m(p, new URL('http://example.com:42/path/to/file'))).toBe(false)
expect(m(p, new URL('ftp://example.com:42/path/to/file'))).toBe(false)
expect(m(p, new URL('https://example.com'))).toBe(false)
expect(m(p, new URL('https://example.com.uk'))).toBe(false)
expect(m(p, new URL('https://sub.example.com'))).toBe(false)
expect(m(p, new URL('https://example.com/path'))).toBe(false)
expect(m(p, new URL('https://example.com/path/to'))).toBe(false)
expect(m(p, new URL('https://example.com/path/to/file'))).toBe(false)
expect(m(p, new URL('https://example.com/path/to/file?q=1'))).toBe(false)
expect(m(p, new URL('http://example.com/path/to/file'))).toBe(false)
expect(m(p, new URL('ftp://example.com/path/to/file'))).toBe(false)
expect(m(p, new URL('https://example.com:81/path/to/file'))).toBe(false)
expect(m(p, new URL('https://example.com:81/path/to/file?q=1'))).toBe(false)
expect(m(p, new URL('https://example.com:42/path/to/file?q=1'))).toBe(false)
expect(m(p, new URL('https://example.com:42/path/to/file?q=1&a=two'))).toBe(
false
)
expect(
m(p, new URL('https://example.com:42/path/to/file?q=1&a=two&s'))
).toBe(false)
expect(
m(p, new URL('https://example.com:42/path/to/file?q=1&a=two&s='))
).toBe(false)
expect(
m(p, new URL('https://example.com:42/path/to/file?q=1&a=two&s=!@'))
).toBe(false)
expect(
m(
p,
new URL(
'https://example.com:42/path/to/file?q=1&a=two&s=!@$^&-_+/()[]{};:~'
)
)
).toBe(true)
expect(
m(
p,
new URL(
'https://example.com:42/path/to/file?q=1&s=!@$^&-_+/()[]{};:~&a=two'
)
)
).toBe(false)
expect(
m(
p,
new URL(
'https://example.com:42/path/to/file?a=two&q=1&s=!@$^&-_+/()[]{};:~'
)
)
).toBe(false)
})
it('should match hostname pattern with single asterisk by itself', () => {
const p = new URL('https://avatars.*.example.com')
expect(m(p, new URL('https://com'))).toBe(false)
expect(m(p, new URL('https://example.com'))).toBe(false)
expect(m(p, new URL('https://sub.example.com'))).toBe(false)
expect(m(p, new URL('https://example.com.uk'))).toBe(false)
expect(m(p, new URL('https://sub.example.com.uk'))).toBe(false)
expect(m(p, new URL('https://avatars.example.com'))).toBe(false)
expect(m(p, new URL('https://avatars.sfo1.example.com'))).toBe(true)
expect(m(p, new URL('https://avatars.iad1.example.com'))).toBe(true)
expect(m(p, new URL('https://more.avatars.iad1.example.com'))).toBe(false)
})
it('should match hostname pattern with single asterisk at beginning', () => {
const p = new URL('https://avatars.*1.example.com')
expect(m(p, new URL('https://com'))).toBe(false)
expect(m(p, new URL('https://example.com'))).toBe(false)
expect(m(p, new URL('https://sub.example.com'))).toBe(false)
expect(m(p, new URL('https://example.com.uk'))).toBe(false)
expect(m(p, new URL('https://sub.example.com.uk'))).toBe(false)
expect(m(p, new URL('https://avatars.example.com'))).toBe(false)
expect(m(p, new URL('https://avatars.sfo1.example.com'))).toBe(true)
expect(m(p, new URL('https://avatars.iad1.example.com'))).toBe(true)
expect(m(p, new URL('https://more.avatars.iad1.example.com'))).toBe(false)
expect(m(p, new URL('https://avatars.sfo2.example.com'))).toBe(false)
expect(m(p, new URL('https://avatars.iad2.example.com'))).toBe(false)
expect(m(p, new URL('https://avatars.1.example.com'))).toBe(true)
})
it('should match hostname pattern with single asterisk in middle', () => {
const p = new URL('https://avatars.*a*.example.com')
expect(m(p, new URL('https://com'))).toBe(false)
expect(m(p, new URL('https://example.com'))).toBe(false)
expect(m(p, new URL('https://sub.example.com'))).toBe(false)
expect(m(p, new URL('https://example.com.uk'))).toBe(false)
expect(m(p, new URL('https://sub.example.com.uk'))).toBe(false)
expect(m(p, new URL('https://avatars.example.com'))).toBe(false)
expect(m(p, new URL('https://avatars.sfo1.example.com'))).toBe(false)
expect(m(p, new URL('https://avatars.iad1.example.com'))).toBe(true)
expect(m(p, new URL('https://more.avatars.iad1.example.com'))).toBe(false)
expect(m(p, new URL('https://avatars.sfo2.example.com'))).toBe(false)
expect(m(p, new URL('https://avatars.iad2.example.com'))).toBe(true)
expect(m(p, new URL('https://avatars.a.example.com'))).toBe(true)
})
it('should match hostname pattern with single asterisk at end', () => {
const p = new URL('https://avatars.ia*.example.com')
expect(m(p, new URL('https://com'))).toBe(false)
expect(m(p, new URL('https://example.com'))).toBe(false)
expect(m(p, new URL('https://sub.example.com'))).toBe(false)
expect(m(p, new URL('https://example.com.uk'))).toBe(false)
expect(m(p, new URL('https://sub.example.com.uk'))).toBe(false)
expect(m(p, new URL('https://avatars.example.com'))).toBe(false)
expect(m(p, new URL('https://avatars.sfo1.example.com'))).toBe(false)
expect(m(p, new URL('https://avatars.iad1.example.com'))).toBe(true)
expect(m(p, new URL('https://more.avatars.iad1.example.com'))).toBe(false)
expect(m(p, new URL('https://avatars.sfo2.example.com'))).toBe(false)
expect(m(p, new URL('https://avatars.iad2.example.com'))).toBe(true)
expect(m(p, new URL('https://avatars.ia.example.com'))).toBe(true)
})
it('should match hostname pattern with double asterisk', () => {
const p = new URL('https://**.example.com')
expect(m(p, new URL('https://com'))).toBe(false)
expect(m(p, new URL('https://example.com'))).toBe(false)
expect(m(p, new URL('https://sub.example.com'))).toBe(true)
expect(m(p, new URL('https://deep.sub.example.com'))).toBe(true)
expect(m(p, new URL('https://example.com.uk'))).toBe(false)
expect(m(p, new URL('https://sub.example.com.uk'))).toBe(false)
expect(m(p, new URL('https://avatars.example.com'))).toBe(true)
expect(m(p, new URL('https://avatars.sfo1.example.com'))).toBe(true)
expect(m(p, new URL('https://avatars.iad1.example.com'))).toBe(true)
expect(m(p, new URL('https://more.avatars.iad1.example.com'))).toBe(true)
})
it('should match pathname pattern with single asterisk by itself', () => {
const p = new URL('https://example.com/act123/*/pic.jpg')
expect(m(p, new URL('https://com'))).toBe(false)
expect(m(p, new URL('https://example.com'))).toBe(false)
expect(m(p, new URL('https://sub.example.com'))).toBe(false)
expect(m(p, new URL('https://example.com.uk'))).toBe(false)
expect(m(p, new URL('https://example.com/act123'))).toBe(false)
expect(m(p, new URL('https://example.com/act123/usr4'))).toBe(false)
expect(m(p, new URL('https://example.com/act123/usr4/pic'))).toBe(false)
expect(m(p, new URL('https://example.com/act123/usr4/picsjpg'))).toBe(false)
expect(m(p, new URL('https://example.com/act123/usr4/pic.jpg'))).toBe(true)
expect(m(p, new URL('https://example.com/act123/usr5/pic.jpg'))).toBe(true)
expect(m(p, new URL('https://example.com/act123/usr6/pic.jpg'))).toBe(true)
expect(m(p, new URL('https://example.com/act123/team/pic.jpg'))).toBe(true)
expect(m(p, new URL('https://example.com/act456/team/pic.jpg'))).toBe(false)
expect(m(p, new URL('https://example.com/act123/.a/pic.jpg'))).toBe(true)
expect(m(p, new URL('https://example.com/act123/team/usr4/pic.jpg'))).toBe(
false
)
expect(m(p, new URL('https://example.com/team/pic.jpg'))).toBe(false)
})
it('should match pathname pattern with single asterisk at the beginning', () => {
const p = new URL('https://example.com/act123/*4/pic.jpg')
expect(m(p, new URL('https://com'))).toBe(false)
expect(m(p, new URL('https://example.com'))).toBe(false)
expect(m(p, new URL('https://sub.example.com'))).toBe(false)
expect(m(p, new URL('https://example.com.uk'))).toBe(false)
expect(m(p, new URL('https://example.com/act123'))).toBe(false)
expect(m(p, new URL('https://example.com/act123/usr4'))).toBe(false)
expect(m(p, new URL('https://example.com/act123/usr4/pic'))).toBe(false)
expect(m(p, new URL('https://example.com/act123/usr4/picsjpg'))).toBe(false)
expect(m(p, new URL('https://example.com/act123/usr4/pic.jpg'))).toBe(true)
expect(m(p, new URL('https://example.com/act123/usr5/pic.jpg'))).toBe(false)
expect(m(p, new URL('https://example.com/act123/team4/pic.jpg'))).toBe(true)
expect(m(p, new URL('https://example.com/act456/team5/pic.jpg'))).toBe(
false
)
expect(m(p, new URL('https://example.com/team/pic.jpg'))).toBe(false)
expect(m(p, new URL('https://example.com/act123/4/pic.jpg'))).toBe(true)
})
it('should match pathname pattern with single asterisk in the middle', () => {
const p = new URL('https://example.com/act123/*sr*/pic.jpg')
expect(m(p, new URL('https://com'))).toBe(false)
expect(m(p, new URL('https://example.com'))).toBe(false)
expect(m(p, new URL('https://sub.example.com'))).toBe(false)
expect(m(p, new URL('https://example.com.uk'))).toBe(false)
expect(m(p, new URL('https://example.com/act123'))).toBe(false)
expect(m(p, new URL('https://example.com/act123/usr4'))).toBe(false)
expect(m(p, new URL('https://example.com/act123/usr4/pic'))).toBe(false)
expect(m(p, new URL('https://example.com/act123/usr4/picsjpg'))).toBe(false)
expect(m(p, new URL('https://example.com/act123/usr4/pic.jpg'))).toBe(true)
expect(m(p, new URL('https://example.com/act123/usr5/pic.jpg'))).toBe(true)
expect(m(p, new URL('https://example.com/act123/.sr6/pic.jpg'))).toBe(true)
expect(m(p, new URL('https://example.com/act123/team4/pic.jpg'))).toBe(
false
)
expect(m(p, new URL('https://example.com/act123/team5/pic.jpg'))).toBe(
false
)
expect(m(p, new URL('https://example.com/team/pic.jpg'))).toBe(false)
expect(m(p, new URL('https://example.com/act123/sr/pic.jpg'))).toBe(true)
})
it('should match pathname pattern with single asterisk at the end', () => {
const p = new URL('https://example.com/act123/usr*/pic.jpg')
expect(m(p, new URL('https://com'))).toBe(false)
expect(m(p, new URL('https://example.com'))).toBe(false)
expect(m(p, new URL('https://sub.example.com'))).toBe(false)
expect(m(p, new URL('https://example.com.uk'))).toBe(false)
expect(m(p, new URL('https://example.com/act123'))).toBe(false)
expect(m(p, new URL('https://example.com/act123/usr4'))).toBe(false)
expect(m(p, new URL('https://example.com/act123/usr4/pic'))).toBe(false)
expect(m(p, new URL('https://example.com/act123/usr4/picsjpg'))).toBe(false)
expect(m(p, new URL('https://example.com/act123/usr4/pic.jpg'))).toBe(true)
expect(m(p, new URL('https://example.com/act123/usr5/pic.jpg'))).toBe(true)
expect(m(p, new URL('https://example.com/act123/usr/pic.jpg'))).toBe(true)
expect(m(p, new URL('https://example.com/act123/team4/pic.jpg'))).toBe(
false
)
expect(m(p, new URL('https://example.com/act456/team5/pic.jpg'))).toBe(
false
)
expect(m(p, new URL('https://example.com/team/pic.jpg'))).toBe(false)
expect(m(p, new URL('https://sub.example.com/act123/usr6/pic.jpg'))).toBe(
false
)
})
it('should match pathname pattern with double asterisk', () => {
const p = new URL('https://example.com/act123/**')
expect(m(p, new URL('https://com'))).toBe(false)
expect(m(p, new URL('https://example.com'))).toBe(false)
expect(m(p, new URL('https://sub.example.com'))).toBe(false)
expect(m(p, new URL('https://example.com.uk'))).toBe(false)
expect(m(p, new URL('https://example.com/act123'))).toBe(true)
expect(m(p, new URL('https://example.com/act123/usr4'))).toBe(true)
expect(m(p, new URL('https://example.com/act123/usr4/pic'))).toBe(true)
expect(m(p, new URL('https://example.com/act123/usr4/picsjpg'))).toBe(true)
expect(m(p, new URL('https://example.com/act123/usr4/pic.jpg'))).toBe(true)
expect(m(p, new URL('https://example.com/act123/usr5/pic.jpg'))).toBe(true)
expect(m(p, new URL('https://example.com/act123/usr6/pic.jpg'))).toBe(true)
expect(m(p, new URL('https://example.com/act123/team/pic.jpg'))).toBe(true)
expect(m(p, new URL('https://example.com/act123/.a/pic.jpg'))).toBe(true)
expect(m(p, new URL('https://example.com/act123/team/.pic.jpg'))).toBe(true)
expect(m(p, new URL('https://example.com/act456/team/pic.jpg'))).toBe(false)
expect(m(p, new URL('https://example.com/team/pic.jpg'))).toBe(false)
expect(m(p, new URL('https://sub.example.com/act123/team/pic.jpg'))).toBe(
false
)
})
})