2022-05-28 02:42:28 +00:00
|
|
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
2015-04-09 10:15:02 +00:00
|
|
|
// Use of this source code is governed by a MIT style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package render
|
|
|
|
|
|
|
|
import (
|
2015-05-07 10:44:52 +00:00
|
|
|
"encoding/xml"
|
2018-01-26 03:46:11 +00:00
|
|
|
"errors"
|
2015-04-09 10:15:02 +00:00
|
|
|
"html/template"
|
2022-12-22 15:18:47 +00:00
|
|
|
"net"
|
2018-01-26 03:46:11 +00:00
|
|
|
"net/http"
|
2015-04-09 10:15:02 +00:00
|
|
|
"net/http/httptest"
|
2018-05-12 03:00:42 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2015-04-09 10:15:02 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-05-10 09:19:26 +00:00
|
|
|
"github.com/gin-gonic/gin/internal/json"
|
2021-09-21 07:22:21 +00:00
|
|
|
testdata "github.com/gin-gonic/gin/testdata/protoexample"
|
2015-04-09 10:15:02 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-14 12:33:08 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-08-19 07:46:31 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
2015-04-09 10:15:02 +00:00
|
|
|
)
|
|
|
|
|
2015-05-09 01:35:31 +00:00
|
|
|
// TODO unit tests
|
|
|
|
// test errors
|
|
|
|
|
2015-04-09 10:15:02 +00:00
|
|
|
func TestRenderJSON(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
2022-03-21 01:43:17 +00:00
|
|
|
data := map[string]any{
|
2018-08-20 07:15:31 +00:00
|
|
|
"foo": "bar",
|
|
|
|
"html": "<b>",
|
2015-05-07 10:44:52 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 03:46:11 +00:00
|
|
|
(JSON{data}).WriteContentType(w)
|
|
|
|
assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
|
|
|
|
|
2015-06-04 03:25:21 +00:00
|
|
|
err := (JSON{data}).Render(w)
|
2015-04-09 10:15:02 +00:00
|
|
|
|
2024-07-14 12:33:08 +00:00
|
|
|
require.NoError(t, err)
|
2020-02-21 09:15:17 +00:00
|
|
|
assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"\\u003cb\\u003e\"}", w.Body.String())
|
2016-12-11 02:14:20 +00:00
|
|
|
assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
|
2015-04-09 10:15:02 +00:00
|
|
|
}
|
|
|
|
|
2023-02-12 02:01:33 +00:00
|
|
|
func TestRenderJSONError(t *testing.T) {
|
2018-01-26 03:46:11 +00:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
data := make(chan int)
|
|
|
|
|
|
|
|
// json: unsupported type: chan int
|
2024-07-14 12:33:08 +00:00
|
|
|
require.Error(t, (JSON{data}).Render(w))
|
2018-01-26 03:46:11 +00:00
|
|
|
}
|
|
|
|
|
2015-04-09 10:15:02 +00:00
|
|
|
func TestRenderIndentedJSON(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
2022-03-21 01:43:17 +00:00
|
|
|
data := map[string]any{
|
2015-04-09 10:15:02 +00:00
|
|
|
"foo": "bar",
|
|
|
|
"bar": "foo",
|
2015-05-18 13:45:24 +00:00
|
|
|
}
|
|
|
|
|
2015-06-04 03:25:21 +00:00
|
|
|
err := (IndentedJSON{data}).Render(w)
|
2015-04-09 10:15:02 +00:00
|
|
|
|
2024-07-14 12:33:08 +00:00
|
|
|
require.NoError(t, err)
|
2018-04-20 02:27:44 +00: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 10:15:02 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 03:46:11 +00:00
|
|
|
func TestRenderIndentedJSONPanics(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
data := make(chan int)
|
|
|
|
|
|
|
|
// json: unsupported type: chan int
|
|
|
|
err := (IndentedJSON{data}).Render(w)
|
2024-07-14 12:33:08 +00:00
|
|
|
require.Error(t, err)
|
2018-01-26 03:46:11 +00:00
|
|
|
}
|
|
|
|
|
2017-07-07 17:21:30 +00:00
|
|
|
func TestRenderSecureJSON(t *testing.T) {
|
|
|
|
w1 := httptest.NewRecorder()
|
2022-03-21 01:43:17 +00:00
|
|
|
data := map[string]any{
|
2017-07-07 17:21:30 +00:00
|
|
|
"foo": "bar",
|
|
|
|
}
|
|
|
|
|
2018-01-26 03:46:11 +00:00
|
|
|
(SecureJSON{"while(1);", data}).WriteContentType(w1)
|
|
|
|
assert.Equal(t, "application/json; charset=utf-8", w1.Header().Get("Content-Type"))
|
|
|
|
|
2017-07-07 17:21:30 +00:00
|
|
|
err1 := (SecureJSON{"while(1);", data}).Render(w1)
|
|
|
|
|
2024-07-14 12:33:08 +00:00
|
|
|
require.NoError(t, err1)
|
2017-07-07 17:21:30 +00:00
|
|
|
assert.Equal(t, "{\"foo\":\"bar\"}", w1.Body.String())
|
|
|
|
assert.Equal(t, "application/json; charset=utf-8", w1.Header().Get("Content-Type"))
|
|
|
|
|
|
|
|
w2 := httptest.NewRecorder()
|
2022-03-21 01:43:17 +00:00
|
|
|
datas := []map[string]any{{
|
2017-07-07 17:21:30 +00:00
|
|
|
"foo": "bar",
|
|
|
|
}, {
|
|
|
|
"bar": "foo",
|
|
|
|
}}
|
|
|
|
|
|
|
|
err2 := (SecureJSON{"while(1);", datas}).Render(w2)
|
2024-07-14 12:33:08 +00:00
|
|
|
require.NoError(t, err2)
|
2017-07-07 17:21:30 +00:00
|
|
|
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 03:46:11 +00: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)
|
2024-07-14 12:33:08 +00:00
|
|
|
require.Error(t, err)
|
2018-01-26 03:46:11 +00:00
|
|
|
}
|
|
|
|
|
2018-04-26 03:52:19 +00:00
|
|
|
func TestRenderJsonpJSON(t *testing.T) {
|
|
|
|
w1 := httptest.NewRecorder()
|
2022-03-21 01:43:17 +00:00
|
|
|
data := map[string]any{
|
2018-04-26 03:52:19 +00:00
|
|
|
"foo": "bar",
|
|
|
|
}
|
|
|
|
|
|
|
|
(JsonpJSON{"x", data}).WriteContentType(w1)
|
|
|
|
assert.Equal(t, "application/javascript; charset=utf-8", w1.Header().Get("Content-Type"))
|
|
|
|
|
|
|
|
err1 := (JsonpJSON{"x", data}).Render(w1)
|
|
|
|
|
2024-07-14 12:33:08 +00:00
|
|
|
require.NoError(t, err1)
|
2019-09-02 12:18:08 +00:00
|
|
|
assert.Equal(t, "x({\"foo\":\"bar\"});", w1.Body.String())
|
2018-04-26 03:52:19 +00:00
|
|
|
assert.Equal(t, "application/javascript; charset=utf-8", w1.Header().Get("Content-Type"))
|
|
|
|
|
|
|
|
w2 := httptest.NewRecorder()
|
2022-03-21 01:43:17 +00:00
|
|
|
datas := []map[string]any{{
|
2018-04-26 03:52:19 +00:00
|
|
|
"foo": "bar",
|
|
|
|
}, {
|
|
|
|
"bar": "foo",
|
|
|
|
}}
|
|
|
|
|
|
|
|
err2 := (JsonpJSON{"x", datas}).Render(w2)
|
2024-07-14 12:33:08 +00:00
|
|
|
require.NoError(t, err2)
|
2019-09-02 12:18:08 +00:00
|
|
|
assert.Equal(t, "x([{\"foo\":\"bar\"},{\"bar\":\"foo\"}]);", w2.Body.String())
|
2018-04-26 03:52:19 +00:00
|
|
|
assert.Equal(t, "application/javascript; charset=utf-8", w2.Header().Get("Content-Type"))
|
|
|
|
}
|
|
|
|
|
2023-05-10 09:19:26 +00:00
|
|
|
type errorWriter struct {
|
|
|
|
bufString string
|
|
|
|
*httptest.ResponseRecorder
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ http.ResponseWriter = (*errorWriter)(nil)
|
|
|
|
|
|
|
|
func (w *errorWriter) Write(buf []byte) (int, error) {
|
|
|
|
if string(buf) == w.bufString {
|
|
|
|
return 0, errors.New(`write "` + w.bufString + `" error`)
|
|
|
|
}
|
|
|
|
return w.ResponseRecorder.Write(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenderJsonpJSONError(t *testing.T) {
|
|
|
|
ew := &errorWriter{
|
|
|
|
ResponseRecorder: httptest.NewRecorder(),
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonpJSON := JsonpJSON{
|
|
|
|
Callback: "foo",
|
|
|
|
Data: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cb := template.JSEscapeString(jsonpJSON.Callback)
|
|
|
|
ew.bufString = cb
|
|
|
|
err := jsonpJSON.Render(ew) // error was returned while writing callback
|
|
|
|
assert.Equal(t, `write "`+cb+`" error`, err.Error())
|
|
|
|
|
|
|
|
ew.bufString = `(`
|
|
|
|
err = jsonpJSON.Render(ew)
|
|
|
|
assert.Equal(t, `write "`+`(`+`" error`, err.Error())
|
|
|
|
|
|
|
|
data, _ := json.Marshal(jsonpJSON.Data) // error was returned while writing data
|
|
|
|
ew.bufString = string(data)
|
|
|
|
err = jsonpJSON.Render(ew)
|
|
|
|
assert.Equal(t, `write "`+string(data)+`" error`, err.Error())
|
|
|
|
|
|
|
|
ew.bufString = `);`
|
|
|
|
err = jsonpJSON.Render(ew)
|
|
|
|
assert.Equal(t, `write "`+`);`+`" error`, err.Error())
|
|
|
|
}
|
|
|
|
|
2018-08-12 14:02:37 +00:00
|
|
|
func TestRenderJsonpJSONError2(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
2022-03-21 01:43:17 +00:00
|
|
|
data := map[string]any{
|
2018-08-12 14:02:37 +00:00
|
|
|
"foo": "bar",
|
|
|
|
}
|
|
|
|
(JsonpJSON{"", data}).WriteContentType(w)
|
|
|
|
assert.Equal(t, "application/javascript; charset=utf-8", w.Header().Get("Content-Type"))
|
|
|
|
|
|
|
|
e := (JsonpJSON{"", data}).Render(w)
|
2024-07-14 12:33:08 +00:00
|
|
|
require.NoError(t, e)
|
2018-08-12 14:02:37 +00:00
|
|
|
|
|
|
|
assert.Equal(t, "{\"foo\":\"bar\"}", w.Body.String())
|
|
|
|
assert.Equal(t, "application/javascript; charset=utf-8", w.Header().Get("Content-Type"))
|
|
|
|
}
|
|
|
|
|
2018-04-26 03:52:19 +00:00
|
|
|
func TestRenderJsonpJSONFail(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
data := make(chan int)
|
|
|
|
|
|
|
|
// json: unsupported type: chan int
|
|
|
|
err := (JsonpJSON{"x", data}).Render(w)
|
2024-07-14 12:33:08 +00:00
|
|
|
require.Error(t, err)
|
2018-04-26 03:52:19 +00:00
|
|
|
}
|
|
|
|
|
2018-07-03 09:17:08 +00:00
|
|
|
func TestRenderAsciiJSON(t *testing.T) {
|
|
|
|
w1 := httptest.NewRecorder()
|
2022-03-21 01:43:17 +00:00
|
|
|
data1 := map[string]any{
|
2018-07-03 09:17:08 +00:00
|
|
|
"lang": "GO语言",
|
|
|
|
"tag": "<br>",
|
|
|
|
}
|
|
|
|
|
|
|
|
err := (AsciiJSON{data1}).Render(w1)
|
|
|
|
|
2024-07-14 12:33:08 +00:00
|
|
|
require.NoError(t, err)
|
2018-07-03 09:17:08 +00:00
|
|
|
assert.Equal(t, "{\"lang\":\"GO\\u8bed\\u8a00\",\"tag\":\"\\u003cbr\\u003e\"}", w1.Body.String())
|
|
|
|
assert.Equal(t, "application/json", w1.Header().Get("Content-Type"))
|
|
|
|
|
|
|
|
w2 := httptest.NewRecorder()
|
2022-10-16 01:49:24 +00:00
|
|
|
data2 := 3.1415926
|
2018-07-03 09:17:08 +00:00
|
|
|
|
|
|
|
err = (AsciiJSON{data2}).Render(w2)
|
2024-07-14 12:33:08 +00:00
|
|
|
require.NoError(t, err)
|
2018-07-03 09:17:08 +00:00
|
|
|
assert.Equal(t, "3.1415926", w2.Body.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenderAsciiJSONFail(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
data := make(chan int)
|
|
|
|
|
|
|
|
// json: unsupported type: chan int
|
2024-07-14 12:33:08 +00:00
|
|
|
require.Error(t, (AsciiJSON{data}).Render(w))
|
2018-07-03 09:17:08 +00:00
|
|
|
}
|
|
|
|
|
2019-05-07 10:32:32 +00:00
|
|
|
func TestRenderPureJSON(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
2022-03-21 01:43:17 +00:00
|
|
|
data := map[string]any{
|
2019-05-07 10:32:32 +00:00
|
|
|
"foo": "bar",
|
|
|
|
"html": "<b>",
|
|
|
|
}
|
|
|
|
err := (PureJSON{data}).Render(w)
|
2024-07-14 12:33:08 +00:00
|
|
|
require.NoError(t, err)
|
2019-05-07 10:32:32 +00:00
|
|
|
assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"<b>\"}\n", w.Body.String())
|
|
|
|
assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
|
|
|
|
}
|
|
|
|
|
2022-03-21 01:43:17 +00:00
|
|
|
type xmlmap map[string]any
|
2015-05-07 10:44:52 +00:00
|
|
|
|
|
|
|
// 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 03:00:17 +00:00
|
|
|
|
|
|
|
return e.EncodeToken(xml.EndElement{Name: start.Name})
|
2015-05-07 10:44:52 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 03:46:11 +00:00
|
|
|
func TestRenderYAML(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
data := `
|
|
|
|
a : Easy!
|
|
|
|
b:
|
|
|
|
c: 2
|
|
|
|
d: [3, 4]
|
|
|
|
`
|
|
|
|
(YAML{data}).WriteContentType(w)
|
2024-03-05 13:54:35 +00:00
|
|
|
assert.Equal(t, "application/yaml; charset=utf-8", w.Header().Get("Content-Type"))
|
2018-01-26 03:46:11 +00:00
|
|
|
|
|
|
|
err := (YAML{data}).Render(w)
|
2024-07-14 12:33:08 +00:00
|
|
|
require.NoError(t, err)
|
2023-01-02 04:40:48 +00:00
|
|
|
assert.Equal(t, "|4-\n a : Easy!\n b:\n \tc: 2\n \td: [3, 4]\n \t\n", w.Body.String())
|
2024-03-05 13:54:35 +00:00
|
|
|
assert.Equal(t, "application/yaml; charset=utf-8", w.Header().Get("Content-Type"))
|
2018-01-26 03:46:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type fail struct{}
|
|
|
|
|
|
|
|
// Hook MarshalYAML
|
2022-03-21 01:43:17 +00:00
|
|
|
func (ft *fail) MarshalYAML() (any, error) {
|
2018-01-26 03:46:11 +00:00
|
|
|
return nil, errors.New("fail")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenderYAMLFail(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
err := (YAML{&fail{}}).Render(w)
|
2024-07-14 12:33:08 +00:00
|
|
|
require.Error(t, err)
|
2018-01-26 03:46:11 +00:00
|
|
|
}
|
|
|
|
|
2022-12-22 15:18:47 +00:00
|
|
|
func TestRenderTOML(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
data := map[string]any{
|
|
|
|
"foo": "bar",
|
|
|
|
"html": "<b>",
|
|
|
|
}
|
|
|
|
(TOML{data}).WriteContentType(w)
|
|
|
|
assert.Equal(t, "application/toml; charset=utf-8", w.Header().Get("Content-Type"))
|
|
|
|
|
|
|
|
err := (TOML{data}).Render(w)
|
2024-07-14 12:33:08 +00:00
|
|
|
require.NoError(t, err)
|
2022-12-22 15:18:47 +00:00
|
|
|
assert.Equal(t, "foo = 'bar'\nhtml = '<b>'\n", w.Body.String())
|
|
|
|
assert.Equal(t, "application/toml; charset=utf-8", w.Header().Get("Content-Type"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenderTOMLFail(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
err := (TOML{net.IPv4bcast}).Render(w)
|
2024-07-14 12:33:08 +00:00
|
|
|
require.Error(t, err)
|
2022-12-22 15:18:47 +00:00
|
|
|
}
|
|
|
|
|
2018-08-19 02:45:56 +00:00
|
|
|
// test Protobuf rendering
|
|
|
|
func TestRenderProtoBuf(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
reps := []int64{int64(1), int64(2)}
|
|
|
|
label := "test"
|
|
|
|
data := &testdata.Test{
|
|
|
|
Label: &label,
|
|
|
|
Reps: reps,
|
|
|
|
}
|
|
|
|
|
|
|
|
(ProtoBuf{data}).WriteContentType(w)
|
|
|
|
protoData, err := proto.Marshal(data)
|
2024-07-14 12:33:08 +00:00
|
|
|
require.NoError(t, err)
|
2018-08-19 02:45:56 +00:00
|
|
|
assert.Equal(t, "application/x-protobuf", w.Header().Get("Content-Type"))
|
|
|
|
|
|
|
|
err = (ProtoBuf{data}).Render(w)
|
|
|
|
|
2024-07-14 12:33:08 +00:00
|
|
|
require.NoError(t, err)
|
2018-09-12 13:21:26 +00:00
|
|
|
assert.Equal(t, string(protoData), w.Body.String())
|
2018-08-19 02:45:56 +00:00
|
|
|
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)
|
2024-07-14 12:33:08 +00:00
|
|
|
require.Error(t, err)
|
2018-08-19 02:45:56 +00:00
|
|
|
}
|
|
|
|
|
2015-05-07 10:44:52 +00:00
|
|
|
func TestRenderXML(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
data := xmlmap{
|
|
|
|
"foo": "bar",
|
|
|
|
}
|
|
|
|
|
2018-01-26 03:46:11 +00:00
|
|
|
(XML{data}).WriteContentType(w)
|
2018-04-20 02:27:44 +00:00
|
|
|
assert.Equal(t, "application/xml; charset=utf-8", w.Header().Get("Content-Type"))
|
2018-01-26 03:46:11 +00:00
|
|
|
|
2015-06-04 03:25:21 +00:00
|
|
|
err := (XML{data}).Render(w)
|
2015-05-07 10:44:52 +00:00
|
|
|
|
2024-07-14 12:33:08 +00:00
|
|
|
require.NoError(t, err)
|
2018-04-20 02:27:44 +00: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 10:44:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenderRedirect(t *testing.T) {
|
2018-01-26 03:46:11 +00:00
|
|
|
req, err := http.NewRequest("GET", "/test-redirect", nil)
|
2024-07-14 12:33:08 +00:00
|
|
|
require.NoError(t, err)
|
2018-01-26 03:46:11 +00:00
|
|
|
|
|
|
|
data1 := Redirect{
|
2018-08-14 01:51:56 +00:00
|
|
|
Code: http.StatusMovedPermanently,
|
2018-01-26 03:46:11 +00:00
|
|
|
Request: req,
|
|
|
|
Location: "/new/location",
|
|
|
|
}
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
err = data1.Render(w)
|
2024-07-14 12:33:08 +00:00
|
|
|
require.NoError(t, err)
|
2018-01-26 03:46:11 +00:00
|
|
|
|
|
|
|
data2 := Redirect{
|
2018-08-14 01:51:56 +00:00
|
|
|
Code: http.StatusOK,
|
2018-01-26 03:46:11 +00:00
|
|
|
Request: req,
|
|
|
|
Location: "/new/location",
|
|
|
|
}
|
|
|
|
|
|
|
|
w = httptest.NewRecorder()
|
2019-10-27 05:58:59 +00:00
|
|
|
assert.PanicsWithValue(t, "Cannot redirect with status code 200", func() {
|
|
|
|
err := data2.Render(w)
|
2024-07-14 12:33:08 +00:00
|
|
|
require.NoError(t, err)
|
2019-10-27 05:58:59 +00:00
|
|
|
})
|
2019-09-10 09:16:37 +00:00
|
|
|
|
|
|
|
data3 := Redirect{
|
|
|
|
Code: http.StatusCreated,
|
|
|
|
Request: req,
|
|
|
|
Location: "/new/location",
|
|
|
|
}
|
|
|
|
|
|
|
|
w = httptest.NewRecorder()
|
|
|
|
err = data3.Render(w)
|
2024-07-14 12:33:08 +00:00
|
|
|
require.NoError(t, err)
|
2018-01-26 03:46:11 +00:00
|
|
|
|
|
|
|
// only improve coverage
|
|
|
|
data2.WriteContentType(w)
|
2015-05-07 10:44:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenderData(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
data := []byte("#!PNG some raw data")
|
|
|
|
|
2015-05-18 13:45:24 +00:00
|
|
|
err := (Data{
|
|
|
|
ContentType: "image/png",
|
|
|
|
Data: data,
|
2015-06-04 03:25:21 +00:00
|
|
|
}).Render(w)
|
2015-05-07 10:44:52 +00:00
|
|
|
|
2024-07-14 12:33:08 +00:00
|
|
|
require.NoError(t, err)
|
2018-04-20 02:27:44 +00:00
|
|
|
assert.Equal(t, "#!PNG some raw data", w.Body.String())
|
|
|
|
assert.Equal(t, "image/png", w.Header().Get("Content-Type"))
|
2015-05-07 10:44:52 +00:00
|
|
|
}
|
|
|
|
|
2015-05-18 13:45:24 +00:00
|
|
|
func TestRenderString(t *testing.T) {
|
2015-04-09 10:15:02 +00:00
|
|
|
w := httptest.NewRecorder()
|
2015-05-07 10:44:52 +00:00
|
|
|
|
2018-01-26 03:46:11 +00:00
|
|
|
(String{
|
|
|
|
Format: "hello %s %d",
|
2022-03-21 01:43:17 +00:00
|
|
|
Data: []any{},
|
2018-01-26 03:46:11 +00:00
|
|
|
}).WriteContentType(w)
|
2018-04-20 02:27:44 +00:00
|
|
|
assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))
|
2018-01-26 03:46:11 +00:00
|
|
|
|
2015-05-18 13:45:24 +00:00
|
|
|
err := (String{
|
|
|
|
Format: "hola %s %d",
|
2022-03-21 01:43:17 +00:00
|
|
|
Data: []any{"manu", 2},
|
2015-06-04 03:25:21 +00:00
|
|
|
}).Render(w)
|
2015-04-09 10:15:02 +00:00
|
|
|
|
2024-07-14 12:33:08 +00:00
|
|
|
require.NoError(t, err)
|
2018-04-20 02:27:44 +00: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 10:15:02 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 03:46:11 +00:00
|
|
|
func TestRenderStringLenZero(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
err := (String{
|
|
|
|
Format: "hola %s %d",
|
2022-03-21 01:43:17 +00:00
|
|
|
Data: []any{},
|
2018-01-26 03:46:11 +00:00
|
|
|
}).Render(w)
|
|
|
|
|
2024-07-14 12:33:08 +00:00
|
|
|
require.NoError(t, err)
|
2018-04-20 02:27:44 +00: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 03:46:11 +00:00
|
|
|
}
|
|
|
|
|
2015-04-09 10:15:02 +00:00
|
|
|
func TestRenderHTMLTemplate(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
templ := template.Must(template.New("t").Parse(`Hello {{.name}}`))
|
2015-05-18 13:45:24 +00:00
|
|
|
|
|
|
|
htmlRender := HTMLProduction{Template: templ}
|
2022-03-21 01:43:17 +00:00
|
|
|
instance := htmlRender.Instance("t", map[string]any{
|
2015-04-09 10:15:02 +00:00
|
|
|
"name": "alexandernyquist",
|
|
|
|
})
|
|
|
|
|
2015-06-04 03:25:21 +00:00
|
|
|
err := instance.Render(w)
|
2015-05-18 13:45:24 +00:00
|
|
|
|
2024-07-14 12:33:08 +00:00
|
|
|
require.NoError(t, err)
|
2018-04-20 02:27:44 +00: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 10:15:02 +00:00
|
|
|
}
|
2018-01-26 03:46:11 +00:00
|
|
|
|
|
|
|
func TestRenderHTMLTemplateEmptyName(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
templ := template.Must(template.New("").Parse(`Hello {{.name}}`))
|
|
|
|
|
|
|
|
htmlRender := HTMLProduction{Template: templ}
|
2022-03-21 01:43:17 +00:00
|
|
|
instance := htmlRender.Instance("", map[string]any{
|
2018-01-26 03:46:11 +00:00
|
|
|
"name": "alexandernyquist",
|
|
|
|
})
|
|
|
|
|
|
|
|
err := instance.Render(w)
|
|
|
|
|
2024-07-14 12:33:08 +00:00
|
|
|
require.NoError(t, err)
|
2018-04-20 02:27:44 +00: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 03:46:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenderHTMLDebugFiles(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
2021-08-19 07:46:31 +00:00
|
|
|
htmlRender := HTMLDebug{
|
|
|
|
Files: []string{"../testdata/template/hello.tmpl"},
|
2018-01-26 03:46:11 +00:00
|
|
|
Glob: "",
|
|
|
|
Delims: Delims{Left: "{[{", Right: "}]}"},
|
|
|
|
FuncMap: nil,
|
|
|
|
}
|
2022-03-21 01:43:17 +00:00
|
|
|
instance := htmlRender.Instance("hello.tmpl", map[string]any{
|
2018-01-26 03:46:11 +00:00
|
|
|
"name": "thinkerou",
|
|
|
|
})
|
|
|
|
|
|
|
|
err := instance.Render(w)
|
|
|
|
|
2024-07-14 12:33:08 +00:00
|
|
|
require.NoError(t, err)
|
2018-04-20 02:27:44 +00: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 03:46:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenderHTMLDebugGlob(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
2021-08-19 07:46:31 +00:00
|
|
|
htmlRender := HTMLDebug{
|
|
|
|
Files: nil,
|
2018-08-12 15:38:31 +00:00
|
|
|
Glob: "../testdata/template/hello*",
|
2018-01-26 03:46:11 +00:00
|
|
|
Delims: Delims{Left: "{[{", Right: "}]}"},
|
|
|
|
FuncMap: nil,
|
|
|
|
}
|
2022-03-21 01:43:17 +00:00
|
|
|
instance := htmlRender.Instance("hello.tmpl", map[string]any{
|
2018-01-26 03:46:11 +00:00
|
|
|
"name": "thinkerou",
|
|
|
|
})
|
|
|
|
|
|
|
|
err := instance.Render(w)
|
|
|
|
|
2024-07-14 12:33:08 +00:00
|
|
|
require.NoError(t, err)
|
2018-04-20 02:27:44 +00: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 03:46:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenderHTMLDebugPanics(t *testing.T) {
|
2021-08-19 07:46:31 +00:00
|
|
|
htmlRender := HTMLDebug{
|
|
|
|
Files: nil,
|
2018-01-26 03:46:11 +00:00
|
|
|
Glob: "",
|
|
|
|
Delims: Delims{"{{", "}}"},
|
|
|
|
FuncMap: nil,
|
|
|
|
}
|
|
|
|
assert.Panics(t, func() { htmlRender.Instance("", nil) })
|
|
|
|
}
|
2018-05-12 03:00:42 +00:00
|
|
|
|
|
|
|
func TestRenderReader(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
body := "#!PNG some raw data"
|
|
|
|
headers := make(map[string]string)
|
|
|
|
headers["Content-Disposition"] = `attachment; filename="filename.png"`
|
2019-02-22 06:20:24 +00:00
|
|
|
headers["x-request-id"] = "requestId"
|
2018-05-12 03:00:42 +00:00
|
|
|
|
|
|
|
err := (Reader{
|
|
|
|
ContentLength: int64(len(body)),
|
|
|
|
ContentType: "image/png",
|
|
|
|
Reader: strings.NewReader(body),
|
|
|
|
Headers: headers,
|
|
|
|
}).Render(w)
|
|
|
|
|
2024-07-14 12:33:08 +00:00
|
|
|
require.NoError(t, err)
|
2018-05-12 03:00:42 +00:00
|
|
|
assert.Equal(t, body, w.Body.String())
|
2018-10-11 23:31:31 +00:00
|
|
|
assert.Equal(t, "image/png", w.Header().Get("Content-Type"))
|
|
|
|
assert.Equal(t, strconv.Itoa(len(body)), w.Header().Get("Content-Length"))
|
|
|
|
assert.Equal(t, headers["Content-Disposition"], w.Header().Get("Content-Disposition"))
|
2019-02-22 06:20:24 +00:00
|
|
|
assert.Equal(t, headers["x-request-id"], w.Header().Get("x-request-id"))
|
2018-05-12 03:00:42 +00:00
|
|
|
}
|
2019-08-05 01:42:59 +00:00
|
|
|
|
|
|
|
func TestRenderReaderNoContentLength(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
body := "#!PNG some raw data"
|
|
|
|
headers := make(map[string]string)
|
|
|
|
headers["Content-Disposition"] = `attachment; filename="filename.png"`
|
|
|
|
headers["x-request-id"] = "requestId"
|
|
|
|
|
|
|
|
err := (Reader{
|
|
|
|
ContentLength: -1,
|
|
|
|
ContentType: "image/png",
|
|
|
|
Reader: strings.NewReader(body),
|
|
|
|
Headers: headers,
|
|
|
|
}).Render(w)
|
|
|
|
|
2024-07-14 12:33:08 +00:00
|
|
|
require.NoError(t, err)
|
2019-08-05 01:42:59 +00:00
|
|
|
assert.Equal(t, body, w.Body.String())
|
|
|
|
assert.Equal(t, "image/png", w.Header().Get("Content-Type"))
|
|
|
|
assert.NotContains(t, "Content-Length", w.Header())
|
|
|
|
assert.Equal(t, headers["Content-Disposition"], w.Header().Get("Content-Disposition"))
|
|
|
|
assert.Equal(t, headers["x-request-id"], w.Header().Get("x-request-id"))
|
|
|
|
}
|
2023-08-12 14:21:56 +00:00
|
|
|
|
|
|
|
func TestRenderWriteError(t *testing.T) {
|
|
|
|
data := []interface{}{"value1", "value2"}
|
|
|
|
prefix := "my-prefix:"
|
|
|
|
r := SecureJSON{Data: data, Prefix: prefix}
|
|
|
|
ew := &errorWriter{
|
|
|
|
bufString: prefix,
|
|
|
|
ResponseRecorder: httptest.NewRecorder(),
|
|
|
|
}
|
|
|
|
err := r.Render(ew)
|
2024-07-14 12:33:08 +00:00
|
|
|
require.Error(t, err)
|
2023-08-12 14:21:56 +00:00
|
|
|
assert.Equal(t, `write "my-prefix:" error`, err.Error())
|
|
|
|
}
|