From 4447aebdda542a9a6027d0a2c7e550eb92082679 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sun, 10 Mar 2024 14:49:51 +0800 Subject: [PATCH] refactor: refactor request handling and improve CORS checks - Refactor `performRequestWithHeaders` function signature for better readability - Standardize the string comparison order in CORS origin checks Signed-off-by: Bo-Yi Wu --- cors_test.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/cors_test.go b/cors_test.go index dedd3cc..1ff9737 100644 --- a/cors_test.go +++ b/cors_test.go @@ -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 @@ -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