Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/workflows/test_mock_server.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Test Mock Server

on:
pull_request:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup Bun
uses: oven-sh/setup-bun@v2
with:
bun-version: latest

- name: Install dependencies
working-directory: ./resources/mock-server
run: bun install

- name: Run tests
working-directory: ./resources/mock-server
run: bun run test
3 changes: 3 additions & 0 deletions resources/mock-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"module": "index.ts",
"type": "module",
"private": true,
"scripts": {
"test": "vitest run"
},
"devDependencies": {
"@types/bun": "latest",
"vitest": "^4.0.8"
Expand Down
8 changes: 4 additions & 4 deletions resources/mock-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ export async function record(
}
}

export async function replay(
export async function replay<T>(
client: Client,
makeRequests: () => Promise<void>
) {
makeRequests: () => Promise<T>
): Promise<T> {
const polly = getPolly(client, { mode: "replay" });

try {
await makeRequests();
return await makeRequests();
} finally {
await polly.stop();
}
Expand Down
26 changes: 13 additions & 13 deletions resources/mock-server/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { HeadersInit } from "bun";
import Fastify from "fastify";
import { getPolly, type Client } from "./index";
import { replay, type Client } from "./index";
import { recordAlgosdkRequests } from "./record";

export type ServerInstance = {
Expand Down Expand Up @@ -42,8 +42,6 @@ export async function startServer(client: Client): Promise<ServerInstance> {

// Catch-all proxy through PollyJS
fastify.all("/*", async (request, reply) => {
const polly = getPolly(client, { mode: "replay" });

const url = new URL(
request.url,
`http://localhost:${LOCALNET_PORTS[client]}`
Expand Down Expand Up @@ -75,19 +73,21 @@ export async function startServer(client: Client): Promise<ServerInstance> {

let response;
try {
response = await fetch(url, {
method: request.method,
headers: request.headers as HeadersInit,
body:
request.method !== "GET" && request.method !== "HEAD"
? JSON.stringify(request.body)
: undefined
});
response = await replay(
client,
async () =>
await fetch(url, {
method: request.method,
headers: request.headers as HeadersInit,
body:
request.method !== "GET" && request.method !== "HEAD"
? JSON.stringify(request.body)
: undefined
})
);
} catch (e) {
reply.status(500).send(JSON.stringify(e));
return;
} finally {
await polly.stop();
}

const data = await response.text();
Expand Down