Skip to content

Commit

Permalink
Merge pull request #20 from maxzhang1985/dev
Browse files Browse the repository at this point in the history
unit test by render
  • Loading branch information
yoyofx authored Dec 2, 2019
2 parents 8aa61d8 + 45780c5 commit 9cea4cf
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 64 deletions.
120 changes: 56 additions & 64 deletions ResponseRender/Render_Test.go → Test/render_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package ResponseRender
package Test

import (
"bytes"
"encoding/xml"
"errors"
"github.com/golang/protobuf/proto"
"github.com/maxzhang1985/yoyogo/ResponseRender"
"github.com/maxzhang1985/yoyogo/Test/testdata/protoexample"
"github.com/stretchr/testify/assert"
"github.com/ugorji/go/codec"
"html/template"
Expand All @@ -21,10 +24,10 @@ func TestRenderMsgPack(t *testing.T) {
"foo": "bar",
}

(MsgPack{data}).WriteContentType(w)
(ResponseRender.MsgPack{data}).WriteContentType(w)
assert.Equal(t, "application/msgpack; charset=utf-8", w.Header().Get("Content-Type"))

err := (MsgPack{data}).Render(w)
err := (ResponseRender.MsgPack{data}).Render(w)

assert.NoError(t, err)

Expand All @@ -46,10 +49,10 @@ func TestRenderJSON(t *testing.T) {
"html": "<b>",
}

(Json{data}).WriteContentType(w)
(ResponseRender.Json{data}).WriteContentType(w)
assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))

err := (Json{data}).Render(w)
err := (ResponseRender.Json{data}).Render(w)

assert.NoError(t, err)
assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"\\u003cb\\u003e\"}\n", w.Body.String())
Expand All @@ -61,7 +64,7 @@ func TestRenderJSONPanics(t *testing.T) {
data := make(chan int)

// json: unsupported type: chan int
assert.Panics(t, func() { assert.NoError(t, (Json{data}).Render(w)) })
assert.Panics(t, func() { assert.NoError(t, (ResponseRender.Json{data}).Render(w)) })
}

func TestRenderIndentedJSON(t *testing.T) {
Expand All @@ -71,7 +74,7 @@ func TestRenderIndentedJSON(t *testing.T) {
"bar": "foo",
}

err := (IndentedJson{data}).Render(w)
err := (ResponseRender.IndentedJson{data}).Render(w)

assert.NoError(t, err)
assert.Equal(t, "{\n \"bar\": \"foo\",\n \"foo\": \"bar\"\n}", w.Body.String())
Expand All @@ -83,7 +86,7 @@ func TestRenderIndentedJSONPanics(t *testing.T) {
data := make(chan int)

// json: unsupported type: chan int
err := (IndentedJson{data}).Render(w)
err := (ResponseRender.IndentedJson{data}).Render(w)
assert.Error(t, err)
}

Expand All @@ -93,10 +96,10 @@ func TestRenderSecureJSON(t *testing.T) {
"foo": "bar",
}

(SecureJson{"while(1);", data}).WriteContentType(w1)
(ResponseRender.SecureJson{"while(1);", data}).WriteContentType(w1)
assert.Equal(t, "application/json; charset=utf-8", w1.Header().Get("Content-Type"))

err1 := (SecureJson{"while(1);", data}).Render(w1)
err1 := (ResponseRender.SecureJson{"while(1);", data}).Render(w1)

assert.NoError(t, err1)
assert.Equal(t, "{\"foo\":\"bar\"}", w1.Body.String())
Expand All @@ -109,7 +112,7 @@ func TestRenderSecureJSON(t *testing.T) {
"bar": "foo",
}}

