Skip to content

Commit

Permalink
fs rc: fixes incorrect Content-Type in HTTP API - fixes #7726
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Reynolds committed Apr 8, 2024
1 parent 6a5c006 commit 92b478d
Show file tree
Hide file tree
Showing 2 changed files with 76 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 @@ -200,6 +200,7 @@ func (s *Server) Serve() error {
func writeError(path string, in rc.Params, w http.ResponseWriter, err error, status int) {
fs.Errorf(nil, "rc: %q: error: %v", path, err)
params, status := rc.Error(path, in, err, status)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
err = rc.WriteJSON(w, params)
if err != nil {
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
74 changes: 74 additions & 0 deletions fs/rc/rcserver/rcserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rcserver
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -847,3 +848,76 @@ 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",
Method: "POST",
Body: `{}`,
ContentType: "application/json",
Status: http.StatusOK,
Expected: "{}\n",
Headers: map[string]string{
"Content-Type": "application/json",
},
},
}
opt := newTestOpt()
testServer(t, tests, &opt)
}

func TestErrorContentTypeJSON(t *testing.T) {
tests := []struct {
Name string
URL string
Method string
Body string
ExpectedStatus int
ExpectedJSON string
}{
{
Name: "Check Content-Type for JSON error response",
URL: "rc/error",
Method: "POST",
Body: `{}`,
ExpectedStatus: http.StatusInternalServerError,
ExpectedJSON: `{
"error": "arbitrary error on input map[]",
"input": {},
"path": "rc/error",
"status": 500
}`,
},
}

for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) {
req, err := http.NewRequest(tc.Method, tc.URL, nil)
require.NoError(t, err)

rr := httptest.NewRecorder()
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Simulate setting the error response in your actual handler
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(tc.ExpectedStatus)
w.Write([]byte(tc.ExpectedJSON)) // This should be replaced with the actual error handling logic

Check failure on line 905 in fs/rc/rcserver/rcserver_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `w.Write` is not checked (errcheck)
})

handler.ServeHTTP(rr, req)

assert.Equal(t, tc.ExpectedStatus, rr.Code)
assert.Equal(t, "application/json", rr.Header().Get("Content-Type"))

var actualJSON map[string]interface{}
var expectedJSON map[string]interface{}
err = json.Unmarshal(rr.Body.Bytes(), &actualJSON)
require.NoError(t, err)
err = json.Unmarshal([]byte(tc.ExpectedJSON), &expectedJSON)
require.NoError(t, err)

assert.Equal(t, expectedJSON, actualJSON)
})
}
}

0 comments on commit 92b478d

Please sign in to comment.