-
Notifications
You must be signed in to change notification settings - Fork 18
/
ldclient_service_endpoints_test.go
210 lines (181 loc) · 7.21 KB
/
ldclient_service_endpoints_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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package ldclient
import (
"bytes"
"io"
"net/http"
"net/url"
"testing"
"time"
"github.com/launchdarkly/go-sdk-common/v3/ldlog"
"github.com/launchdarkly/go-sdk-common/v3/ldlogtest"
"github.com/launchdarkly/go-server-sdk/v7/interfaces"
"github.com/launchdarkly/go-server-sdk/v7/internal/endpoints"
"github.com/launchdarkly/go-server-sdk/v7/ldcomponents"
th "github.com/launchdarkly/go-test-helpers/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// These tests verify that the SDK is using the expected base URIs in various configurations.
// Since we need to be able to intercept requests that would normally go to the production service
// endpoints, and we don't care about simulating realistic responses, we'll use a fake HTTP client
// rather than an embedded server.
type recordingClientFactory struct {
requestURLs chan url.URL
status int
}
const customURI = "http://custom/"
func mustParseURI(s string) url.URL {
u, err := url.Parse(s)
if err != nil {
panic(err)
}
return *u
}
func baseURIOf(u url.URL) url.URL {
u.Path = "/"
return u
}
func newRecordingClientFactory(status int) *recordingClientFactory {
return &recordingClientFactory{status: status, requestURLs: make(chan url.URL, 100)}
}
func (r *recordingClientFactory) MakeClient() *http.Client {
c := *http.DefaultClient
c.Transport = r
return &c
}
func (r *recordingClientFactory) RoundTrip(req *http.Request) (*http.Response, error) {
r.requestURLs <- *req.URL
return &http.Response{
StatusCode: r.status,
Body: io.NopCloser(bytes.NewBuffer(nil)),
}, nil
}
func (r *recordingClientFactory) requireRequest(t *testing.T) url.URL {
return th.RequireValue(t, r.requestURLs, time.Second, "timed out waiting for request")
}
func TestDefaultStreamingDataSourceBaseUri(t *testing.T) {
rec := newRecordingClientFactory(401)
config := Config{
Events: ldcomponents.NoEvents(),
HTTP: ldcomponents.HTTPConfiguration().HTTPClientFactory(rec.MakeClient),
}
client, _ := MakeCustomClient(testSdkKey, config, time.Second*5)
require.NotNil(t, client)
defer client.Close()
u := rec.requireRequest(t)
assert.Equal(t, mustParseURI(endpoints.DefaultStreamingBaseURI), baseURIOf(u))
}
func TestDefaultPollingDataSourceBaseUri(t *testing.T) {
rec := newRecordingClientFactory(401)
config := Config{
DataSource: ldcomponents.PollingDataSource(),
Events: ldcomponents.NoEvents(),
HTTP: ldcomponents.HTTPConfiguration().HTTPClientFactory(rec.MakeClient),
}
client, _ := MakeCustomClient(testSdkKey, config, time.Second*5)
require.NotNil(t, client)
defer client.Close()
u := rec.requireRequest(t)
assert.Equal(t, mustParseURI(endpoints.DefaultPollingBaseURI), baseURIOf(u))
}
func TestDefaultEventsBaseUri(t *testing.T) {
rec := newRecordingClientFactory(401)
config := Config{
DataSource: ldcomponents.ExternalUpdatesOnly(),
HTTP: ldcomponents.HTTPConfiguration().HTTPClientFactory(rec.MakeClient),
}
client, _ := MakeCustomClient(testSdkKey, config, time.Second*5)
require.NotNil(t, client)
defer client.Close()
u := rec.requireRequest(t)
assert.Equal(t, mustParseURI(endpoints.DefaultEventsBaseURI), baseURIOf(u))
}
func TestCustomStreamingBaseURI(t *testing.T) {
rec := newRecordingClientFactory(401)
mockLog := ldlogtest.NewMockLog()
config := Config{
Events: ldcomponents.NoEvents(),
HTTP: ldcomponents.HTTPConfiguration().HTTPClientFactory(rec.MakeClient),
Logging: ldcomponents.Logging().Loggers(mockLog.Loggers),
ServiceEndpoints: interfaces.ServiceEndpoints{Streaming: customURI},
}
client, _ := MakeCustomClient(testSdkKey, config, time.Second*5)
require.NotNil(t, client)
defer client.Close()
u := rec.requireRequest(t)
assert.Equal(t, mustParseURI(customURI), baseURIOf(u))
mockLog.AssertMessageMatch(t, false, ldlog.Error, "You have set custom ServiceEndpoints without specifying")
}
func TestCustomPollingBaseURI(t *testing.T) {
rec := newRecordingClientFactory(401)
mockLog := ldlogtest.NewMockLog()
config := Config{
DataSource: ldcomponents.PollingDataSource(),
Events: ldcomponents.NoEvents(),
HTTP: ldcomponents.HTTPConfiguration().HTTPClientFactory(rec.MakeClient),
Logging: ldcomponents.Logging().Loggers(mockLog.Loggers),
ServiceEndpoints: interfaces.ServiceEndpoints{Polling: customURI},
}
client, _ := MakeCustomClient(testSdkKey, config, time.Second*5)
require.NotNil(t, client)
defer client.Close()
u := rec.requireRequest(t)
assert.Equal(t, mustParseURI(customURI), baseURIOf(u))
mockLog.AssertMessageMatch(t, false, ldlog.Error, "You have set custom ServiceEndpoints without specifying")
}
func TestCustomEventsBaseURI(t *testing.T) {
rec := newRecordingClientFactory(401)
mockLog := ldlogtest.NewMockLog()
config := Config{
DataSource: ldcomponents.ExternalUpdatesOnly(),
HTTP: ldcomponents.HTTPConfiguration().HTTPClientFactory(rec.MakeClient),
Logging: ldcomponents.Logging().Loggers(mockLog.Loggers),
ServiceEndpoints: interfaces.ServiceEndpoints{Events: customURI},
}
client, _ := MakeCustomClient(testSdkKey, config, time.Second*5)
require.NotNil(t, client)
defer client.Close()
u := rec.requireRequest(t)
assert.Equal(t, mustParseURI(customURI), baseURIOf(u))
mockLog.AssertMessageMatch(t, false, ldlog.Error, "You have set custom ServiceEndpoints without specifying")
}
func TestErrorIsLoggedIfANecessaryURIIsNotSetWhenOtherCustomURIsAreSet(t *testing.T) {
rec := newRecordingClientFactory(401)
mockLog1 := ldlogtest.NewMockLog()
config1 := Config{
Events: ldcomponents.NoEvents(),
HTTP: ldcomponents.HTTPConfiguration().HTTPClientFactory(rec.MakeClient),
Logging: ldcomponents.Logging().Loggers(mockLog1.Loggers),
ServiceEndpoints: interfaces.ServiceEndpoints{Events: customURI},
}
client1, _ := MakeCustomClient(testSdkKey, config1, time.Second*5)
require.NotNil(t, client1)
client1.Close()
mockLog1.AssertMessageMatch(t, true, ldlog.Error,
"You have set custom ServiceEndpoints without specifying the Streaming base URI")
mockLog2 := ldlogtest.NewMockLog()
config2 := Config{
DataSource: ldcomponents.PollingDataSource(),
Events: ldcomponents.NoEvents(),
HTTP: ldcomponents.HTTPConfiguration().HTTPClientFactory(rec.MakeClient),
Logging: ldcomponents.Logging().Loggers(mockLog2.Loggers),
ServiceEndpoints: interfaces.ServiceEndpoints{Streaming: customURI},
}
client2, _ := MakeCustomClient(testSdkKey, config2, time.Second*5)
require.NotNil(t, client2)
client2.Close()
mockLog2.AssertMessageMatch(t, true, ldlog.Error,
"You have set custom ServiceEndpoints without specifying the Polling base URI")
mockLog3 := ldlogtest.NewMockLog()
config3 := Config{
DataSource: ldcomponents.ExternalUpdatesOnly(),
HTTP: ldcomponents.HTTPConfiguration().HTTPClientFactory(rec.MakeClient),
Logging: ldcomponents.Logging().Loggers(mockLog3.Loggers),
ServiceEndpoints: interfaces.ServiceEndpoints{Streaming: customURI},
}
client3, _ := MakeCustomClient(testSdkKey, config3, time.Second*5)
require.NotNil(t, client3)
client3.Close()
mockLog3.AssertMessageMatch(t, true, ldlog.Error,
"You have set custom ServiceEndpoints without specifying the Events base URI")
}