| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package retryafter |
| 9 |
|
| 10 |
import ( |
| 11 |
"net/http" |
| 12 |
"testing" |
| 13 |
"time" |
| 14 |
|
| 15 |
"github.com/stretchr/testify/assert" |
| 16 |
) |
| 17 |
|
| 18 |
func TestBackoffFromResponse(t *testing.T) { |
| 19 |
currentTime := time.Now().Truncate(time.Second) |
| 20 |
now = func() time.Time { |
| 21 |
return currentTime |
| 22 |
} |
| 23 |
|
| 24 |
defaultBackoff := time.Duration(123) |
| 25 |
|
| 26 |
for name, tt := range map[string]struct { |
| 27 |
headerValue string |
| 28 |
expected time.Duration |
| 29 |
}{ |
| 30 |
"AsDate": { |
| 31 |
headerValue: currentTime.In(time.UTC).Add(5 * time.Hour).Format(http.TimeFormat), |
| 32 |
expected: time.Duration(5) * time.Hour, |
| 33 |
}, |
| 34 |
"AsSeconds": { |
| 35 |
headerValue: "12345", |
| 36 |
expected: time.Duration(12345) * time.Second, |
| 37 |
}, |
| 38 |
"Missing": { |
| 39 |
headerValue: "", |
| 40 |
expected: defaultBackoff, |
| 41 |
}, |
| 42 |
"Bad": { |
| 43 |
headerValue: "invalid", |
| 44 |
expected: defaultBackoff, |
| 45 |
}, |
| 46 |
} { |
| 47 |
t.Run(name, func(t *testing.T) { |
| 48 |
parsed := Parse(tt.headerValue, defaultBackoff) |
| 49 |
assert.Equal(t, tt.expected, parsed) |
| 50 |
}) |
| 51 |
} |
| 52 |
} |