|
| 1 | +/** |
| 2 | + * @jest-environment jsdom |
| 3 | + * |
| 4 | + * The HTTP status is authoritative, and the body may not be the host's at all. |
| 5 | + * |
| 6 | + * A proxy, tunnel or captive portal answers with an HTML error page. Parsing that as |
| 7 | + * JSON first turned a plain "502 Bad Gateway" into "Unexpected token '<'", which names |
| 8 | + * the parser rather than the thing that went wrong, and sent whoever hit it looking in |
| 9 | + * the wrong place. The mirror case is a 200 with an empty body: json() rejects, so a |
| 10 | + * save that had actually landed was reported as failed. |
| 11 | + * |
| 12 | + * clayjs has read the body as text and parsed it defensively for a while |
| 13 | + * (src/core/save-core.js). This is the same shape, brought across. |
| 14 | + */ |
| 15 | + |
| 16 | +jest.mock('../src/core/isAdminOfCurrentResource.js', () => ({ isEditMode: true })); |
| 17 | + |
| 18 | +jest.mock('../src/core/snapshot.js', () => ({ |
| 19 | + captureForSave: jest.fn(() => '<html>captured</html>'), |
| 20 | + isCodeMirrorPage: jest.fn(() => false), |
| 21 | + getCodeMirrorContents: jest.fn(() => ''), |
| 22 | + beforeSave: jest.fn(), |
| 23 | + getPageContents: jest.fn(), |
| 24 | + onSnapshot: jest.fn(), |
| 25 | + onPrepareForSave: jest.fn(), |
| 26 | +})); |
| 27 | + |
| 28 | +import { savePage } from '../src/core/savePageCore.js'; |
| 29 | + |
| 30 | +const respond = (fields) => { |
| 31 | + global.fetch = jest.fn(() => Promise.resolve(fields)); |
| 32 | +}; |
| 33 | + |
| 34 | +let logged; |
| 35 | + |
| 36 | +beforeEach(() => { |
| 37 | + logged = jest.spyOn(console, 'error').mockImplementation(() => {}); |
| 38 | +}); |
| 39 | + |
| 40 | +afterEach(() => { |
| 41 | + jest.clearAllMocks(); |
| 42 | +}); |
| 43 | + |
| 44 | +test("an intermediary's HTML error page reports the status, not a parse error", async () => { |
| 45 | + respond({ |
| 46 | + ok: false, |
| 47 | + status: 502, |
| 48 | + statusText: 'Bad Gateway', |
| 49 | + text: () => Promise.resolve('<!DOCTYPE html><html><body>502 Bad Gateway</body></html>'), |
| 50 | + }); |
| 51 | + |
| 52 | + await savePage(); |
| 53 | + |
| 54 | + const err = logged.mock.calls[0][1]; |
| 55 | + expect(err.message).toContain('502'); |
| 56 | + expect(err.message).not.toContain('JSON'); |
| 57 | + expect(err.message).not.toContain('Unexpected token'); |
| 58 | +}); |
| 59 | + |
| 60 | +test('a 2xx with an empty body is a save that landed, not a failure', async () => { |
| 61 | + respond({ ok: true, status: 200, statusText: 'OK', text: () => Promise.resolve('') }); |
| 62 | + |
| 63 | + const result = await savePage(); |
| 64 | + |
| 65 | + expect(logged).not.toHaveBeenCalled(); |
| 66 | + expect(result.msgType).toBe('success'); |
| 67 | +}); |
| 68 | + |
| 69 | +// The host's own error message still wins when it sent one. Tolerating a body that is |
| 70 | +// not ours must not mean ignoring the body that is. |
| 71 | +test("the host's own message is preferred over the bare status", async () => { |
| 72 | + respond({ |
| 73 | + ok: false, |
| 74 | + status: 403, |
| 75 | + statusText: 'Forbidden', |
| 76 | + text: () => Promise.resolve(JSON.stringify({ msg: 'You do not own this page' })), |
| 77 | + }); |
| 78 | + |
| 79 | + await savePage(); |
| 80 | + |
| 81 | + expect(logged.mock.calls[0][1].message).toBe('You do not own this page'); |
| 82 | +}); |
0 commit comments