-
Notifications
You must be signed in to change notification settings - Fork 7
/
adstxt_test.go
174 lines (135 loc) · 4.68 KB
/
adstxt_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
package adstxt
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
// TestGetMultiple testing fetch and parse multile Ads.txt files from remote hosts
func TestGetMultiple(t *testing.T) {
h := func(req *Request, res *Response, err error) {
if res.Request != req {
t.Errorf("Expected Ads.txt response to include pointer to the request")
}
if len(res.DataRecords) != 1 {
t.Errorf("Expected single DataRecord but found [%d]", len(res.DataRecords))
}
if len(res.Variables) != 1 {
t.Errorf("Expected single Variable record but found [%d]", len(res.Variables))
}
}
// expected response
const expected = "greenadexchange.com,XF7342,DIRECT\nsubdomain=test.com"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, expected)
w.Header().Set("Content-Type", "text/plain")
}))
defer ts.Close()
// request mock
req, _ := NewRequest(ts.URL)
requests := make([]*Request, 1)
requests[0] = req
GetMultiple(requests, HandlerFunc(h))
}
// TestGet testing fetch and parse Ads.txt file from remote host
func TestGet(t *testing.T) {
// expected response
const expected = "greenadexchange.com,XF7342,DIRECT\nsubdomain=test.com"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, expected)
w.Header().Set("Content-Type", "text/plain")
}))
defer ts.Close()
// request mock
req, _ := NewRequest(ts.URL)
res, err := Get(req)
if err != nil {
t.Error(err)
}
if res.Request != req {
t.Errorf("Expected Ads.txt response to include pointer to the request")
}
if len(res.DataRecords) != 1 {
t.Errorf("Expected single DataRecord but found [%d]", len(res.DataRecords))
}
if len(res.Variables) != 1 {
t.Errorf("Expected single Variable record but found [%d]", len(res.Variables))
}
}
// TestParseBody test paring []byte array into []Line array
func TestParseBody(t *testing.T) {
body := []string{
"this is the first line",
"this is the second line",
"this is the third line",
}
b := []byte(strings.Join(body, "\n"))
res, err := ParseBody(b)
if err != nil {
t.Error(err)
}
if len(res.Body) != len(body) {
t.Errorf("Expected number of lines to be %d and not %d", len(body), len(res.Body))
}
for index, line := range res.Body {
if line != body[index] {
t.Errorf("Expected line #%d to be [%s] and not [%s]", index, body[index], line)
}
}
b = []byte(strings.Join(body, "\r\n"))
res, err = ParseBody(b)
if err != nil {
t.Error(err)
}
if len(res.Body) != len(body) {
t.Errorf("Expected number of lines to be %d and not %d", len(body), len(res.Body))
}
for index, line := range res.Body {
if line != body[index] {
t.Errorf("Expected line #%d to be [%s] and not [%s]", index, body[index], line)
}
}
}
// TestParseRecordsSuccess test parsing to valid Ads.txt file
func TestParseRecordsSuccess(t *testing.T) {
b := []byte("greenadexchange.com,XF7342, DIRECT\ngreenadexchange.com, XF7342, DIRECT, 5jyxf8k54\n#greenadexchange.com,XF7342,DIRECT\nsubdomain=dev.example.com")
res, err := ParseBody(b)
if err != nil {
t.Errorf("Expected no errors [%s]", err.Error())
}
if len(res.DataRecords) != 2 {
t.Errorf("Failed to parse Ads.txt records, expected number of records to be 2 and not [%d]", len(res.DataRecords))
}
if len(res.Variables) != 1 {
t.Errorf("Failed to parse Ads.txt variables, expected number of variables to be 1 and not [%d]", len(res.Variables))
}
if len(res.Warnings) > 0 {
t.Errorf("Expected no warning when parsing lines, but received [%d] warnings", len(res.Warnings))
}
}
// TestParseRecordsFailure test parsing to invalid Ads.txt file
func TestParseRecordsFailure(t *testing.T) {
b1 := []byte("greenadexchange.com,XF7342,\ngreenadexchange.com, XF7342, DIRECT, 5jyxf8k54\n#greenadexchange.com,XF7342,DIRECT\nsubdomain=dev.example.com")
res, err := ParseBody(b1)
if err != nil {
t.Error(err)
}
if len(res.Warnings) == 0 {
t.Error("Expected warnings when parsing Ads.txt with invalid Ads.txt lines")
}
if res.Warnings[0].Message != "Missing type of account/relationship (required)" {
t.Error("Expected warning message to indicate account type is missing")
}
b2 := []byte("###this is a comment\ngreenadexchange.com, XF7342, DIRECT, 5jyxf8k54\n#greenadexchange.com,XF7342,DIRECT\nsubdomains=dev.example.com")
res, err = ParseBody(b2)
if len(res.Warnings) == 0 {
t.Error("Expected warnings when parsing Ads.txt with invalid Variable type line")
}
if res.Warnings[0].Message != "[subdomains] is not a valid Variable type" {
t.Error("Expected warning message to indicate account type is missing")
}
if len(res.DataRecords) == 0 {
t.Error("Expected parsing to valid DataRecords line to success")
}
}