Skip to content

Commit

Permalink
refactor: refactor request handling and improve CORS checks
Browse files Browse the repository at this point in the history
- Refactor `performRequestWithHeaders` function signature for better readability
- Standardize the string comparison order in CORS origin checks

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
  • Loading branch information
appleboy committed Mar 10, 2024
1 parent 9d49f16 commit 4447aeb
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions cors_test.go
Expand Up @@ -54,7 +54,11 @@ func performRequest(r http.Handler, method, origin string) *httptest.ResponseRec
return performRequestWithHeaders(r, method, "/", origin, http.Header{})
}

func performRequestWithHeaders(r http.Handler, method, path, origin string, header http.Header) *httptest.ResponseRecorder {
func performRequestWithHeaders(
r http.Handler,
method, path, origin string,
header http.Header,
) *httptest.ResponseRecorder {
req, _ := http.NewRequestWithContext(context.Background(), method, path, nil)
// From go/net/http/request.go:
// For incoming requests, the Host header is promoted to the
Expand Down Expand Up @@ -472,11 +476,11 @@ func TestMultiGroupRouter(t *testing.T) {
AllowOriginWithContextFunc: func(c *gin.Context, origin string) bool {
path := c.Request.URL.Path
if strings.HasPrefix(path, "/app1") {
return "http://app1.example.com" == origin
return origin == "http://app1.example.com"
}

if strings.HasPrefix(path, "/app2") {
return "http://app2.example.com" == origin
return origin == "http://app2.example.com"
}

// app 3 allows all origins
Expand Down

0 comments on commit 4447aeb

Please sign in to comment.