|
| 1 | +import { Buffer } from 'buffer'; |
| 2 | +import { test, expect } from '../../playwright'; |
| 3 | + |
| 4 | +// A small SVG whose bytes the sniffer classifies as image/svg+xml. Small SVG bodies are kept as |
| 5 | +// selectable text (no base64 copy), so the preview must build the <img> src from that text — |
| 6 | +// regression: it used to read the absent base64 buffer and render "base64,undefined". |
| 7 | +const SVG_BODY |
| 8 | + = '<?xml version="1.0" encoding="UTF-8"?>\n' |
| 9 | + + '<svg xmlns="http://www.w3.org/2000/svg" width="400" height="300" viewBox="0 0 400 300">' |
| 10 | + + '<rect width="400" height="300" fill="red"/></svg>'; |
| 11 | +const INLINE_SVG_SRC = `data:image/svg+xml;base64,${Buffer.from(SVG_BODY).toString('base64')}`; |
| 12 | + |
| 13 | +test.describe('response body — SVG preview', () => { |
| 14 | + test.use({ viewport: { width: 1280, height: 900 } }); |
| 15 | + |
| 16 | + test.beforeEach(async ({ page }) => { |
| 17 | + await page.route('**/api/users**', (route) => |
| 18 | + route.fulfill({ |
| 19 | + status: 200, |
| 20 | + headers: { 'content-type': 'image/svg+xml' }, |
| 21 | + body: SVG_BODY |
| 22 | + }) |
| 23 | + ); |
| 24 | + }); |
| 25 | + |
| 26 | + test('renders the SVG image from its inline markup, not a broken data URL', async ({ |
| 27 | + page, |
| 28 | + playground, |
| 29 | + responsePane |
| 30 | + }) => { |
| 31 | + await page.goto('/#/?pg=1&dock=bottom'); |
| 32 | + await playground.openSidebarItem('get users'); |
| 33 | + await responsePane.send(); |
| 34 | + |
| 35 | + // An SVG is an image, so the pane defaults to preview and renders an <img>. |
| 36 | + await expect(responsePane.previewImage).toBeVisible(); |
| 37 | + |
| 38 | + // The src is the SVG markup encoded inline — not the "base64,undefined" the bug produced. |
| 39 | + await expect(responsePane.previewImage).toHaveAttribute('src', INLINE_SVG_SRC); |
| 40 | + |
| 41 | + // And the browser actually decoded it: a broken data URL leaves naturalWidth at 0. |
| 42 | + await expect |
| 43 | + .poll(() => |
| 44 | + responsePane.previewImage.evaluate( |
| 45 | + (img: HTMLImageElement) => img.complete && img.naturalWidth > 0 |
| 46 | + ) |
| 47 | + ) |
| 48 | + .toBe(true); |
| 49 | + }); |
| 50 | +}); |
0 commit comments