-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
156 lines (146 loc) · 3.29 KB
/
main.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
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"bytes"
"github.com/CloudyKit/jet/v6"
xerr "github.com/goclub/error"
xhttp "github.com/goclub/http"
xjson "github.com/goclub/json"
"net/http"
"os"
"path"
"reflect"
)
type NewsRequest struct {
NewsID int64 `query:"newsID"`
}
type NewsReply struct {
Title string `json:"title"`
Context string `json:"context"`
xerr.Resp
}
type AuditRequest struct {
NewsID int64 `query:"newsID"`
}
type AuditStatus int8
const (
EnumAuditStatusQueue AuditStatus = 1
EnumAuditStatusDone AuditStatus = 2
EnumAuditStatusReject AuditStatus = 3
)
type AuditReply struct {
Status AuditStatus `json:"status"`
RejectReason string `json:"rejectReason"`
xerr.Resp
}
var view *jet.Set
func init() {
loader := jet.NewOSFileSystemLoader(path.Join(os.Getenv("GOPATH"), "src/github.com/goclub/http/example/internal/mock"))
opts := []jet.Option{}
opts = append(opts, jet.InDevelopmentMode())
opts = append(opts, jet.WithDelims("[[", "]]"))
view = jet.NewSet(
loader,
opts...,
)
view.AddGlobalFunc("xjson", func(a jet.Arguments) reflect.Value {
v := a.Get(0).Interface()
buffer := bytes.NewBuffer(nil)
err := xjson.NewEncoder(buffer).Encode(v)
if err != nil {
return reflect.ValueOf("encode json fail")
}
return reflect.ValueOf(buffer.Bytes())
})
}
type TemplateRender struct {
}
func (tr TemplateRender) Render(templatePath string, data interface{}, w http.ResponseWriter) (err error) {
t, err := view.GetTemplate(templatePath)
if err != nil {
return
}
return t.Execute(w, nil, data)
}
func main() {
ms := xhttp.NewMockServer(xhttp.MockServerOption{
DefaultReply: map[string]interface{}{
"pass": xerr.Resp{},
"fail": xerr.Resp{
Error: xerr.RespError{
Code: 1,
Message: "错误消息",
},
},
},
Render: TemplateRender{},
})
defer ms.Listen(3422)
ms.URL(xhttp.Mock{
Route: xhttp.Route{xhttp.GET, "/news"},
Request: xhttp.MockRequest{
"main": NewsRequest{
NewsID: 1,
},
},
Reply: xhttp.MockReply{
"pass": NewsReply{
Title: "goclub/http 发布 mock 功能",
Context: "全新版本,全新体验,解放前端",
},
"fail": xerr.Resp{
Error: xerr.RespError{
Code: 1,
Message: "新闻ID错误",
},
},
},
})
ms.URL(xhttp.Mock{
Route: xhttp.Route{xhttp.GET, "/audit/status"},
Request: xhttp.MockRequest{
"main": AuditReply{},
},
Match: func(c *xhttp.Context) (key string) {
return xhttp.MockMatchSceneCount(c, map[string]map[string]string{
"": {
"1": "queue",
"2": "pass",
"": "pass",
},
"finalReject": {
"1": "queue",
"2": "reject",
"": "reject",
},
})
},
DisableDefaultReply: "pass",
Reply: xhttp.MockReply{
"done": AuditReply{
Status: EnumAuditStatusDone,
},
"reject": AuditReply{
Status: EnumAuditStatusReject,
RejectReason: "内容非法",
},
"queue": AuditReply{
Status: EnumAuditStatusQueue,
},
},
})
ms.URL(xhttp.Mock{
Route: xhttp.Route{xhttp.GET, "/handleFunc"},
HandleFunc: func(c *xhttp.Context, replyKey string, data interface{}) error {
return c.WriteJSON("handleFunc " + replyKey)
},
})
ms.URL(xhttp.Mock{
Route: xhttp.Route{xhttp.GET, "/render"},
Reply: xhttp.MockReply{
"pass": map[string]interface{}{
"name": "nimo",
},
},
Render: "./page.html",
})
}