Skip to content

Commit

Permalink
fs rc: fixes incorrect Content-Type in HTTP API - fixes rclone#7726
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Reynolds committed Apr 5, 2024
1 parent 6a5c006 commit 1fa9dce
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions fs/rc/rcserver/rcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions fs/rc/rcserver/rcserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 1fa9dce

Please sign in to comment.