Skip to content

Commit

Permalink
chore: use undici for rpc proxy tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zone117x committed Nov 19, 2024
1 parent 02a7138 commit 6ab509e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 16 deletions.
42 changes: 33 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
"strict-event-emitter-types": "2.0.0",
"tiny-secp256k1": "2.2.1",
"ts-unused-exports": "7.0.3",
"undici": "6.21.0",
"uuid": "8.3.2",
"ws": "7.5.10",
"zone-file": "2.0.0-beta.3"
Expand Down
2 changes: 0 additions & 2 deletions src/api/routes/core-node-rpc-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,7 @@ export const CoreNodeRpcProxyRouter: FastifyPluginAsync<
upstream: `http://${stacksNodeRpcEndpoint}`,
rewritePrefix: '/v2',
http2: false,
// Use the `node:http` agent for requests https://github.com/fastify/fastify-reply-from?tab=readme-ov-file#globalagent
globalAgent: true,
http: {},
preValidation: async (req, reply) => {
if (getReqUrl(req).pathname !== '/v2/transactions') {
return;
Expand Down
22 changes: 17 additions & 5 deletions tests/api/v2-proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as nock from 'nock';
import { DbBlock } from '../../src/datastore/common';
import { PgWriteStore } from '../../src/datastore/pg-write-store';
import { migrate } from '../utils/test-helpers';
import { MockAgent, setGlobalDispatcher, getGlobalDispatcher } from 'undici';

describe('v2-proxy tests', () => {
let db: PgWriteStore;
Expand Down Expand Up @@ -39,14 +40,20 @@ describe('v2-proxy tests', () => {
);
return [, () => restoreEnvVars()] as const;
},
() => {
const agent = new MockAgent();
const originalAgent = getGlobalDispatcher();
setGlobalDispatcher(agent);
return [agent, () => setGlobalDispatcher(originalAgent)] as const;
},
async () => {
const apiServer = await startApiServer({
datastore: db,
chainId: ChainID.Mainnet,
});
return [apiServer, apiServer.terminate] as const;
},
async (_, api) => {
async (_, mockAgent, api) => {
const primaryStubbedResponse = {
cost_scalar_change_by_byte: 0.00476837158203125,
estimated_cost: {
Expand Down Expand Up @@ -77,12 +84,17 @@ describe('v2-proxy tests', () => {
transaction_payload:
'021af942874ce525e87f21bbe8c121b12fac831d02f4086765742d696e666f0b7570646174652d696e666f00000000',
};
nock(`http://${primaryProxyEndpoint}`)
.post('/v2/fees/transaction')
.once()

mockAgent
.get(`http://${primaryProxyEndpoint}`)
.intercept({
path: '/v2/fees/transaction',
method: 'POST',
})
.reply(200, JSON.stringify(primaryStubbedResponse), {
'Content-Type': 'application/json',
headers: { 'Content-Type': 'application/json' },
});

const postTxReq = await supertest(api.server)
.post(`/v2/fees/transaction`)
.set('Content-Type', 'application/json')
Expand Down

0 comments on commit 6ab509e

Please sign in to comment.