Skip to content

test: add CORS middleware tests (#79) - #97

Merged
andrewtavis merged 2 commits into
scribe-org:mainfrom
prince-0408:test/cors-middleware-79
Sep 8, 2026
Merged

andrewtavis merged 2 commits into
scribe-org:mainfrom
prince-0408:test/cors-middleware-79

Conversation

@prince-0408

Copy link
Copy Markdown
Contributor

Contributor checklist

  • This pull request is on a separate branch and not the main branch
  • I have ran the ./pre-commit executable as well as make lint and have fixed all reported issues

Description

Adds unit tests for the CORS middleware (SetupCORS()) to ensure proper CORS headers are set across all scenarios, matching the requirements of #79:

  • Changes:

    • api/middleware_test.go:
      • Normal GET asserts all four Access-Control-* headers and downstream handler execution.
      • OPTIONS preflight returns HTTP 204 without reaching the handler.
      • Error responses (400, 403, 404, 500) preserve all four CORS headers.
      • Edge cases for unrouted 404 and other HTTP methods (POST, PUT, DELETE).
    • go.mod: Promotes github.com/stretchr/testify v1.10.0 as required by TESTING.md.
  • Verification & Coverage:

    • make fmt: Clean.
    • make test: All tests pass.
    • make lint (revive): 0 issues.
    • Coverage:
      • api/middleware.go:SetupCORS: 0.0% -> 100.0%
      • Overall coverage: 2.3% -> 3.4%

Related issue

@andrewtavis
andrewtavis requested a review from DeleMike September 6, 2026 15:39
@andrewtavis

Copy link
Copy Markdown
Member

Thanks for the PR here, @prince-0408! @DeleMike, would you be able to take a look?

@andrewtavis andrewtavis added the no-changelog No changelog entry is needed for this pull request label Sep 6, 2026
@DeleMike

DeleMike commented Sep 7, 2026

Copy link
Copy Markdown
Collaborator

Thanks @andrewtavis for notifying me. And thanks @prince-0408, I will look into this later today or early hours tomorrow 🤞🏾

Good PR comment! Thank you! :)

@DeleMike

DeleMike commented Sep 8, 2026

Copy link
Copy Markdown
Collaborator

reviewing...

There is no need for this flag as we have no auth for now. Scribe-Server API is a public API.

@DeleMike DeleMike left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

All looks great to me! :)

Great work @prince-0408

Comment thread api/middleware.go
func SetupCORS() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Hi,

I removed Access-Control-Allow-Credentials. I believe it must NOT be set for now. The Fetch spec forbids pairing it with a wildcard origin, and browsers reject any credentialed request that receives both(we were initially setting both, but this PR will remove the one we don't use).

If credentials are ever needed, the wildcard above has to be replaced by a reflected, allowlisted origin first.

For now, we are a public API so I believe we should remove it for now.

@DeleMike

DeleMike commented Sep 8, 2026

Copy link
Copy Markdown
Collaborator

Hi @andrewtavis , we can merge if you are satisfied :)

@andrewtavis andrewtavis left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

praise: Looks great to me, @prince-0408! Thanks for the review, @DeleMike 😊 Mostly going off your review as I trust that you know what we're looking for here :)

@andrewtavis
andrewtavis merged commit f220bd8 into scribe-org:main Sep 8, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-changelog No changelog entry is needed for this pull request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add tests for CORS middleware

3 participants