Skip to content

chore: add E2E tests for graceful shutdown and Hono error handling#1213

Merged
chimurai merged 3 commits intomasterfrom
more-tests
Apr 25, 2026
Merged

chore: add E2E tests for graceful shutdown and Hono error handling#1213
chimurai merged 3 commits intomasterfrom
more-tests

Conversation

@chimurai
Copy link
Copy Markdown
Owner

@chimurai chimurai commented Apr 25, 2026

Summary by CodeRabbit

  • Tests

    • Added E2E tests for graceful shutdown behavior
    • Added E2E tests for proxy error handling
  • Refactor

    • Improved internal type safety for socket handling

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 25, 2026

📝 Walkthrough

Walkthrough

This PR refines the middleware's TypeScript server discovery typing by using a more specific socket cast, and introduces two E2E test suites: one verifying graceful shutdown behavior when the HTTP server closes, and another validating error handling in the Hono middleware integration.

Changes

Cohort / File(s) Summary
Middleware Typing
src/http-proxy-middleware.ts
Adjusts socket type casting for server discovery to use net.Socket & { server?: https.Server } instead of explicit https.Server annotation, refining TypeScript type inference without altering functionality.
E2E Test Suites
test/e2e/graceful-shutdown.spec.ts, test/e2e/hono.spec.ts
Adds two new E2E test files: one mocking httpxy to verify ProxyServer.close() is invoked exactly once when the HTTP server closes; another testing Hono middleware error handling by ensuring a thrown router callback results in HTTP 500 with Proxy Error response body.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

  • PR #1193: Modifies the same Hono E2E test file (test/e2e/hono.spec.ts) and introduces Hono-specific middleware support, indicating parallel or overlapping feature development.

Poem

🐰 Hops through the socket's gentle cast,
Gracefully we shut things down at last,
Error caught with Hono's care,
Proxy flows through typed air,
Tests ensure our shutdown's fair!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title accurately describes the main changes: two new E2E test suites for graceful shutdown and Hono error handling, matching the additions in the changeset.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch more-tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Apr 25, 2026

npm i https://pkg.pr.new/http-proxy-middleware@1213

commit: e8983c6

@coveralls
Copy link
Copy Markdown

Coverage Status

coverage: 94.78% (+0.8%) from 93.956% — more-tests into master

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (3)
test/e2e/graceful-shutdown.spec.ts (3)

29-48: Consider closing proxyServer in afterEach to avoid leaks on test failure.

proxyServer is started inside each it and only closed at the end of the test body. If an assertion fails (or closeServer is never reached), the listening server leaks across tests, which can cascade into port conflicts or hung Vitest workers. Moving the cleanup into afterEach makes the suite more robust.

♻️ Proposed cleanup
   afterEach(async () => {
+    if (proxyServer?.listening) {
+      await closeServer(proxyServer);
+    }
     await mockTargetServer.stop();
   });
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@test/e2e/graceful-shutdown.spec.ts` around lines 29 - 48, The tests can leak
the listening proxyServer if a test fails before calling closeServer; update the
cleanup to close any started proxyServer in afterEach by invoking
closeServer(proxyServer) when proxyServer is defined (and await it), and swallow
or rethrow errors as appropriate; locate the proxyServer variable and the
existing afterEach/mockTargetServer.stop sequence and add the proxyServer
shutdown there (and reset proxyServer to undefined after closing) so each test
always attempts to close the server even on failure.

61-61: Minor: prefer 127.0.0.1 for consistency.

Other E2E suites in this PR (e.g., test/e2e/hono.spec.ts) target http://127.0.0.1:${port}. Using localhost here can cause flakiness on dual-stack hosts where DNS resolves to ::1 while the server listens on IPv4 only. Aligning on 127.0.0.1 is safer and consistent with the rest of the suite.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@test/e2e/graceful-shutdown.spec.ts` at line 61, Replace the hardcoded
"localhost" host in the E2E request with the IPv4 loopback to avoid DNS/IPv6
flakiness: update the call that constructs the URL using SERVER_PORT (the
request(`http://localhost:${SERVER_PORT}`).get('/api').expect(200) invocation)
to use "127.0.0.1" instead of "localhost" so the test targets
http://127.0.0.1:${SERVER_PORT} and matches the other E2E suites.

79-79: Redundant mock setup.

mockTargetServer.forGet('/api').thenReply(200, 'OK') is already configured in beforeEach (line 42). This duplicate registration adds another handler for the same path; it's harmless here but obscures intent. Safe to remove.

♻️ Proposed fix
-    await mockTargetServer.forGet('/api').thenReply(200, 'OK');
-
     // Multiple requests should not cause multiple close subscriptions
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@test/e2e/graceful-shutdown.spec.ts` at line 79, Remove the redundant mock
registration: delete the duplicate call to
mockTargetServer.forGet('/api').thenReply(200, 'OK') in the test body since the
same route is already registered in the beforeEach setup; look for the
mockTargetServer.forGet invocation in graceful-shutdown.spec.ts and keep only
the beforeEach-registered handler to avoid adding duplicate handlers for '/api'.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@test/e2e/graceful-shutdown.spec.ts`:
- Around line 29-48: The tests can leak the listening proxyServer if a test
fails before calling closeServer; update the cleanup to close any started
proxyServer in afterEach by invoking closeServer(proxyServer) when proxyServer
is defined (and await it), and swallow or rethrow errors as appropriate; locate
the proxyServer variable and the existing afterEach/mockTargetServer.stop
sequence and add the proxyServer shutdown there (and reset proxyServer to
undefined after closing) so each test always attempts to close the server even
on failure.
- Line 61: Replace the hardcoded "localhost" host in the E2E request with the
IPv4 loopback to avoid DNS/IPv6 flakiness: update the call that constructs the
URL using SERVER_PORT (the
request(`http://localhost:${SERVER_PORT}`).get('/api').expect(200) invocation)
to use "127.0.0.1" instead of "localhost" so the test targets
http://127.0.0.1:${SERVER_PORT} and matches the other E2E suites.
- Line 79: Remove the redundant mock registration: delete the duplicate call to
mockTargetServer.forGet('/api').thenReply(200, 'OK') in the test body since the
same route is already registered in the beforeEach setup; look for the
mockTargetServer.forGet invocation in graceful-shutdown.spec.ts and keep only
the beforeEach-registered handler to avoid adding duplicate handlers for '/api'.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 30dca38e-a42b-4b81-b086-c8cdccfe3ba7

📥 Commits

Reviewing files that changed from the base of the PR and between 7cd407e and e8983c6.

📒 Files selected for processing (3)
  • src/http-proxy-middleware.ts
  • test/e2e/graceful-shutdown.spec.ts
  • test/e2e/hono.spec.ts

@chimurai chimurai changed the title chore: more tests chore: add E2E tests for graceful shutdown and Hono error handling Apr 25, 2026
@chimurai chimurai merged commit 415588f into master Apr 25, 2026
21 checks passed
@chimurai chimurai deleted the more-tests branch April 25, 2026 18:55
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