forked from mautrix/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_internal_test.go
70 lines (62 loc) · 1.56 KB
/
client_internal_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
package mautrix
import (
"bytes"
"context"
"net/http"
"testing"
"time"
"github.com/rs/zerolog"
"github.com/tidwall/gjson"
)
func TestBackoffFromResponse(t *testing.T) {
now := time.Now().Truncate(time.Second)
defaultBackoff := time.Duration(123)
for name, tt := range map[string]struct {
headerValue string
expected time.Duration
expectedLog string
}{
"AsDate": {
headerValue: now.In(time.UTC).Add(5 * time.Hour).Format(http.TimeFormat),
expected: time.Duration(5) * time.Hour,
expectedLog: "",
},
"AsSeconds": {
headerValue: "12345",
expected: time.Duration(12345) * time.Second,
expectedLog: "",
},
"Missing": {
headerValue: "",
expected: defaultBackoff,
expectedLog: "",
},
"Bad": {
headerValue: "invalid",
expected: defaultBackoff,
expectedLog: `Failed to parse Retry-After header value`,
},
} {
t.Run(name, func(t *testing.T) {
var out bytes.Buffer
c := &Client{Log: zerolog.New(&out)}
actual := parseBackoffFromResponse(
(&http.Request{}).WithContext(c.Log.WithContext(context.Background())),
&http.Response{
Header: http.Header{
"Retry-After": []string{tt.headerValue},
},
},
now,
time.Duration(123),
)
if actual != tt.expected {
t.Fatalf("Backoff duration output mismatch, expected %s, got %s", tt.expected, actual)
}
lastLogged := gjson.GetBytes(out.Bytes(), zerolog.MessageFieldName).Str
if lastLogged != tt.expectedLog {
t.Fatalf(`Log line mismatch, expected "%s", got "%s"`, tt.expectedLog, lastLogged)
}
})
}
}