err2 := (SecureJson{"while(1);", datas}).Render(w2)
err2 := (ResponseRender.SecureJson{"while(1);", datas}).Render(w2)
assert.NoError(t, err2)
assert.Equal(t, "while(1);[{\"foo\":\"bar\"},{\"bar\":\"foo\"}]", w2.Body.String())
assert.Equal(t, "application/json; charset=utf-8", w2.Header().Get("Content-Type"))
Expand All @@ -120,7 +123,7 @@ func TestRenderSecureJSONFail(t *testing.T) {
data := make(chan int)

// json: unsupported type: chan int
err := (SecureJson{"while(1);", data}).Render(w)
err := (ResponseRender.SecureJson{"while(1);", data}).Render(w)
assert.Error(t, err)
}

Expand All @@ -130,10 +133,10 @@ func TestRenderJsonpJSON(t *testing.T) {
"foo": "bar",
}

(Jsonp{"x", data}).WriteContentType(w1)
(ResponseRender.Jsonp{"x", data}).WriteContentType(w1)
assert.Equal(t, "application/javascript; charset=utf-8", w1.Header().Get("Content-Type"))

err1 := (Jsonp{"x", data}).Render(w1)
err1 := (ResponseRender.Jsonp{"x", data}).Render(w1)

assert.NoError(t, err1)
assert.Equal(t, "x({\"foo\":\"bar\"});", w1.Body.String())
Expand All @@ -146,7 +149,7 @@ func TestRenderJsonpJSON(t *testing.T) {
"bar": "foo",
}}

