-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock_server_test.go
267 lines (230 loc) · 6.5 KB
/
mock_server_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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package gotten_test
import (
"context"
"crypto/md5"
"encoding/json"
"fmt"
"github.com/Hexilee/gotten"
"github.com/Hexilee/gotten/headers"
"github.com/Hexilee/gotten/mock"
"github.com/go-chi/chi"
"io"
"io/ioutil"
"net/http"
"strconv"
)
var (
database *Database
router chi.Router
mockClient gotten.Client
)
func init() {
database = newDatabase()
go database.Run()
database.Add(2018, 10, 1, &TestPost{"Hexilee", "Start!", "Hello world!"})
router = chi.NewRouter()
router.Get("/post/{year}/{month}/{day}", getPost)
router.Get("/text", getText)
router.Post("/post/{year}/{month}/{day}", addPost)
router.Post("/post", addPostByForm)
router.Post("/avatar", addAvatar)
mockBuilder := mock.NewClientBuilder()
mockBuilder.Register("mock.io", router)
mockClient = mockBuilder.Build()
}
func getText(w http.ResponseWriter, _ *http.Request) {
w.Header().Set(headers.HeaderContentType, headers.MIMETextPlain)
w.WriteHeader(http.StatusOK)
w.Write([]byte("true"))
}
func getPost(w http.ResponseWriter, r *http.Request) {
year, _ := strconv.Atoi(chi.URLParam(r, "year"))
month, _ := strconv.Atoi(chi.URLParam(r, "month"))
day, _ := strconv.Atoi(chi.URLParam(r, "day"))
page, err := strconv.Atoi(r.URL.Query().Get("page"))
if err != nil {
page = 1
}
limit, err := strconv.Atoi(r.URL.Query().Get("limit"))
if err != nil {
limit = 10
}
posts := database.Get(year, month, day, page, limit)
result, _ := json.Marshal(&posts)
w.Header().Set(headers.HeaderContentType, headers.MIMEApplicationJSONCharsetUTF8)
w.WriteHeader(http.StatusOK)
w.Write(result)
}
func addPost(w http.ResponseWriter, r *http.Request) {
var result AddedData
defer func() {
w.Header().Set(headers.HeaderContentType, headers.MIMEApplicationJSONCharsetUTF8)
w.WriteHeader(http.StatusCreated)
respData, _ := json.Marshal(&result)
w.Write(respData)
}()
year, _ := strconv.Atoi(chi.URLParam(r, "year"))
month, _ := strconv.Atoi(chi.URLParam(r, "month"))
day, _ := strconv.Atoi(chi.URLParam(r, "day"))
body := r.Body
defer body.Close()
data, err := ioutil.ReadAll(body)
if err != nil {
result.Success = false
return
}
var post TestPost
err = json.Unmarshal(data, &post)
if err != nil {
result.Success = false
return
}
result = *database.Add(year, month, day, &post)
}
func addPostByForm(w http.ResponseWriter, r *http.Request) {
var result AddedData
defer func() {
w.Header().Set(headers.HeaderContentType, headers.MIMEApplicationJSONCharsetUTF8)
w.WriteHeader(http.StatusCreated)
respData, _ := json.Marshal(&result)
w.Write(respData)
}()
r.ParseForm()
year, _ := strconv.Atoi(r.PostForm.Get("year"))
month, _ := strconv.Atoi(r.PostForm.Get("month"))
day, _ := strconv.Atoi(r.PostForm.Get("day"))
postStr := r.PostForm.Get("post")
var post TestPost
err := json.Unmarshal([]byte(postStr), &post)
if err != nil {
result.Success = false
return
}
result = *database.Add(year, month, day, &post)
}
func addAvatar(w http.ResponseWriter, r *http.Request) {
var result UploadedData
defer func() {
w.Header().Set(headers.HeaderContentType, headers.MIMEApplicationJSONCharsetUTF8)
w.WriteHeader(http.StatusCreated)
respData, _ := json.Marshal(&result)
w.Write(respData)
}()
defer r.Body.Close()
r.ParseMultipartForm(32 << 20)
file, handler, err := r.FormFile("avatar")
if err != nil {
fmt.Printf("%#v\n", err.Error())
return
}
result.Filename = handler.Filename
result.FileSize = handler.Size
result.Uid, err = strconv.Atoi(r.PostFormValue("uid"))
if err != nil {
return
}
result.Username = r.PostFormValue("username")
descData := r.PostFormValue("description")
var description AvatarDescription
if json.Unmarshal([]byte(descData), &description) != nil {
return
}
result.Creator = description.Creator
result.CreatedAt = description.CreatedAt
h := md5.New()
io.Copy(h, file)
result.Hash = fmt.Sprintf("%x", h.Sum(nil))
}
type (
TestPost struct {
Author string `json:"author"`
Title string `json:"title"`
Content string `json:"content"`
}
GetterData struct {
dataChan chan<- []*TestPost
year, month, day, page, limit int
}
AdderData struct {
dataChan chan<- *AddedData
year, month, day int
post *TestPost
}
AddedData struct {
Success bool `json:"success"`
Year int `json:"year"`
Month int `json:"month"`
Day int `json:"day"`
Order int `json:"order"`
}
Database struct {
cancel context.CancelFunc
getterChan chan *GetterData
adderChan chan *AdderData
data map[string][]*TestPost
}
)
func newDatabase() *Database {
return &Database{
cancel: func() {},
getterChan: make(chan *GetterData, 100),
adderChan: make(chan *AdderData, 100),
data: make(map[string][]*TestPost),
}
}
func (database *Database) Run() {
ctx, cancel := context.WithCancel(context.Background())
database.cancel = cancel
Loop:
for {
select {
case getterData := <-database.getterChan:
var resultList []*TestPost
totalList := database.data[genKey(getterData.year, getterData.month, getterData.day)]
length := len(totalList)
offset := (getterData.page - 1) * getterData.limit
max := getterData.page * getterData.limit
if length <= offset {
resultList = make([]*TestPost, 0)
} else if length <= max {
resultList = make([]*TestPost, length-offset)
copy(resultList, totalList[offset:])
} else {
resultList = make([]*TestPost, getterData.limit)
copy(resultList, totalList[offset:max-1])
}
getterData.dataChan <- resultList
case adderData := <-database.adderChan:
key := genKey(adderData.year, adderData.month, adderData.day)
if database.data[key] == nil {
database.data[key] = make([]*TestPost, 0)
}
database.data[key] = append(database.data[key], adderData.post)
adderData.dataChan <- &AddedData{true, adderData.year, adderData.month, adderData.day, len(database.data[key])}
case <-ctx.Done():
break Loop
}
}
}
func (database *Database) Stop() {
database.cancel()
}
func (database *Database) Get(year, month, day, page, limit int) []*TestPost {
dataChan := make(chan []*TestPost, 1)
database.getterChan <- &GetterData{dataChan, year, month, day, page, limit}
select {
case posts := <-dataChan:
return posts
}
}
func (database *Database) Add(year, month, day int, post *TestPost) *AddedData {
dataChan := make(chan *AddedData, 1)
database.adderChan <- &AdderData{dataChan, year, month, day, post}
select {
case added := <-dataChan:
return added
}
}
func genKey(year, month, day int) string {
return fmt.Sprintf("%d-%d-%d", year, month, day)
}