|
| 1 | +/** |
| 2 | + * E2E tests for the assertStatusCode option using a real HTTP fixture server. |
| 3 | + * |
| 4 | + * The html-input path produces snapshots with no `status` field, so the |
| 5 | + * assertion in CrawlerHost.iterSnapshots is silently skipped there. To |
| 6 | + * exercise the assertion proper, these tests drive the crawler against a |
| 7 | + * local fixture server with `engine: 'curl'`. |
| 8 | + * |
| 9 | + * URL note: `assertNormalizedUrl` in src/services/misc.ts rejects bare IP |
| 10 | + * literals in non-public ranges unconditionally, so the fixture URL uses |
| 11 | + * the `localhost` hostname (server binds dual-stack). |
| 12 | + */ |
| 13 | +import { describe, it, before, after } from 'node:test'; |
| 14 | +import assert from 'node:assert'; |
| 15 | +import { getAgent } from '../helpers/client'; |
| 16 | +import { startFixtureServer, FixtureServer } from '../helpers/fixture-server'; |
| 17 | + |
| 18 | +describe('assertStatusCode against real network fixture', () => { |
| 19 | + let fixture: FixtureServer; |
| 20 | + |
| 21 | + before(async () => { |
| 22 | + fixture = await startFixtureServer(); |
| 23 | + }); |
| 24 | + |
| 25 | + after(async () => { |
| 26 | + await fixture.close(); |
| 27 | + }); |
| 28 | + |
| 29 | + async function crawlUrl(body: Record<string, unknown>, headers: Record<string, string> = {}) { |
| 30 | + let req = getAgent() |
| 31 | + .post('/') |
| 32 | + .set('Accept', 'application/json') |
| 33 | + .set('Content-Type', 'application/json'); |
| 34 | + for (const [k, v] of Object.entries(headers)) { |
| 35 | + req = req.set(k, v); |
| 36 | + } |
| 37 | + return req.send(body); |
| 38 | + } |
| 39 | + |
| 40 | + // ── matching status: request succeeds ────────────────────────────────── |
| 41 | + |
| 42 | + it('200 fixture passes when assertStatusCode matches', async () => { |
| 43 | + const res = await crawlUrl({ |
| 44 | + url: fixture.url('/status/200'), |
| 45 | + engine: 'curl', |
| 46 | + assertStatusCode: 200, |
| 47 | + }); |
| 48 | + assert.strictEqual(res.status, 200); |
| 49 | + assert.match(res.body.data.content, /Status 200/); |
| 50 | + }); |
| 51 | + |
| 52 | + it('404 fixture passes when assertStatusCode: 404 matches the real status', async () => { |
| 53 | + const res = await crawlUrl({ |
| 54 | + url: fixture.url('/status/404'), |
| 55 | + engine: 'curl', |
| 56 | + assertStatusCode: 404, |
| 57 | + }); |
| 58 | + assert.strictEqual(res.status, 200); |
| 59 | + assert.match(res.body.data.content, /Status 404/); |
| 60 | + }); |
| 61 | + |
| 62 | + it('200 fixture without assertStatusCode still works (default behavior)', async () => { |
| 63 | + const res = await crawlUrl({ |
| 64 | + url: fixture.url('/status/200'), |
| 65 | + engine: 'curl', |
| 66 | + }); |
| 67 | + assert.strictEqual(res.status, 200); |
| 68 | + assert.match(res.body.data.content, /Status 200/); |
| 69 | + }); |
| 70 | + |
| 71 | + it('404 fixture without assertStatusCode does not throw (status check is opt-in)', async () => { |
| 72 | + const res = await crawlUrl({ |
| 73 | + url: fixture.url('/status/404'), |
| 74 | + engine: 'curl', |
| 75 | + }); |
| 76 | + assert.strictEqual(res.status, 200); |
| 77 | + assert.match(res.body.data.content, /Status 404/); |
| 78 | + }); |
| 79 | + |
| 80 | + // ── mismatching status: request rejected with AssertionFailureError ──── |
| 81 | + |
| 82 | + it('200 expected vs 404 actual throws AssertionFailureError (body form)', async () => { |
| 83 | + const res = await crawlUrl({ |
| 84 | + url: fixture.url('/status/404'), |
| 85 | + engine: 'curl', |
| 86 | + assertStatusCode: 200, |
| 87 | + }); |
| 88 | + assert.ok(res.status >= 400, `Expected error status, got ${res.status}`); |
| 89 | + assert.strictEqual(res.body.name, 'AssertionFailureError'); |
| 90 | + assert.match( |
| 91 | + res.body.message || res.body.readableMessage, |
| 92 | + /Expected status code 200 but got 404/ |
| 93 | + ); |
| 94 | + }); |
| 95 | + |
| 96 | + it('200 expected vs 500 actual throws AssertionFailureError', async () => { |
| 97 | + const res = await crawlUrl({ |
| 98 | + url: fixture.url('/status/500'), |
| 99 | + engine: 'curl', |
| 100 | + assertStatusCode: 200, |
| 101 | + }); |
| 102 | + assert.ok(res.status >= 400); |
| 103 | + assert.strictEqual(res.body.name, 'AssertionFailureError'); |
| 104 | + assert.match( |
| 105 | + res.body.message || res.body.readableMessage, |
| 106 | + /Expected status code 200 but got 500/ |
| 107 | + ); |
| 108 | + }); |
| 109 | + |
| 110 | + it('404 expected vs 200 actual throws AssertionFailureError', async () => { |
| 111 | + const res = await crawlUrl({ |
| 112 | + url: fixture.url('/status/200'), |
| 113 | + engine: 'curl', |
| 114 | + assertStatusCode: 404, |
| 115 | + }); |
| 116 | + assert.ok(res.status >= 400); |
| 117 | + assert.strictEqual(res.body.name, 'AssertionFailureError'); |
| 118 | + assert.match( |
| 119 | + res.body.message || res.body.readableMessage, |
| 120 | + /Expected status code 404 but got 200/ |
| 121 | + ); |
| 122 | + }); |
| 123 | + |
| 124 | + // ── header form behaves the same ──────────────────────────────────────── |
| 125 | + |
| 126 | + it('X-Assert-Status-Code header: match passes', async () => { |
| 127 | + const res = await crawlUrl( |
| 128 | + { url: fixture.url('/status/200'), engine: 'curl' }, |
| 129 | + { 'X-Assert-Status-Code': '200' } |
| 130 | + ); |
| 131 | + assert.strictEqual(res.status, 200); |
| 132 | + }); |
| 133 | + |
| 134 | + it('X-Assert-Status-Code header: mismatch throws AssertionFailureError', async () => { |
| 135 | + const res = await crawlUrl( |
| 136 | + { url: fixture.url('/status/500'), engine: 'curl' }, |
| 137 | + { 'X-Assert-Status-Code': '200' } |
| 138 | + ); |
| 139 | + assert.ok(res.status >= 400); |
| 140 | + assert.strictEqual(res.body.name, 'AssertionFailureError'); |
| 141 | + assert.match( |
| 142 | + res.body.message || res.body.readableMessage, |
| 143 | + /Expected status code 200 but got 500/ |
| 144 | + ); |
| 145 | + }); |
| 146 | + |
| 147 | + it('X-Assert-Status-Code: foo (non-numeric) is ignored — request succeeds despite 404', async () => { |
| 148 | + const res = await crawlUrl( |
| 149 | + { url: fixture.url('/status/404'), engine: 'curl' }, |
| 150 | + { 'X-Assert-Status-Code': 'foo' } |
| 151 | + ); |
| 152 | + assert.strictEqual(res.status, 200); |
| 153 | + assert.match(res.body.data.content, /Status 404/); |
| 154 | + }); |
| 155 | +}); |
0 commit comments