Skip to content

Commit

Permalink
Use correct API in readyHandler (grafana/phlare#516)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rustin170506 committed Feb 14, 2023
1 parent 054edda commit 0bde110
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/phlare/phlare.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ func (f *Phlare) readyHandler(sm *services.Manager) http.HandlerFunc {
return
}

http.Error(w, "ready", http.StatusOK)
util.WriteTextResponse(w, "ready")
}
}

Expand Down
8 changes: 8 additions & 0 deletions pkg/util/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,11 @@ func WriteHTMLResponse(w http.ResponseWriter, message string) {
// Ignore inactionable errors.
_, _ = w.Write([]byte(message))
}

// WriteTextResponse sends message as text/plain response with 200 status code.
func WriteTextResponse(w http.ResponseWriter, message string) {
w.Header().Set("Content-Type", "text/plain")

// Ignore inactionable errors.
_, _ = w.Write([]byte(message))
}
20 changes: 20 additions & 0 deletions pkg/util/http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package util_test

import (
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"

"github.com/grafana/phlare/pkg/util"
)

func TestWriteTextResponse(t *testing.T) {
w := httptest.NewRecorder()

util.WriteTextResponse(w, "hello world")

assert.Equal(t, 200, w.Code)
assert.Equal(t, "hello world", w.Body.String())
assert.Equal(t, "text/plain", w.Header().Get("Content-Type"))
}

0 comments on commit 0bde110

Please sign in to comment.