| 1 |
package utils |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
"time" |
| 6 |
|
| 7 |
"github.com/stretchr/testify/require" |
| 8 |
) |
| 9 |
|
| 10 |
// refParseHTTPDate mirrors net/http.ParseTime: trim the whitespace textproto |
| 11 |
// would, then try the three allowed layouts in order. It is the behavioral |
| 12 |
// reference for ParseHTTPDate in the unit and fuzz tests. |
| 13 |
func refParseHTTPDate(s string) (time.Time, error) { |
| 14 |
start, end := 0, len(s) |
| 15 |
for start < end && isHTTPDateSpace(s[start]) { |
| 16 |
start++ |
| 17 |
} |
| 18 |
for start < end && isHTTPDateSpace(s[end-1]) { |
| 19 |
end-- |
| 20 |
} |
| 21 |
s = s[start:end] |
| 22 |
var t time.Time |
| 23 |
var err error |
| 24 |
for _, layout := range []string{httpDateLayout, time.RFC850, time.ANSIC} { |
| 25 |
t, err = time.Parse(layout, s) |
| 26 |
if err == nil { |
| 27 |
return t, nil |
| 28 |
} |
| 29 |
} |
| 30 |
return t, err |
| 31 |
} |
| 32 |
|
| 33 |
func httpDateSamples() []time.Time { |
| 34 |
return []time.Time{ |
| 35 |
{}, // zero time: Mon, 01 Jan 0001 |
| 36 |
time.Unix(0, 0), |
| 37 |
time.Unix(784111777, 0), // the RFC 9110 example date |
| 38 |
time.Date(2000, time.February, 29, 23, 59, 59, 0, time.UTC), // leap day |
| 39 |
time.Date(1900, time.February, 28, 0, 0, 0, 0, time.UTC), // century non-leap |
| 40 |
time.Date(9999, time.December, 31, 23, 59, 59, 0, time.UTC), // last 4-digit second |
| 41 |
time.Date(0, time.January, 1, 0, 0, 0, 0, time.UTC), // year 0000 |
| 42 |
time.Date(2026, time.July, 23, 12, 34, 56, 789, time.UTC), // sub-second truncated by format |
| 43 |
time.Date(2026, time.July, 23, 20, 0, 0, 0, time.FixedZone("PST", -8*3600)), // non-UTC input |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
func Test_AppendHTTPDate(t *testing.T) { |
| 48 |
t.Parallel() |
| 49 |
for _, tm := range httpDateSamples() { |
| 50 |
want := tm.UTC().Format(httpDateLayout) |
| 51 |
require.Equal(t, want, string(AppendHTTPDate(nil, tm))) |
| 52 |
require.Equal(t, want, FormatHTTPDate(tm)) |
| 53 |
require.Len(t, want, httpDateLen) |
| 54 |
} |
| 55 |
|
| 56 |
// Out-of-range years delegate to time.AppendFormat. |
| 57 |
for _, tm := range []time.Time{ |
| 58 |
time.Date(10000, time.January, 1, 0, 0, 0, 0, time.UTC), |
| 59 |
time.Date(-42, time.January, 1, 0, 0, 0, 0, time.UTC), |
| 60 |
} { |
| 61 |
want := tm.UTC().Format(httpDateLayout) |
| 62 |
require.Equal(t, want, string(AppendHTTPDate(nil, tm))) |
| 63 |
} |
| 64 |
|
| 65 |
// Appending must preserve existing dst content. |
| 66 |
got := AppendHTTPDate([]byte("Date: "), time.Unix(0, 0)) |
| 67 |
require.Equal(t, "Date: Thu, 01 Jan 1970 00:00:00 GMT", string(got)) |
| 68 |
} |
| 69 |
|
| 70 |
func Test_ParseHTTPDate(t *testing.T) { |
| 71 |
t.Parallel() |
| 72 |
|
| 73 |
// Canonical fast-path inputs round-trip through the formatter. |
| 74 |
for _, tm := range httpDateSamples() { |
| 75 |
s := FormatHTTPDate(tm) |
| 76 |
got, err := ParseHTTPDate(s) |
| 77 |
require.NoError(t, err, "input %q", s) |
| 78 |
require.True(t, got.Equal(tm.Truncate(time.Second)), "input %q: got %v", s, got) |
| 79 |
require.Same(t, time.UTC, got.Location(), "input %q", s) |
| 80 |
|
| 81 |
gotBytes, err := ParseHTTPDate([]byte(s)) |
| 82 |
require.NoError(t, err) |
| 83 |
require.True(t, gotBytes.Equal(got)) |
| 84 |
} |
| 85 |
|
| 86 |
// Every input, valid or not, must agree with the net/http.ParseTime |
| 87 |
// reference on outcome, instant, and location. |
| 88 |
inputs := []string{ |
| 89 |
"Sun, 06 Nov 1994 08:49:37 GMT", |
| 90 |
"Sunday, 06-Nov-94 08:49:37 GMT", // RFC 850 |
| 91 |
"Sun Nov 6 08:49:37 1994", // ANSI C asctime |
| 92 |
" Sun, 06 Nov 1994 08:49:37 GMT\r\n", // padded, per textproto trim |
| 93 |
"sun, 06 nov 1994 08:49:37 GMT", // lowercase names: stdlib folds case |
| 94 |
"Sun, 06 Nov 1994 08:49:37 UTC", // non-GMT zone literal |
| 95 |
"Mon, 06 Nov 1994 08:49:37 GMT", // wrong weekday for the date: accepted |
| 96 |
"Tue, 29 Feb 2000 12:00:00 GMT", // leap day |
| 97 |
"Tue, 29 Feb 1900 12:00:00 GMT", // century non-leap: rejected |
| 98 |
"Tue, 30 Feb 2020 12:00:00 GMT", |
| 99 |
"Tue, 00 Feb 2020 12:00:00 GMT", |
| 100 |
"Tue, 32 Jan 2020 12:00:00 GMT", |
| 101 |
"Tue, 15 Jan 2020 24:00:00 GMT", |
| 102 |
"Tue, 15 Jan 2020 12:60:00 GMT", |
| 103 |
"Tue, 15 Jan 2020 12:00:60 GMT", |
| 104 |
"Foo, 15 Jan 2020 12:00:00 GMT", |
| 105 |
"Tue, 15 Foo 2020 12:00:00 GMT", |
| 106 |
"Tue, 15 Jan 2020 12:00:00 GM", |
| 107 |
"Tue, 15 Jan 2020 12:00:00", |
| 108 |
"", |
| 109 |
"not a date", |
| 110 |
"Tue, 1x Jan 2020 12:00:00 GMT", |
| 111 |
"Tue, 15 Jan 2x20 12:00:00 GMT", |
| 112 |
} |
| 113 |
for _, in := range inputs { |
| 114 |
want, wantErr := refParseHTTPDate(in) |
| 115 |
got, err := ParseHTTPDate(in) |
| 116 |
if wantErr != nil { |
| 117 |
require.Error(t, err, "input %q", in) |
| 118 |
continue |
| 119 |
} |
| 120 |
require.NoError(t, err, "input %q", in) |
| 121 |
require.True(t, got.Equal(want), "input %q: got %v, want %v", in, got, want) |
| 122 |
require.Equal(t, want.Location().String(), got.Location().String(), "input %q", in) |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
func Benchmark_AppendHTTPDate(b *testing.B) { |
| 127 |
tm := time.Unix(784111777, 0) |
| 128 |
dst := make([]byte, 0, httpDateLen) |
| 129 |
b.Run("fiber", func(b *testing.B) { |
| 130 |
b.ReportAllocs() |
| 131 |
for b.Loop() { |
| 132 |
AppendHTTPDate(dst, tm) |
| 133 |
} |
| 134 |
}) |
| 135 |
b.Run("default", func(b *testing.B) { |
| 136 |
b.ReportAllocs() |
| 137 |
for b.Loop() { |
| 138 |
tm.UTC().AppendFormat(dst, httpDateLayout) |
| 139 |
} |
| 140 |
}) |
| 141 |
} |
| 142 |
|
| 143 |
func Benchmark_ParseHTTPDate(b *testing.B) { |
| 144 |
input := "Sun, 06 Nov 1994 08:49:37 GMT" |
| 145 |
b.Run("fiber", func(b *testing.B) { |
| 146 |
b.ReportAllocs() |
| 147 |
for b.Loop() { |
| 148 |
if _, err := ParseHTTPDate(input); err != nil { |
| 149 |
b.Fatal(err) |
| 150 |
} |
| 151 |
} |
| 152 |
}) |
| 153 |
b.Run("default", func(b *testing.B) { |
| 154 |
b.ReportAllocs() |
| 155 |
for b.Loop() { |
| 156 |
if _, err := time.Parse(httpDateLayout, input); err != nil { |
| 157 |
b.Fatal(err) |
| 158 |
} |
| 159 |
} |
| 160 |
}) |
| 161 |
} |
| 162 |
|
| 163 |
// Test_HTTPDate_DaySweep pins the formatter and parser to the time package for every day of the years 0..9999. |
| 164 |
func Test_HTTPDate_DaySweep(t *testing.T) { |
| 165 |
t.Parallel() |
| 166 |
require.Equal(t, int64(httpDateMinUnix), time.Date(0, time.January, 1, 0, 0, 0, 0, time.UTC).Unix()) |
| 167 |
require.Equal(t, int64(httpDateMaxUnix), time.Date(9999, time.December, 31, 23, 59, 59, 0, time.UTC).Unix()) |
| 168 |
|
| 169 |
var buf [httpDateLen]byte |
| 170 |
var want [httpDateLen]byte |
| 171 |
for day := int64(0); day <= (httpDateMaxUnix-httpDateMinUnix)/secondsPerDay; day++ { |
| 172 |
sod := (day * 7919) % secondsPerDay |
| 173 |
tm := time.Unix(httpDateMinUnix+day*secondsPerDay+sod, 0) |
| 174 |
got := AppendHTTPDate(buf[:0], tm) |
| 175 |
exp := tm.UTC().AppendFormat(want[:0], httpDateLayout) |
| 176 |
if string(got) != string(exp) { |
| 177 |
t.Fatalf("day %d: got %q, want %q", day, got, exp) |
| 178 |
} |
| 179 |
parsed, err := ParseHTTPDate(got) |
| 180 |
if err != nil { |
| 181 |
t.Fatalf("day %d: parse %q: %v", day, got, err) |
| 182 |
} |
| 183 |
if parsed != tm.UTC() { //nolint:revive // the exact Time value (not just the instant) is what is being pinned |
| 184 |
t.Fatalf("day %d: parsed %q to %v (%#v), want %v (%#v)", day, got, parsed, parsed, tm.UTC(), tm.UTC()) |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
// The instants just outside the four-digit-year range take the stdlib path. |
| 189 |
for _, sec := range []int64{httpDateMinUnix - 1, httpDateMaxUnix + 1} { |
| 190 |
tm := time.Unix(sec, 0) |
| 191 |
require.Equal(t, tm.UTC().Format(httpDateLayout), FormatHTTPDate(tm)) |
| 192 |
} |
| 193 |
} |