/
resolve-url.test.ts
182 lines (161 loc) · 5.87 KB
/
resolve-url.test.ts
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
import {
resolveUrl,
resolveAbsoluteUrlWithPathname,
getSocialImageFallbackMetadataBase,
} from './resolve-url'
// required to be resolved as URL with resolveUrl()
describe('metadata: resolveUrl', () => {
it('should return null when url is falsy', () => {
expect(resolveUrl('', null)).toBe(null)
expect(resolveUrl(null, null)).toBe(null)
expect(resolveUrl(undefined, null)).toBe(null)
})
it('should return url itself when metadataBase is null or url is valid URL', () => {
expect(resolveUrl('https://example.com/abc', null)).toEqual(
new URL('https://example.com/abc')
)
expect(resolveUrl(new URL('https://example.com/def'), null)).toEqual(
new URL('https://example.com/def')
)
})
it('should compose with metadataBase when url is relative or absolute', () => {
const metadataBase = new URL('https://example.com/abc')
expect(resolveUrl('/def', metadataBase)).toEqual(
new URL('https://example.com/abc/def')
)
expect(resolveUrl('../def', metadataBase)).toEqual(
new URL('https://example.com/def')
)
expect(resolveUrl('foo', metadataBase)).toEqual(
new URL('https://example.com/abc/foo')
)
})
it('should ignore metadataBase when url is valid URL', () => {
const metadataBase = new URL('https://example.com/abc')
expect(resolveUrl('https://example.com/def', metadataBase)).toEqual(
new URL('https://example.com/def')
)
expect(resolveUrl(new URL('https://bar.com/ghi'), metadataBase)).toEqual(
new URL('https://bar.com/ghi')
)
})
})
describe('resolveAbsoluteUrlWithPathname', () => {
describe('trailingSlash is false', () => {
const metadataBase = new URL('https://example.com/')
const opts = {
trailingSlash: false,
pathname: '/',
}
const resolver = (url: string | URL) =>
resolveAbsoluteUrlWithPathname(url, metadataBase, opts)
it('should resolve absolute internal url', () => {
expect(resolver('https://example.com/foo')).toBe(
'https://example.com/foo'
)
})
})
describe('trailingSlash is true', () => {
const metadataBase = new URL('https://example.com/')
const opts = {
trailingSlash: true,
pathname: '/',
}
const resolver = (url: string | URL) =>
resolveAbsoluteUrlWithPathname(url, metadataBase, opts)
it('should add trailing slash to relative url', () => {
expect(resolver('/foo')).toBe('https://example.com/foo/')
})
it('should add trailing slash to absolute internal url', () => {
expect(resolver('https://example.com/foo')).toBe(
'https://example.com/foo/'
)
expect(resolver(new URL('https://example.com/foo'))).toBe(
'https://example.com/foo/'
)
})
it('should not add trailing slash to external url', () => {
expect(resolver('https://external.org/foo')).toBe(
'https://external.org/foo'
)
expect(resolver(new URL('https://external.org/foo'))).toBe(
'https://external.org/foo'
)
})
it('should not add trailing slash to absolute internal url with query', () => {
expect(resolver('https://example.com/foo?bar')).toBe(
'https://example.com/foo?bar'
)
expect(resolver(new URL('https://example.com/foo?bar'))).toBe(
'https://example.com/foo?bar'
)
})
it('should not add trailing slash to relative url with query', () => {
expect(resolver('/foo?bar')).toBe('https://example.com/foo?bar')
expect(resolver(new URL('/foo?bar', metadataBase))).toBe(
'https://example.com/foo?bar'
)
})
})
})
describe('getSocialImageFallbackMetadataBase', () => {
describe('fallbackMetadataBase when metadataBase is not present', () => {
let originalEnv: NodeJS.ProcessEnv
function getSocialImageFallbackMetadataBaseHelper(): string {
return getSocialImageFallbackMetadataBase(null).fallbackMetadataBase.href
}
beforeEach(() => {
originalEnv = process.env
})
afterEach(() => {
delete process.env.VERCEL_URL
delete process.env.VERCEL_ENV
delete process.env.VERCEL_BRANCH_URL
delete process.env.VERCEL_PROJECT_PRODUCTION_URL
process.env = originalEnv
})
it('should return localhost url in local dev mode', () => {
// @ts-expect-error override process env
process.env.NODE_ENV = 'development'
expect(getSocialImageFallbackMetadataBaseHelper()).toBe(
'http://localhost:3000/'
)
})
it('should return local url in local build mode', () => {
// @ts-expect-error override process env
process.env.NODE_ENV = 'production'
expect(getSocialImageFallbackMetadataBaseHelper()).toBe(
'http://localhost:3000/'
)
})
it('should prefer branch url in preview deployment if presents', () => {
// @ts-expect-error override process env
process.env.NODE_ENV = 'production'
process.env.VERCEL_ENV = 'preview'
process.env.VERCEL_BRANCH_URL = 'branch-url'
process.env.VERCEL_URL = 'vercel-url'
expect(getSocialImageFallbackMetadataBaseHelper()).toBe(
'https://branch-url/'
)
})
it('should return vercel url in preview deployment if only it presents', () => {
// @ts-expect-error override process env
process.env.NODE_ENV = 'production'
process.env.VERCEL_ENV = 'preview'
process.env.VERCEL_URL = 'vercel-url'
expect(getSocialImageFallbackMetadataBaseHelper()).toBe(
'https://vercel-url/'
)
})
it('should return project production url in production deployment', () => {
// @ts-expect-error override process env
process.env.NODE_ENV = 'production'
process.env.VERCEL_ENV = 'production'
process.env.VERCEL_URL = 'vercel-url'
process.env.VERCEL_PROJECT_PRODUCTION_URL = 'production-url'
expect(getSocialImageFallbackMetadataBaseHelper()).toBe(
'https://production-url/'
)
})
})
})