|
| 1 | +import type { AddressInfo } from 'net' |
1 | 2 | import type { CollectionSlug, Payload } from 'payload'
|
2 | 3 |
|
3 | 4 | import { randomUUID } from 'crypto'
|
4 | 5 | import fs from 'fs'
|
| 6 | +import { createServer } from 'http' |
5 | 7 | import path from 'path'
|
6 |
| -import { _internal_safeFetchGlobal, getFileByPath } from 'payload' |
| 8 | +import { _internal_safeFetchGlobal, createPayloadRequest, getFileByPath } from 'payload' |
7 | 9 | import { fileURLToPath } from 'url'
|
8 | 10 | import { promisify } from 'util'
|
9 | 11 |
|
10 | 12 | import type { NextRESTClient } from '../helpers/NextRESTClient.js'
|
11 | 13 | import type { Enlarge, Media } from './payload-types.js'
|
12 | 14 |
|
| 15 | +// eslint-disable-next-line payload/no-relative-monorepo-imports |
| 16 | +import { getExternalFile } from '../../packages/payload/src/uploads/getExternalFile.js' |
13 | 17 | import { initPayloadInt } from '../helpers/initPayloadInt.js'
|
14 | 18 | import { createStreamableFile } from './createStreamableFile.js'
|
15 | 19 | import {
|
@@ -632,6 +636,50 @@ describe('Collections - Uploads', () => {
|
632 | 636 | fetchSpy.mockRestore()
|
633 | 637 | })
|
634 | 638 |
|
| 639 | + it('getExternalFile should not filter out payload cookies when externalFileHeaderFilter is not defined and the URL is not external', async () => { |
| 640 | + const testCookies = ['payload-token=123', 'other-cookie=456', 'payload-something=789'].join( |
| 641 | + '; ', |
| 642 | + ) |
| 643 | + |
| 644 | + const fetchSpy = jest.spyOn(global, 'fetch') |
| 645 | + |
| 646 | + // spin up a temporary server so fetch to the local doesn't fail |
| 647 | + const server = createServer((req, res) => { |
| 648 | + res.writeHead(200, { 'Content-Type': 'application/json' }) |
| 649 | + res.end(JSON.stringify({ ok: true })) |
| 650 | + }) |
| 651 | + await new Promise((res) => server.listen(0, undefined, undefined, res)) |
| 652 | + |
| 653 | + const port = (server.address() as AddressInfo).port |
| 654 | + const baseUrl = `http://localhost:${port}` |
| 655 | + |
| 656 | + const req = await createPayloadRequest({ |
| 657 | + config: payload.config, |
| 658 | + request: new Request(baseUrl, { |
| 659 | + headers: new Headers({ |
| 660 | + cookie: testCookies, |
| 661 | + origin: baseUrl, |
| 662 | + }), |
| 663 | + }), |
| 664 | + }) |
| 665 | + |
| 666 | + await getExternalFile({ |
| 667 | + data: { url: '/api/media/image.png' }, |
| 668 | + req, |
| 669 | + uploadConfig: { skipSafeFetch: true }, |
| 670 | + }) |
| 671 | + |
| 672 | + const [[, options]] = fetchSpy.mock.calls |
| 673 | + const cookieHeader = options.headers.cookie |
| 674 | + |
| 675 | + expect(cookieHeader).toContain('payload-token=123') |
| 676 | + expect(cookieHeader).toContain('payload-something=789') |
| 677 | + expect(cookieHeader).toContain('other-cookie=456') |
| 678 | + |
| 679 | + fetchSpy.mockRestore() |
| 680 | + await new Promise((res) => server.close(res)) |
| 681 | + }) |
| 682 | + |
635 | 683 | it('should keep all cookies when externalFileHeaderFilter is defined', async () => {
|
636 | 684 | const testCookies = ['payload-token=123', 'other-cookie=456', 'payload-something=789'].join(
|
637 | 685 | '; ',
|
|
0 commit comments