|
| 1 | +import type { RemixRequest } from '../../src/utils/vendor/types'; |
| 2 | +import { normalizeRemixRequest } from '../../src/utils/web-fetch'; |
| 3 | + |
| 4 | +class Headers { |
| 5 | + private _headers: Record<string, string> = {}; |
| 6 | + |
| 7 | + constructor(headers?: Iterable<[string, string]>) { |
| 8 | + if (headers) { |
| 9 | + for (const [key, value] of headers) { |
| 10 | + this.set(key, value); |
| 11 | + } |
| 12 | + } |
| 13 | + } |
| 14 | + static fromEntries(entries: Iterable<[string, string]>): Headers { |
| 15 | + return new Headers(entries); |
| 16 | + } |
| 17 | + entries(): IterableIterator<[string, string]> { |
| 18 | + return Object.entries(this._headers)[Symbol.iterator](); |
| 19 | + } |
| 20 | + |
| 21 | + [Symbol.iterator](): IterableIterator<[string, string]> { |
| 22 | + return this.entries(); |
| 23 | + } |
| 24 | + |
| 25 | + get(key: string): string | null { |
| 26 | + return this._headers[key] ?? null; |
| 27 | + } |
| 28 | + |
| 29 | + has(key: string): boolean { |
| 30 | + return this._headers[key] !== undefined; |
| 31 | + } |
| 32 | + |
| 33 | + set(key: string, value: string): void { |
| 34 | + this._headers[key] = value; |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +class Request { |
| 39 | + private _url: string; |
| 40 | + private _options: { method: string; body?: any; headers: Headers }; |
| 41 | + |
| 42 | + constructor(url: string, options: { method: string; body?: any; headers: Headers }) { |
| 43 | + this._url = url; |
| 44 | + this._options = options; |
| 45 | + } |
| 46 | + |
| 47 | + get method() { |
| 48 | + return this._options.method; |
| 49 | + } |
| 50 | + |
| 51 | + get url() { |
| 52 | + return this._url; |
| 53 | + } |
| 54 | + |
| 55 | + get headers() { |
| 56 | + return this._options.headers; |
| 57 | + } |
| 58 | + |
| 59 | + get body() { |
| 60 | + return this._options.body; |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +describe('normalizeRemixRequest', () => { |
| 65 | + it('should normalize remix web-fetch request', () => { |
| 66 | + const headers = new Headers(); |
| 67 | + headers.set('Accept', 'text/html,application/json'); |
| 68 | + headers.set('Cookie', 'name=value'); |
| 69 | + const request = new Request('https://example.com/api/json?id=123', { |
| 70 | + method: 'GET', |
| 71 | + headers: headers as any, |
| 72 | + }); |
| 73 | + |
| 74 | + const expected = { |
| 75 | + agent: undefined, |
| 76 | + hash: '', |
| 77 | + headers: { |
| 78 | + Accept: 'text/html,application/json', |
| 79 | + Connection: 'close', |
| 80 | + Cookie: 'name=value', |
| 81 | + 'User-Agent': 'node-fetch', |
| 82 | + }, |
| 83 | + hostname: 'example.com', |
| 84 | + href: 'https://example.com/api/json?id=123', |
| 85 | + insecureHTTPParser: undefined, |
| 86 | + ip: null, |
| 87 | + method: 'GET', |
| 88 | + originalUrl: 'https://example.com/api/json?id=123', |
| 89 | + path: '/api/json?id=123', |
| 90 | + pathname: '/api/json', |
| 91 | + port: '', |
| 92 | + protocol: 'https:', |
| 93 | + query: undefined, |
| 94 | + search: '?id=123', |
| 95 | + }; |
| 96 | + |
| 97 | + const normalizedRequest = normalizeRemixRequest(request as unknown as RemixRequest); |
| 98 | + expect(normalizedRequest).toEqual(expected); |
| 99 | + }); |
| 100 | +}); |
0 commit comments