diff --git a/config.go b/config.go index 9dc0c85..1e71bc7 100644 --- a/config.go +++ b/config.go @@ -36,7 +36,10 @@ func (cors *cors) applyCors(c *gin.Context) { // request is not a CORS request return } - host := c.Request.Header.Get("Host") + // go/net/http/request.go + // For incoming requests, the Host header is promoted to the + // Request.Host field and removed from the Header map. + host := c.Request.Host if origin == "http://"+host || origin == "https://"+host { // request is not a CORS request but have origin header. // for example, use fetch api diff --git a/cors_test.go b/cors_test.go index c260e88..0dad266 100644 --- a/cors_test.go +++ b/cors_test.go @@ -42,7 +42,14 @@ func performRequest(r http.Handler, method, origin string) *httptest.ResponseRec func performRequestWithHeaders(r http.Handler, method, origin string, headers map[string]string) *httptest.ResponseRecorder { req, _ := http.NewRequest(method, "/", nil) + // go/net/http/request.go + // For incoming requests, the Host header is promoted to the + // Request.Host field and removed from the Header map. for k, v := range headers { + if k == "Host" { + req.Host = v + continue + } req.Header.Set(k, v) } if len(origin) > 0 {