Fix initial lint violations.

Signed-off-by: Matt Moyer <moyerm@vmware.com>
This commit is contained in:
Matt Moyer 2020-07-07 14:54:14 -05:00
parent 9bcd532c19
commit 82f89c501a
4 changed files with 22 additions and 7 deletions

View File

@ -6,9 +6,10 @@ SPDX-License-Identifier: Apache-2.0
package main package main
import ( import (
"github.com/suzerain-io/placeholder-name/pkg/handlers"
"log" "log"
"net/http" "net/http"
"github.com/suzerain-io/placeholder-name/pkg/handlers"
) )
func main() { func main() {

View File

@ -7,7 +7,7 @@ package handlers
import "net/http" import "net/http"
const JsonMimeType = "application/json; charset=utf-8" const JSONMimeType = "application/json; charset=utf-8"
const HeaderNameContentType = "Content-Type" const HeaderNameContentType = "Content-Type"
func New() http.Handler { func New() http.Handler {

View File

@ -19,7 +19,7 @@ type healthzHandler struct{}
func (h healthzHandler) ServeHTTP(responseWriter http.ResponseWriter, _ *http.Request) { func (h healthzHandler) ServeHTTP(responseWriter http.ResponseWriter, _ *http.Request) {
response := healthzResponse{"OK"} response := healthzResponse{"OK"}
js, _ := json.Marshal(response) js, _ := json.Marshal(response)
responseWriter.Header().Set(HeaderNameContentType, JsonMimeType) responseWriter.Header().Set(HeaderNameContentType, JSONMimeType)
_, _ = responseWriter.Write(js) _, _ = responseWriter.Write(js)
} }

View File

@ -6,23 +6,37 @@ SPDX-License-Identifier: Apache-2.0
package handlers_test package handlers_test
import ( import (
"github.com/stretchr/testify/require" "context"
"github.com/suzerain-io/placeholder-name/pkg/handlers"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
"time"
"github.com/stretchr/testify/require"
"github.com/suzerain-io/placeholder-name/pkg/handlers"
) )
func TestHealthzReturnsOkWithJsonBody(t *testing.T) { func TestHealthzReturnsOkWithJsonBody(t *testing.T) {
expect := require.New(t) expect := require.New(t)
server := httptest.NewServer(handlers.New()) server := httptest.NewServer(handlers.New())
defer server.Close() defer server.Close()
client := http.Client{}
response, err := client.Get(server.URL + "/healthz") // Create a request context with a short timeout.
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
// Initialize an HTTP GET request to /healthz
req, err := http.NewRequestWithContext(ctx, http.MethodGet, server.URL+"/healthz", nil)
expect.NoError(err) expect.NoError(err)
// Perform the request and assert that we received a response.
response, err := http.DefaultClient.Do(req)
expect.NoError(err)
defer response.Body.Close()
// Assert that we got an HTTP 200 with the correct content type and JSON body.
expect.Equal(http.StatusOK, response.StatusCode) expect.Equal(http.StatusOK, response.StatusCode)
expect.Equal("application/json; charset=utf-8", response.Header.Get("content-type")) expect.Equal("application/json; charset=utf-8", response.Header.Get("content-type"))
body, err := ioutil.ReadAll(response.Body) body, err := ioutil.ReadAll(response.Body)