|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { test } = require('node:test') |
| 4 | +const { MockAgent, interceptors } = require('..') |
| 5 | +const DecoratorHandler = require('../lib/handler/decorator-handler') |
| 6 | +const { tspl } = require('@matteo.collina/tspl') |
| 7 | + |
| 8 | +test('MockAgent with delayed response and AbortSignal should not cause uncaught errors', async (t) => { |
| 9 | + const p = tspl(t, { plan: 2 }) |
| 10 | + |
| 11 | + const agent = new MockAgent() |
| 12 | + t.after(() => agent.close()) |
| 13 | + |
| 14 | + const mockPool = agent.get('https://example.com') |
| 15 | + mockPool.intercept({ path: '/test', method: 'GET' }) |
| 16 | + .reply(200, { success: true }, { headers: { 'content-type': 'application/json' } }) |
| 17 | + .delay(100) |
| 18 | + |
| 19 | + const ac = new AbortController() |
| 20 | + |
| 21 | + // Abort the request after 10ms |
| 22 | + setTimeout(() => { |
| 23 | + ac.abort(new Error('Request aborted')) |
| 24 | + }, 10) |
| 25 | + |
| 26 | + try { |
| 27 | + await agent.request({ |
| 28 | + origin: 'https://example.com', |
| 29 | + path: '/test', |
| 30 | + method: 'GET', |
| 31 | + signal: ac.signal |
| 32 | + }) |
| 33 | + p.fail('Should have thrown an error') |
| 34 | + } catch (err) { |
| 35 | + p.ok(err.message === 'Request aborted' || err.name === 'AbortError', 'Error should be related to abort') |
| 36 | + } |
| 37 | + |
| 38 | + // Wait for the delayed response to fire - should not cause any uncaught errors |
| 39 | + await new Promise(resolve => setTimeout(resolve, 150)) |
| 40 | + |
| 41 | + p.ok(true, 'No uncaught errors after delayed response') |
| 42 | +}) |
| 43 | + |
| 44 | +test('MockAgent with delayed response and composed interceptor (decompress) should not cause uncaught errors', async (t) => { |
| 45 | + const p = tspl(t, { plan: 2 }) |
| 46 | + |
| 47 | + // The decompress interceptor has assertions that fail if onResponseStart is called after onError |
| 48 | + const agent = new MockAgent().compose(interceptors.decompress()) |
| 49 | + t.after(() => agent.close()) |
| 50 | + |
| 51 | + const mockPool = agent.get('https://example.com') |
| 52 | + mockPool.intercept({ path: '/test', method: 'GET' }) |
| 53 | + .reply(200, { success: true }, { headers: { 'content-type': 'application/json' } }) |
| 54 | + .delay(100) |
| 55 | + |
| 56 | + const ac = new AbortController() |
| 57 | + |
| 58 | + // Abort the request after 10ms |
| 59 | + setTimeout(() => { |
| 60 | + ac.abort(new Error('Request aborted')) |
| 61 | + }, 10) |
| 62 | + |
| 63 | + try { |
| 64 | + await agent.request({ |
| 65 | + origin: 'https://example.com', |
| 66 | + path: '/test', |
| 67 | + method: 'GET', |
| 68 | + signal: ac.signal |
| 69 | + }) |
| 70 | + p.fail('Should have thrown an error') |
| 71 | + } catch (err) { |
| 72 | + p.ok(err.message === 'Request aborted' || err.name === 'AbortError', 'Error should be related to abort') |
| 73 | + } |
| 74 | + |
| 75 | + // Wait for the delayed response to fire - should not cause any uncaught errors |
| 76 | + await new Promise(resolve => setTimeout(resolve, 150)) |
| 77 | + |
| 78 | + p.ok(true, 'No uncaught errors after delayed response') |
| 79 | +}) |
| 80 | + |
| 81 | +test('MockAgent with delayed response and DecoratorHandler should not call onResponseStart after onError', async (t) => { |
| 82 | + const p = tspl(t, { plan: 2 }) |
| 83 | + |
| 84 | + class TestDecoratorHandler extends DecoratorHandler { |
| 85 | + #onErrorCalled = false |
| 86 | + |
| 87 | + onResponseStart (controller, statusCode, headers, statusMessage) { |
| 88 | + if (this.#onErrorCalled) { |
| 89 | + p.fail('onResponseStart should not be called after onError') |
| 90 | + } |
| 91 | + return super.onResponseStart(controller, statusCode, headers, statusMessage) |
| 92 | + } |
| 93 | + |
| 94 | + onResponseError (controller, err) { |
| 95 | + this.#onErrorCalled = true |
| 96 | + return super.onResponseError(controller, err) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + const agent = new MockAgent() |
| 101 | + t.after(() => agent.close()) |
| 102 | + |
| 103 | + const mockPool = agent.get('https://example.com') |
| 104 | + mockPool.intercept({ path: '/test', method: 'GET' }) |
| 105 | + .reply(200, { success: true }, { headers: { 'content-type': 'application/json' } }) |
| 106 | + .delay(100) |
| 107 | + |
| 108 | + const ac = new AbortController() |
| 109 | + |
| 110 | + // Abort the request after 10ms |
| 111 | + setTimeout(() => { |
| 112 | + ac.abort(new Error('Request aborted')) |
| 113 | + }, 10) |
| 114 | + |
| 115 | + const originalDispatch = agent.dispatch.bind(agent) |
| 116 | + agent.dispatch = (opts, handler) => { |
| 117 | + const decoratedHandler = new TestDecoratorHandler(handler) |
| 118 | + return originalDispatch(opts, decoratedHandler) |
| 119 | + } |
| 120 | + |
| 121 | + try { |
| 122 | + await agent.request({ |
| 123 | + origin: 'https://example.com', |
| 124 | + path: '/test', |
| 125 | + method: 'GET', |
| 126 | + signal: ac.signal |
| 127 | + }) |
| 128 | + p.fail('Should have thrown an error') |
| 129 | + } catch (err) { |
| 130 | + p.ok(err.message === 'Request aborted' || err.name === 'AbortError', 'Error should be related to abort') |
| 131 | + } |
| 132 | + |
| 133 | + // Wait for the delayed response to fire |
| 134 | + await new Promise(resolve => setTimeout(resolve, 150)) |
| 135 | + |
| 136 | + p.ok(true, 'Decorator handler invariants maintained') |
| 137 | +}) |
0 commit comments