-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit.go
113 lines (90 loc) · 2.37 KB
/
unit.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
package fasthttpunit
import (
"fmt"
"reflect"
"runtime"
"strings"
"testing"
"time"
"github.com/buaazp/fasthttprouter"
"github.com/valyala/fasthttp"
)
type Unit struct {
casePath string
conf *Conf
t *testing.T
server *fasthttp.Server
}
func NewUnit(conf *Conf, t *testing.T, s *fasthttp.Server) *Unit {
u := &Unit{
t: t,
server: s,
conf: conf,
}
return u
}
func NewUnitWithHandler(conf *Conf, t *testing.T, handler fasthttp.RequestHandler) *Unit {
s := &fasthttp.Server{
Handler: handler,
}
return NewUnit(conf, t, s)
}
func NewUnitWithRouter(conf *Conf, t *testing.T, r *fasthttprouter.Router) *Unit {
return NewUnitWithHandler(conf, t, r.Handler)
}
func (u *Unit) Test(mockList ...func()) {
u.Green("--UNIT TEST WILL START AT %d SECONDS LATER--", u.conf.Delay)
delay := time.Duration(u.conf.Delay) * time.Second
time.Sleep(delay)
if len(mockList) > 0 {
for _, mock := range mockList {
funcName := runtime.FuncForPC(reflect.ValueOf(mock).Pointer()).Name()
u.Green("START RUN mock:%s", funcName)
mock()
}
u.Green("---RUN mock done---")
}
u.Green("----START UNIT TEST----")
for _, api := range u.conf.ApiList {
u.Green("--------START TEST API:【%s】--------", api.Desc)
for _, cs := range api.CaseList {
u.testCase(api, cs)
}
fmt.Println()
}
}
func (u *Unit) testCase(api *Api, cs Case) {
var (
req = CaseToRequest(api, cs)
resp = fasthttp.AcquireResponse()
)
defer fasthttp.ReleaseRequest(req)
defer fasthttp.ReleaseResponse(resp)
rw := NewReadWriter(u.server)
u.Green("@@@@@ START RUN CASE:【%s】 %s", cs.Desc, strings.TrimSuffix(req.String(), "\n"))
err := rw.Request(req, resp)
if err != nil {
u.Fatal("REQUEST ERROR:%s", err)
}
if !IsExpected(cs.ExpectedType, cs.Expected, resp) {
u.Red("###########EXECUTE API【%s】 CASE:【%s】FAILED###########", api.Desc, cs.Desc)
u.Yellow("RESPONSE STATUS:%d BODY:%s\n",
resp.StatusCode(),
resp.Body(),
)
} else {
u.Green("###########PASS###########\n")
}
}
func (u *Unit) Fatal(format string, args ...interface{}) {
u.t.Fatal(Red(format, args...))
}
func (u *Unit) Red(format string, args ...interface{}) {
u.t.Logf(Red(format, args...))
}
func (u *Unit) Green(format string, args ...interface{}) {
u.t.Logf(Green(format, args...))
}
func (u *Unit) Yellow(format string, args ...interface{}) {
u.t.Logf(Yellow(format, args...))
}