-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathrfc822_test.go
219 lines (200 loc) · 7.01 KB
/
rfc822_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
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
package clock
import (
"encoding/json"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
)
type testStruct struct {
Time RFC822Time `json:"ts" yaml:"ts"`
}
func TestRFC822New(t *testing.T) {
stdTime, err := Parse(RFC3339, "2019-08-29T11:20:07.123456+03:00")
assert.NoError(t, err)
rfc822TimeFromTime := NewRFC822Time(stdTime)
rfc822TimeFromUnix := NewRFC822TimeFromUnix(stdTime.Unix())
assert.True(t, rfc822TimeFromTime.Equal(rfc822TimeFromUnix.Time),
"want=%s, got=%s", rfc822TimeFromTime.Time, rfc822TimeFromUnix.Time)
// Parsing from numerical offset to abbreviated offset is not always reliable. In this
// context Go will fallback to the known numerical offset.
assert.Equal(t, "Thu, 29 Aug 2019 11:20:07 +0300", rfc822TimeFromTime.String())
assert.Equal(t, "Thu, 29 Aug 2019 08:20:07 UTC", rfc822TimeFromUnix.String())
}
// NewRFC822Time truncates to second precision.
func TestRFC822SecondPrecision(t *testing.T) {
stdTime1, err := Parse(RFC3339, "2019-08-29T11:20:07.111111+03:00")
assert.NoError(t, err)
stdTime2, err := Parse(RFC3339, "2019-08-29T11:20:07.999999+03:00")
assert.NoError(t, err)
assert.False(t, stdTime1.Equal(stdTime2))
rfc822Time1 := NewRFC822Time(stdTime1)
rfc822Time2 := NewRFC822Time(stdTime2)
assert.True(t, rfc822Time1.Equal(rfc822Time2.Time),
"want=%s, got=%s", rfc822Time1.Time, rfc822Time2.Time)
}
func TestRFC822YAMLMarshaler(t *testing.T) {
rfcTime := NewRFC822Time(Date(1955, November, 12, 6, 38, 0, 0, UTC))
ts := testStruct{Time: rfcTime}
encoded, err := yaml.Marshal(ts)
assert.NoError(t, err)
assert.Equal(t, "ts: Sat, 12 Nov 1955 06:38:00 UTC\n", string(encoded))
var decoded testStruct
err = yaml.Unmarshal(encoded, &decoded)
assert.NoError(t, err)
assert.Equal(t, rfcTime, decoded.Time)
}
// Marshaled representation is truncated down to second precision.
func TestRFC822Marshaling(t *testing.T) {
stdTime, err := Parse(RFC3339Nano, "2019-08-29T11:20:07.123456789+03:30")
assert.NoError(t, err)
ts := testStruct{Time: NewRFC822Time(stdTime)}
encoded, err := json.Marshal(&ts)
assert.NoError(t, err)
assert.Equal(t, `{"ts":"Thu, 29 Aug 2019 11:20:07 +0330"}`, string(encoded))
}
func TestRFC822Unmarshaling(t *testing.T) {
for i, tc := range []struct {
inRFC822 string
outRFC3339 string
outRFC822 string
}{{
inRFC822: "Thu, 29 Aug 2019 11:20:07 GMT",
outRFC3339: "2019-08-29T11:20:07Z",
outRFC822: "Thu, 29 Aug 2019 11:20:07 GMT",
}, {
inRFC822: "Thu, 29 Aug 2019 11:20:07 MSK",
// Extrapolating the numerical offset from an abbreviated offset is unreliable. In
// this test case the RFC3339 will have the incorrect result due to limitation in
// Go's time parser.
outRFC3339: "2019-08-29T11:20:07Z",
outRFC822: "Thu, 29 Aug 2019 11:20:07 MSK",
}, {
inRFC822: "Thu, 29 Aug 2019 11:20:07 -0000",
outRFC3339: "2019-08-29T11:20:07Z",
outRFC822: "Thu, 29 Aug 2019 11:20:07 -0000",
}, {
inRFC822: "Thu, 29 Aug 2019 11:20:07 +0000",
outRFC3339: "2019-08-29T11:20:07Z",
outRFC822: "Thu, 29 Aug 2019 11:20:07 +0000",
}, {
inRFC822: "Thu, 29 Aug 2019 11:20:07 +0300",
outRFC3339: "2019-08-29T11:20:07+03:00",
outRFC822: "Thu, 29 Aug 2019 11:20:07 +0300",
}, {
inRFC822: "Thu, 29 Aug 2019 11:20:07 +0330",
outRFC3339: "2019-08-29T11:20:07+03:30",
outRFC822: "Thu, 29 Aug 2019 11:20:07 +0330",
}, {
inRFC822: "Sun, 01 Sep 2019 11:20:07 +0300",
outRFC3339: "2019-09-01T11:20:07+03:00",
outRFC822: "Sun, 01 Sep 2019 11:20:07 +0300",
}, {
inRFC822: "Sun, 1 Sep 2019 11:20:07 +0300",
outRFC3339: "2019-09-01T11:20:07+03:00",
outRFC822: "Sun, 01 Sep 2019 11:20:07 +0300",
}, {
inRFC822: "Sun, 1 Sep 2019 11:20:07 +0300",
outRFC3339: "2019-09-01T11:20:07+03:00",
outRFC822: "Sun, 01 Sep 2019 11:20:07 +0300",
}, {
inRFC822: "Sun, 1 Sep 2019 11:20:07 UTC",
outRFC3339: "2019-09-01T11:20:07Z",
outRFC822: "Sun, 01 Sep 2019 11:20:07 UTC",
}, {
inRFC822: "Sun, 1 Sep 2019 11:20:07 UTC",
outRFC3339: "2019-09-01T11:20:07Z",
outRFC822: "Sun, 01 Sep 2019 11:20:07 UTC",
}, {
inRFC822: "Sun, 1 Sep 2019 11:20:07 GMT",
outRFC3339: "2019-09-01T11:20:07Z",
outRFC822: "Sun, 01 Sep 2019 11:20:07 GMT",
}, {
inRFC822: "Fri, 21 Nov 1997 09:55:06 -0600 (MDT)",
outRFC3339: "1997-11-21T09:55:06-06:00",
outRFC822: "Fri, 21 Nov 1997 09:55:06 MDT",
}} {
t.Run(tc.inRFC822, func(t *testing.T) {
tcDesc := fmt.Sprintf("Test case #%d: %v", i, tc)
var ts testStruct
inEncoded := []byte(fmt.Sprintf(`{"ts":%q}`, tc.inRFC822))
err := json.Unmarshal(inEncoded, &ts)
assert.NoError(t, err, tcDesc)
assert.Equal(t, tc.outRFC3339, ts.Time.Format(RFC3339), tcDesc)
actualEncoded, err := json.Marshal(&ts)
assert.NoError(t, err, tcDesc)
outEncoded := fmt.Sprintf(`{"ts":%q}`, tc.outRFC822)
assert.Equal(t, outEncoded, string(actualEncoded), tcDesc)
})
}
}
func TestRFC822UnmarshalingError(t *testing.T) {
for _, tc := range []struct {
inEncoded string
outError string
}{{
inEncoded: `{"ts": "Thu, 29 Aug 2019 11:20:07"}`,
outError: `parsing time "Thu, 29 Aug 2019 11:20:07" as "January 2 2006 15:04 -0700 (MST)": cannot parse "Thu, 29 Aug 2019 11:20:07" as "January"`,
}, {
inEncoded: `{"ts": "foo"}`,
outError: `parsing time "foo" as "January 2 2006 15:04 -0700 (MST)": cannot parse "foo" as "January"`,
}, {
inEncoded: `{"ts": 42}`,
outError: "invalid syntax",
}} {
t.Run(tc.inEncoded, func(t *testing.T) {
var ts testStruct
err := json.Unmarshal([]byte(tc.inEncoded), &ts)
assert.EqualError(t, err, tc.outError)
})
}
}
func TestParseRFC822Time(t *testing.T) {
for _, tt := range []struct {
rfc822Time string
}{
{"Thu, 3 Jun 2021 12:01:05 MST"},
{"Thu, 3 Jun 2021 12:01:05 -0700"},
{"Thu, 3 Jun 2021 12:01:05 -0700 (MST)"},
{"2 Jun 2021 17:06:41 GMT"},
{"2 Jun 2021 17:06:41 -0700"},
{"2 Jun 2021 17:06:41 -0700 (MST)"},
{"Mon, 30 August 2021 11:05:00 -0400"},
{"Thu, 3 June 2021 12:01:05 MST"},
{"Thu, 3 June 2021 12:01:05 -0700"},
{"Thu, 3 June 2021 12:01:05 -0700 (MST)"},
{"2 June 2021 17:06:41 GMT"},
{"2 June 2021 17:06:41 -0700"},
{"2 June 2021 17:06:41 -0700 (MST)"},
{"Wed, Nov 03 2021 17:48:06 CST"},
{"Wed, November 03 2021 17:48:06 CST"},
// Timestamps without seconds.
{"Sun, 31 Oct 2021 12:10 -0500"},
{"Thu, 3 Jun 2021 12:01 MST"},
{"Thu, 3 Jun 2021 12:01 -0700"},
{"Thu, 3 Jun 2021 12:01 -0700 (MST)"},
{"2 Jun 2021 17:06 GMT"},
{"2 Jun 2021 17:06 -0700"},
{"2 Jun 2021 17:06 -0700 (MST)"},
{"Mon, 30 August 2021 11:05 -0400"},
{"Thu, 3 June 2021 12:01 MST"},
{"Thu, 3 June 2021 12:01 -0700"},
{"Thu, 3 June 2021 12:01 -0700 (MST)"},
{"2 June 2021 17:06 GMT"},
{"2 June 2021 17:06 -0700"},
{"2 June 2021 17:06 -0700 (MST)"},
{"Wed, Nov 03 2021 17:48 CST"},
{"Wed, November 03 2021 17:48 CST"},
} {
t.Run(tt.rfc822Time, func(t *testing.T) {
_, err := ParseRFC822Time(tt.rfc822Time)
assert.NoError(t, err)
})
}
}
func TestStringWithOffset(t *testing.T) {
now := time.Now().UTC()
r := NewRFC822Time(now)
assert.Equal(t, now.Format(time.RFC1123Z), r.StringWithOffset())
}