From e70eb62d35fbf441d41b1bb35d75fbe43c6f402d Mon Sep 17 00:00:00 2001 From: x86kernel Date: Thu, 6 Apr 2017 05:39:49 +0000 Subject: [PATCH 1/3] Colorize Response for html --- formatter/formatter_test.go | 17 +++++++++++------ formatter/html.go | 17 +++++++++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/formatter/formatter_test.go b/formatter/formatter_test.go index 3c6f407..5b14a54 100644 --- a/formatter/formatter_test.go +++ b/formatter/formatter_test.go @@ -6,6 +6,7 @@ import ( "github.com/asciimoo/wuzz/config" "github.com/nwidger/jsoncolor" + "github.com/x86kernel/htmlcolor" ) func TestFormat(t *testing.T) { @@ -17,18 +18,22 @@ func TestFormat(t *testing.T) { var htmlBuffer bytes.Buffer New(configFixture(true), "text/html; charset=utf-8").Format(&htmlBuffer, []byte("unfomatted")) - if htmlBuffer.String() != "unfomatted" { + var htmltargetBuffer bytes.Buffer + htmlcolor.NewFormatter().Format(&htmltargetBuffer, []byte("unfomatted")) + htmltarget := htmltargetBuffer.String() + + if htmlBuffer.String() != htmltarget { t.Error("Expected html to eq " + htmlBuffer.String()) } var jsonEnabledBuffer bytes.Buffer New(configFixture(true), "application/json; charset=utf-8").Format(&jsonEnabledBuffer, []byte("{\"json\": \"some value\"}")) - var targetBuffer bytes.Buffer - jsoncolor.NewFormatter().Format(&targetBuffer, []byte("{\"json\": \"some value\"}")) - target := targetBuffer.String() + var jsontargetBuffer bytes.Buffer + jsoncolor.NewFormatter().Format(&jsontargetBuffer, []byte("{\"json\": \"some value\"}")) + jsontarget := jsontargetBuffer.String() - if jsonEnabledBuffer.String() != target { - t.Error("Expected json to eq \n" + jsonEnabledBuffer.String() + "\nbut not\n" + target) + if jsonEnabledBuffer.String() != jsontarget { + t.Error("Expected json to eq \n" + jsonEnabledBuffer.String() + "\nbut not\n" + jsontarget) } var jsonDisabledBuffer bytes.Buffer diff --git a/formatter/html.go b/formatter/html.go index 7a4ad4d..7a3d9c3 100644 --- a/formatter/html.go +++ b/formatter/html.go @@ -1,9 +1,26 @@ package formatter +import ( + "bytes" + "io" + + "github.com/x86kernel/htmlcolor" +) + type htmlFormatter struct { textFormatter } +func (f *htmlFormatter) Format(writer io.Writer, data []byte) error { + htmlFormatter := htmlcolor.NewFormatter() + buf := bytes.NewBuffer(make([]byte, 0, len(data))) + htmlFormatter.Format(buf, data) + + writer.Write(buf.Bytes()) + + return nil +} + func (f *htmlFormatter) Title() string { return "[html]" } From 272fe31414e27c42fb530c9e2965888e6d41e46d Mon Sep 17 00:00:00 2001 From: x86kernel Date: Thu, 6 Apr 2017 06:41:46 +0000 Subject: [PATCH 2/3] error handling --- formatter/html.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/formatter/html.go b/formatter/html.go index 7a3d9c3..dc17a0d 100644 --- a/formatter/html.go +++ b/formatter/html.go @@ -14,11 +14,14 @@ type htmlFormatter struct { func (f *htmlFormatter) Format(writer io.Writer, data []byte) error { htmlFormatter := htmlcolor.NewFormatter() buf := bytes.NewBuffer(make([]byte, 0, len(data))) - htmlFormatter.Format(buf, data) + err := htmlFormatter.Format(buf, data) - writer.Write(buf.Bytes()) + if err == io.EOF { + writer.Write(buf.Bytes()) + return nil + } - return nil + return errors.New("html formatter error") } func (f *htmlFormatter) Title() string { From cd9fe21b907f21a0d088fc38ea0e33f230bb5a4a Mon Sep 17 00:00:00 2001 From: x86kernel Date: Thu, 6 Apr 2017 06:43:36 +0000 Subject: [PATCH 3/3] import errors --- formatter/html.go | 1 + 1 file changed, 1 insertion(+) diff --git a/formatter/html.go b/formatter/html.go index dc17a0d..fa9bb94 100644 --- a/formatter/html.go +++ b/formatter/html.go @@ -2,6 +2,7 @@ package formatter import ( "bytes" + "errors" "io" "github.com/x86kernel/htmlcolor"