-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfetch_test.go
More file actions
256 lines (222 loc) · 6.89 KB
/
fetch_test.go
File metadata and controls
256 lines (222 loc) · 6.89 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package main
import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
type roundTripFunc func(*http.Request) (*http.Response, error)
func (fn roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return fn(req)
}
func TestFetchServerListSuccessUpdatesState(t *testing.T) {
setupDurafmt()
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/get-games" {
t.Fatalf("unexpected path: %s", r.URL.Path)
}
if got := r.Header.Get("User-Agent"); got != UserAgent {
t.Fatalf("unexpected user agent: %s", got)
}
fmt.Fprint(w, makeServerListJSON(MinValidCount+1))
}))
defer server.Close()
restore := configureFetchTestState(t)
defer restore()
*sParam.URL = server.URL
sParam.LastRefresh = time.Time{}
sParam.LastAttempt = time.Time{}
sParam.ServerList.Servers = seedServers(2)
sParam.ServersCount = len(sParam.ServerList.Servers)
sParam.PlayerCount = 99
fetchHTTPClient = func() *http.Client {
return server.Client()
}
if err := fetchServerList(); err != nil {
t.Fatalf("fetchServerList returned error: %v", err)
}
if len(sParam.ServerList.Servers) != MinValidCount+1 {
t.Fatalf("expected %d servers, got %d", MinValidCount+1, len(sParam.ServerList.Servers))
}
if sParam.ServersCount != MinValidCount+1 {
t.Fatalf("expected ServersCount to be updated, got %d", sParam.ServersCount)
}
if sParam.PlayerCount != MinValidCount+1 {
t.Fatalf("expected PlayerCount to be updated, got %d", sParam.PlayerCount)
}
if sParam.LastRefresh.IsZero() {
t.Fatal("expected LastRefresh to be updated")
}
for _, server := range sParam.ServerList.Servers {
if strings.Contains(server.Tags[0], "[") || strings.Contains(server.Tags[0], "]") {
t.Fatalf("expected sanitized tags, got %q", server.Tags[0])
}
}
}
func TestFetchServerListNetworkErrorKeepsState(t *testing.T) {
setupDurafmt()
restore := configureFetchTestState(t)
defer restore()
original := seedServers(3)
sParam.ServerList.Servers = original
sParam.ServersCount = len(original)
sParam.PlayerCount = 7
sParam.LastRefresh = time.Unix(123, 0).UTC()
sParam.LastAttempt = time.Time{}
fetchHTTPClient = func() *http.Client {
return &http.Client{
Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {
return nil, errors.New("boom")
}),
}
}
err := fetchServerList()
if err == nil {
t.Fatal("expected error")
}
if len(sParam.ServerList.Servers) != len(original) {
t.Fatalf("expected stale servers to remain, got %d", len(sParam.ServerList.Servers))
}
if sParam.PlayerCount != 7 {
t.Fatalf("expected PlayerCount to remain unchanged, got %d", sParam.PlayerCount)
}
if !sParam.LastRefresh.Equal(time.Unix(123, 0).UTC()) {
t.Fatalf("expected LastRefresh to remain unchanged, got %v", sParam.LastRefresh)
}
}
func TestFetchServerListRejectsNon200(t *testing.T) {
setupDurafmt()
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "denied", http.StatusTooManyRequests)
}))
defer server.Close()
restore := configureFetchTestState(t)
defer restore()
*sParam.URL = server.URL
sParam.ServerList.Servers = seedServers(4)
sParam.ServersCount = 4
sParam.PlayerCount = 11
sParam.LastRefresh = time.Unix(222, 0).UTC()
fetchHTTPClient = func() *http.Client {
return server.Client()
}
err := fetchServerList()
if err == nil {
t.Fatal("expected error")
}
if !strings.Contains(err.Error(), "unexpected upstream status 429") {
t.Fatalf("unexpected error: %v", err)
}
if len(sParam.ServerList.Servers) != 4 {
t.Fatalf("expected stale servers to remain, got %d", len(sParam.ServerList.Servers))
}
if !sParam.LastRefresh.Equal(time.Unix(222, 0).UTC()) {
t.Fatalf("expected LastRefresh to remain unchanged, got %v", sParam.LastRefresh)
}
}
func TestFetchServerListRejectsInvalidJSON(t *testing.T) {
setupDurafmt()
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "{not-json")
}))
defer server.Close()
restore := configureFetchTestState(t)
defer restore()
*sParam.URL = server.URL
sParam.ServerList.Servers = seedServers(5)
sParam.ServersCount = 5
sParam.PlayerCount = 12
sParam.LastRefresh = time.Unix(333, 0).UTC()
fetchHTTPClient = func() *http.Client {
return server.Client()
}
err := fetchServerList()
if err == nil {
t.Fatal("expected error")
}
if len(sParam.ServerList.Servers) != 5 {
t.Fatalf("expected stale servers to remain, got %d", len(sParam.ServerList.Servers))
}
if !sParam.LastRefresh.Equal(time.Unix(333, 0).UTC()) {
t.Fatalf("expected LastRefresh to remain unchanged, got %v", sParam.LastRefresh)
}
}
func TestFetchServerListRejectsUndersizedPayload(t *testing.T) {
setupDurafmt()
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, makeServerListJSON(MinValidCount))
}))
defer server.Close()
restore := configureFetchTestState(t)
defer restore()
*sParam.URL = server.URL
sParam.ServerList.Servers = seedServers(6)
sParam.ServersCount = 6
sParam.PlayerCount = 13
sParam.LastRefresh = time.Unix(444, 0).UTC()
fetchHTTPClient = func() *http.Client {
return server.Client()
}
err := fetchServerList()
if err == nil {
t.Fatal("expected error")
}
if !strings.Contains(err.Error(), "returned only") {
t.Fatalf("unexpected error: %v", err)
}
if len(sParam.ServerList.Servers) != 6 {
t.Fatalf("expected stale servers to remain, got %d", len(sParam.ServerList.Servers))
}
if !sParam.LastRefresh.Equal(time.Unix(444, 0).UTC()) {
t.Fatalf("expected LastRefresh to remain unchanged, got %v", sParam.LastRefresh)
}
}
func configureFetchTestState(t *testing.T) func() {
t.Helper()
oldState := sParam
oldClient := fetchHTTPClient
username := "user"
token := "token"
baseURL := "https://example.invalid"
sParam = ServerStateData{
URL: &baseURL,
Username: &username,
Token: &token,
UserAgent: UserAgent,
}
return func() {
sParam = oldState
fetchHTTPClient = oldClient
}
}
func seedServers(count int) []ServerListItem {
servers := make([]ServerListItem, 0, count)
for i := 1; i <= count; i++ {
servers = append(servers, ServerListItem{
Name: fmt.Sprintf("seed-%d", i),
Players: []string{"seed-player"},
Tags: []string{"seed-tag"},
})
}
return servers
}
func makeServerListJSON(count int) string {
parts := make([]string, 0, count)
for i := 1; i <= count; i++ {
parts = append(parts, fmt.Sprintf(`{
"Application_version":{"Game_version":"1.1.%d","Build_mode":"","Build_version":0,"Platform":"linux"},
"Description":"[font=default-bold]Desc %d[/font]",
"Game_time_elapsed":%d,
"Has_password":false,
"Host_address":"127.0.0.1:%d",
"Mod_count":0,
"Name":"[color=red]Server %d[/color]",
"Players":["player-%d"],
"Tags":["[color=blue]tag-%d[/color]"]
}`, i, i, i, 3000+i, i, i, i))
}
return "[" + strings.Join(parts, ",") + "]"
}