Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1749,3 +1749,28 @@ type MethodTransformedHandler struct{}
func (h *RawParamHandler) CallSomethingInSnakeCase(ctx context.Context, v int) (int, error) {
return v + 1, nil
}

func TestContentTypeHeader(t *testing.T) {
rpcHandler := SimpleServerHandler{}

rpcServer := NewServer()
rpcServer.Register("SimpleServerHandler", &rpcHandler)

testServ := httptest.NewServer(rpcServer)
defer testServ.Close()

// Test that the Content-Type header is set correctly
resp, err := http.Post(testServ.URL, "application/json", strings.NewReader(`{"jsonrpc": "2.0", "method": "SimpleServerHandler.Inc", "params": [], "id": 1}`))
require.NoError(t, err)
defer resp.Body.Close()

contentType := resp.Header.Get("Content-Type")
assert.Equal(t, "application/json", contentType, "Content-Type header should be application/json")

// Verify the response is still valid JSON
var jsonResp response
err = json.NewDecoder(resp.Body).Decode(&jsonResp)
require.NoError(t, err)
assert.Equal(t, "2.0", jsonResp.Jsonrpc)
assert.Equal(t, float64(1), jsonResp.ID) // JSON numbers are unmarshaled as float64
}
3 changes: 3 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ func (s *RPCServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

// Set Content-Type header for JSON-RPC responses
w.Header().Set("Content-Type", "application/json")

ctx = context.WithValue(ctx, connectionTypeCtxKey, ConnectionTypeHTTP)
s.handleReader(ctx, r.Body, w, rpcError)
}
Expand Down