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 (
|
2017-02-23 17:08:37 +03:00
|
|
|
"bytes"
|
2015-05-07 13:44:52 +03:00
|
|
|
"encoding/xml"
|
2018-01-26 06:46:11 +03:00
|
|
|
"errors"
|
2015-04-09 13:15:02 +03:00
|
|
|
"html/template"
|
2018-01-26 06:46:11 +03:00
|
|
|
"net/http"
|
2015-04-09 13:15:02 +03:00
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2017-02-23 17:08:37 +03:00
|
|
|
"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
|
|
|
|
|
2017-02-23 17:08:37 +03:00
|
|
|
func TestRenderMsgPack(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
data := map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
}
|
|
|
|
|
2018-01-26 06:46:11 +03:00
|
|
|
(MsgPack{data}).WriteContentType(w)
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, "application/msgpack; charset=utf-8", w.Header().Get("Content-Type"))
|
2018-01-26 06:46:11 +03:00
|
|
|
|
2017-02-23 17:08:37 +03:00
|
|
|
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()))
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, "application/msgpack; charset=utf-8", w.Header().Get("Content-Type"))
|
2017-02-23 17:08:37 +03:00
|
|
|
}
|
|
|
|
|
2015-04-09 13:15:02 +03:00
|
|
|
func TestRenderJSON(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
2015-05-07 13:44:52 +03:00
|
|
|
data := map[string]interface{}{
|
2015-04-09 13:15:02 +03:00
|
|
|
"foo": "bar",
|
2015-05-07 13:44:52 +03:00
|
|
|
}
|
|
|
|
|
2018-01-26 06:46:11 +03:00
|
|
|
(JSON{data}).WriteContentType(w)
|
|
|
|
assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
|
|
|
|
|
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)
|
2016-12-11 05:14:20 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-01-26 06:46:11 +03:00
|
|
|
func TestRenderJSONPanics(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
data := make(chan int)
|
|
|
|
|
|
|
|
// json: unsupported type: chan int
|
|
|
|
assert.Panics(t, func() { (JSON{data}).Render(w) })
|
|
|
|
}
|
|
|
|
|
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)
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, "{\n \"bar\": \"foo\",\n \"foo\": \"bar\"\n}", w.Body.String())
|
|
|
|
assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
|
2015-04-09 13:15:02 +03:00
|
|
|
}
|
|
|
|
|
2018-01-26 06:46:11 +03:00
|
|
|
func TestRenderIndentedJSONPanics(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
data := make(chan int)
|
|
|
|
|
|
|
|
// json: unsupported type: chan int
|
|
|
|
err := (IndentedJSON{data}).Render(w)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2017-07-07 20:21:30 +03:00
|
|
|
func TestRenderSecureJSON(t *testing.T) {
|
|
|
|
w1 := httptest.NewRecorder()
|
|
|
|
data := map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
}
|
|
|
|
|
2018-01-26 06:46:11 +03:00
|
|
|
(SecureJSON{"while(1);", data}).WriteContentType(w1)
|
|
|
|
assert.Equal(t, "application/json; charset=utf-8", w1.Header().Get("Content-Type"))
|
|
|
|
|
2017-07-07 20:21:30 +03:00
|
|
|
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"))
|
|
|
|
}
|
|
|
|
|
2018-01-26 06:46:11 +03:00
|
|
|
func TestRenderSecureJSONFail(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
data := make(chan int)
|
|
|
|
|
|
|
|
// json: unsupported type: chan int
|
|
|
|
err := (SecureJSON{"while(1);", data}).Render(w)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2015-05-07 13:44:52 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2017-12-21 06:00:17 +03:00
|
|
|
|
|
|
|
return e.EncodeToken(xml.EndElement{Name: start.Name})
|
2015-05-07 13:44:52 +03:00
|
|
|
}
|
|
|
|
|
2018-01-26 06:46:11 +03:00
|
|
|
func TestRenderYAML(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
data := `
|
|
|
|
a : Easy!
|
|
|
|
b:
|
|
|
|
c: 2
|
|
|
|
d: [3, 4]
|
|
|
|
`
|
|
|
|
(YAML{data}).WriteContentType(w)
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, "application/x-yaml; charset=utf-8", w.Header().Get("Content-Type"))
|
2018-01-26 06:46:11 +03:00
|
|
|
|
|
|
|
err := (YAML{data}).Render(w)
|
|
|
|
assert.NoError(t, err)
|
2018-04-20 05:27:44 +03:00
|
|
|
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"))
|
2018-01-26 06:46:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type fail struct{}
|
|
|
|
|
|
|
|
// Hook MarshalYAML
|
|
|
|
func (ft *fail) MarshalYAML() (interface{}, error) {
|
|
|
|
return nil, errors.New("fail")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenderYAMLFail(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
err := (YAML{&fail{}}).Render(w)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2015-05-07 13:44:52 +03:00
|
|
|
func TestRenderXML(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
data := xmlmap{
|
|
|
|
"foo": "bar",
|
|
|
|
}
|
|
|
|
|
2018-01-26 06:46:11 +03:00
|
|
|
(XML{data}).WriteContentType(w)
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, "application/xml; charset=utf-8", w.Header().Get("Content-Type"))
|
2018-01-26 06:46:11 +03:00
|
|
|
|
2015-06-04 06:25:21 +03:00
|
|
|
err := (XML{data}).Render(w)
|
2015-05-07 13:44:52 +03:00
|
|
|
|
|
|
|
assert.NoError(t, err)
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, "<map><foo>bar</foo></map>", w.Body.String())
|
|
|
|
assert.Equal(t, "application/xml; charset=utf-8", w.Header().Get("Content-Type"))
|
2015-05-07 13:44:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenderRedirect(t *testing.T) {
|
2018-01-26 06:46:11 +03:00
|
|
|
req, err := http.NewRequest("GET", "/test-redirect", nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
data1 := Redirect{
|
|
|
|
Code: 301,
|
|
|
|
Request: req,
|
|
|
|
Location: "/new/location",
|
|
|
|
}
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
err = data1.Render(w)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
data2 := Redirect{
|
|
|
|
Code: 200,
|
|
|
|
Request: req,
|
|
|
|
Location: "/new/location",
|
|
|
|
}
|
|
|
|
|
|
|
|
w = httptest.NewRecorder()
|
|
|
|
assert.Panics(t, func() { data2.Render(w) })
|
|
|
|
|
|
|
|
// only improve coverage
|
|
|
|
data2.WriteContentType(w)
|
2015-05-07 13:44:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2015-05-07 13:44:52 +03:00
|
|
|
|
|
|
|
assert.NoError(t, err)
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, "#!PNG some raw data", w.Body.String())
|
|
|
|
assert.Equal(t, "image/png", w.Header().Get("Content-Type"))
|
2015-05-07 13:44:52 +03:00
|
|
|
}
|
|
|
|
|
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-07 13:44:52 +03:00
|
|
|
|
2018-01-26 06:46:11 +03:00
|
|
|
(String{
|
|
|
|
Format: "hello %s %d",
|
|
|
|
Data: []interface{}{},
|
|
|
|
}).WriteContentType(w)
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))
|
2018-01-26 06:46:11 +03:00
|
|
|
|
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)
|
2018-04-20 05:27:44 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-01-26 06:46:11 +03:00
|
|
|
func TestRenderStringLenZero(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
err := (String{
|
|
|
|
Format: "hola %s %d",
|
|
|
|
Data: []interface{}{},
|
|
|
|
}).Render(w)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, "hola %s %d", w.Body.String())
|
|
|
|
assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))
|
2018-01-26 06:46:11 +03:00
|
|
|
}
|
|
|
|
|
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)
|
2018-04-20 05:27:44 +03:00
|
|
|
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
|
|
|
}
|
2018-01-26 06:46:11 +03:00
|
|
|
|
|
|
|
func TestRenderHTMLTemplateEmptyName(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
templ := template.Must(template.New("").Parse(`Hello {{.name}}`))
|
|
|
|
|
|
|
|
htmlRender := HTMLProduction{Template: templ}
|
|
|
|
instance := htmlRender.Instance("", map[string]interface{}{
|
|
|
|
"name": "alexandernyquist",
|
|
|
|
})
|
|
|
|
|
|
|
|
err := instance.Render(w)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, "Hello alexandernyquist", w.Body.String())
|
|
|
|
assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))
|
2018-01-26 06:46:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenderHTMLDebugFiles(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
htmlRender := HTMLDebug{Files: []string{"../fixtures/basic/hello.tmpl"},
|
|
|
|
Glob: "",
|
|
|
|
Delims: Delims{Left: "{[{", Right: "}]}"},
|
|
|
|
FuncMap: nil,
|
|
|
|
}
|
|
|
|
instance := htmlRender.Instance("hello.tmpl", map[string]interface{}{
|
|
|
|
"name": "thinkerou",
|
|
|
|
})
|
|
|
|
|
|
|
|
err := instance.Render(w)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, "<h1>Hello thinkerou</h1>", w.Body.String())
|
|
|
|
assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))
|
2018-01-26 06:46:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenderHTMLDebugGlob(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
htmlRender := HTMLDebug{Files: nil,
|
|
|
|
Glob: "../fixtures/basic/hello*",
|
|
|
|
Delims: Delims{Left: "{[{", Right: "}]}"},
|
|
|
|
FuncMap: nil,
|
|
|
|
}
|
|
|
|
instance := htmlRender.Instance("hello.tmpl", map[string]interface{}{
|
|
|
|
"name": "thinkerou",
|
|
|
|
})
|
|
|
|
|
|
|
|
err := instance.Render(w)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
2018-04-20 05:27:44 +03:00
|
|
|
assert.Equal(t, "<h1>Hello thinkerou</h1>", w.Body.String())
|
|
|
|
assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))
|
2018-01-26 06:46:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenderHTMLDebugPanics(t *testing.T) {
|
|
|
|
htmlRender := HTMLDebug{Files: nil,
|
|
|
|
Glob: "",
|
|
|
|
Delims: Delims{"{{", "}}"},
|
|
|
|
FuncMap: nil,
|
|
|
|
}
|
|
|
|
assert.Panics(t, func() { htmlRender.Instance("", nil) })
|
|
|
|
}
|