From 7889b60e559ab3a991a103a426464093ac0e8350 Mon Sep 17 00:00:00 2001 From: maxzhang1985 Date: Mon, 2 Dec 2019 23:03:21 +0800 Subject: [PATCH 1/2] unit test by render --- ResponseRender/{Render_Test.go => render_test.go} | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) rename ResponseRender/{Render_Test.go => render_test.go} (98%) diff --git a/ResponseRender/Render_Test.go b/ResponseRender/render_test.go similarity index 98% rename from ResponseRender/Render_Test.go rename to ResponseRender/render_test.go index 3b313c24..7b7e6e6a 100644 --- a/ResponseRender/Render_Test.go +++ b/ResponseRender/render_test.go @@ -289,11 +289,11 @@ func TestRenderProtoBuf(t *testing.T) { //protoData, err := proto.Marshal(data) //assert.NoError(t, err) assert.Equal(t, "application/x-protobuf", w.Header().Get("Content-Type")) + //data2 := proto.String("") + //_ = (ProtoBuf{data2}).Render(w) - err := (ProtoBuf{data}).Render(w) - - assert.NoError(t, err) - assert.Equal(t, string(protoData), w.Body.String()) + //assert.NoError(t, err) + assert.Equal(t, protoData, w.Body.String()) assert.Equal(t, "application/x-protobuf", w.Header().Get("Content-Type")) } From 45780c5315ae01009e4c57384d912cb7531b2b53 Mon Sep 17 00:00:00 2001 From: maxzhang1985 Date: Mon, 2 Dec 2019 23:29:29 +0800 Subject: [PATCH 2/2] unit test by render --- {ResponseRender => Test}/render_test.go | 124 +++++++++++------------- Test/testdata/protoexample/test.pb.go | 113 +++++++++++++++++++++ Test/testdata/protoexample/test.proto | 12 +++ Test/testdata/template/hello.tmpl | 1 + Test/testdata/template/raw.tmpl | 1 + 5 files changed, 185 insertions(+), 66 deletions(-) rename {ResponseRender => Test}/render_test.go (79%) create mode 100644 Test/testdata/protoexample/test.pb.go create mode 100644 Test/testdata/protoexample/test.proto create mode 100644 Test/testdata/template/hello.tmpl create mode 100644 Test/testdata/template/raw.tmpl diff --git a/ResponseRender/render_test.go b/Test/render_test.go similarity index 79% rename from ResponseRender/render_test.go rename to Test/render_test.go index 7b7e6e6a..1f325b69 100644 --- a/ResponseRender/render_test.go +++ b/Test/render_test.go @@ -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" @@ -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) @@ -46,10 +49,10 @@ func TestRenderJSON(t *testing.T) { "html": "", } - (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()) @@ -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) { @@ -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()) @@ -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) } @@ -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()) @@ -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")) @@ -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) } @@ -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()) @@ -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")) @@ -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()) @@ -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) } @@ -183,7 +186,7 @@ func TestRenderAsciiJSON(t *testing.T) { "tag": "
", } - 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()) @@ -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()) } @@ -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) { @@ -211,7 +214,7 @@ func TestRenderPureJSON(t *testing.T) { "foo": "bar", "html": "", } - err := (PureJson{data}).Render(w) + err := (ResponseRender.PureJson{data}).Render(w) assert.NoError(t, err) assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"\"}\n", w.Body.String()) assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type")) @@ -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")) @@ -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")) - //data2 := proto.String("") - //_ = (ProtoBuf{data2}).Render(w) + err = (ResponseRender.ProtoBuf{data}).Render(w) - //assert.NoError(t, err) - assert.Equal(t, protoData, w.Body.String()) + 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, "bar", w.Body.String()) @@ -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", @@ -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", @@ -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", @@ -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) @@ -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) @@ -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) @@ -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", }) @@ -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", }) @@ -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{}{ @@ -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{}{ @@ -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) }) diff --git a/Test/testdata/protoexample/test.pb.go b/Test/testdata/protoexample/test.pb.go new file mode 100644 index 00000000..21997ca1 --- /dev/null +++ b/Test/testdata/protoexample/test.pb.go @@ -0,0 +1,113 @@ +// Code generated by protoc-gen-go. +// source: test.proto +// DO NOT EDIT! + +/* +Package protoexample is a generated protocol buffer package. + +It is generated from these files: + test.proto + +It has these top-level messages: + Test +*/ +package protoexample + +import proto "github.com/golang/protobuf/proto" +import math "math" + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = math.Inf + +type FOO int32 + +const ( + FOO_X FOO = 17 +) + +var FOO_name = map[int32]string{ + 17: "X", +} +var FOO_value = map[string]int32{ + "X": 17, +} + +func (x FOO) Enum() *FOO { + p := new(FOO) + *p = x + return p +} +func (x FOO) String() string { + return proto.EnumName(FOO_name, int32(x)) +} +func (x *FOO) UnmarshalJSON(data []byte) error { + value, err := proto.UnmarshalJSONEnum(FOO_value, data, "FOO") + if err != nil { + return err + } + *x = FOO(value) + return nil +} + +type Test struct { + Label *string `protobuf:"bytes,1,req,name=label" json:"label,omitempty"` + Type *int32 `protobuf:"varint,2,opt,name=type,def=77" json:"type,omitempty"` + Reps []int64 `protobuf:"varint,3,rep,name=reps" json:"reps,omitempty"` + Optionalgroup *Test_OptionalGroup `protobuf:"group,4,opt,name=OptionalGroup" json:"optionalgroup,omitempty"` + XXX_unrecognized []byte `json:"-"` +} + +func (m *Test) Reset() { *m = Test{} } +func (m *Test) String() string { return proto.CompactTextString(m) } +func (*Test) ProtoMessage() {} + +const Default_Test_Type int32 = 77 + +func (m *Test) GetLabel() string { + if m != nil && m.Label != nil { + return *m.Label + } + return "" +} + +func (m *Test) GetType() int32 { + if m != nil && m.Type != nil { + return *m.Type + } + return Default_Test_Type +} + +func (m *Test) GetReps() []int64 { + if m != nil { + return m.Reps + } + return nil +} + +func (m *Test) GetOptionalgroup() *Test_OptionalGroup { + if m != nil { + return m.Optionalgroup + } + return nil +} + +type Test_OptionalGroup struct { + RequiredField *string `protobuf:"bytes,5,req" json:"RequiredField,omitempty"` + XXX_unrecognized []byte `json:"-"` +} + +func (m *Test_OptionalGroup) Reset() { *m = Test_OptionalGroup{} } +func (m *Test_OptionalGroup) String() string { return proto.CompactTextString(m) } +func (*Test_OptionalGroup) ProtoMessage() {} + +func (m *Test_OptionalGroup) GetRequiredField() string { + if m != nil && m.RequiredField != nil { + return *m.RequiredField + } + return "" +} + +func init() { + proto.RegisterEnum("protoexample.FOO", FOO_name, FOO_value) +} diff --git a/Test/testdata/protoexample/test.proto b/Test/testdata/protoexample/test.proto new file mode 100644 index 00000000..3e734287 --- /dev/null +++ b/Test/testdata/protoexample/test.proto @@ -0,0 +1,12 @@ +package protoexample; + +enum FOO {X=17;}; + +message Test { + required string label = 1; + optional int32 type = 2[default=77]; + repeated int64 reps = 3; + optional group OptionalGroup = 4{ + required string RequiredField = 5; + } +} diff --git a/Test/testdata/template/hello.tmpl b/Test/testdata/template/hello.tmpl new file mode 100644 index 00000000..0767ef3f --- /dev/null +++ b/Test/testdata/template/hello.tmpl @@ -0,0 +1 @@ +

Hello {[{.name}]}

\ No newline at end of file diff --git a/Test/testdata/template/raw.tmpl b/Test/testdata/template/raw.tmpl new file mode 100644 index 00000000..8bc75703 --- /dev/null +++ b/Test/testdata/template/raw.tmpl @@ -0,0 +1 @@ +Date: {[{.now | formatAsDate}]}