forked from brahma-adshonor/gorr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
native_http_recorder_test.go
89 lines (66 loc) · 1.82 KB
/
native_http_recorder_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
package gorr
import (
"bytes"
"compress/gzip"
"fmt"
"github.com/brahma-adshonor/gohook"
"github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"net/url"
"sync"
"testing"
"time"
)
var (
reqData = []byte("req data for http")
rspData = []byte("rsp data for http")
)
type dummyHttpHandle struct{}
func (h *dummyHttpHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Printf("http recorder dummy http server\n")
var buf bytes.Buffer
writer := gzip.NewWriter(&buf)
writer.Write(rspData)
writer.Close()
w.Write(buf.Bytes())
w.WriteHeader(233)
w.Header().Set("Content-Encoding", "gzip")
}
func TestRegister(t *testing.T) {
UnHookHttpFunc()
assert.Nil(t, HookHttpServerHandler())
defer UnHookHttpServerHandler()
fmt.Printf("debug info:%s\n", gohook.ShowDebugInfo())
h := &dummyHttpHandle{}
fmt.Printf("start testing http.Handle hook, h:%v\n", h)
http.Handle("/test_point", h)
assert.Equal(t, 0, len(handlerMap))
var wg sync.WaitGroup
wg.Add(1)
RegisterHttpRecorder("/test_point2", func(p, n string, r *HttpData, s *HttpData) {
fmt.Printf("recorder handler enter!\n")
assert.Equal(t, 233, s.Status)
assert.Equal(t, reqData, r.Body)
assert.Equal(t, rspData, s.Body)
wg.Done()
}, nil)
http.Handle("/test_point2", h)
assert.Equal(t, 1, len(handlerMap))
go http.ListenAndServe("127.0.0.1:2333", nil)
time.Sleep(time.Duration(500) * time.Millisecond)
c := http.Client{Timeout: time.Duration(1) * time.Second}
rsp, err2 := c.Post("http://localhost:2333/test_point2", "application/json", bytes.NewBuffer(reqData))
if err2 != nil {
errReal, ok := err2.(*url.Error)
if ok {
fmt.Printf("post err:%v\n", *errReal)
}
}
assert.Nil(t, err2)
d, err3 := ioutil.ReadAll(rsp.Body)
assert.Nil(t, err3)
assert.Equal(t, rspData, d)
fmt.Printf("resp data:%s\n", string(d))
wg.Wait()
}