Skip to content

test: add coverage for origin callback edge cases#401

Open
mahmoodhamdi wants to merge 1 commit intoexpressjs:masterfrom
mahmoodhamdi:test/improve-origin-callback-coverage
Open

test: add coverage for origin callback edge cases#401
mahmoodhamdi wants to merge 1 commit intoexpressjs:masterfrom
mahmoodhamdi:test/improve-origin-callback-coverage

Conversation

@mahmoodhamdi
Copy link
Copy Markdown

What

Added 7 tests covering origin callback behaviors that were previously untested, despite 100% line coverage.

Why

While working with cors in my Express APIs, I was reading through the test suite and noticed several origin callback edge cases that lacked explicit test coverage. Even though line coverage was already at 100%, these behavioral scenarios weren't being validated:

  • Origin parameter forwarding — verifies the callback receives the actual req.headers.origin value
  • Error propagation — verifies errors from the origin callback are correctly forwarded to next(err)
  • String return value — verifies that returning a specific string (not just true/false) from the origin callback sets the correct Access-Control-Allow-Origin and Vary headers
  • Preflight with regexp origin — verifies OPTIONS requests work correctly when origin is a RegExp
  • Preflight with array origin — verifies OPTIONS requests work correctly when origin is an array of checks
  • Preflight with origin: true — verifies OPTIONS requests reflect the request origin and include default methods
  • Methods as string — verifies that passing methods as a comma-separated string (not just an array) works on preflight

Testing

56 passing (42ms)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files |     100 |      100 |     100 |     100 |
 index.js |     100 |      100 |     100 |     100 |
----------|---------|----------|---------|---------|-------------------
  • All 56 tests pass (49 existing + 7 new)
  • Lint passes
  • 100% coverage maintained

Add tests for behaviors that were previously untested:

- Origin callback receives the correct request origin parameter
- Errors from origin callback are forwarded to next()
- Origin callback returning a specific string (not just boolean)
- Preflight requests with regexp origin
- Preflight requests with array of origin checks
- Preflight requests with origin set to true
- Methods option specified as a string
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.

1 participant