|
| 1 | +import { createApiAdapter, isBinaryResponseEnvelope } from './http-client'; |
| 2 | + |
| 3 | +import type { Adapter, AdapterMethod } from './types'; |
| 4 | + |
| 5 | +/** |
| 6 | + * A real 1x1 red PNG. Its first byte is 0x89, which is not valid UTF-8 — the |
| 7 | + * exact kind of byte that `response.text()` corrupts into U+FFFD. This is the |
| 8 | + * regression fixture for the binary-response corruption bug. |
| 9 | + */ |
| 10 | +const PNG_BASE64 = |
| 11 | + 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=='; |
| 12 | +const PNG_BYTES = Buffer.from(PNG_BASE64, 'base64'); |
| 13 | + |
| 14 | +const CONFIG = { dotcmsUrl: 'https://example.dotcms.com', authToken: 'test-token' }; |
| 15 | + |
| 16 | +function getRequestMethod(adapter: Adapter): AdapterMethod { |
| 17 | + const method = adapter.methods.get('request'); |
| 18 | + if (!method) { |
| 19 | + throw new Error('request method not registered'); |
| 20 | + } |
| 21 | + return method; |
| 22 | +} |
| 23 | + |
| 24 | +/** Build a Response-like stub backed by a fixed body buffer. */ |
| 25 | +function makeResponse( |
| 26 | + body: Buffer | string, |
| 27 | + { |
| 28 | + contentType, |
| 29 | + ok = true, |
| 30 | + status = 200, |
| 31 | + statusText = 'OK', |
| 32 | + contentLength |
| 33 | + }: { |
| 34 | + contentType: string; |
| 35 | + ok?: boolean; |
| 36 | + status?: number; |
| 37 | + statusText?: string; |
| 38 | + // Override the Content-Length header independently of the actual body — |
| 39 | + // lets us simulate a server that advertises an oversized response. |
| 40 | + contentLength?: string; |
| 41 | + } |
| 42 | +): Response { |
| 43 | + const buffer = typeof body === 'string' ? Buffer.from(body, 'utf-8') : body; |
| 44 | + const headerValues: Record<string, string | null> = { |
| 45 | + 'content-type': contentType, |
| 46 | + 'content-length': contentLength ?? String(buffer.byteLength) |
| 47 | + }; |
| 48 | + return { |
| 49 | + ok, |
| 50 | + status, |
| 51 | + statusText, |
| 52 | + headers: { get: (name: string) => headerValues[name.toLowerCase()] ?? null }, |
| 53 | + json: async () => JSON.parse(buffer.toString('utf-8')), |
| 54 | + text: async () => buffer.toString('utf-8'), |
| 55 | + arrayBuffer: async () => |
| 56 | + buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength) |
| 57 | + } as unknown as Response; |
| 58 | +} |
| 59 | + |
| 60 | +describe('createApiAdapter response parsing', () => { |
| 61 | + const fetchMock = jest.fn(); |
| 62 | + |
| 63 | + beforeEach(() => { |
| 64 | + fetchMock.mockReset(); |
| 65 | + global.fetch = fetchMock as unknown as typeof fetch; |
| 66 | + }); |
| 67 | + |
| 68 | + it('round-trips a binary (PNG) body as a base64 envelope without corrupting bytes', async () => { |
| 69 | + fetchMock.mockResolvedValue(makeResponse(PNG_BYTES, { contentType: 'image/png' })); |
| 70 | + |
| 71 | + const adapter = createApiAdapter(CONFIG); |
| 72 | + const result = await getRequestMethod(adapter).execute({ path: '/dA/abc123' }); |
| 73 | + |
| 74 | + expect(isBinaryResponseEnvelope(result)).toBe(true); |
| 75 | + const envelope = result as { |
| 76 | + __dotcmsBinary: true; |
| 77 | + contentType: string; |
| 78 | + base64: string; |
| 79 | + byteLength: number; |
| 80 | + }; |
| 81 | + expect(envelope.contentType).toBe('image/png'); |
| 82 | + expect(envelope.byteLength).toBe(PNG_BYTES.byteLength); |
| 83 | + // The decoded bytes must be byte-exact to the source — the actual regression guard. |
| 84 | + expect(Buffer.from(envelope.base64, 'base64').equals(PNG_BYTES)).toBe(true); |
| 85 | + }); |
| 86 | + |
| 87 | + it('parses JSON content as an object', async () => { |
| 88 | + fetchMock.mockResolvedValue( |
| 89 | + makeResponse(JSON.stringify({ hello: 'world' }), { contentType: 'application/json' }) |
| 90 | + ); |
| 91 | + |
| 92 | + const adapter = createApiAdapter(CONFIG); |
| 93 | + const result = await getRequestMethod(adapter).execute({ path: '/api/v1/x' }); |
| 94 | + |
| 95 | + expect(result).toEqual({ hello: 'world' }); |
| 96 | + }); |
| 97 | + |
| 98 | + it('returns textual content types as strings', async () => { |
| 99 | + fetchMock.mockResolvedValue( |
| 100 | + makeResponse('<root/>', { contentType: 'application/xml; charset=utf-8' }) |
| 101 | + ); |
| 102 | + |
| 103 | + const adapter = createApiAdapter(CONFIG); |
| 104 | + const result = await getRequestMethod(adapter).execute({ path: '/api/x.xml' }); |
| 105 | + |
| 106 | + expect(result).toBe('<root/>'); |
| 107 | + }); |
| 108 | + |
| 109 | + it('treats +json content types as textual strings', async () => { |
| 110 | + fetchMock.mockResolvedValue( |
| 111 | + makeResponse('{"a":1}', { contentType: 'application/vnd.api+json' }) |
| 112 | + ); |
| 113 | + |
| 114 | + const adapter = createApiAdapter(CONFIG); |
| 115 | + const result = await getRequestMethod(adapter).execute({ path: '/api/x' }); |
| 116 | + |
| 117 | + expect(result).toBe('{"a":1}'); |
| 118 | + }); |
| 119 | + |
| 120 | + it('forces the binary path when responseType is "base64", even for JSON', async () => { |
| 121 | + fetchMock.mockResolvedValue( |
| 122 | + makeResponse(JSON.stringify({ hello: 'world' }), { contentType: 'application/json' }) |
| 123 | + ); |
| 124 | + |
| 125 | + const adapter = createApiAdapter(CONFIG); |
| 126 | + const result = await getRequestMethod(adapter).execute({ |
| 127 | + path: '/api/v1/x', |
| 128 | + responseType: 'base64' |
| 129 | + }); |
| 130 | + |
| 131 | + expect(isBinaryResponseEnvelope(result)).toBe(true); |
| 132 | + }); |
| 133 | + |
| 134 | + it('reads the error body as text regardless of content-type', async () => { |
| 135 | + fetchMock.mockResolvedValue( |
| 136 | + makeResponse('<html>Not Found</html>', { |
| 137 | + contentType: 'text/html', |
| 138 | + ok: false, |
| 139 | + status: 404, |
| 140 | + statusText: 'Not Found' |
| 141 | + }) |
| 142 | + ); |
| 143 | + |
| 144 | + const adapter = createApiAdapter(CONFIG); |
| 145 | + await expect(getRequestMethod(adapter).execute({ path: '/dA/missing' })).rejects.toThrow( |
| 146 | + 'HTTP 404 Not Found: <html>Not Found</html>' |
| 147 | + ); |
| 148 | + }); |
| 149 | + |
| 150 | + it('rejects an oversized binary response via Content-Length before buffering', async () => { |
| 151 | + const oversized = String(26 * 1024 * 1024); // 26MB > 25MB cap |
| 152 | + const arrayBuffer = jest.fn(); |
| 153 | + fetchMock.mockResolvedValue({ |
| 154 | + ok: true, |
| 155 | + status: 200, |
| 156 | + statusText: 'OK', |
| 157 | + headers: { |
| 158 | + get: (name: string) => |
| 159 | + name.toLowerCase() === 'content-type' |
| 160 | + ? 'application/octet-stream' |
| 161 | + : name.toLowerCase() === 'content-length' |
| 162 | + ? oversized |
| 163 | + : null |
| 164 | + }, |
| 165 | + arrayBuffer |
| 166 | + } as unknown as Response); |
| 167 | + |
| 168 | + const adapter = createApiAdapter(CONFIG); |
| 169 | + await expect(getRequestMethod(adapter).execute({ path: '/dA/huge' })).rejects.toThrow( |
| 170 | + 'exceeds the' |
| 171 | + ); |
| 172 | + // The body must never be buffered when Content-Length already exceeds the cap. |
| 173 | + expect(arrayBuffer).not.toHaveBeenCalled(); |
| 174 | + }); |
| 175 | + |
| 176 | + describe('isBinaryResponseEnvelope', () => { |
| 177 | + it('accepts a fully-formed envelope', () => { |
| 178 | + expect( |
| 179 | + isBinaryResponseEnvelope({ |
| 180 | + __dotcmsBinary: true, |
| 181 | + contentType: 'image/png', |
| 182 | + base64: 'AA==', |
| 183 | + byteLength: 1 |
| 184 | + }) |
| 185 | + ).toBe(true); |
| 186 | + }); |
| 187 | + |
| 188 | + it('rejects an envelope missing contentType or byteLength', () => { |
| 189 | + expect(isBinaryResponseEnvelope({ __dotcmsBinary: true, base64: 'AA==' })).toBe(false); |
| 190 | + expect( |
| 191 | + isBinaryResponseEnvelope({ |
| 192 | + __dotcmsBinary: true, |
| 193 | + base64: 'AA==', |
| 194 | + contentType: 'image/png' |
| 195 | + }) |
| 196 | + ).toBe(false); |
| 197 | + }); |
| 198 | + |
| 199 | + it('rejects non-envelope values', () => { |
| 200 | + expect(isBinaryResponseEnvelope(null)).toBe(false); |
| 201 | + expect(isBinaryResponseEnvelope('string')).toBe(false); |
| 202 | + expect(isBinaryResponseEnvelope({ hello: 'world' })).toBe(false); |
| 203 | + }); |
| 204 | + }); |
| 205 | +}); |
0 commit comments