File: retry_after_test.go

package info (click to toggle)
golang-github-getsentry-sentry-go 0.29.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,404 kB
  • sloc: makefile: 75; sh: 21
file content (58 lines) | stat: -rw-r--r-- 1,097 bytes parent folder | download | duplicates (2)
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
package ratelimit

import (
	"testing"
	"time"
)

func TestParseRetryAfter(t *testing.T) {
	defaultDeadline := Deadline(now.Add(defaultRetryAfter))
	tests := map[string]struct {
		input   string
		want    Deadline
		wantErr bool // if true, want is set to defaultDeadline
	}{
		// Invalid input
		"Empty": {
			input:   "",
			wantErr: true,
		},
		"BadString": {
			input:   "x",
			wantErr: true,
		},
		"Negative": {
			input:   "-1",
			wantErr: true,
		},
		"Float": {
			input:   "5.0",
			wantErr: true,
		},
		// Valid input
		"Integer": {
			input: "1337",
			want:  Deadline(now.Add(1337 * time.Second)),
		},
		"Date": {
			input: "Fri, 08 Mar 2019 11:17:09 GMT",
			want:  Deadline(time.Date(2019, 3, 8, 11, 17, 9, 0, time.UTC)),
		},
	}
	for name, tt := range tests {
		tt := tt
		t.Run(name, func(t *testing.T) {
			got, err := parseRetryAfter(tt.input, now)
			want := tt.want
			if tt.wantErr {
				want = defaultDeadline
				if err == nil {
					t.Errorf("got err = nil, want non-nil")
				}
			}
			if !got.Equal(want) {
				t.Errorf("got %v, want %v", got, want)
			}
		})
	}
}