-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathkong_test.go
130 lines (101 loc) · 4.58 KB
/
kong_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/gojektech/heimdall/httpclient"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
Timeout = 1000 * time.Millisecond
HTTPClient = httpclient.NewClient()
)
func TestNewKongClient(t *testing.T) {
kclient := newKongClient("127.0.0.1", "9000", Timeout)
assert.Equal(t, "127.0.0.1:9000", kclient.kongAdminURL)
assert.NotNil(t, kclient.httpClient)
}
func TestUpstreamsSuccess(t *testing.T) {
httpServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{ "data" : [ {"id": "123-123", "name": "upstream1"}, {"id": "123-124", "name": "upstream2" }] }`))
}))
defer httpServer.Close()
kclient := &kongClient{httpClient: HTTPClient, kongAdminURL: httpServer.URL}
upstreams, err := kclient.upstreams()
require.NoError(t, err, "should not have failed to get upstreams")
require.Equal(t, 2, len(upstreams))
assert.Equal(t, "123-123", upstreams[0].ID)
assert.Equal(t, "upstream1", upstreams[0].Name)
assert.Equal(t, "123-124", upstreams[1].ID)
assert.Equal(t, "upstream2", upstreams[1].Name)
}
func TestUpstreamFailureInvalidURL(t *testing.T) {
kclient := &kongClient{httpClient: HTTPClient, kongAdminURL: "kgp://foo.com"}
upstreams, err := kclient.upstreams()
require.Error(t, err, "should have failed to get upstreams")
assert.Equal(t, 0, len(upstreams))
}
func TestUpstreamsFailureUnmarshal(t *testing.T) {
httpServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{ "data" : [ {"id": "123-123", "name": "upstream1"}, {"id": "123-124", "name": "upstream2"] }`))
}))
defer httpServer.Close()
kclient := &kongClient{httpClient: HTTPClient, kongAdminURL: httpServer.URL}
upstreams, err := kclient.upstreams()
require.Error(t, err, "should have failed to get upstreams")
require.Equal(t, 0, len(upstreams))
}
func TestTargetsForSuccess(t *testing.T) {
httpServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{ "data" : [ {"id": "123-123", "target": "1.2.3.4:8080","weight": 1, "upstream_id": "123-122"}, {"id": "123-124", "target": "1.2.3.5:8080","weight": 0, "upstream_id": "12223"}] }`))
}))
defer httpServer.Close()
kclient := &kongClient{httpClient: HTTPClient, kongAdminURL: httpServer.URL}
targets, err := kclient.targetsFor("upstream1")
require.NoError(t, err, "should not have failed to get targets")
require.Equal(t, 2, len(targets))
assert.Equal(t, "123-123", targets[0].ID)
assert.Equal(t, "1.2.3.4:8080", targets[0].URL)
assert.Equal(t, 1, targets[0].Weight)
assert.Equal(t, "123-122", targets[0].UpstreamID)
assert.Equal(t, "123-124", targets[1].ID)
assert.Equal(t, "1.2.3.5:8080", targets[1].URL)
assert.Equal(t, 0, targets[1].Weight)
assert.Equal(t, "12223", targets[1].UpstreamID)
}
func TestTargetsForFailureInvalidURL(t *testing.T) {
kclient := &kongClient{httpClient: HTTPClient, kongAdminURL: "kgp://foo.com"}
upstreams, err := kclient.targetsFor("upstream1")
require.Error(t, err, "should have failed to get upstreams")
assert.Equal(t, 0, len(upstreams))
}
func TestTargetsForFailureUnmarshal(t *testing.T) {
httpServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{ "data" : [ {"id": "123-123", "target": "1.2.3.4:8080","weight": 1, "upstream_id": "121212"}, {"id": "123-124", "target": "1.2.3.5:8080","weight": "0 }] }`))
}))
defer httpServer.Close()
kclient := &kongClient{httpClient: HTTPClient, kongAdminURL: httpServer.URL}
upstreams, err := kclient.targetsFor("upstream1")
require.Error(t, err, "should have failed to get upstreams")
require.Equal(t, 0, len(upstreams))
}
func TestSetTargetWeightForSuccess(t *testing.T) {
httpServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusCreated)
}))
defer httpServer.Close()
kclient := &kongClient{httpClient: HTTPClient, kongAdminURL: httpServer.URL}
err := kclient.setTargetWeightFor("upstream1", "target1", 100)
require.NoError(t, err, "should not have failed to set target weight")
}
func TestSetTargetWeightForFailure(t *testing.T) {
httpServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}))
defer httpServer.Close()
kclient := &kongClient{httpClient: HTTPClient, kongAdminURL: httpServer.URL}
err := kclient.setTargetWeightFor("upstream1", "target1", 100)
require.Error(t, err, "should have failed to set target weight")
}