-
Notifications
You must be signed in to change notification settings - Fork 7
/
record_test.go
295 lines (248 loc) · 10.3 KB
/
record_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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package adstxt
import (
"encoding/json"
"testing"
)
// TestParseDataRecordWith3Fields test parsing Ads.txt data record line with 3 fields
func TestParseDataRecordWith3Fields(t *testing.T) {
line := "greenadexchange.com,XF7342, DIRECT"
expected := DataRecord{
AdverterDomain: "greenadexchange.com",
PublisherAccountID: "XF7342",
AccountType: "DIRECT",
}
r, w := parseDataRecord(line)
if w != nil {
t.Errorf("Expected no parse warning when parsing [%s] [%v]", line, w)
}
if r.AdverterDomain != expected.AdverterDomain {
t.Errorf("Expected Adverter Domain for [%s] to be [%s] but received [%s]", line, expected.AdverterDomain, r.AdverterDomain)
}
if r.PublisherAccountID != expected.PublisherAccountID {
t.Errorf("Expected Publisher Account Id for [%s] to be [%s] but received [%s]", line, expected.PublisherAccountID, r.PublisherAccountID)
}
if r.AccountType != expected.AccountType {
t.Errorf("Expected Account Type for [%s] to be [%s] but received [%s]", line, expected.AccountType, r.AccountType)
}
}
// TestParseDateRecordWith4Fields test parsing Ads.txt data record line with 4 fields
func TestParseDateRecordWith4Fields(t *testing.T) {
line := "greenadexchange.com, XF7342, DIRECT, 5jyxf8k54"
expected := DataRecord{
AdverterDomain: "greenadexchange.com",
PublisherAccountID: "XF7342",
AccountType: "DIRECT",
CertAuthorityID: "5jyxf8k54",
}
r, w := parseDataRecord(line)
if w != nil {
t.Errorf("Expected no parse warnings when parsing [%s] [%v]", line, w)
}
if r.AdverterDomain != expected.AdverterDomain {
t.Errorf("Expected Adverter Domain for [%s] to be [%s] but received [%s]", line, expected.AdverterDomain, r.AdverterDomain)
}
if r.PublisherAccountID != expected.PublisherAccountID {
t.Errorf("Expected Publisher Account Id for [%s] to be [%s] but received [%s]", line, expected.PublisherAccountID, r.PublisherAccountID)
}
if r.AccountType != expected.AccountType {
t.Errorf("Expected Account Type for [%s] to be [%s] but received [%s]", line, expected.AccountType, r.AccountType)
}
if r.CertAuthorityID != expected.CertAuthorityID {
t.Errorf("Expected Account Type for [%s] to be [%s] but received [%s]", line, expected.CertAuthorityID, r.CertAuthorityID)
}
}
// TestParseDataRecordWithWrongNumberOfFields test parsing Ads.txt data record line with wrong number of fields
func TestParseDataRecordWithWrongNumberOfFields(t *testing.T) {
line := "greenadexchange.com, XF7342"
r, e := parseDataRecord(line)
if e == nil {
t.Errorf("Expected error when parsing [%s] [%v]", line, r)
}
line = "greenadexchange.com, XF7342, DIRECT, 5jyxf8k54, not-valid"
r, e = parseDataRecord(line)
if e == nil {
t.Errorf("Expected error when parsing [%s] [%v]", line, r)
}
}
// TestParseDataRecordAccountType test parsing DataRecord account type field
func TestParseDataRecordAccountType(t *testing.T) {
// invalid accont type
line := "greenadexchange.com, XF7342, unknown"
r, w := parseDataRecord(line)
if w == nil {
t.Errorf("Expected parse warnings when parsing [%s] [%v]", line, w)
}
// case sensitive account type
line = "greenadexchange.com, XF7342, direct"
r, w = parseDataRecord(line)
if w != nil {
t.Errorf("Expected no parse warnings when parsing [%s] [%v]", line, w)
}
if r.AccountType != accountTypeReseller && r.AccountType != accountTypeDirect {
t.Errorf("Expected account type [%s] to be a valid account type", r.AccountType)
}
}
// TestParseDataRecordWithComment test parsing of DataRecord line with comment
func TestParseDataRecordWithComment(t *testing.T) {
line := removeComment("advertising.com,17429, DIRECT, #video, US")
expected := DataRecord{
AdverterDomain: "advertising.com",
PublisherAccountID: "17429",
AccountType: "DIRECT",
}
r, w := parseDataRecord(line)
if w != nil {
t.Errorf("Expected no errors when parsing [%s] [%v]", line, w)
}
if r.AdverterDomain != expected.AdverterDomain {
t.Errorf("Expected Adverter Domain for [%s] to be [%s] but received [%s]", line, expected.AdverterDomain, r.AdverterDomain)
}
if r.PublisherAccountID != expected.PublisherAccountID {
t.Errorf("Expected Publisher Account Id for [%s] to be [%s] but received [%s]", line, expected.PublisherAccountID, r.PublisherAccountID)
}
if r.AccountType != expected.AccountType {
t.Errorf("Expected Account Type for [%s] to be [%s] but received [%s]", line, expected.AccountType, r.AccountType)
}
}
// TestParseDataRecordWithInvalidAdvertisingSystemName test parsing Ads.txt data record line with in valid domain name of the advertising system
func TestParseDataRecordWithInvalidAdvertisingSystemName(t *testing.T) {
line := "greenadexchange,XF7342,DIRECT"
_, w := parseDataRecord(line)
if w == nil {
t.Errorf("Expected error when parsing [%s] [%v]", line, w)
}
line = "greenadexchange.com, 185, RESELLER"
r, err := parseDataRecord(line)
if err != nil {
t.Errorf("Expected no error when parsing [%s]", line)
}
expected := DataRecord{
AdverterDomain: "greenadexchange.com",
PublisherAccountID: "185",
AccountType: "RESELLER",
}
// compare DataRecords
if r.AdverterDomain != expected.AdverterDomain || r.PublisherAccountID != expected.PublisherAccountID || r.AccountType != expected.AccountType {
t.Errorf("Failed to compare parsed DataRecord [%v] to expected DataRecord [%v]", r, expected)
}
}
// TestParseDataRecordWithInvalidCertName test parsing Ads.txt data record line with in valid certification authority
func TestParseDataRecordWithInvalidCertName(t *testing.T) {
line := "greenadexchange.com,185,DIRECT,<invalid>"
r, w := parseDataRecord(line)
if w == nil {
t.Errorf("Expected error when parsing [%s] [%v]", line, w)
}
expected := DataRecord{
AdverterDomain: "greenadexchange.com",
PublisherAccountID: "185",
AccountType: "DIRECT",
CertAuthorityID: "<invalid>",
}
// compare DataRecords
if r.AdverterDomain != expected.AdverterDomain || r.PublisherAccountID != expected.PublisherAccountID || r.AccountType != expected.AccountType {
t.Errorf("Failed to compare parsed DataRecord [%v] to expected DataRecord [%v]", r, expected)
}
}
// TestDataRecordJsonEncode test encoding DataRecord to json
func TestDataRecordJsonEncode(t *testing.T) {
line := "greenadexchange.com, XF7342, DIRECT, 5jyxf8k54"
r, w := parseDataRecord(line)
if w != nil {
t.Errorf("Expected no errors when parsing [%s] [%v]", line, w)
}
j, _ := json.Marshal(r)
if string(j) != "{\"adverterdomain\":\"greenadexchange.com\",\"publisheraccountid\":\"XF7342\",\"accountype\":\"DIRECT\",\"certauthorityid\":\"5jyxf8k54\"}" {
t.Errorf("Json encoded DataRecord is different than expected [%s]", string(j))
}
// test Json encode without optional value
line = "greenadexchange.com, XF7342, DIRECT"
r, w = parseDataRecord(line)
if w != nil {
t.Errorf("Expected no errors when parsing [%s] [%v]", line, w)
}
j, _ = json.Marshal(r)
if string(j) != "{\"adverterdomain\":\"greenadexchange.com\",\"publisheraccountid\":\"XF7342\",\"accountype\":\"DIRECT\"}" {
t.Errorf("Json encoded DataRecord is different than expected [%s]", string(j))
}
}
// TestDataRecordJsonDecode test decode DataRecord from json
func TestDataRecordJsonDecode(t *testing.T) {
j := []byte("{\"adverterdomain\":\"greenadexchange.com\",\"publisheraccountid\":\"XF7342\",\"accountype\":\"DIRECT\",\"certauthorityid\":\"5jyxf8k54\"}")
var r DataRecord
err := json.Unmarshal(j, &r)
if err != nil {
t.Error(err)
}
expected := DataRecord{
AdverterDomain: "greenadexchange.com",
PublisherAccountID: "XF7342",
AccountType: "DIRECT",
CertAuthorityID: "5jyxf8k54",
}
if r.AdverterDomain != expected.AdverterDomain {
t.Errorf("Expected Adverter Domain to be [%s] but received [%s]", expected.AdverterDomain, r.AdverterDomain)
}
if r.PublisherAccountID != expected.PublisherAccountID {
t.Errorf("Expected Publisher Account Id to be [%s] but received [%s]", expected.PublisherAccountID, r.PublisherAccountID)
}
if r.AccountType != expected.AccountType {
t.Errorf("Expected Account Type to be [%s] but received [%s]", expected.AccountType, r.AccountType)
}
if r.CertAuthorityID != expected.CertAuthorityID {
t.Errorf("Expected Account Type to be [%s] but received [%s]", expected.CertAuthorityID, r.CertAuthorityID)
}
}
// TestParseSubDomainVariable test parsing subdomain Variable type
func TestParseSubDomainVariable(t *testing.T) {
subdomain := "subdomain=dev.example.com"
v, w := parseVariable(subdomain)
if w != nil {
t.Errorf("Expected no errors when parsing [%s] [%v]", subdomain, w)
}
if v.Type != varTypeSubdomain {
t.Errorf("Expected variable type for [%s] to be [%s] but received [%s]", subdomain, varTypeSubdomain, v.Type)
}
if v.Value != "dev.example.com" {
t.Errorf("Expected variable value for [%s] to be [dev.example.com] but received [%s]", subdomain, v.Value)
}
}
// TestParseContactVariable test parsing contact Variable type
func TestParseContactVariable(t *testing.T) {
contact := "contact=test@example.com"
v, w := parseVariable(contact)
if w != nil {
t.Errorf("Expected no errors when parsing [%s] [%v]", contact, w)
}
if v.Type != varTypeContact {
t.Errorf("Expected variable type for [%s] to be [%s] but received [%s]", contact, varTypeContact, v.Type)
}
if v.Value != "test@example.com" {
t.Errorf("Expected variable value for [%s] to be [test@example.com] but received [%s]", contact, v.Value)
}
}
// TestParseNotSupportedVariable test parsing not supported Variable type
func TestParseNotSupportedVariable(t *testing.T) {
notSupported := "notSupported=dev.example.com"
v, w := parseVariable(notSupported)
if w == nil {
t.Errorf("Expected parsing error when parsing [%s]", notSupported)
}
if v != nil {
t.Errorf("Expected parsing error when parsing [%s] and empty variable [%v]", notSupported, v)
}
if w.Message != "[notSupported] is not a valid Variable type" {
t.Errorf("Expected error type for [%s] to be [%s] but received [%v]", notSupported, "[notSupported] is not a valid Variable type", w)
}
}
// TestRemoveComment test creating new line with Ads.txt comment
func TestRemoveComment(t *testing.T) {
s := "advertising.com,17429, DIRECT, #video, US"
l := removeComment(s)
if l == s {
t.Errorf("Expected comment to be removed from line [%s]", s)
}
if l != "advertising.com,17429, DIRECT," {
t.Errorf("Expected parsed line text to be [%s] and not [%s]", "advertising.com,17429, DIRECT, ", l)
}
}