From 9b1809cc93b737d070c8a82653d474e26ea18a98 Mon Sep 17 00:00:00 2001 From: Ivan Smirnov Date: Sat, 22 Sep 2018 14:28:51 -0700 Subject: [PATCH 1/2] add json pretty prints for /healthz --- web.go | 14 +++++++++++++- web_test.go | 26 +++++++++++++++++++------- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/web.go b/web.go index 8653c8e..6b4e610 100644 --- a/web.go +++ b/web.go @@ -8,6 +8,7 @@ import ( "sync" "github.com/Jeffail/gabs" + "encoding/json" ) type context struct { @@ -79,6 +80,17 @@ func HealthHandler(w http.ResponseWriter, r *http.Request) { // VarsHandler responds to /varz request and prints config. func VarsHandler(c *context, w http.ResponseWriter, r *http.Request) (int, error) { w.WriteHeader(http.StatusOK) - io.WriteString(w, "Config: "+c.config.String()) + io.WriteString(w, jsonPrettyPrint(c.config.String())) return 200, nil } + +// https://stackoverflow.com/a/36544455/5117259 +func jsonPrettyPrint(in string) string { + var out bytes.Buffer + err := json.Indent(&out, []byte(in), "", "\t") + if err != nil { + return in + } + out.WriteString("\n") + return out.String() +} diff --git a/web_test.go b/web_test.go index e86577d..f82f281 100644 --- a/web_test.go +++ b/web_test.go @@ -236,12 +236,12 @@ func BenchmarkIndexHandler(b *testing.B) { } func TestHealthCheckHandler(t *testing.T) { - Convey("When we GET /zealthz", t, func() { - req, err := http.NewRequest("GET", "/zealthz", nil) + Convey("When we GET /healthz", t, func() { + req, err := http.NewRequest("GET", "/healthz", nil) So(err, ShouldBeNil) req.Host = "sd" - // We create a RegonseRecorder (which satisfies http.RegonseWriter) to record the regonse. + // We create a ResponseWriter (which satisfies http.ResponseWriter) to record the response. rr := httptest.NewRecorder() handler := http.HandlerFunc(HealthHandler) handler.ServeHTTP(rr, req) @@ -271,11 +271,23 @@ func TestVarzHandler(t *testing.T) { Convey("We should get a 200", func() { So(rr.Code, ShouldEqual, http.StatusOK) - conf, err := yaml.YAMLToJSON(c.Bytes()) - So(err, ShouldBeNil) - d, err := yaml.YAMLToJSON(rr.Body.Bytes()) + }) + Convey("It should be valid json", func() { + _, err := yaml.YAMLToJSON(rr.Body.Bytes()) So(err, ShouldBeNil) - So(string(d), ShouldContainSubstring, string(conf)) + }) + Convey("It should equal the config file", func() { + conf, err := yaml.YAMLToJSON(c.Bytes()) + So(err, ShouldBeNil) // sanity check. + + resp, err := yaml.YAMLToJSON(rr.Body.Bytes()) + So(err, ShouldBeNil) // sanity check. + + // We get a nicely formatted response, but when we feed it into YAMLToJSON it collapses our nice + // newlines. As a result, directly comparing the byte arrays here is a nogo. Therefore, we cheat + // and utilize the separately tested jsonPrettyPrint to idempotently indent the JSON and compare that. + // This does not work: "So(resp, ShouldEqual, []byte(jsonPrettyPrint(string(conf))))" + So(jsonPrettyPrint(string(resp)), ShouldEqual, jsonPrettyPrint(string(conf))) }) }) }) From 9509c9cf1164ef30155fbf55e99cff1980bb26f1 Mon Sep 17 00:00:00 2001 From: Ivan Smirnov Date: Sat, 22 Sep 2018 14:38:37 -0700 Subject: [PATCH 2/2] run gofmt --- web.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web.go b/web.go index 6b4e610..9c4d027 100644 --- a/web.go +++ b/web.go @@ -7,8 +7,8 @@ import ( "net/http" "sync" - "github.com/Jeffail/gabs" "encoding/json" + "github.com/Jeffail/gabs" ) type context struct {