|
| 1 | +import * as t from "tap"; |
| 2 | +import { addHook, removeHook, executeHooks, OutboundRequestInfo } from "./hooks"; |
| 3 | + |
| 4 | +t.test("it works", async (t) => { |
| 5 | + let hookOneCalls = 0; |
| 6 | + let hookTwoCalls = 0; |
| 7 | + |
| 8 | + const testRequest: OutboundRequestInfo = { |
| 9 | + url: new URL("https://example.com"), |
| 10 | + port: 443, |
| 11 | + method: "GET", |
| 12 | + }; |
| 13 | + |
| 14 | + function hook1(request: OutboundRequestInfo) { |
| 15 | + t.equal(request.url.href, "https://example.com/"); |
| 16 | + t.equal(request.port, 443); |
| 17 | + t.equal(request.method, "GET"); |
| 18 | + hookOneCalls++; |
| 19 | + } |
| 20 | + |
| 21 | + function hook2(request: OutboundRequestInfo) { |
| 22 | + t.equal(request.url.href, "https://example.com/"); |
| 23 | + t.equal(request.port, 443); |
| 24 | + t.equal(request.method, "GET"); |
| 25 | + hookTwoCalls++; |
| 26 | + } |
| 27 | + |
| 28 | + function hook3() { |
| 29 | + throw new Error("hook3 should not be called"); |
| 30 | + } |
| 31 | + |
| 32 | + t.same(hookOneCalls, 0, "hookOneCalls starts at 0"); |
| 33 | + t.same(hookTwoCalls, 0, "hookTwoCalls starts at 0"); |
| 34 | + |
| 35 | + executeHooks("beforeOutboundRequest", testRequest); |
| 36 | + |
| 37 | + t.same(hookOneCalls, 0, "hookOneCalls still at 0"); |
| 38 | + t.same(hookTwoCalls, 0, "hookTwoCalls still at 0"); |
| 39 | + |
| 40 | + addHook("beforeOutboundRequest", hook1); |
| 41 | + // @ts-expect-error some other hook is not defined in the types |
| 42 | + addHook("someOtherHook", hook3); |
| 43 | + executeHooks("beforeOutboundRequest", testRequest); |
| 44 | + |
| 45 | + t.equal(hookOneCalls, 1, "hook1 called once"); |
| 46 | + t.equal(hookTwoCalls, 0, "hook2 not called"); |
| 47 | + |
| 48 | + addHook("beforeOutboundRequest", hook2); |
| 49 | + executeHooks("beforeOutboundRequest", testRequest); |
| 50 | + |
| 51 | + t.equal(hookOneCalls, 2, "hook1 called twice"); |
| 52 | + t.equal(hookTwoCalls, 1, "hook2 called once"); |
| 53 | + |
| 54 | + removeHook("beforeOutboundRequest", hook1); |
| 55 | + executeHooks("beforeOutboundRequest", testRequest); |
| 56 | + |
| 57 | + t.equal(hookOneCalls, 2, "hook1 still called twice"); |
| 58 | + t.equal(hookTwoCalls, 2, "hook2 called twice"); |
| 59 | + |
| 60 | + removeHook("beforeOutboundRequest", hook2); |
| 61 | + executeHooks("beforeOutboundRequest", testRequest); |
| 62 | + |
| 63 | + t.equal(hookOneCalls, 2, "hook1 still called twice"); |
| 64 | + t.equal(hookTwoCalls, 2, "hook2 still called twice"); |
| 65 | +}); |
| 66 | + |
| 67 | +t.test("it handles errors gracefully", async (t) => { |
| 68 | + let successCalls = 0; |
| 69 | + |
| 70 | + function throwingHook() { |
| 71 | + throw new Error("This should be caught"); |
| 72 | + } |
| 73 | + |
| 74 | + function successHook() { |
| 75 | + successCalls++; |
| 76 | + } |
| 77 | + |
| 78 | + const testRequest: OutboundRequestInfo = { |
| 79 | + url: new URL("https://example.com"), |
| 80 | + port: 443, |
| 81 | + method: "POST", |
| 82 | + }; |
| 83 | + |
| 84 | + addHook("beforeOutboundRequest", throwingHook); |
| 85 | + addHook("beforeOutboundRequest", successHook); |
| 86 | + |
| 87 | + // Should not throw even though one hook throws |
| 88 | + t.doesNotThrow(() => { |
| 89 | + executeHooks("beforeOutboundRequest", testRequest); |
| 90 | + }); |
| 91 | + |
| 92 | + t.equal(successCalls, 1, "success hook still called despite error in other hook"); |
| 93 | + |
| 94 | + removeHook("beforeOutboundRequest", throwingHook); |
| 95 | + removeHook("beforeOutboundRequest", successHook); |
| 96 | +}); |
| 97 | + |
| 98 | +t.test("it handles async hooks with rejected promises", async (t) => { |
| 99 | + let asyncCalls = 0; |
| 100 | + |
| 101 | + async function asyncHook() { |
| 102 | + asyncCalls++; |
| 103 | + throw new Error("Async error"); |
| 104 | + } |
| 105 | + |
| 106 | + const testRequest: OutboundRequestInfo = { |
| 107 | + url: new URL("https://example.com"), |
| 108 | + port: 443, |
| 109 | + method: "DELETE", |
| 110 | + }; |
| 111 | + |
| 112 | + addHook("beforeOutboundRequest", asyncHook); |
| 113 | + |
| 114 | + // Should not throw even though async hook rejects |
| 115 | + t.doesNotThrow(() => { |
| 116 | + executeHooks("beforeOutboundRequest", testRequest); |
| 117 | + }); |
| 118 | + |
| 119 | + t.equal(asyncCalls, 1, "async hook was called"); |
| 120 | + |
| 121 | + removeHook("beforeOutboundRequest", asyncHook); |
| 122 | +}); |
| 123 | + |
| 124 | +t.test("it prevents duplicate hooks using Set", async (t) => { |
| 125 | + let hookCalls = 0; |
| 126 | + |
| 127 | + function hook() { |
| 128 | + hookCalls++; |
| 129 | + } |
| 130 | + |
| 131 | + const testRequest: OutboundRequestInfo = { |
| 132 | + url: new URL("https://example.com"), |
| 133 | + port: 443, |
| 134 | + method: "GET", |
| 135 | + }; |
| 136 | + |
| 137 | + addHook("beforeOutboundRequest", hook); |
| 138 | + addHook("beforeOutboundRequest", hook); // Try to add the same hook again |
| 139 | + |
| 140 | + executeHooks("beforeOutboundRequest", testRequest); |
| 141 | + |
| 142 | + t.equal(hookCalls, 1, "hook only called once despite being added twice"); |
| 143 | + |
| 144 | + removeHook("beforeOutboundRequest", hook); |
| 145 | +}); |
0 commit comments