NiniOak 3 weeks ago
parent e9aea767d6
commit d47b2a81d2

@ -684,8 +684,7 @@ func requireHasHeadersSet(t *testing.T, a *TestAgent, method, path string, body
hdrs := resp.Header() hdrs := resp.Header()
reqHdrs := req.Header reqHdrs := req.Header
fmt.Println("Request Headers:", reqHdrs.Get("Content-Type")) fmt.Println("Body: ", resp.Body.String())
fmt.Println("Response Headers:", hdrs.Get("Content-Type"))
require.Equal(t, "*", hdrs.Get("Access-Control-Allow-Origin"), require.Equal(t, "*", hdrs.Get("Access-Control-Allow-Origin"),
"Access-Control-Allow-Origin header value incorrect") "Access-Control-Allow-Origin header value incorrect")
@ -720,7 +719,7 @@ func TestUIResponseHeaders(t *testing.T) {
defer a.Shutdown() defer a.Shutdown()
//response header for the UI appears to be being handled by the UI itself. //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) { 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") assert.True(t, nextHandlerCalled, "expected next handler to be called")
} }
func TestValidateContentTypeHeader(t *testing.T) {
}

@ -15,42 +15,47 @@ const (
) )
// ContentTypeRule defines a rule for content type determination // ContentTypeRule defines a rule for content type determination
type ContentTypeRule struct { type contentTypeRule struct {
Path string path string
Method string method string
ContentType string contentType string
} }
var contentTypeRules = []ContentTypeRule{ var contentTypeRules = []contentTypeRule{
{ {
Path: "/v1/snapshot", path: "/v1/snapshot",
Method: http.MethodGet, method: http.MethodGet,
ContentType: "application/x-gzip", contentType: "application/x-gzip",
}, },
{ {
Path: "/v1/snapshot", path: "/v1/snapshot",
Method: http.MethodPut, method: http.MethodPut,
ContentType: "application/octet-stream", contentType: "application/octet-stream",
}, },
{ {
Path: "/v1/kv", path: "/v1/kv",
Method: http.MethodPut, method: http.MethodPut,
ContentType: "application/octet-stream", contentType: "application/octet-stream",
}, },
{ {
Path: "/v1/kv", path: "/v1/kv",
Method: http.MethodDelete, method: http.MethodDelete,
ContentType: "", contentType: "",
}, },
{ {
Path: "/v1/kv", path: "/v1/kv",
Method: http.MethodGet, method: http.MethodGet,
ContentType: "", contentType: "",
}, },
{ {
Path: "/v1/event/fire", path: "/v1/event/fire",
Method: http.MethodPut, method: http.MethodPut,
ContentType: "application/octet-stream", 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 // Check against defined endpoint and required content type rules
for _, rule := range contentTypeRules { for _, rule := range contentTypeRules {
if matchesRule(req, rule) { 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 // matchesRule checks if a request matches a content type rule
func matchesRule(req *http.Request, rule ContentTypeRule) bool { func matchesRule(req *http.Request, rule contentTypeRule) bool {
return strings.HasPrefix(req.URL.Path, rule.Path) && return strings.HasPrefix(req.URL.Path, rule.path) &&
(rule.Method == "" || req.Method == rule.Method) (rule.method == "" || req.Method == rule.method)
} }
// isIndexPage checks if the request is for the index page // isIndexPage checks if the request is for the index page

Loading…
Cancel
Save