-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathproxy-handlers.test.ts
More file actions
95 lines (75 loc) · 2.96 KB
/
Copy pathproxy-handlers.test.ts
File metadata and controls
95 lines (75 loc) · 2.96 KB
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
import { NextRequest } from 'next/server'
import { beforeEach, describe, expect, it, vi } from 'vitest'
const getMiddlewareRedirectMock = vi.hoisted(() => vi.fn())
const getRewriteForPathMock = vi.hoisted(() => vi.fn())
vi.mock('@/configs/env-flags', () => ({ ALLOW_SEO_INDEXING: false }))
vi.mock('@/lib/utils/redirects', () => ({
getMiddlewareRedirectFromPath: getMiddlewareRedirectMock,
}))
vi.mock('@/lib/utils/rewrites', () => ({
getRewriteForPath: getRewriteForPathMock,
}))
const {
handleMiddlewareRedirect,
handleRouteRewritePassthrough,
handleMiddlewareRewrite,
handleAuthGate,
} = await import('@/core/server/proxy/handlers')
function request(path: string): NextRequest {
return new NextRequest(`https://app.e2b.dev${path}`)
}
beforeEach(() => {
getMiddlewareRedirectMock.mockReset().mockReturnValue(undefined)
getRewriteForPathMock.mockReset().mockReturnValue({ config: undefined })
})
describe('proxy handlers', () => {
it('redirects with the configured status and headers', () => {
getMiddlewareRedirectMock.mockReturnValue({
destination: '/new-home',
statusCode: 308,
headers: { 'x-custom': 'yes' },
})
const response = handleMiddlewareRedirect(request('/old-home'))
expect(response?.status).toBe(308)
expect(response?.headers.get('location')).toContain('/new-home')
})
it('passes catch-all route rewrites through untouched', () => {
getRewriteForPathMock.mockReturnValue({ config: { domain: 'x' } })
const response = handleRouteRewritePassthrough(request('/docs'))
expect(response).not.toBeNull()
expect(response?.headers.get('location')).toBeNull()
})
it('rewrites middleware-managed paths to the configured origin', () => {
getRewriteForPathMock.mockReturnValue({
config: { domain: 'docs.e2b.dev' },
rule: { pathPreprocessor: (p: string) => p.replace('/docs', '') },
})
const response = handleMiddlewareRewrite(request('/docs/guide'))
expect(response?.headers.get('x-middleware-rewrite')).toContain(
'docs.e2b.dev/guide'
)
expect(response?.headers.get('X-Robots-Tag')).toBe('noindex, nofollow')
})
it('redirects unauthenticated dashboard pages to sign-in with returnTo', async () => {
const response = await handleAuthGate(
request('/dashboard/team-x?tab=settings'),
false
)
const location = response.headers.get('location')
expect(location).toContain('/sign-in')
expect(location).toContain(
'returnTo=%2Fdashboard%2Fteam-x%3Ftab%3Dsettings'
)
})
it('preserves terminal paths and query params for sign-in', async () => {
for (const path of [
'/dashboard/terminal?template=base&command=ls',
'/dashboard/team-x/terminal?template=base&command=ls',
]) {
const response = await handleAuthGate(request(path), false)
const location = response.headers.get('location')
expect(location).toContain('/sign-in')
expect(location).toContain(`returnTo=${encodeURIComponent(path)}`)
}
})
})