mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-23 10:02:10 +08:00
tests
This commit is contained in:
parent
f71e5432df
commit
98acb016cf
@ -6,9 +6,9 @@ package render
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin/json"
|
"github.com/gin-gonic/gin/json"
|
||||||
)
|
)
|
||||||
@ -127,12 +127,12 @@ func (r AsciiJSON) Render(w http.ResponseWriter) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
result := ""
|
result := ""
|
||||||
for _, r := range ret {
|
for _, r := range string(ret) {
|
||||||
cvt := ""
|
cvt := ""
|
||||||
if r < 128 {
|
if r < 128 {
|
||||||
cvt = string(r)
|
cvt = string(r)
|
||||||
} else {
|
} else {
|
||||||
cvt = `\u` + strconv.FormatInt(int64(r), 16)
|
cvt = fmt.Sprintf("\\u%04x", int64(r))
|
||||||
}
|
}
|
||||||
result = result + cvt
|
result = result + cvt
|
||||||
}
|
}
|
||||||
@ -142,5 +142,5 @@ func (r AsciiJSON) Render(w http.ResponseWriter) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r AsciiJSON) WriteContentType(w http.ResponseWriter) {
|
func (r AsciiJSON) WriteContentType(w http.ResponseWriter) {
|
||||||
writeContentType(w, jsonContentType)
|
writeContentType(w, jsonAsciiContentType)
|
||||||
}
|
}
|
||||||
|
@ -167,6 +167,35 @@ func TestRenderJsonpJSONFail(t *testing.T) {
|
|||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRenderAsciiJSON(t *testing.T) {
|
||||||
|
w1 := httptest.NewRecorder()
|
||||||
|
data1 := map[string]interface{}{
|
||||||
|
"lang": "GO语言",
|
||||||
|
"tag": "<br>",
|
||||||
|
}
|
||||||
|
|
||||||
|
err := (AsciiJSON{data1}).Render(w1)
|
||||||
|
|
||||||
|
assert.NoError(t, err)
|
||||||
|
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()
|
||||||
|
data2 := float64(3.1415926)
|
||||||
|
|
||||||
|
err = (AsciiJSON{data2}).Render(w2)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
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
|
||||||
|
assert.Error(t, (AsciiJSON{data}).Render(w))
|
||||||
|
}
|
||||||
|
|
||||||
type xmlmap map[string]interface{}
|
type xmlmap map[string]interface{}
|
||||||
|
|
||||||
// Allows type H to be used with xml.Marshal
|
// Allows type H to be used with xml.Marshal
|
||||||
|
Loading…
x
Reference in New Issue
Block a user