-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathServiceTester.go
144 lines (135 loc) · 3.34 KB
/
ServiceTester.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package s
import (
"fmt"
"github.com/ssgo/config"
"github.com/ssgo/log"
"github.com/ssgo/u"
"reflect"
"testing"
)
//var testServer *httptest.Server
//var testHeaders = make(map[string]string)
//
//func StartTestService() *httptest.Server {
// initConfig()
// testServer = httptest.NewServer(http.Handler(&routeHandler{}))
// //if recordLogs {fmt.Println()}
// //fmt.Println("Start test service\n")
// return testServer
//}
//
//func SetTestHeader(k string, v string) {
// testHeaders[k] = v
//}
func ResetAllSets() {
config.ResetConfigEnv()
Config = ServiceConfig{}
inited = false
rewrites = make(map[string]*rewriteInfo)
regexRewrites = make([]*rewriteInfo, 0)
proxies = make(map[string]*proxyInfo, 0)
regexProxies = make([]*proxyInfo, 0)
statics = make(map[string]*string)
usedSessionIdKey = ""
//sessionCreator = nil
//sessionObjects = map[*http.Request]map[reflect.Type]any{}
injectObjects = map[reflect.Type]any{}
webServices = make(map[string]*webServiceType)
regexWebServices = make([]*webServiceType, 0)
inFilters = make([]func(*map[string]any, *Request, *Response, *log.Logger) any, 0)
outFilters = make([]func(map[string]any, *Request, *Response, any, *log.Logger) (any, bool), 0)
websocketServices = make(map[string]*websocketServiceType)
regexWebsocketServices = make([]*websocketServiceType, 0)
webAuthChecker = nil
webSocketActionAuthChecker = nil
}
//func testRequest(method string, path string, body []byte) (*http.Response, []byte, error) {
// client := &http.Client{}
// var bodyReader io.Reader = nil
// if body != nil {
// bodyReader = bytes.NewReader(body)
// }
// req, err := http.NewRequest(method, testServer.URL+path, bodyReader)
// if err != nil {
// return nil, nil, err
// }
//
// for k, v := range testHeaders {
// req.Header.Add(k, v)
// }
// res, err := client.Do(req)
// if err != nil {
// return nil, nil, err
// }
// defer res.Body.Close()
//
// result, err := ioutil.ReadAll(res.Body)
// if err != nil {
// return nil, nil, err
// }
// res.Body.Close()
//
// return res, result, nil
//}
//
//func TestGet(path string) (*http.Response, []byte, error) {
// return testRequest("GET", path, nil)
//}
//
////func TestPost(path string, args map[string]string) ([]byte, error) {
//// return testRequest("POST", path, nil)
////}
//
//func TestService(path string, args Map) any {
//
// argsObjectBytes, _ := json.Marshal(args)
//
// _, result, err := testRequest("POST", path, argsObjectBytes)
// if err != nil {
// fmt.Println("testRequest", err)
// return nil
// }
//
// var resultObject any
// err = json.Unmarshal(result, &resultObject)
// if err != nil {
// fmt.Println("Unmarshal", err)
// return nil
// }
//
// return resultObject
//}
//
//func StopTestService() {
// testServer.Close()
// //if recordLogs {fmt.Println()}
// //fmt.Println("\n\nStop test service")
//}
type Testing struct {
tt *testing.T
tb *testing.B
}
func T(tt *testing.T) *Testing {
t := new(Testing)
t.tt = tt
return t
}
func B(tb *testing.B) *Testing {
t := new(Testing)
t.tb = tb
return t
}
func (t *Testing) Test(tests bool, comment string, addons ...any) {
if !tests {
fmt.Println(" \x1b[0;41m失败\x1b[0m", comment, u.JsonP(addons), ".")
if t.tt != nil {
t.tt.Error(comment, addons)
panic(comment)
} else if t.tb != nil {
t.tb.Error(comment, addons)
panic(comment)
}
} else {
fmt.Println(" \x1b[0;42m成功\x1b[0m", comment)
}
}