-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock_server.go
69 lines (65 loc) · 1.63 KB
/
mock_server.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
package main
import (
"fmt"
"net/http"
"strings"
"time"
)
type testHandler struct {
response []byte
chunks []Chunk
headers map[string]string
code int
handleFunc func(w http.ResponseWriter, r *http.Request)
}
func (th *testHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if th.handleFunc != nil {
th.handleFunc(w, r)
return
}
req := fmt.Sprintf("%s %s", r.Method, r.URL.String())
logger.Info("testHandler", "req", req)
defer func() {
logger.Info("testHandler", "handled", req)
}()
for k, v := range th.headers {
w.Header().Set(k, v)
}
if len(th.chunks) > 0 {
w.Header().Set("Transfer-Encoding", "chunked")
w.Header().Del("Content-Encoding") // compression in chunked mode is not supported
flush := w.(http.Flusher).Flush
w.WriteHeader(th.code)
flush()
// tick := time.Now()
for _, c := range th.chunks {
time.Sleep(time.Duration(c.Delay) * time.Millisecond)
// logger.Printf("testHandler :%v -> '%s'\n", time.Since(tick), c.msg)
// tick = time.Now()
_, err := w.Write([]byte(c.Data + "\n"))
if err != nil {
logger.Error("testHandler", "req", req, "error", err)
return
}
flush()
}
} else {
data := th.response
if len(data) > minCompressSize && strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
w.Header().Set("Content-Encoding", "gzip")
data = mustCompress(data)
} else {
w.Header().Del("Content-Encoding")
}
w.Header().Set("Content-Length", fmt.Sprint(len(data)))
w.WriteHeader(th.code)
w.Write(data)
}
}
func mustCompress(data []byte) []byte {
compressed, err := compress(data)
if err != nil {
panic(err)
}
return compressed
}