err2 := (Jsonp{"x", datas}).Render(w2)
err2 := (ResponseRender.Jsonp{"x", datas}).Render(w2)
assert.NoError(t, err2)
assert.Equal(t, "x([{\"foo\":\"bar\"},{\"bar\":\"foo\"}]);", w2.Body.String())
assert.Equal(t, "application/javascript; charset=utf-8", w2.Header().Get("Content-Type"))
Expand All @@ -157,10 +160,10 @@ func TestRenderJsonpJSONError2(t *testing.T) {
data := map[string]interface{}{
"foo": "bar",
}
(Jsonp{"", data}).WriteContentType(w)
(ResponseRender.Jsonp{"", data}).WriteContentType(w)
assert.Equal(t, "application/javascript; charset=utf-8", w.Header().Get("Content-Type"))

e := (Jsonp{"", data}).Render(w)
e := (ResponseRender.Jsonp{"", data}).Render(w)
assert.NoError(t, e)

assert.Equal(t, "{\"foo\":\"bar\"}", w.Body.String())
Expand All @@ -172,7 +175,7 @@ func TestRenderJsonpJSONFail(t *testing.T) {
data := make(chan int)

// json: unsupported type: chan int
err := (Jsonp{"x", data}).Render(w)
err := (ResponseRender.Jsonp{"x", data}).Render(w)
assert.Error(t, err)
}

Expand All @@ -183,7 +186,7 @@ func TestRenderAsciiJSON(t *testing.T) {
"tag": "<br>",
}

err := (AsciiJson{data1}).Render(w1)
err := (ResponseRender.AsciiJson{data1}).Render(w1)

assert.NoError(t, err)
assert.Equal(t, "{\"lang\":\"GO\\u8bed\\u8a00\",\"tag\":\"\\u003cbr\\u003e\"}", w1.Body.String())
Expand All @@ -192,7 +195,7 @@ func TestRenderAsciiJSON(t *testing.T) {
w2 := httptest.NewRecorder()
data2 := float64(3.1415926)

err = (AsciiJson{data2}).Render(w2)
err = (ResponseRender.AsciiJson{data2}).Render(w2)
assert.NoError(t, err)
assert.Equal(t, "3.1415926", w2.Body.String())
}
Expand All @@ -202,7 +205,7 @@ func TestRenderAsciiJSONFail(t *testing.T) {
data := make(chan int)

// json: unsupported type: chan int
assert.Error(t, (AsciiJson{data}).Render(w))
assert.Error(t, (ResponseRender.AsciiJson{data}).Render(w))
}

func TestRenderPureJSON(t *testing.T) {
Expand All @@ -211,7 +214,7 @@ func TestRenderPureJSON(t *testing.T) {
"foo": "bar",
"html": "<b>",
}
err := (PureJson{data}).Render(w)
err := (ResponseRender.PureJson{data}).Render(w)
assert.NoError(t, err)
assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"<b>\"}\n", w.Body.String())
assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
Expand Down Expand Up @@ -249,10 +252,10 @@ b:
c: 2
d: [3, 4]
`
(YAML{data}).WriteContentType(w)
(ResponseRender.YAML{data}).WriteContentType(w)
assert.Equal(t, "application/x-yaml; charset=utf-8", w.Header().Get("Content-Type"))

err := (YAML{data}).Render(w)
err := (ResponseRender.YAML{data}).Render(w)
assert.NoError(t, err)
assert.Equal(t, "\"\\na : Easy!\\nb:\\n\\tc: 2\\n\\td: [3, 4]\\n\\t\"\n", w.Body.String())
assert.Equal(t, "application/x-yaml; charset=utf-8", w.Header().Get("Content-Type"))
Expand All @@ -267,53 +270,42 @@ func (ft *fail) MarshalYAML() (interface{}, error) {

func TestRenderYAMLFail(t *testing.T) {
w := httptest.NewRecorder()
err := (YAML{&fail{}}).Render(w)
err := (ResponseRender.YAML{&fail{}}).Render(w)
assert.Error(t, err)
}

// test Protobuf rendering
func TestRenderProtoBuf(t *testing.T) {
w := httptest.NewRecorder()
data := ""
//reps := []int64{int64(1), int64(2)}
//label := "test"
reps := []int64{int64(1), int64(2)}
label := "test"

//data := &testdata.Test{
// Label: &label,
// Reps: reps,
//}

(ProtoBuf{data}).WriteContentType(w)
protoData := ""
data := &protoexample.Test{
Label: &label,
Reps: reps,
}

//protoData, err := proto.Marshal(data)
//assert.NoError(t, err)
(ResponseRender.ProtoBuf{data}).WriteContentType(w)
protoData, err := proto.Marshal(data)
assert.NoError(t, err)
assert.Equal(t, "application/x-protobuf", w.Header().Get("Content-Type"))

err := (ProtoBuf{data}).Render(w)
err = (ResponseRender.ProtoBuf{data}).Render(w)

assert.NoError(t, err)
assert.Equal(t, string(protoData), w.Body.String())
assert.Equal(t, "application/x-protobuf", w.Header().Get("Content-Type"))
}

func TestRenderProtoBufFail(t *testing.T) {
//w := httptest.NewRecorder()
//data := &testdata.Test{}
//err := (ProtoBuf{data}).Render(w)
//assert.Error(t, err)
}

func TestRenderXML(t *testing.T) {
w := httptest.NewRecorder()
data := xmlmap{
"foo": "bar",
}

(XML{data}).WriteContentType(w)
(ResponseRender.XML{data}).WriteContentType(w)
assert.Equal(t, "application/xml; charset=utf-8", w.Header().Get("Content-Type"))

err := (XML{data}).Render(w)
err := (ResponseRender.XML{data}).Render(w)

assert.NoError(t, err)
assert.Equal(t, "<map><foo>bar</foo></map>", w.Body.String())
Expand All @@ -324,7 +316,7 @@ func TestRenderRedirect(t *testing.T) {
req, err := http.NewRequest("GET", "/test-redirect", nil)
assert.NoError(t, err)

data1 := Redirect{
data1 := ResponseRender.Redirect{
Code: http.StatusMovedPermanently,
Request: req,
Location: "/new/location",
Expand All @@ -334,7 +326,7 @@ func TestRenderRedirect(t *testing.T) {
err = data1.Render(w)
assert.NoError(t, err)

data2 := Redirect{
data2 := ResponseRender.Redirect{
Code: http.StatusOK,
Request: req,
Location: "/new/location",
Expand All @@ -343,7 +335,7 @@ func TestRenderRedirect(t *testing.T) {
w = httptest.NewRecorder()
assert.PanicsWithValue(t, "Cannot redirect with status code 200", func() { data2.Render(w) })

data3 := Redirect{
data3 := ResponseRender.Redirect{
Code: http.StatusCreated,
Request: req,
Location: "/new/location",
Expand All @@ -361,7 +353,7 @@ func TestRenderData(t *testing.T) {
w := httptest.NewRecorder()
data := []byte("#!PNG some raw data")

err := (Data{
err := (ResponseRender.Data{
ContentType: "image/png",
Data: data,
}).Render(w)
Expand All @@ -374,13 +366,13 @@ func TestRenderData(t *testing.T) {
func TestRenderString(t *testing.T) {
w := httptest.NewRecorder()

(Text{
(ResponseRender.Text{
Format: "hello %s %d",
Data: []interface{}{},
}).WriteContentType(w)
assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))

err := (Text{
err := (ResponseRender.Text{
Format: "hola %s %d",
Data: []interface{}{"manu", 2},
}).Render(w)
Expand All @@ -393,7 +385,7 @@ func TestRenderString(t *testing.T) {
func TestRenderStringLenZero(t *testing.T) {
w := httptest.NewRecorder()

err := (Text{
err := (ResponseRender.Text{
Format: "hola %s %d",
Data: []interface{}{},
}).Render(w)
Expand All @@ -407,7 +399,7 @@ func TestRenderHTMLTemplate(t *testing.T) {
w := httptest.NewRecorder()
templ := template.Must(template.New("t").Parse(`Hello {{.name}}`))

htmlRender := HTMLProduction{Template: templ}
htmlRender := ResponseRender.HTMLProduction{Template: templ}
instance := htmlRender.Instance("t", map[string]interface{}{
"name": "alexandernyquist",
})
Expand All @@ -423,7 +415,7 @@ func TestRenderHTMLTemplateEmptyName(t *testing.T) {
w := httptest.NewRecorder()
templ := template.Must(template.New("").Parse(`Hello {{.name}}`))

htmlRender := HTMLProduction{Template: templ}
htmlRender := ResponseRender.HTMLProduction{Template: templ}
instance := htmlRender.Instance("", map[string]interface{}{
"name": "alexandernyquist",
})
Expand All @@ -437,9 +429,9 @@ func TestRenderHTMLTemplateEmptyName(t *testing.T) {

func TestRenderHTMLDebugFiles(t *testing.T) {
w := httptest.NewRecorder()
htmlRender := HTMLDebug{Files: []string{"../testdata/template/hello.tmpl"},
htmlRender := ResponseRender.HTMLDebug{Files: []string{"testdata/template/hello.tmpl"},
Glob: "",
Delims: Delims{Left: "{[{", Right: "}]}"},
Delims: ResponseRender.Delims{Left: "{[{", Right: "}]}"},
FuncMap: nil,
}
instance := htmlRender.Instance("hello.tmpl", map[string]interface{}{
Expand All @@ -455,9 +447,9 @@ func TestRenderHTMLDebugFiles(t *testing.T) {

func TestRenderHTMLDebugGlob(t *testing.T) {
w := httptest.NewRecorder()
htmlRender := HTMLDebug{Files: nil,
Glob: "../testdata/template/hello*",
Delims: Delims{Left: "{[{", Right: "}]}"},
htmlRender := ResponseRender.HTMLDebug{Files: nil,
Glob: "testdata/template/hello*",
Delims: ResponseRender.Delims{Left: "{[{", Right: "}]}"},
FuncMap: nil,
}
instance := htmlRender.Instance("hello.tmpl", map[string]interface{}{
Expand All @@ -472,9 +464,9 @@ func TestRenderHTMLDebugGlob(t *testing.T) {
}

func TestRenderHTMLDebugPanics(t *testing.T) {
htmlRender := HTMLDebug{Files: nil,
htmlRender := ResponseRender.HTMLDebug{Files: nil,
Glob: "",
Delims: Delims{"{{", "}}"},
Delims: ResponseRender.Delims{"{{", "}}"},
FuncMap: nil,
}
assert.Panics(t, func() { htmlRender.Instance("", nil) })
Expand Down
Loading

0 comments on commit 9cea4cf

Please sign in to comment.