gin/render/render_test.go

192 lines
4.6 KiB
Go
Raw Normal View History

2015-04-09 13:15:02 +03:00
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package render
import (
"bytes"
"encoding/xml"
2015-04-09 13:15:02 +03:00
"html/template"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/ugorji/go/codec"
2015-04-09 13:15:02 +03:00
)
2015-05-09 04:35:31 +03:00
// TODO unit tests
// test errors
func TestRenderMsgPack(t *testing.T) {
w := httptest.NewRecorder()
data := map[string]interface{}{
"foo": "bar",
}
err := (MsgPack{data}).Render(w)
assert.NoError(t, err)
h := new(codec.MsgpackHandle)
assert.NotNil(t, h)
buf := bytes.NewBuffer([]byte{})
assert.NotNil(t, buf)
err = codec.NewEncoder(buf, h).Encode(data)
assert.NoError(t, err)
assert.Equal(t, w.Body.String(), string(buf.Bytes()))
assert.Equal(t, w.Header().Get("Content-Type"), "application/msgpack; charset=utf-8")
}
2015-04-09 13:15:02 +03:00
func TestRenderJSON(t *testing.T) {
w := httptest.NewRecorder()
data := map[string]interface{}{
2015-04-09 13:15:02 +03:00
"foo": "bar",
}
2015-06-04 06:25:21 +03:00
err := (JSON{data}).Render(w)
2015-04-09 13:15:02 +03:00
assert.NoError(t, err)
assert.Equal(t, "{\"foo\":\"bar\"}", w.Body.String())
assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
2015-04-09 13:15:02 +03:00
}
func TestRenderIndentedJSON(t *testing.T) {
w := httptest.NewRecorder()
2015-05-18 16:45:24 +03:00
data := map[string]interface{}{
2015-04-09 13:15:02 +03:00
"foo": "bar",
"bar": "foo",
2015-05-18 16:45:24 +03:00
}
2015-06-04 06:25:21 +03:00
err := (IndentedJSON{data}).Render(w)
2015-04-09 13:15:02 +03:00
assert.NoError(t, err)
assert.Equal(t, w.Body.String(), "{\n \"bar\": \"foo\",\n \"foo\": \"bar\"\n}")
assert.Equal(t, w.Header().Get("Content-Type"), "application/json; charset=utf-8")
}
func TestRenderSecureJSON(t *testing.T) {
w1 := httptest.NewRecorder()
data := map[string]interface{}{
"foo": "bar",
}
err1 := (SecureJSON{"while(1);", data}).Render(w1)
assert.NoError(t, err1)
assert.Equal(t, "{\"foo\":\"bar\"}", w1.Body.String())
assert.Equal(t, "application/json; charset=utf-8", w1.Header().Get("Content-Type"))
w2 := httptest.NewRecorder()
datas := []map[string]interface{}{{
"foo": "bar",
}, {
"bar": "foo",
}}
err2 := (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"))
}
type xmlmap map[string]interface{}
// Allows type H to be used with xml.Marshal
func (h xmlmap) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
start.Name = xml.Name{
Space: "",
Local: "map",
}
if err := e.EncodeToken(start); err != nil {
return err
}
for key, value := range h {
elem := xml.StartElement{
Name: xml.Name{Space: "", Local: key},
Attr: []xml.Attr{},
}
if err := e.EncodeElement(value, elem); err != nil {
return err
}
}
if err := e.EncodeToken(xml.EndElement{Name: start.Name}); err != nil {
return err
}
return nil
}
func TestRenderXML(t *testing.T) {
w := httptest.NewRecorder()
data := xmlmap{
"foo": "bar",
}
2015-06-04 06:25:21 +03:00
err := (XML{data}).Render(w)
assert.NoError(t, err)
assert.Equal(t, "<map><foo>bar</foo></map>", w.Body.String())
assert.Equal(t, "application/xml; charset=utf-8", w.Header().Get("Content-Type"))
}
func TestRenderRedirect(t *testing.T) {
// TODO
}
func TestRenderData(t *testing.T) {
w := httptest.NewRecorder()
data := []byte("#!PNG some raw data")
2015-05-18 16:45:24 +03:00
err := (Data{
ContentType: "image/png",
Data: data,
2015-06-04 06:25:21 +03:00
}).Render(w)
assert.NoError(t, err)
assert.Equal(t, "#!PNG some raw data", w.Body.String())
assert.Equal(t, "image/png", w.Header().Get("Content-Type"))
}
2015-05-18 16:45:24 +03:00
func TestRenderString(t *testing.T) {
2015-04-09 13:15:02 +03:00
w := httptest.NewRecorder()
2015-05-18 16:45:24 +03:00
err := (String{
Format: "hola %s %d",
Data: []interface{}{"manu", 2},
2015-06-04 06:25:21 +03:00
}).Render(w)
2015-04-09 13:15:02 +03:00
assert.NoError(t, err)
assert.Equal(t, "hola manu 2", w.Body.String())
assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))
2015-04-09 13:15:02 +03:00
}
func TestRenderStringHTML(t *testing.T) {
w := httptest.NewRecorder()
err := (StringHTML{
Format: "<html><body><h1>Hola mi %s numero %d</h1></body></html>",
Data: []interface{}{"amigo", 1},
}).Render(w)
assert.NoError(t, err)
assert.Equal(t, "<html><body><h1>Hola mi amigo numero 1</h1></body></html>", w.Body.String())
assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))
}
2015-04-09 13:15:02 +03:00
func TestRenderHTMLTemplate(t *testing.T) {
w := httptest.NewRecorder()
templ := template.Must(template.New("t").Parse(`Hello {{.name}}`))
2015-05-18 16:45:24 +03:00
htmlRender := HTMLProduction{Template: templ}
instance := htmlRender.Instance("t", map[string]interface{}{
2015-04-09 13:15:02 +03:00
"name": "alexandernyquist",
})
2015-06-04 06:25:21 +03:00
err := instance.Render(w)
2015-05-18 16:45:24 +03:00
2015-04-09 13:15:02 +03:00
assert.NoError(t, err)
assert.Equal(t, "Hello alexandernyquist", w.Body.String())
assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))
2015-04-09 13:15:02 +03:00
}