Skip to content

Commit

Permalink
fix: Add Content-Type header to the health endpoint (#2634)
Browse files Browse the repository at this point in the history
* fix: Content-Type and Grpc-Metadata-Content-Type headers with the health endpoint #2611

* fix: no need to set grpc content type header

* fix: remove unnecessary grpc header comment

* test: lighten the Content-Type header test
  • Loading branch information
GreyXor authored May 4, 2022
1 parent 0843118 commit 61facaa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions runtime/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ func WithHealthEndpointAt(healthCheckClient grpc_health_v1.HealthClient, endpoin
return
}

w.Header().Set("Content-Type", "application/json")

if resp.GetStatus() != grpc_health_v1.HealthCheckResponse_SERVING {
var err error
switch resp.GetStatus() {
Expand Down
20 changes: 20 additions & 0 deletions runtime/mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,26 @@ func TestWithHealthzEndpoint_serviceParam(t *testing.T) {
}
}

func TestWithHealthzEndpoint_header(t *testing.T) {
for _, tt := range healthCheckTests {
t.Run(tt.name, func(t *testing.T) {
mux := runtime.NewServeMux(runtime.WithHealthzEndpoint(&dummyHealthCheckClient{status: tt.status, code: tt.code}))

r := httptest.NewRequest(http.MethodGet, "/healthz", nil)
rr := httptest.NewRecorder()

mux.ServeHTTP(rr, r)

if actualHeader := rr.Header().Get("Content-Type"); actualHeader != "application/json" {
t.Errorf(
"result http header Content-Type for grpc code %q and status %q should be application/json, got %s",
tt.code, tt.status, actualHeader,
)
}
})
}
}

var _ grpc_health_v1.HealthClient = (*dummyHealthCheckClient)(nil)

type dummyHealthCheckClient struct {
Expand Down

0 comments on commit 61facaa

Please # to comment.