mirror of https://github.com/asciimoo/wuzz
feature: result formatter extensibility with minor refactor and several testsy
parent
e965e65506
commit
e6e364370d
@ -0,0 +1,23 @@
|
||||
package formatter
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
type binaryFormatter struct {
|
||||
}
|
||||
|
||||
func (f *binaryFormatter) Format(writer io.Writer, data []byte) error {
|
||||
fmt.Fprint(writer, hex.Dump(data))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *binaryFormatter) Title() string {
|
||||
return "[binary]"
|
||||
}
|
||||
|
||||
func (f *binaryFormatter) Searchable() bool {
|
||||
return false
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package formatter
|
||||
|
||||
import (
|
||||
"io"
|
||||
"mime"
|
||||
"strings"
|
||||
|
||||
"github.com/asciimoo/wuzz/config"
|
||||
)
|
||||
|
||||
type ResponseFormatter interface {
|
||||
Format(writer io.Writer, data []byte) error
|
||||
Title() string
|
||||
Searchable() bool
|
||||
}
|
||||
|
||||
func New(appConfig *config.Config, contentType string) ResponseFormatter {
|
||||
ctype, _, err := mime.ParseMediaType(contentType)
|
||||
if err == nil && appConfig.General.FormatJSON && (ctype == config.ContentTypes["json"] || strings.HasSuffix(ctype, "+json")) {
|
||||
return &jsonFormatter{}
|
||||
} else if strings.Contains(contentType, "text/html") {
|
||||
return &htmlFormatter{}
|
||||
} else if strings.Index(contentType, "text") == -1 && strings.Index(contentType, "application") == -1 {
|
||||
return &binaryFormatter{}
|
||||
} else {
|
||||
return &textFormatter{}
|
||||
}
|
||||
}
|
@ -0,0 +1,97 @@
|
||||
package formatter
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/asciimoo/wuzz/config"
|
||||
"github.com/nwidger/jsoncolor"
|
||||
)
|
||||
|
||||
func TestFormat(t *testing.T) {
|
||||
var binBuffer bytes.Buffer
|
||||
New(configFixture(true), "octet-stream").Format(&binBuffer, []byte("some binary data"))
|
||||
if binBuffer.String() != "00000000 73 6f 6d 65 20 62 69 6e 61 72 79 20 64 61 74 61 |some binary data|\n" {
|
||||
t.Error("Expected binary to eq " + binBuffer.String())
|
||||
}
|
||||
|
||||
var htmlBuffer bytes.Buffer
|
||||
New(configFixture(true), "text/html; charset=utf-8").Format(&htmlBuffer, []byte("<html><span>unfomatted</span></html>"))
|
||||
if htmlBuffer.String() != "<html><span>unfomatted</span></html>" {
|
||||
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()
|
||||
|
||||
if jsonEnabledBuffer.String() != target {
|
||||
t.Error("Expected json to eq \n" + jsonEnabledBuffer.String() + "\nbut not\n" + target)
|
||||
}
|
||||
|
||||
var jsonDisabledBuffer bytes.Buffer
|
||||
New(configFixture(false), "application/json; charset=utf-8").Format(&jsonDisabledBuffer, []byte("{\"json\": \"some value\"}"))
|
||||
if jsonDisabledBuffer.String() != "{\"json\": \"some value\"}" {
|
||||
t.Error("Expected json to eq " + jsonDisabledBuffer.String())
|
||||
}
|
||||
|
||||
var textBuffer bytes.Buffer
|
||||
New(configFixture(true), "text/html; charset=utf-8").Format(&textBuffer, []byte("some text"))
|
||||
if textBuffer.String() != "some text" {
|
||||
t.Error("Expected text to eq " + textBuffer.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestTitle(t *testing.T) {
|
||||
//binary
|
||||
title := New(configFixture(true), "octet-stream").Title()
|
||||
if title != "[binary]" {
|
||||
t.Error("for octet-stream content type expected title ", title, "to be [binary]")
|
||||
}
|
||||
|
||||
//html
|
||||
title = New(configFixture(true), "text/html; charset=utf-8").Title()
|
||||
if title != "[html]" {
|
||||
t.Error("For text/html content type expected title ", title, " to be [html]")
|
||||
}
|
||||
|
||||
//json
|
||||
title = New(configFixture(true), "application/json; charset=utf-8").Title()
|
||||
if title != "[json]" {
|
||||
t.Error("For text/html content type expected title ", title, " to be [json]")
|
||||
}
|
||||
|
||||
//text
|
||||
title = New(configFixture(true), "text/plain; charset=utf-8").Title()
|
||||
if title != "[text]" {
|
||||
t.Error("For text/html content type expected title ", title, " to be [text]")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchable(t *testing.T) {
|
||||
if New(configFixture(true), "octet-stream").Searchable() {
|
||||
t.Error("binary file can't be searchable")
|
||||
}
|
||||
|
||||
if !New(configFixture(true), "text/html").Searchable() {
|
||||
t.Error("text/html should be searchable")
|
||||
}
|
||||
|
||||
if !New(configFixture(true), "application/json").Searchable() {
|
||||
t.Error("application/json should be searchable")
|
||||
}
|
||||
if !New(configFixture(true), "text/plain").Searchable() {
|
||||
t.Error("text/plain should be searchable")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func configFixture(jsonEnabled bool) *config.Config {
|
||||
return &config.Config{
|
||||
General: config.GeneralOptions{
|
||||
FormatJSON: jsonEnabled,
|
||||
},
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package formatter
|
||||
|
||||
type htmlFormatter struct {
|
||||
textFormatter
|
||||
}
|
||||
|
||||
func (f *htmlFormatter) Title() string {
|
||||
return "[html]"
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package formatter
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"io"
|
||||
|
||||
"github.com/nwidger/jsoncolor"
|
||||
)
|
||||
|
||||
type jsonFormatter struct {
|
||||
textFormatter
|
||||
}
|
||||
|
||||
func (f *jsonFormatter) Format(writer io.Writer, data []byte) error {
|
||||
jsonFormatter := jsoncolor.NewFormatter()
|
||||
buf := bytes.NewBuffer(make([]byte, 0, len(data)))
|
||||
err := jsonFormatter.Format(buf, data)
|
||||
if err == nil {
|
||||
writer.Write(buf.Bytes())
|
||||
return nil
|
||||
}
|
||||
return errors.New("json formatter error")
|
||||
}
|
||||
|
||||
func (f *jsonFormatter) Title() string {
|
||||
return "[json]"
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package formatter
|
||||
|
||||
import (
|
||||
"io"
|
||||
)
|
||||
|
||||
type textFormatter struct {
|
||||
}
|
||||
|
||||
func (f *textFormatter) Format(writer io.Writer, data []byte) error {
|
||||
_, err := writer.Write(data)
|
||||
return err
|
||||
}
|
||||
|
||||
func (f *textFormatter) Title() string {
|
||||
return "[text]"
|
||||
}
|
||||
|
||||
func (f *textFormatter) Searchable() bool {
|
||||
return true
|
||||
}
|
Loading…
Reference in New Issue