-
Notifications
You must be signed in to change notification settings - Fork 1
/
consul_test.go
71 lines (57 loc) · 1.62 KB
/
consul_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
package consul
import (
"net/http"
"net/url"
"testing"
consul "github.com/hashicorp/consul/api"
"github.com/nbio/st"
"gopkg.in/h2non/gock.v0"
"gopkg.in/vinxi/utils.v0"
)
func TestConsulSimpleClient(t *testing.T) {
defer gock.Off()
gock.New("http://consul.io").
Get("/v1/health/service/web").
Reply(200).
Type("json").
BodyString(consulResponse)
config := NewConfig("web", "http://demo.consul.io")
gock.InterceptClient(config.Instances[0].HttpClient)
consul := New(config)
w := utils.NewWriterStub()
req := &http.Request{URL: &url.URL{}}
var called bool
consul.HandleHTTP(w, req, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
called = true
}))
st.Expect(t, called, true)
st.Expect(t, req.Host, "127.0.0.1:80")
st.Expect(t, req.URL.Host, "127.0.0.1:80")
st.Expect(t, w.Code, 200)
st.Expect(t, string(w.Body), "")
}
func TestConsulCustomMapper(t *testing.T) {
defer gock.Off()
gock.New("http://consul.io").
Get("/v1/health/service/web").
Reply(200).
Type("json").
BodyString(consulResponse)
config := NewConfig("web", "http://demo.consul.io")
config.Mapper = func(list []*consul.ServiceEntry) []string {
return MapConsulEntries(list)
}
gock.InterceptClient(config.Instances[0].HttpClient)
consul := New(config)
w := utils.NewWriterStub()
req := &http.Request{URL: &url.URL{}}
var called bool
consul.HandleHTTP(w, req, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
called = true
}))
st.Expect(t, called, true)
st.Expect(t, req.Host, "127.0.0.1:80")
st.Expect(t, req.URL.Host, "127.0.0.1:80")
st.Expect(t, w.Code, 200)
st.Expect(t, string(w.Body), "")
}