-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathformat_test.go
199 lines (186 loc) · 5.61 KB
/
format_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
package nntp_test
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/mrincompetent/nntp"
)
func TestHeaderFormat_ParseXoverLine(t *testing.T) {
testTimezone := time.FixedZone("", -int(5*time.Hour.Seconds()))
tests := []struct {
name string
line string
format *nntp.OverviewFormat
expectedHeader nntp.Header
}{
{
name: "successful",
line: `1 some subject some author Sun, 10 May 2020 00:32:22 +0000 <some-msg-id> 67755 519 Xref: news.some-newsserver.com some-alternative-group-1:123 some-alternative-group-2:456 some-alternative-group-3:789`,
format: nntp.NewOverviewFormat([]string{
"Subject:",
"From:",
"Date:",
"Message-ID:",
"References:",
":bytes",
":lines",
"Xref:full",
}),
expectedHeader: nntp.Header{
MessageNumber: 1,
Subject: "some subject",
Author: "some author",
Date: time.Date(2020, 5, 10, 0, 32, 22, 0, time.FixedZone("", 0)),
MessageID: "<some-msg-id>",
References: "",
Bytes: 67755,
Lines: 519,
Additional: map[string]string{
"Xref": "news.some-newsserver.com some-alternative-group-1:123 some-alternative-group-2:456 some-alternative-group-3:789",
},
},
},
{
name: "successful - rfc3977 - 1",
line: `3000234 some other subject "Test Author" <[email protected]> 6 Oct 1998 04:38:40 -0500 <some-other-msg-id> <[email protected]> 1234 17 Xref: news.some-newsserver.com some-alternative-group-1:123`,
format: nntp.NewOverviewFormat([]string{
"Subject:",
"From:",
"Date:",
"Message-ID:",
"References:",
":bytes",
":lines",
"Xref:full",
}),
expectedHeader: nntp.Header{
MessageNumber: 3000234,
Subject: "some other subject",
Author: "\"Test Author\" <[email protected]>",
Date: time.Date(1998, 10, 6, 4, 38, 40, 0, testTimezone),
MessageID: "<some-other-msg-id>",
References: "<[email protected]>",
Bytes: 1234,
Lines: 17,
Additional: map[string]string{
"Xref": "news.some-newsserver.com some-alternative-group-1:123",
},
},
},
{
name: "successful - rfc3977 - 2",
line: `0 some other subject "Test Author" <[email protected]> 6 Oct 1998 04:38:40 -0500 <some-other-msg-id> <[email protected]> 1234 17 Xref: news.some-newsserver.com some-alternative-group-1:123`,
format: nntp.NewOverviewFormat([]string{
"Subject:",
"From:",
"Date:",
"Message-ID:",
"References:",
":bytes",
":lines",
"Xref:full",
}),
expectedHeader: nntp.Header{
MessageNumber: 0,
Subject: "some other subject",
Author: "\"Test Author\" <[email protected]>",
Date: time.Date(1998, 10, 6, 4, 38, 40, 0, testTimezone),
MessageID: "<some-other-msg-id>",
References: "<[email protected]>",
Bytes: 1234,
Lines: 17,
Additional: map[string]string{
"Xref": "news.some-newsserver.com some-alternative-group-1:123",
},
},
},
{
name: "successful - rfc3977 - 3",
line: `3000235 Another test article <[email protected]> (Test Author) 6 Oct 1998 04:38:45 -0500 <some-other-msg-id> 4818 37 Distribution: fi`,
format: nntp.NewOverviewFormat([]string{
"Subject:",
"From:",
"Date:",
"Message-ID:",
"References:",
":bytes",
":lines",
"Xref:full",
"Distribution:full",
}),
expectedHeader: nntp.Header{
MessageNumber: 3000235,
Subject: "Another test article",
Author: "<[email protected]> (Test Author)",
Date: time.Date(1998, 10, 6, 4, 38, 45, 0, testTimezone),
MessageID: "<some-other-msg-id>",
References: "",
Bytes: 4818,
Lines: 37,
Additional: map[string]string{
"Distribution": "fi",
"Xref": "",
},
},
},
{
name: "successful - missing :lines & :bytes",
line: `1 some subject some author Sun, 10 May 2020 00:32:22 +0000 <some-msg-id> `,
format: nntp.NewOverviewFormat([]string{
"Subject:",
"From:",
"Date:",
"Message-ID:",
"References:",
":bytes",
":lines",
"Xref:full",
}),
expectedHeader: nntp.Header{
MessageNumber: 1,
Subject: "some subject",
Author: "some author",
Date: time.Date(2020, 5, 10, 0, 32, 22, 0, time.FixedZone("", 0)),
MessageID: "<some-msg-id>",
References: "",
Bytes: 0,
Lines: 0,
},
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
header, err := test.format.ParseXoverLine(test.line)
require.NoError(t, err, "Failed to parse header")
assert.Equal(t, test.expectedHeader, header)
})
}
}
func TestParseDate(t *testing.T) {
loc, err := time.LoadLocation("Europe/Berlin")
require.NoError(t, err, "Failed to get timezone")
expectedDate := time.Date(2020, 1, 1, 12, 34, 56, 0, loc)
dates := []string{
"1 Jan 2020 12:34:56 +0100",
"Wed, 01 Jan 2020 12:34:56 +0100",
"Wed, 01 Jan 2020 13:34:56 CEST",
"Wed, 01 Jan 2020 12:34:56 +0100 (CET)",
"Wed, 01 Jan 20 12:34:56 CET",
"01 Jan 20 12:34:56 CET",
"01 Jan 2020 12:34:56 CET",
}
for _, s := range dates {
s := s
t.Run(s, func(t *testing.T) {
gotDate, err := nntp.ParseDate(s)
require.NoError(t, err, "Failed to parse date")
t.Logf("Got date: %s", gotDate.Format(time.RFC3339))
t.Logf("Expected date: %s", expectedDate.Format(time.RFC3339))
if !gotDate.Equal(expectedDate) {
t.Error("Returned date does not match expected date")
}
})
}
}