-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsoniter_test.go
37 lines (31 loc) · 1016 Bytes
/
jsoniter_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package jsoniterserialize
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gin-gonic/gin"
jsoniter "github.com/json-iterator/go"
"gopkg.in/go-playground/assert.v1"
)
type Form struct {
Name string `json:"name"`
}
var jsoniterAPI = jsoniter.Config{}.Froze()
func jsoniterTest(c *gin.Context) {
var form Form
if err := c.ShouldBindWith(&form, JsoniterBinding{jsoniterAPI}); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"message": "bad request"})
}
c.Render(http.StatusOK, JsoniterRender{jsoniterAPI, map[string]string{"message": "hello " + form.Name}})
}
func TestSerialize(t *testing.T) {
router := gin.Default()
router.POST("/test", jsoniterTest)
req := httptest.NewRequest("POST", "/test", strings.NewReader(`{"name":"world"}`))
req.Header.Add("Content-Type", "application/json")
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, w.Code, http.StatusOK)
assert.Equal(t, jsoniterAPI.Get(w.Body.Bytes(), "message").ToString(), "hello world")
}