-
Notifications
You must be signed in to change notification settings - Fork 453
Expand file tree
/
Copy pathpatchRequest.test.ts
More file actions
70 lines (62 loc) · 2.82 KB
/
patchRequest.test.ts
File metadata and controls
70 lines (62 loc) · 2.82 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
import { describe, expect, it } from 'vitest';
import { patchRequest } from '../server/utils';
describe('patchRequest', () => {
it('preserves the URL including path and query string', () => {
const original = new Request('https://example.com/path?token=xyz&foo=bar');
const cloned = patchRequest(original);
expect(cloned.url).toBe(original.url);
});
it('preserves an encoded nested redirect_url with its own query and port', () => {
// Mirrors the shape reported in the TanStack + Lovable handshake bug:
// the outer URL's `redirect_url` param is a percent-encoded inner URL with
// a port and its own query string, which must survive the clone verbatim.
const nested = 'https://localhost:8080/?token=abc';
const original = new Request(`https://example.com/handshake?redirect_url=${encodeURIComponent(nested)}`);
const cloned = patchRequest(original);
expect(cloned.url).toBe(original.url);
expect(new URL(cloned.url).searchParams.get('redirect_url')).toBe(nested);
});
it('preserves x-forwarded-* headers', () => {
const original = new Request('https://example.com/', {
headers: { 'x-forwarded-host': 'example.com', 'x-forwarded-proto': 'https' },
});
const cloned = patchRequest(original);
expect(cloned.headers.get('x-forwarded-host')).toBe('example.com');
expect(cloned.headers.get('x-forwarded-proto')).toBe('https');
});
it('preserves the method', () => {
const original = new Request('https://example.com/', { method: 'POST' });
const cloned = patchRequest(original);
expect(cloned.method).toBe('POST');
});
it('preserves redirect and cache', () => {
const original = new Request('https://example.com/', {
redirect: 'manual',
cache: 'no-cache',
});
const cloned = patchRequest(original);
expect(cloned.redirect).toBe('manual');
expect(cloned.cache).toBe('no-cache');
});
it('forwards signal aborts from the original request', () => {
const controller = new AbortController();
const original = new Request('https://example.com/', { signal: controller.signal });
const cloned = patchRequest(original);
expect(cloned.signal.aborted).toBe(false);
controller.abort();
expect(cloned.signal.aborted).toBe(true);
});
it('clones POST requests without forwarding the body', () => {
// patchRequest deliberately omits `body` from the cloned init (see #7020)
// so the original request's body stays intact for downstream consumers and
// the undici duplex issues the helper was written to avoid do not resurface.
const original = new Request('https://example.com/api', {
method: 'POST',
body: 'payload',
headers: { 'content-type': 'text/plain' },
});
const cloned = patchRequest(original);
expect(cloned.method).toBe('POST');
expect(cloned.body).toBeNull();
});
});