gin/render/render_test.go

131 lines
3.0 KiB
Go
Raw Permalink 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 (
"encoding/xml"
2015-04-09 13:15:02 +03:00
"html/template"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
2015-05-09 04:35:31 +03:00
// TODO unit tests
// test errors
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-05-18 16:45:24 +03:00
err := (JSON{data}).Write(w)
2015-04-09 13:15:02 +03:00
assert.NoError(t, err)
assert.Equal(t, w.Body.String(), "{\"foo\":\"bar\"}\n")
assert.Equal(t, w.Header().Get("Content-Type"), "application/json; charset=utf-8")
}
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
}
err := (IndentedJSON{data}).Write(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")
}
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-05-18 16:45:24 +03:00
err := (XML{data}).Write(w)
assert.NoError(t, err)
assert.Equal(t, w.Body.String(), "<map><foo>bar</foo></map>")
assert.Equal(t, w.Header().Get("Content-Type"), "application/xml; charset=utf-8")
}
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,
}).Write(w)
assert.NoError(t, err)
assert.Equal(t, w.Body.String(), "#!PNG some raw data")
assert.Equal(t, w.Header().Get("Content-Type"), "image/png")
}
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},
}).Write(w)
2015-04-09 13:15:02 +03:00
assert.NoError(t, err)
assert.Equal(t, w.Body.String(), "hola manu 2")
assert.Equal(t, w.Header().Get("Content-Type"), "text/plain; charset=utf-8")
}
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-05-18 16:45:24 +03:00
err := instance.Write(w)
2015-04-09 13:15:02 +03:00
assert.NoError(t, err)
assert.Equal(t, w.Body.String(), "Hello alexandernyquist")
assert.Equal(t, w.Header().Get("Content-Type"), "text/html; charset=utf-8")
}