-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexnow_test.go
262 lines (254 loc) · 6.74 KB
/
indexnow_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
// package indexnow implements the IndexNow submission protocol as described in
// https://www.indexnow.org/documentation
package indexnow
import (
"io/ioutil"
"net/http"
"strings"
"testing"
)
type roundTripFunc func(r *http.Request) (*http.Response, error)
func (rtp roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) {
return rtp(r)
}
func TestNew(t *testing.T) {
type args struct {
searchEngineHost string
own *Ownership
rt http.RoundTripper
}
tests := []struct {
name string
args args
want *IndexNow
}{
{
"initializes the struct correctly",
args{
"example.com",
&Ownership{
Key: "key",
KeyLocation: "keyLocation",
},
roundTripFunc(func(r *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(strings.NewReader("")),
}, nil
}),
},
&IndexNow{
searchEngineHost: "example.com",
key: "key",
keyLocation: "keyLocation",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := New(tt.args.searchEngineHost, tt.args.own, tt.args.rt)
if got.searchEngineHost != tt.want.searchEngineHost {
t.Errorf("New().searchHost = %v, want %v", got.searchEngineHost, tt.want.searchEngineHost)
}
if got.key != tt.want.key {
t.Errorf("New().key = %v, want %v", got.key, tt.want.key)
}
if got.keyLocation != tt.want.keyLocation {
t.Errorf("New().keyLocation = %v, want %v", got.keyLocation, tt.want.keyLocation)
}
})
}
}
func TestGetSubmitUrl(t *testing.T) {
type args struct {
searchEngineHost string
}
tests := []struct {
name string
args args
want string
}{
{
"generates the correct index url",
args{
"www.example.com",
},
"https://www.example.com/indexnow",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := GetSubmitUrl(tt.args.searchEngineHost); got.String() != tt.want {
t.Errorf("GetSubmitUrl() = %v, want %v", got.String(), tt.want)
}
})
}
}
func TestGetSingleSubmitUrl(t *testing.T) {
type args struct {
searchEngineHost string
key string
keyLocation string
urlToAdd string
}
tests := []struct {
name string
args args
want string
}{
{
"generates correct submit url with key",
args{
"www.example.com",
"aabbcceeff",
"",
"https://www.example.org/",
},
"https://www.example.com/indexnow?key=aabbcceeff&url=https%3A%2F%2Fwww.example.org%2F",
},
{
"generates correct submit url with keyLocation",
args{
"www.example.com",
"",
"https://www.example.org/key-location/aabbcceeff.txt",
"https://www.example.org/",
},
"https://www.example.com/indexnow?key=&keyLocation=https%3A%2F%2Fwww.example.org%2Fkey-location%2Faabbcceeff.txt&url=https%3A%2F%2Fwww.example.org%2F",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := GetSingleSubmitUrl(tt.args.searchEngineHost, tt.args.key, tt.args.keyLocation, tt.args.urlToAdd); got != tt.want {
t.Errorf("GetSingleSubmitUrl() = %v, want %v", got, tt.want)
}
})
}
}
func TestIndexNow_SubmitSingleURL(t *testing.T) {
type fields struct {
searchEngineHost string
own *Ownership
rt http.RoundTripper
}
type args struct {
urlToAdd string
}
tests := []struct {
name string
fields fields
args args
wantStatusCode int
wantErr bool
}{
{
"submitting single url works as expected",
fields{
"www.example.com",
&Ownership{Key: "key"},
roundTripFunc(func(r *http.Request) (*http.Response, error) {
wantSubmitUrl := "https://www.example.com/indexnow?key=key&url=http%3A%2F%2Fwww.example.org"
if r.URL.String() != wantSubmitUrl {
t.Errorf("IndexNow.SubmitSingleURL() url = %v, want %v", r.URL.String(), wantSubmitUrl)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(strings.NewReader("success")),
}, nil
}),
},
args{"http://www.example.org"},
http.StatusOK,
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
in := New(
tt.fields.searchEngineHost,
tt.fields.own,
tt.fields.rt,
)
got, err := in.SubmitSingleURL(tt.args.urlToAdd)
if (err != nil) != tt.wantErr {
t.Errorf("IndexNow.SubmitSingleURL() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got.StatusCode != tt.wantStatusCode {
t.Errorf("IndexNow.SubmitSingleURL() = %v, want %v", got.StatusCode, tt.wantStatusCode)
}
})
}
}
func TestIndexNow_SubmitBatchURLs(t *testing.T) {
type fields struct {
searchEngineHost string
own *Ownership
rt http.RoundTripper
}
type args struct {
host string
urlsToAdd []string
}
tests := []struct {
name string
fields fields
args args
wantStatusCode int
wantErr bool
}{
{
"submits batch of urls",
fields{
"www.example.com",
&Ownership{KeyLocation: "http://example.org/keyLocation.txt"},
roundTripFunc(func(r *http.Request) (*http.Response, error) {
wantSubmitUrl := "https://www.example.com/indexnow"
wantContentType := "application/json; charset=utf-8"
wantBody := `{"host":"example.org","key":"","keyLocation":"http://example.org/keyLocation.txt","urlList":["https://example.org","http://example.org","http://example.org/test"]}`
if r.URL.String() != wantSubmitUrl {
t.Errorf("IndexNow.SubmitSingleURL() url = %v, want %v", r.URL.String(), wantSubmitUrl)
}
if r.Header.Get("content-type") != wantContentType {
t.Errorf("IndexNow.SubmitSingleURL() contentType = %v, want %v", r.Header.Get("content-type"), wantContentType)
}
body, _ := ioutil.ReadAll(r.Body)
if string(body) != wantBody {
t.Errorf("IndexNow.SubmitSingleURL() body = %v, want %v", string(body), wantBody)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(strings.NewReader("success")),
}, nil
}),
},
args{
"example.org",
[]string{
"https://example.org",
"http://example.org",
"http://example.org/test",
},
},
http.StatusOK,
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
in := New(
tt.fields.searchEngineHost,
tt.fields.own,
tt.fields.rt,
)
got, err := in.SubmitBatchURLs(tt.args.host, tt.args.urlsToAdd)
if (err != nil) != tt.wantErr {
t.Errorf("IndexNow.SubmitBatchURLs() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got.StatusCode != tt.wantStatusCode {
t.Errorf("IndexNow.SubmitBatchURLs() = %v, want %v", got.StatusCode, tt.wantStatusCode)
}
})
}
}