-
Notifications
You must be signed in to change notification settings - Fork 15
test: add CORS middleware tests (#79) #97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
andrewtavis
merged 2 commits into
scribe-org:main
from
prince-0408:test/cors-middleware-79
Sep 8, 2026
+164
−1
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| // SPDX-License-Identifier: GPL-3.0-or-later | ||
|
|
||
| package api | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/gin-gonic/gin" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func assertCORSHeaders(t *testing.T, headers http.Header) { | ||
| t.Helper() | ||
|
|
||
| assert.Equal(t, "*", headers.Get("Access-Control-Allow-Origin")) | ||
| assert.Empty(t, headers.Get("Access-Control-Allow-Credentials"), | ||
| "Allow-Credentials must not be set alongside a wildcard Allow-Origin") | ||
| assert.Equal( | ||
| t, | ||
| "Content-Type, Content-Length, Accept-Encoding, "+ | ||
| "X-CSRF-Token, Authorization, accept, origin, "+ | ||
| "Cache-Control, X-Requested-With", | ||
| headers.Get("Access-Control-Allow-Headers"), | ||
| ) | ||
| assert.Equal(t, "POST, OPTIONS, GET, PUT, DELETE", headers.Get("Access-Control-Allow-Methods")) | ||
| } | ||
|
|
||
| func setupTestRouter(t *testing.T) *gin.Engine { | ||
| t.Helper() | ||
|
|
||
| previousMode := gin.Mode() | ||
| gin.SetMode(gin.TestMode) | ||
| t.Cleanup(func() { gin.SetMode(previousMode) }) | ||
|
|
||
| router := gin.New() | ||
| router.Use(SetupCORS()) | ||
| return router | ||
| } | ||
|
|
||
| func TestSetupCORS_NormalGET(t *testing.T) { | ||
| router := setupTestRouter(t) | ||
|
|
||
| handlerReached := false | ||
| router.GET("/test", func(c *gin.Context) { | ||
| handlerReached = true | ||
| c.String(http.StatusOK, "ok") | ||
| }) | ||
|
|
||
| req := httptest.NewRequest(http.MethodGet, "/test", nil) | ||
| rec := httptest.NewRecorder() | ||
| router.ServeHTTP(rec, req) | ||
|
|
||
| require.Equal(t, http.StatusOK, rec.Code) | ||
| assert.True(t, handlerReached, "handler should be reached for normal GET") | ||
| assert.Equal(t, "ok", rec.Body.String()) | ||
| assertCORSHeaders(t, rec.Header()) | ||
| } | ||
|
|
||
| func TestSetupCORS_OPTIONS(t *testing.T) { | ||
| router := setupTestRouter(t) | ||
|
|
||
| handlerReached := false | ||
| router.OPTIONS("/test", func(c *gin.Context) { | ||
| handlerReached = true | ||
| c.String(http.StatusOK, "should not be reached") | ||
| }) | ||
|
|
||
| req := httptest.NewRequest(http.MethodOptions, "/test", nil) | ||
| rec := httptest.NewRecorder() | ||
| router.ServeHTTP(rec, req) | ||
|
|
||
| require.Equal(t, http.StatusNoContent, rec.Code) | ||
| assert.False(t, handlerReached, "handler must not be reached on OPTIONS preflight") | ||
| assert.Empty(t, rec.Body.String()) | ||
| assertCORSHeaders(t, rec.Header()) | ||
| } | ||
|
|
||
| func TestSetupCORS_ErrorResponses(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| statusCode int | ||
| handler gin.HandlerFunc | ||
| }{ | ||
| { | ||
| name: "400 Bad Request with AbortWithStatusJSON", | ||
| statusCode: http.StatusBadRequest, | ||
| handler: func(c *gin.Context) { | ||
| c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "bad request"}) | ||
| }, | ||
| }, | ||
| { | ||
| name: "403 Forbidden with AbortWithStatus", | ||
| statusCode: http.StatusForbidden, | ||
| handler: func(c *gin.Context) { | ||
| c.AbortWithStatus(http.StatusForbidden) | ||
| }, | ||
| }, | ||
| { | ||
| name: "404 Not Found from handler", | ||
| statusCode: http.StatusNotFound, | ||
| handler: func(c *gin.Context) { | ||
| c.String(http.StatusNotFound, "not found") | ||
| }, | ||
| }, | ||
| { | ||
| name: "500 Internal Server Error with AbortWithStatus", | ||
| statusCode: http.StatusInternalServerError, | ||
| handler: func(c *gin.Context) { | ||
| c.AbortWithStatus(http.StatusInternalServerError) | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| router := setupTestRouter(t) | ||
| router.GET("/error", tt.handler) | ||
|
|
||
| req := httptest.NewRequest(http.MethodGet, "/error", nil) | ||
| rec := httptest.NewRecorder() | ||
| router.ServeHTTP(rec, req) | ||
|
|
||
| require.Equal(t, tt.statusCode, rec.Code) | ||
| assertCORSHeaders(t, rec.Header()) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestSetupCORS_UnroutedRoute(t *testing.T) { | ||
| router := setupTestRouter(t) | ||
|
|
||
| req := httptest.NewRequest(http.MethodGet, "/nonexistent", nil) | ||
| rec := httptest.NewRecorder() | ||
| router.ServeHTTP(rec, req) | ||
|
|
||
| require.Equal(t, http.StatusNotFound, rec.Code) | ||
| assertCORSHeaders(t, rec.Header()) | ||
| } | ||
|
|
||
| func TestSetupCORS_OtherMethods(t *testing.T) { | ||
| methods := []string{http.MethodPost, http.MethodPut, http.MethodDelete} | ||
|
|
||
| for _, method := range methods { | ||
| t.Run(method, func(t *testing.T) { | ||
| router := setupTestRouter(t) | ||
| router.Handle(method, "/test", func(c *gin.Context) { | ||
| c.Status(http.StatusOK) | ||
| }) | ||
|
|
||
| req := httptest.NewRequest(method, "/test", nil) | ||
| rec := httptest.NewRecorder() | ||
| router.ServeHTTP(rec, req) | ||
|
|
||
| require.Equal(t, http.StatusOK, rec.Code) | ||
| assertCORSHeaders(t, rec.Header()) | ||
| }) | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
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. TheFetch specforbids 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.