-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_test_test.go
118 lines (108 loc) · 3.03 KB
/
example_test_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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package xhttp_test
import (
"context"
xerr "github.com/goclub/error"
xhttp "github.com/goclub/http"
"github.com/stretchr/testify/assert"
"log"
"mime/multipart"
"net/http"
"strconv"
"testing"
)
type RequestHome struct {
ID string `json:"id"`
Name string `json:"name"`
Age int `json:"age"`
}
type ReplyHome struct {
IDNameAge string `json:"idNameAge"`
}
func newTestRouter() *xhttp.Router {
router := xhttp.NewRouter(xhttp.RouterOption{
OnCatchError: func(c *xhttp.Context, err error) error {
c.WriteStatusCode(500)
log.Print(err)
return c.WriteBytes([]byte("error"))
},
OnCatchPanic: func(c *xhttp.Context, recoverValue interface{}) error {
c.WriteStatusCode(500)
log.Print(recoverValue)
return c.WriteBytes([]byte("panic"))
},
})
router.HandleFunc(xhttp.Route{xhttp.POST, "/"}, func(c *xhttp.Context) (err error) {
request := RequestHome{}
err = c.BindRequest(&request)
if err != nil {
return
}
reply := ReplyHome{}
reply.IDNameAge = request.ID + ":" + request.Name + ":" + strconv.FormatInt(int64(request.Age), 10)
return c.WriteJSON(reply)
})
router.HandleFunc(xhttp.Route{xhttp.GET, "/count"}, func(c *xhttp.Context) (err error) {
cookieName := "count"
var count uint64
cookie, hasCookie, err := c.Cookie(cookieName)
if err != nil {
return
}
if hasCookie {
count, err = strconv.ParseUint(cookie.Value, 10, 64)
if err != nil {
return
}
}
count++
c.SetCookie(&http.Cookie{
Name: cookieName,
Value: strconv.FormatUint(count, 10),
})
return c.WriteBytes([]byte(strconv.FormatUint(count, 10)))
})
router.HandleFunc(xhttp.Route{xhttp.GET, "/error"}, func(c *xhttp.Context) (err error) {
return xerr.New("abc")
})
router.HandleFunc(xhttp.Route{xhttp.GET, "/panic"}, func(c *xhttp.Context) (err error) {
panic("123")
return nil
})
router.HandleFunc(xhttp.Route{xhttp.POST, "/form"}, func(c *xhttp.Context) (err error) {
return c.WriteBytes([]byte(c.Request.FormValue("name")))
})
return router
}
func TestTest(t *testing.T) {
router := newTestRouter()
test := xhttp.NewTest(t, router)
test.RequestJSON(xhttp.Route{xhttp.POST, "/"}, RequestHome{
ID: "1",
Name: "nimo",
Age: 18,
}).ExpectJSON(200, ReplyHome{IDNameAge: "1:nimo:18"})
test.RequestJSON(xhttp.Route{xhttp.GET, "/count"}, nil).ExpectString(200, "1")
test.RequestJSON(xhttp.Route{xhttp.GET, "/count"}, nil).ExpectString(200, "2")
test.RequestJSON(xhttp.Route{xhttp.POST, "/count"}, nil).ExpectString(405, "")
test.RequestJSON(xhttp.Route{xhttp.GET, "/error"}, nil).ExpectString(500, "error")
test.RequestJSON(xhttp.Route{xhttp.GET, "/panic"}, nil).ExpectString(500, "panic")
{
r, err := xhttp.SendRequest{
FormData: TestFormDataReq{
Name: "nimo",
},
}.HttpRequest(context.TODO(), xhttp.POST, "/form")
assert.NoError(t, err)
test.Request(r).ExpectString(200, "nimo")
}
}
type TestFormDataReq struct {
Name string
}
func (v TestFormDataReq) FormData(formWriter *multipart.Writer) (err error) {
err = formWriter.WriteField("name", v.Name)
if err != nil {
return
}
return
}