test: add unit tests for security middleware (JWT, CORS, rate limit, …#669
Closed
mmcintosh wants to merge 1 commit intoSonicJs-Org:mainfrom
Closed
test: add unit tests for security middleware (JWT, CORS, rate limit, …#669mmcintosh wants to merge 1 commit intoSonicJs-Org:mainfrom
mmcintosh wants to merge 1 commit intoSonicJs-Org:mainfrom
Conversation
…security headers) Add 34 new unit tests covering all security middleware that previously had 0 test coverage: - rate-limit.test.ts (15 tests): KV-based sliding window, 429 responses, headers, IP extraction, error handling - security-headers.test.ts (11 tests): all 5 headers, conditional HSTS for dev/prod - cors.test.ts (10 tests): origin allowlist, preflight, multi-origin, rejection - auth.test.ts (+8 tests): JWT_SECRET env var in generateToken, verifyToken, requireAuth, optionalAuth Resolves SEC-UT-1 through SEC-UT-4
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
test: Security Middleware Unit Tests
Target repo:
SonicJs-Org/sonicjsBranch:
test/security-middleware-unit-testsBase:
mainDescription
Add comprehensive unit tests for all security middleware introduced in PRs #659–#663. These middleware features (JWT secret externalization, CORS origin allowlist, rate limiting, security headers) were shipped without unit test coverage. This PR adds 34 new tests to validate their behavior.
Companion to #660 (JWT), #661 (CORS), #662 (rate limiting), #663 (security headers)
Changes
New Test Files
packages/core/src/__tests__/middleware/rate-limit.test.tsX-RateLimit-*andRetry-Afterheaders, resets on window expiration, extracts IP fromcf-connecting-ip/x-forwarded-for/ fallback, isolates key prefixes, gracefully continues on KV errorspackages/core/src/__tests__/middleware/security-headers.test.tsX-Content-Type-Options,X-Frame-Options,Referrer-Policy,Permissions-Policy,Strict-Transport-Security), conditional HSTS (skipped in development, applied in production/staging/unset), correct header count per environmentpackages/core/src/__tests__/middleware/cors.test.tsCORS_ORIGINSenv var: no env = reject all cross-origin, single origin, multi-origin comma-separated, preflight OPTIONS handling,X-API-Keyin allowed headers, allowed HTTP methods, same-origin passthroughUpdated Test File
packages/core/src/__tests__/middleware/auth.test.tsgenerateToken/verifyTokenwith custom secret, wrong secret rejection, custom-secret token vs fallback mismatch,requireAuthmiddleware usingc.env.JWT_SECRET,optionalAuthmiddleware usingc.env.JWT_SECRETTesting
Unit Tests
E2E Tests
Technical Details
app.request()for realistic HTTP-level testingPerformance Impact
Breaking Changes
None — no runtime code changes.
Migration Notes
No action required.
Checklist