From 1fa9dced0b4821f98bc791501b29159aa3598e0f Mon Sep 17 00:00:00 2001 From: Kyle Reynolds Date: Fri, 5 Apr 2024 14:06:41 -0600 Subject: [PATCH] fs rc: fixes incorrect Content-Type in HTTP API - fixes #7726 --- fs/rc/rcserver/rcserver.go | 2 ++ fs/rc/rcserver/rcserver_test.go | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/fs/rc/rcserver/rcserver.go b/fs/rc/rcserver/rcserver.go index 21378e8ec5f82..58ed7ed79d1f6 100644 --- a/fs/rc/rcserver/rcserver.go +++ b/fs/rc/rcserver/rcserver.go @@ -201,6 +201,7 @@ func writeError(path string, in rc.Params, w http.ResponseWriter, err error, sta fs.Errorf(nil, "rc: %q: error: %v", path, err) params, status := rc.Error(path, in, err, status) w.WriteHeader(status) + w.Header().Set("Content-Type", "application/json") err = rc.WriteJSON(w, params) if err != nil { // can't return the error at this point @@ -294,6 +295,7 @@ func (s *Server) handlePost(w http.ResponseWriter, r *http.Request, path string) } fs.Debugf(nil, "rc: %q: reply %+v: %v", path, out, err) + w.Header().Set("Content-Type", "application/json") err = rc.WriteJSON(w, out) if err != nil { // can't return the error at this point - but have a go anyway diff --git a/fs/rc/rcserver/rcserver_test.go b/fs/rc/rcserver/rcserver_test.go index 47ec89e588c21..ac52990dff97d 100644 --- a/fs/rc/rcserver/rcserver_test.go +++ b/fs/rc/rcserver/rcserver_test.go @@ -847,3 +847,22 @@ func TestServeModTime(t *testing.T) { }} testServer(t, tests, &opt) } + +func TestContentTypeJSON(t *testing.T) { + tests := []testRun{ + { + Name: "Check Content-Type for JSON response", + URL: "rc/noop", // Assuming rc/noop returns JSON + Method: "POST", + Body: `{}`, // Empty JSON body + ContentType: "application/json", + Status: http.StatusOK, + Expected: "{}\n", // Expected JSON response + Headers: map[string]string{ + "Content-Type": "application/json", // Adjusted to match actual behavior + }, + }, + } + opt := newTestOpt() + testServer(t, tests, &opt) +}