forked from go-graphite/graphite-clickhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_server.go
54 lines (42 loc) · 837 Bytes
/
test_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
package clickhouse
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"sync"
)
type TestRequest struct {
Query []byte
}
type TestHandler struct {
sync.Mutex
request []TestRequest
}
type TestServer struct {
*httptest.Server
handler *TestHandler
}
func (h *TestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
body, _ := ioutil.ReadAll(r.Body)
req := TestRequest{
Query: body,
}
h.Lock()
if h.request == nil {
h.request = make([]TestRequest, 0)
}
h.request = append(h.request, req)
h.Unlock()
}
func NewTestServer() *TestServer {
h := &TestHandler{
request: make([]TestRequest, 0),
}
srv := httptest.NewServer(h)
return &TestServer{Server: srv, handler: h}
}
func (srv *TestServer) Requests() []TestRequest {
srv.handler.Lock()
defer srv.handler.Unlock()
return srv.handler.request
}