mirror of
https://github.com/asciimoo/wuzz
synced 2024-11-10 13:10:29 +00:00
Merge pull request #98 from x86kernel/master
Colorize Response for html
This commit is contained in:
commit
66c5aa2526
@ -6,6 +6,7 @@ import (
|
|||||||
|
|
||||||
"github.com/asciimoo/wuzz/config"
|
"github.com/asciimoo/wuzz/config"
|
||||||
"github.com/nwidger/jsoncolor"
|
"github.com/nwidger/jsoncolor"
|
||||||
|
"github.com/x86kernel/htmlcolor"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFormat(t *testing.T) {
|
func TestFormat(t *testing.T) {
|
||||||
@ -17,18 +18,22 @@ func TestFormat(t *testing.T) {
|
|||||||
|
|
||||||
var htmlBuffer bytes.Buffer
|
var htmlBuffer bytes.Buffer
|
||||||
New(configFixture(true), "text/html; charset=utf-8").Format(&htmlBuffer, []byte("<html><span>unfomatted</span></html>"))
|
New(configFixture(true), "text/html; charset=utf-8").Format(&htmlBuffer, []byte("<html><span>unfomatted</span></html>"))
|
||||||
if htmlBuffer.String() != "<html><span>unfomatted</span></html>" {
|
var htmltargetBuffer bytes.Buffer
|
||||||
|
htmlcolor.NewFormatter().Format(&htmltargetBuffer, []byte("<html><span>unfomatted</span></html>"))
|
||||||
|
htmltarget := htmltargetBuffer.String()
|
||||||
|
|
||||||
|
if htmlBuffer.String() != htmltarget {
|
||||||
t.Error("Expected html to eq " + htmlBuffer.String())
|
t.Error("Expected html to eq " + htmlBuffer.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
var jsonEnabledBuffer bytes.Buffer
|
var jsonEnabledBuffer bytes.Buffer
|
||||||
New(configFixture(true), "application/json; charset=utf-8").Format(&jsonEnabledBuffer, []byte("{\"json\": \"some value\"}"))
|
New(configFixture(true), "application/json; charset=utf-8").Format(&jsonEnabledBuffer, []byte("{\"json\": \"some value\"}"))
|
||||||
var targetBuffer bytes.Buffer
|
var jsontargetBuffer bytes.Buffer
|
||||||
jsoncolor.NewFormatter().Format(&targetBuffer, []byte("{\"json\": \"some value\"}"))
|
jsoncolor.NewFormatter().Format(&jsontargetBuffer, []byte("{\"json\": \"some value\"}"))
|
||||||
target := targetBuffer.String()
|
jsontarget := jsontargetBuffer.String()
|
||||||
|
|
||||||
if jsonEnabledBuffer.String() != target {
|
if jsonEnabledBuffer.String() != jsontarget {
|
||||||
t.Error("Expected json to eq \n" + jsonEnabledBuffer.String() + "\nbut not\n" + target)
|
t.Error("Expected json to eq \n" + jsonEnabledBuffer.String() + "\nbut not\n" + jsontarget)
|
||||||
}
|
}
|
||||||
|
|
||||||
var jsonDisabledBuffer bytes.Buffer
|
var jsonDisabledBuffer bytes.Buffer
|
||||||
|
@ -1,9 +1,30 @@
|
|||||||
package formatter
|
package formatter
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"github.com/x86kernel/htmlcolor"
|
||||||
|
)
|
||||||
|
|
||||||
type htmlFormatter struct {
|
type htmlFormatter struct {
|
||||||
textFormatter
|
textFormatter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *htmlFormatter) Format(writer io.Writer, data []byte) error {
|
||||||
|
htmlFormatter := htmlcolor.NewFormatter()
|
||||||
|
buf := bytes.NewBuffer(make([]byte, 0, len(data)))
|
||||||
|
err := htmlFormatter.Format(buf, data)
|
||||||
|
|
||||||
|
if err == io.EOF {
|
||||||
|
writer.Write(buf.Bytes())
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return errors.New("html formatter error")
|
||||||
|
}
|
||||||
|
|
||||||
func (f *htmlFormatter) Title() string {
|
func (f *htmlFormatter) Title() string {
|
||||||
return "[html]"
|
return "[html]"
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user