|
1 | 1 | import type { DevToolsDockEntry } from '@vitejs/devtools-kit' |
2 | 2 | import { describe, expect, it } from 'vitest' |
3 | | -import { renderDockImportsMap } from '../plugins/server' |
| 3 | +import { redirectDevToolsMountPath, renderDockImportsMap } from '../plugins/server' |
4 | 4 |
|
5 | 5 | describe('renderDockImportsMap', () => { |
6 | 6 | it('uses default importName when omitted', () => { |
@@ -55,3 +55,78 @@ describe('renderDockImportsMap', () => { |
55 | 55 | expect(code).toContain('r["renderPanel"]') |
56 | 56 | }) |
57 | 57 | }) |
| 58 | + |
| 59 | +describe('redirectDevToolsMountPath', () => { |
| 60 | + function callRedirect(url: string, originalUrl = url) { |
| 61 | + let statusCode: number | undefined |
| 62 | + let location: string | undefined |
| 63 | + let ended = false |
| 64 | + let nextCalled = false |
| 65 | + |
| 66 | + redirectDevToolsMountPath( |
| 67 | + { originalUrl, url } as any, |
| 68 | + { |
| 69 | + set statusCode(value: number) { |
| 70 | + statusCode = value |
| 71 | + }, |
| 72 | + get statusCode() { |
| 73 | + return statusCode ?? 200 |
| 74 | + }, |
| 75 | + setHeader(name: string, value: string) { |
| 76 | + if (name === 'Location') { |
| 77 | + location = value |
| 78 | + } |
| 79 | + }, |
| 80 | + end() { |
| 81 | + ended = true |
| 82 | + }, |
| 83 | + } as any, |
| 84 | + () => { |
| 85 | + nextCalled = true |
| 86 | + }, |
| 87 | + ) |
| 88 | + |
| 89 | + return { |
| 90 | + ended, |
| 91 | + location, |
| 92 | + nextCalled, |
| 93 | + statusCode, |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + it('redirects the mounted devtools root to the canonical trailing slash URL', () => { |
| 98 | + expect(callRedirect('/', '/.devtools')).toEqual({ |
| 99 | + ended: true, |
| 100 | + location: '/.devtools/', |
| 101 | + nextCalled: false, |
| 102 | + statusCode: 302, |
| 103 | + }) |
| 104 | + }) |
| 105 | + |
| 106 | + it('preserves the query string when redirecting the mounted devtools root', () => { |
| 107 | + expect(callRedirect('/?foo=bar', '/.devtools?foo=bar')).toEqual({ |
| 108 | + ended: true, |
| 109 | + location: '/.devtools/?foo=bar', |
| 110 | + nextCalled: false, |
| 111 | + statusCode: 302, |
| 112 | + }) |
| 113 | + }) |
| 114 | + |
| 115 | + it('passes through the canonical trailing slash URL', () => { |
| 116 | + expect(callRedirect('/', '/.devtools/')).toEqual({ |
| 117 | + ended: false, |
| 118 | + location: undefined, |
| 119 | + nextCalled: true, |
| 120 | + statusCode: undefined, |
| 121 | + }) |
| 122 | + }) |
| 123 | + |
| 124 | + it('passes through devtools subpaths', () => { |
| 125 | + expect(callRedirect('/auth', '/.devtools/auth')).toEqual({ |
| 126 | + ended: false, |
| 127 | + location: undefined, |
| 128 | + nextCalled: true, |
| 129 | + statusCode: undefined, |
| 130 | + }) |
| 131 | + }) |
| 132 | +}) |
0 commit comments