-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock_test.go
82 lines (66 loc) · 1.73 KB
/
mock_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
package baker_test
import (
"fmt"
"net/http"
"net/http/httptest"
"net/netip"
"testing"
"time"
"github.com/alinz/baker.go"
"github.com/alinz/baker.go/pkg/collection"
"github.com/alinz/baker.go/rule"
)
func MockDriver(t *testing.T, confs ...interface{ WriteResponse(w http.ResponseWriter) }) <-chan *baker.Container {
containers := make(chan *baker.Container, len(confs))
httpServers := make([]*httptest.Server, 0, len(confs))
for i, conf := range confs {
server := func(conf interface{ WriteResponse(w http.ResponseWriter) }) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/config" {
conf.WriteResponse(w)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{}`))
}))
}(conf)
addr, err := netip.ParseAddrPort(server.Listener.Addr().String())
if err != nil {
t.Fatal(err)
}
containers <- &baker.Container{
ID: fmt.Sprintf("container-%d", i),
Addr: addr,
Path: "/config",
}
httpServers = append(httpServers, server)
}
t.Cleanup(func() {
for _, server := range httpServers {
server.Close()
}
})
return containers
}
func StartBakerServer(t *testing.T, containers <-chan *baker.Container, size int) (url string) {
done := make(chan struct{}, 1)
baker := baker.New(
containers,
baker.WithPingDuration(1*time.Second),
baker.WithRules(
rule.RegisterAppendPath(),
rule.RegisterReplacePath(),
rule.RegisterRateLimiter(),
),
baker.WithOnAfterPinger(func(containerSet *collection.Set[string, *baker.Container]) {
if containerSet.Len() == size && done != nil {
close(done)
done = nil
}
}),
)
s := httptest.NewServer(baker)
t.Cleanup(s.Close)
<-done
return s.URL
}