-
Notifications
You must be signed in to change notification settings - Fork 1
test(wallet-service): isolate tests making real fullnode HTTP calls #394
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
andreabadesso
merged 7 commits into
master
from
fix/txproposal-utxo-unlock-test-network-isolation
Apr 24, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cb2c399
test(wallet-service): isolate txProposalUtxoUnlock test from network
andreabadesso d194d58
test(wallet-service): isolate POST /tx/proposal CORS test from network
andreabadesso 2fb5ba5
test(wallet-service): block outbound HTTP + add seedFullnodeVersionDa…
andreabadesso f01d1df
test(wallet-service): tighten outbound HTTP blocker
andreabadesso 08f9706
Merge branch 'master' into fix/txproposal-utxo-unlock-test-network-is…
andreabadesso d9e4341
test(wallet-service): read NETWORK at call time in defaultTestVersion…
andreabadesso 05522c2
Merge branch 'master' into fix/txproposal-utxo-unlock-test-network-is…
andreabadesso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,99 @@ | ||
| /* eslint-disable @typescript-eslint/no-empty-function */ | ||
| import http from 'http'; | ||
| import https from 'https'; | ||
| import { config } from 'dotenv'; | ||
| import { stopGLLBackgroundTask } from '@hathor/wallet-lib'; | ||
|
|
||
| Object.defineProperty(global, '_bitcore', { get() { return undefined; }, set() {} }); | ||
|
|
||
| stopGLLBackgroundTask(); | ||
| config(); | ||
|
|
||
| /** | ||
| * Block all real outbound HTTP/HTTPS requests from unit tests. | ||
| * | ||
| * Tests must mock their network dependencies. If a test accidentally reaches | ||
| * this point, it means a code path escaped mocking (e.g. a handler calls | ||
| * `fullnode.version()` without the `version_data` DB cache being seeded, or | ||
| * a direct `axios.get(...)` without a `jest.spyOn` / `jest.mock`). We throw | ||
| * a loud, explanatory error instead of silently hitting the public internet. | ||
| * | ||
| * Hosts listed in `ALLOWED_HOSTS` are permitted — the list is intentionally | ||
| * empty for the wallet-service unit suite. Integration tests use a separate | ||
| * jest config and should opt in explicitly if they need real connections. | ||
| */ | ||
| const ALLOWED_HOSTS = new Set<string>([]); | ||
|
|
||
| type RequestArg = string | URL | http.RequestOptions; | ||
|
|
||
| const normalizeHost = (host: string | undefined): string => { | ||
| if (!host) return '<unknown>'; | ||
| // `host` may include a port (e.g. `localhost:3000`) and IPv6 forms may be | ||
| // bracketed (e.g. `[::1]:3000`). Let URL parsing strip both consistently — | ||
| // ALLOWED_HOSTS is keyed by hostname only. | ||
| try { | ||
| return new URL(`http://${host}`).hostname; | ||
| } catch { | ||
| return host; | ||
| } | ||
| }; | ||
|
|
||
| const extractHostname = (arg: RequestArg | undefined): string => { | ||
| if (!arg) return '<unknown>'; | ||
| if (typeof arg === 'string') { | ||
| try { | ||
| return new URL(arg).hostname; | ||
| } catch { | ||
| return arg; | ||
| } | ||
| } | ||
| if (arg instanceof URL) return arg.hostname; | ||
| return arg.hostname || normalizeHost(arg.host); | ||
| }; | ||
|
|
||
| const describeRequest = (protocol: 'http' | 'https', arg: RequestArg | undefined): string => { | ||
| if (!arg) return `${protocol}://<unknown>`; | ||
| if (typeof arg === 'string') return arg; | ||
| if (arg instanceof URL) return arg.toString(); | ||
| const host = arg.host || arg.hostname || '<unknown>'; | ||
| const path = arg.path || '/'; | ||
| return `${protocol}://${host}${path}`; | ||
| }; | ||
|
|
||
| const blockRequest = (protocol: 'http' | 'https', originalRequest: typeof http.request) => ( | ||
| (...args: unknown[]) => { | ||
| const firstArg = args[0] as RequestArg | undefined; | ||
| const hostname = extractHostname(firstArg); | ||
| if (ALLOWED_HOSTS.has(hostname)) { | ||
| // @ts-ignore - passthrough | ||
| return originalRequest(...args); | ||
| } | ||
| throw new Error( | ||
| `[jestSetup] Blocked outbound ${protocol.toUpperCase()} request to ${describeRequest(protocol, firstArg)}. ` | ||
| + 'Tests must not make real network calls. Mock the HTTP client ' | ||
| + '(jest.spyOn / jest.mock) or seed the relevant DB cache (see ' | ||
| + 'tests/utils.ts#seedFullnodeVersionData).', | ||
| ); | ||
| } | ||
| ); | ||
|
|
||
| // Node's `http.get` / `https.get` keep an internal reference to the original | ||
| // `http.request`, so patching only `request` would leave `get` as a bypass. | ||
| // Delegate `get` through the patched `request` and call `.end()` ourselves to | ||
| // preserve the stock `get()` behavior for any allow-listed host. | ||
| const blockGet = (blockedRequest: typeof http.request) => ( | ||
| (...args: unknown[]) => { | ||
| // @ts-ignore - passthrough to wrapped request overloads | ||
| const req = blockedRequest(...args); | ||
| req.end(); | ||
| return req; | ||
| } | ||
| ); | ||
|
|
||
| const blockedHttpRequest = blockRequest('http', http.request) as typeof http.request; | ||
| const blockedHttpsRequest = blockRequest('https', https.request) as typeof https.request; | ||
|
|
||
| http.request = blockedHttpRequest; | ||
| https.request = blockedHttpsRequest; | ||
| http.get = blockGet(blockedHttpRequest) as typeof http.get; | ||
| https.get = blockGet(blockedHttpsRequest) as typeof https.get; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.