Skip to content

Extract proxy server into testable modules#1632

Merged
kmcginnes merged 3 commits intoaws:mainfrom
kmcginnes:proxy-server-testability
Apr 6, 2026
Merged

Extract proxy server into testable modules#1632
kmcginnes merged 3 commits intoaws:mainfrom
kmcginnes:proxy-server-testability

Conversation

@kmcginnes
Copy link
Copy Markdown
Collaborator

@kmcginnes kmcginnes commented Apr 3, 2026

Description

Refactors the monolithic node-server.ts in the proxy server package into three focused, independently testable modules:

  • createApp — Express app setup with all routes and middleware, extracted from node-server.ts so route handlers can be tested with supertest without starting a real server
  • createServer — HTTP/HTTPS server creation decision, separated so the TLS branching logic can be tested in isolation
  • resolveServerConfig — Server configuration resolution (port selection, HTTPS detection, certificate paths, base URL construction), extracted so config logic can be unit tested without side effects

node-server.ts becomes a thin orchestrator that wires these modules together.

What changed

  • Extracted createApp() with all route handlers, middleware, and error handling into app.ts
  • Extracted createServer() for HTTP vs HTTPS server creation into server.ts
  • Extracted resolveServerConfig() and buildBaseUrl() into server-config.ts
  • Added global test setup (setupTests.ts) with credential safety mocks to prevent real AWS credentials or HTTP requests from leaking in tests
  • Removed dead code: unused app.locals.env assignment, unreachable else branch for static file virtual path
  • Added 121 total tests across the proxy server package (up from 56)

No behavioral changes

All route logic, middleware ordering, IAM signing, query cancellation, and error handling are preserved exactly. The refactoring is purely structural.

Validation

  • pnpm checks passes (lint, format, types)
  • pnpm test passes with 121 tests across 7 test files
  • Each commit is independently green (checks + tests)

Related Issues

Check List

  • I confirm that my contribution is made under the terms of the Apache 2.0 license.
  • I have verified pnpm checks passes with no errors.
  • I have verified pnpm test passes with no failures.
  • I have covered new added functionality with unit tests if necessary.
  • I have updated documentation if necessary.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 3, 2026

Codecov Report

❌ Patch coverage is 85.65401% with 34 lines in your changes missing coverage. Please review.
✅ Project coverage is 62.88%. Comparing base (c5affc5) to head (c794295).
⚠️ Report is 116 commits behind head on main.

Files with missing lines Patch % Lines
packages/graph-explorer-proxy-server/src/app.ts 84.33% 31 Missing and 3 partials ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #1632       +/-   ##
===========================================
+ Coverage   47.81%   62.88%   +15.07%     
===========================================
  Files         382      393       +11     
  Lines        8525     9218      +693     
  Branches     3159     3511      +352     
===========================================
+ Hits         4076     5797     +1721     
+ Misses       3070     2373      -697     
+ Partials     1379     1048      -331     
Flag Coverage Δ
unittests 62.88% <85.65%> (+15.07%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

.post("/logger")
.set("level", "verbose")
.set("message", JSON.stringify("msg"));
expect(response.status).toBe(500);
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue created to deal with oddness of this endpoint.

const response = await request(app)
.post(`/${route}`)
.send({ query: "test query" });
expect(response.status).toBe(500);
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kmcginnes kmcginnes force-pushed the proxy-server-testability branch from 5d52f4f to 99ee896 Compare April 3, 2026 18:15
Comment on lines +79 to +87
it("sets useHttps to false when HTTPS is enabled but cert files do not exist", () => {
vi.spyOn(fs, "existsSync").mockReturnValue(false);

const config = resolveServerConfig(
createEnv({ PROXY_SERVER_HTTPS_CONNECTION: true }),
);

expect(config.useHttps).toBe(false);
});
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kmcginnes kmcginnes force-pushed the proxy-server-testability branch from 99ee896 to c794295 Compare April 3, 2026 20:45
@kmcginnes kmcginnes marked this pull request as ready for review April 3, 2026 23:17
app.post("/sparql", async (req, res, next) => {
const logger = getLogger();
// Gather info from the headers
const headers = req.headers as DbQueryIncomingHttpHeaders;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the next 10 lines feel like something to be extracted into its own middleware but I understand that this is more structural refactoring, so I guess, this is not in scope

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. I'll likely push all the header parsing in to a utility using Zod for validation when I work on:

@kmcginnes kmcginnes merged commit 8096949 into aws:main Apr 6, 2026
4 checks passed
@kmcginnes kmcginnes deleted the proxy-server-testability branch April 6, 2026 19:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants