|
1 | 1 | import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; |
2 | 2 | import { tmpdir } from 'node:os'; |
3 | 3 | import { join } from 'node:path'; |
| 4 | +import { getProxyRequestHeaders } from 'h3'; |
4 | 5 | import { createRouteRulesMatcher } from 'h3/rules'; |
| 6 | +import { createFetch } from 'ofetch'; |
5 | 7 | import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
6 | 8 |
|
7 | 9 | import { |
@@ -303,6 +305,39 @@ describe('analogNitroPlugin', () => { |
303 | 305 | expect(hookFn).toHaveBeenCalledWith('build:config', expect.any(Function)); |
304 | 306 | }); |
305 | 307 |
|
| 308 | + it.each([ |
| 309 | + [{ node: true }, true], |
| 310 | + [{ node: false, noExternals: true }, false], |
| 311 | + [{ node: true, noExternals: true }, false], |
| 312 | + [{ node: false }, false], |
| 313 | + ])( |
| 314 | + 'externalizes rxjs only for presets with a module resolver (%o)', |
| 315 | + async (presetOptions, externalized) => { |
| 316 | + const plugin = analogNitroPlugin({ workspaceRoot }); |
| 317 | + callConfig(plugin, projectRoot); |
| 318 | + const hooks: Record<string, (...args: any[]) => unknown> = {}; |
| 319 | + await (plugin as any).nitro.setup({ |
| 320 | + options: { |
| 321 | + rootDir: projectRoot, |
| 322 | + buildDir: join(projectRoot, '.nitro'), |
| 323 | + handlers: [], |
| 324 | + scanDirs: [], |
| 325 | + virtual: {}, |
| 326 | + renderer: {}, |
| 327 | + ...presetOptions, |
| 328 | + }, |
| 329 | + hooks: { hook: (name: string, fn: any) => (hooks[name] = fn) }, |
| 330 | + }); |
| 331 | + |
| 332 | + const rolldown: any = { plugins: [], output: {} }; |
| 333 | + hooks['build:config'](undefined, { rolldown }); |
| 334 | + |
| 335 | + expect( |
| 336 | + (rolldown.external ?? []).some((p: RegExp) => p.test('rxjs')), |
| 337 | + ).toBe(externalized); |
| 338 | + }, |
| 339 | + ); |
| 340 | + |
306 | 341 | it('resolves a built SSR entry emitted with a .js extension', async () => { |
307 | 342 | const buildDir = join(projectRoot, '.nitro'); |
308 | 343 | const ssrDir = join(buildDir, 'vite/services/ssr'); |
@@ -472,20 +507,54 @@ describe('analogNitroPlugin', () => { |
472 | 507 | vi.restoreAllMocks(); |
473 | 508 | }); |
474 | 509 |
|
475 | | - function createEntry(renderer: (...args: any[]) => unknown) { |
| 510 | + function createEntry( |
| 511 | + renderer: (...args: any[]) => unknown, |
| 512 | + nitroServerFetch = vi.fn(), |
| 513 | + ) { |
476 | 514 | vi.stubGlobal('$fetch', vi.fn()); |
477 | 515 | const plugin = analogNitroPlugin({ workspaceRoot }); |
478 | 516 | callConfig(plugin, projectRoot, 'serve'); |
479 | 517 | return runModule( |
480 | 518 | callLoad(plugin, '\0virtual:@analogjs/nitro/ssr-entry'), |
481 | 519 | { |
482 | | - nitroServerFetch: vi.fn(), |
483 | | - createFetch: () => vi.fn(), |
| 520 | + nitroServerFetch, |
| 521 | + getProxyRequestHeaders, |
| 522 | + createFetch, |
484 | 523 | renderer, |
485 | 524 | }, |
486 | 525 | ); |
487 | 526 | } |
488 | 527 |
|
| 528 | + it('forwards the request headers and signal to in-process fetches', async () => { |
| 529 | + const nitroServerFetch = vi.fn(async () => Response.json({})); |
| 530 | + const entry = createEntry(async (_url, _template, ctx) => { |
| 531 | + await ctx.fetch('/api/data', { headers: { accept: 'text/plain' } }); |
| 532 | + return '<html></html>'; |
| 533 | + }, nitroServerFetch); |
| 534 | + const abort = new AbortController(); |
| 535 | + |
| 536 | + await entry.fetch( |
| 537 | + new Request('http://localhost/x', { |
| 538 | + headers: { |
| 539 | + authorization: 'Bearer token', |
| 540 | + accept: 'text/html', |
| 541 | + 'keep-alive': 'timeout=5', |
| 542 | + 'if-none-match': '"page"', |
| 543 | + }, |
| 544 | + signal: abort.signal, |
| 545 | + }), |
| 546 | + ); |
| 547 | + const [url, init] = nitroServerFetch.mock.calls[0] as any[]; |
| 548 | + abort.abort(); |
| 549 | + |
| 550 | + expect(url).toBe('http://localhost/api/data'); |
| 551 | + expect(init.headers.get('authorization')).toBe('Bearer token'); |
| 552 | + expect(init.headers.get('accept')).toBe('text/plain'); |
| 553 | + expect(init.headers.has('keep-alive')).toBe(false); |
| 554 | + expect(init.headers.has('if-none-match')).toBe(false); |
| 555 | + expect(init.signal.aborted).toBe(true); |
| 556 | + }); |
| 557 | + |
489 | 558 | it('responds with the status of a failed render', async () => { |
490 | 559 | const entry = createEntry(() => { |
491 | 560 | throw Object.assign(new Error('Not found'), { statusCode: 404 }); |
|
0 commit comments