|
1 | 1 | import { describe, expect, test } from 'vitest'; |
2 | 2 | import { |
| 3 | + attachSsrPerfInjectorMiddleware, |
3 | 4 | collectSsrPreloadEntries, |
4 | 5 | extractSsrPreloadEntriesFromHtml, |
5 | 6 | injectSsrDevtoolsIntoHtml, |
@@ -74,4 +75,77 @@ describe('ssr preload middleware helpers', () => { |
74 | 75 | expect(nextHtml).toContain('window.__QWIK_SSR_PRELOADS__'); |
75 | 76 | expect(nextHtml).toContain('/build/q-a.js'); |
76 | 77 | }); |
| 78 | + |
| 79 | + test('normalizes array accept headers before checking for html requests', () => { |
| 80 | + let middleware: |
| 81 | + | (( |
| 82 | + req: { headers: Record<string, string | string[] | undefined>; url?: string }, |
| 83 | + res: { |
| 84 | + write: (...args: any[]) => any; |
| 85 | + end: (...args: any[]) => any; |
| 86 | + setHeader: (name: string, value: any) => void; |
| 87 | + }, |
| 88 | + next: (err?: unknown) => void, |
| 89 | + ) => void) |
| 90 | + | undefined; |
| 91 | + |
| 92 | + attachSsrPerfInjectorMiddleware({ |
| 93 | + middlewares: { |
| 94 | + use(fn: typeof middleware) { |
| 95 | + middleware = fn; |
| 96 | + }, |
| 97 | + }, |
| 98 | + }); |
| 99 | + |
| 100 | + expect(middleware).toBeTypeOf('function'); |
| 101 | + |
| 102 | + const html = '<html><head></head><body></body></html>'; |
| 103 | + let written = ''; |
| 104 | + let ended = false; |
| 105 | + const headers = new Map<string, number>(); |
| 106 | + const res = { |
| 107 | + write(chunk: unknown) { |
| 108 | + written += String(chunk); |
| 109 | + return true; |
| 110 | + }, |
| 111 | + end(chunk?: unknown) { |
| 112 | + if (chunk) { |
| 113 | + written += String(chunk); |
| 114 | + } |
| 115 | + ended = true; |
| 116 | + return this; |
| 117 | + }, |
| 118 | + setHeader(name: string, value: number) { |
| 119 | + headers.set(name, value); |
| 120 | + }, |
| 121 | + }; |
| 122 | + const processWithPerf = process as typeof process & { |
| 123 | + __QWIK_SSR_PERF__?: unknown[]; |
| 124 | + }; |
| 125 | + processWithPerf.__QWIK_SSR_PERF__ = [{ component: 'App', phase: 'ssr', duration: 1 }]; |
| 126 | + |
| 127 | + try { |
| 128 | + middleware!( |
| 129 | + { |
| 130 | + headers: { |
| 131 | + accept: ['application/xhtml+xml', 'text/html'], |
| 132 | + }, |
| 133 | + url: '/demo', |
| 134 | + }, |
| 135 | + res, |
| 136 | + () => {}, |
| 137 | + ); |
| 138 | + |
| 139 | + expect(ended).toBe(false); |
| 140 | + res.end(html); |
| 141 | + } finally { |
| 142 | + delete processWithPerf.__QWIK_SSR_PERF__; |
| 143 | + } |
| 144 | + |
| 145 | + expect(headers.get('Content-Length')).toBeGreaterThan(0); |
| 146 | + expect(written).toContain('qwik:ssr-perf'); |
| 147 | + expect(ended).toBe(true); |
| 148 | + expect(written).toContain('<html><head>'); |
| 149 | + expect(written).toContain('<script>'); |
| 150 | + }); |
77 | 151 | }); |
0 commit comments