mirror of
https://github.com/vmware-tanzu/pinniped.git
synced 2026-01-07 05:57:02 +00:00
Add a simple /healthz endpoint
- Also remove the old hello world code Signed-off-by: Andrew Keesler <akeesler@vmware.com>
This commit is contained in:
committed by
Andrew Keesler
parent
cc81dd04e9
commit
57a22f99aa
17
pkg/handlers/handlers.go
Normal file
17
pkg/handlers/handlers.go
Normal file
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
Copyright 2020 VMware, Inc.
|
||||
SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
package handlers
|
||||
|
||||
import "net/http"
|
||||
|
||||
const JsonMimeType = "application/json; charset=utf-8"
|
||||
const HeaderNameContentType = "Content-Type"
|
||||
|
||||
func New() http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle("/healthz", newHealthzHandler())
|
||||
return mux
|
||||
}
|
||||
28
pkg/handlers/healthz_handler.go
Normal file
28
pkg/handlers/healthz_handler.go
Normal file
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
Copyright 2020 VMware, Inc.
|
||||
SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type healthzResponse struct {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
type healthzHandler struct{}
|
||||
|
||||
func (h healthzHandler) ServeHTTP(responseWriter http.ResponseWriter, _ *http.Request) {
|
||||
response := healthzResponse{"OK"}
|
||||
js, _ := json.Marshal(response)
|
||||
responseWriter.Header().Set(HeaderNameContentType, JsonMimeType)
|
||||
_, _ = responseWriter.Write(js)
|
||||
}
|
||||
|
||||
func newHealthzHandler() http.Handler {
|
||||
return healthzHandler{}
|
||||
}
|
||||
31
pkg/handlers/healthz_handler_test.go
Normal file
31
pkg/handlers/healthz_handler_test.go
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
Copyright 2020 VMware, Inc.
|
||||
SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
package handlers_test
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/suzerain-io/placeholder-name/pkg/handlers"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHealthzReturnsOkWithJsonBody(t *testing.T) {
|
||||
expect := require.New(t)
|
||||
server := httptest.NewServer(handlers.New())
|
||||
defer server.Close()
|
||||
client := http.Client{}
|
||||
|
||||
response, err := client.Get(server.URL + "/healthz")
|
||||
|
||||
expect.NoError(err)
|
||||
expect.Equal(http.StatusOK, response.StatusCode)
|
||||
expect.Equal("application/json; charset=utf-8", response.Header.Get("content-type"))
|
||||
body, err := ioutil.ReadAll(response.Body)
|
||||
expect.NoError(err)
|
||||
expect.JSONEq(`{"status": "OK"}`, string(body))
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
/*
|
||||
Copyright 2020 VMware, Inc.
|
||||
SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
package hello
|
||||
|
||||
type HelloSayer interface {
|
||||
SayHello() string
|
||||
}
|
||||
|
||||
type helloSayerImpl struct{}
|
||||
|
||||
func (helloSayerImpl) SayHello() string { return "hello" }
|
||||
|
||||
func NewHelloSayer() HelloSayer {
|
||||
return helloSayerImpl{}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
/*
|
||||
Copyright 2020 VMware, Inc.
|
||||
SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
package hello
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHelloSayerImpl_SayHello(t *testing.T) {
|
||||
actualGreeting := NewHelloSayer().SayHello()
|
||||
if actualGreeting != "hello" {
|
||||
t.Errorf("expected to say hello but said %v", actualGreeting)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user