diff --git a/agent/http_test.go b/agent/http_test.go index c4c0f70413..b5edf33667 100644 --- a/agent/http_test.go +++ b/agent/http_test.go @@ -684,8 +684,7 @@ func requireHasHeadersSet(t *testing.T, a *TestAgent, method, path string, body hdrs := resp.Header() reqHdrs := req.Header - fmt.Println("Request Headers:", reqHdrs.Get("Content-Type")) - fmt.Println("Response Headers:", hdrs.Get("Content-Type")) + fmt.Println("Body: ", resp.Body.String()) require.Equal(t, "*", hdrs.Get("Access-Control-Allow-Origin"), "Access-Control-Allow-Origin header value incorrect") @@ -720,7 +719,7 @@ func TestUIResponseHeaders(t *testing.T) { defer a.Shutdown() //response header for the UI appears to be being handled by the UI itself. - requireHasHeadersSet(t, a, http.MethodGet, "/ui", nil, api.JSONContentType) + requireHasHeadersSet(t, a, http.MethodGet, "/ui", nil, api.PlainContentType) } func TestErrorContentTypeHeaderSet(t *testing.T) { @@ -1911,6 +1910,3 @@ func TestWithRemoteAddrHandler_InvalidAddr(t *testing.T) { assert.True(t, nextHandlerCalled, "expected next handler to be called") } - -func TestValidateContentTypeHeader(t *testing.T) { -} diff --git a/api/content_type.go b/api/content_type.go index ce1d1d3f17..6bdb5c1a57 100644 --- a/api/content_type.go +++ b/api/content_type.go @@ -15,42 +15,47 @@ const ( ) // ContentTypeRule defines a rule for content type determination -type ContentTypeRule struct { - Path string - Method string - ContentType string +type contentTypeRule struct { + path string + method string + contentType string } -var contentTypeRules = []ContentTypeRule{ +var contentTypeRules = []contentTypeRule{ { - Path: "/v1/snapshot", - Method: http.MethodGet, - ContentType: "application/x-gzip", + path: "/v1/snapshot", + method: http.MethodGet, + contentType: "application/x-gzip", }, { - Path: "/v1/snapshot", - Method: http.MethodPut, - ContentType: "application/octet-stream", + path: "/v1/snapshot", + method: http.MethodPut, + contentType: "application/octet-stream", }, { - Path: "/v1/kv", - Method: http.MethodPut, - ContentType: "application/octet-stream", + path: "/v1/kv", + method: http.MethodPut, + contentType: "application/octet-stream", }, { - Path: "/v1/kv", - Method: http.MethodDelete, - ContentType: "", + path: "/v1/kv", + method: http.MethodDelete, + contentType: "", }, { - Path: "/v1/kv", - Method: http.MethodGet, - ContentType: "", + path: "/v1/kv", + method: http.MethodGet, + contentType: "", }, { - Path: "/v1/event/fire", - Method: http.MethodPut, - ContentType: "application/octet-stream", + path: "/v1/event/fire", + method: http.MethodPut, + contentType: "application/octet-stream", + }, + { + path: "/ui", + method: http.MethodGet, + contentType: PlainContentType, }, } @@ -72,7 +77,7 @@ func DetermineContentType(req *http.Request) string { // Check against defined endpoint and required content type rules for _, rule := range contentTypeRules { if matchesRule(req, rule) { - return rule.ContentType + return rule.contentType } } @@ -81,9 +86,9 @@ func DetermineContentType(req *http.Request) string { } // matchesRule checks if a request matches a content type rule -func matchesRule(req *http.Request, rule ContentTypeRule) bool { - return strings.HasPrefix(req.URL.Path, rule.Path) && - (rule.Method == "" || req.Method == rule.Method) +func matchesRule(req *http.Request, rule contentTypeRule) bool { + return strings.HasPrefix(req.URL.Path, rule.path) && + (rule.method == "" || req.Method == rule.method) } // isIndexPage checks if the request is for the index page