-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml_test.go
More file actions
455 lines (361 loc) · 13.7 KB
/
Copy pathxml_test.go
File metadata and controls
455 lines (361 loc) · 13.7 KB
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
package sitemap
// sitemap formatting with syntactic sugar. © Arthur Mingard 2022
import (
"fmt"
"strings"
"testing"
"time"
"github.com/antchfx/xmlquery"
"github.com/stretchr/testify/assert"
)
const (
testDefaultXML string = `<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></urlset>`
testDefaultSitemapIndexXML string = `<?xml version="1.0" encoding="UTF-8"?><sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></sitemapindex>`
testDefaultPrettyXML string = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\nXXXXX<url>\nXXXXXXXXXX<lastmod>2021-08-15</lastmod>\nXXXXX</url>\n</urlset>"
testFullNewsXML string = `<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"><url><lastmod>2021-08-15</lastmod><news:news><news:publication><news:name>SOMENAME</news:name><news:language>en</news:language></news:publication><news:publication_date>2021-08-15</news:publication_date><news:title>TITLE</news:title></news:news></url></urlset>`
testFullImageXML string = `<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"><url><loc>https://somedomain.com/article</loc><lastmod>2021-08-15</lastmod><image:image><image:loc>https://somedomain.com/image.jpg</image:loc></image:image></url></urlset>`
testFullVideoXML string = `<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1"><url><loc>https://somedomain.com/article</loc><lastmod>2021-08-15</lastmod><video:video><video:title>Some title</video:title><video:thumbnail_loc>https://somedomain.com/thumb.jpg</video:thumbnail_loc><video:content_loc>https://somedomain.com/video.mp4</video:content_loc><video:player_loc>https://somedomain.com/player</video:player_loc><video:duration>100</video:duration><video:expiration_date>2021-08-15</video:expiration_date><video:rating>2</video:rating><video:view_count>100</video:view_count><video:publication_date>2021-08-15</video:publication_date></video:video></url></urlset>`
)
var fixedTime = time.Date(2021, 8, 15, 14, 30, 45, 100, time.UTC)
func getDoc(str string) (*xmlquery.Node, error) {
return xmlquery.Parse(strings.NewReader(str))
}
func findOne(str, tags string) (*xmlquery.Node, error) {
doc, err := getDoc(str)
if err != nil {
return nil, err
}
tag := xmlquery.FindOne(doc, tags)
if tag != nil {
return tag, nil
}
return nil, fmt.Errorf(`Failed to find %s`, tags)
}
func findAll(str, tags string) ([]*xmlquery.Node, error) {
doc, err := getDoc(str)
if err != nil {
return nil, err
}
list := xmlquery.Find(doc, tags)
if list != nil {
return list, nil
}
return nil, fmt.Errorf(`Failed to find %s`, tags)
}
func TestDefaults(t *testing.T) {
xml := New()
out, _ := xml.OutputString()
assert.Equal(t, testDefaultXML, out, "Should output default empty XML")
}
func TestNewSitemapIndex(t *testing.T) {
xml := NewSitemapIndex()
out, _ := xml.OutputString()
assert.Equal(t, testDefaultSitemapIndexXML, out, "Should output default empty XML")
}
func TestNewSitemapIndexAdd(t *testing.T) {
xml := NewSitemapIndex()
loc := NewLoc()
xml.Add(loc)
assert.Equal(t, "sitemap", xml.parent.Locations[0].XMLName.Local, "Should have an XML node local of 'sitemap")
}
func TestPrettyOutput(t *testing.T) {
xml := New()
loc := NewLoc()
loc.SetLastModified(Date(fixedTime))
xml.Add(loc)
out, _ := xml.OutputPrettyString("", "XXXXX")
assert.Equal(t, testDefaultPrettyXML, out, "Should output prettified XML")
}
func TestXMLAddDefaultUrl(t *testing.T) {
xml := New()
loc := NewLoc()
xml.Add(loc)
out, _ := xml.OutputString()
tag, _ := findOne(out, "//urlset/url/loc")
assert.Nil(t, tag, "Should have urlset, nested url nodes but without location")
lastModTag, _ := findOne(out, "//urlset/url/lastmod")
assert.NotNil(t, lastModTag, "Should have default last modified")
}
func TestXMLAddEntryWithLocation(t *testing.T) {
xml := New()
loc := NewLoc()
loc.SetLocation("https://domain.com")
xml.Add(loc)
out, _ := xml.OutputString()
tag, _ := findOne(out, "//urlset/url/loc")
assert.NotNil(t, tag, "Should have urlset,nested url nodes, and a location field")
}
func TestDefaultType(t *testing.T) {
xml := New()
out, _ := xml.OutputString()
doc, _ := findOne(out, "//urlset/@xmlns")
assert.Equal(t, doc.InnerText(), XMLNSDefault.Value, "Should contain correct default type")
}
func TestSetNews(t *testing.T) {
xml := New()
loc := NewLoc()
news := NewNews()
loc.SetNews(news)
xml.Add(loc)
out, _ := xml.OutputString()
doc, _ := findOne(out, "//urlset/@xmlns:news")
assert.Equal(t, doc.InnerText(), XMLNSNews.Value, "Should contain correct XMLNS attr")
}
func TestSetNewsValues(t *testing.T) {
xml := New()
loc := NewLoc()
loc.SetLastModified(Date(fixedTime))
news := NewNews()
news.SetTitle("TITLE")
news.SetName("SOMENAME")
news.SetPublicationDate(Date(fixedTime))
news.SetLanguage(LanguageEN)
loc.SetNews(news)
xml.Add(loc)
out, _ := xml.OutputString()
assert.Equal(t, testFullNewsXML, out, "Should output news XML")
}
func TestAddImage(t *testing.T) {
xml := New()
loc := NewLoc()
image := NewImage()
loc.AddImage(image)
xml.Add(loc)
out, _ := xml.OutputString()
doc, _ := findOne(out, "//urlset/@xmlns:image")
assert.Equal(t, doc.InnerText(), XMLNSImage.Value, "Should contain correct XMLNS attr")
}
func TestAddImageValues(t *testing.T) {
xml := New()
loc := NewLoc()
loc.SetLocation("https://somedomain.com/article")
loc.SetLastModified(Date(fixedTime))
image := NewImage()
image.SetLocation("https://somedomain.com/image.jpg")
loc.AddImage(image)
xml.Add(loc)
out, _ := xml.OutputString()
assert.Equal(t, testFullImageXML, out, "Should output image XML")
}
func TestAddVideo(t *testing.T) {
xml := New()
loc := NewLoc()
video := NewVideo()
loc.AddVideo(video)
xml.Add(loc)
out, _ := xml.OutputString()
doc, _ := findOne(out, "//urlset/@xmlns:video")
assert.Equal(t, doc.InnerText(), XMLNSVideo.Value, "Should contain correct XMLNS attr")
}
func TestAddVideoValues(t *testing.T) {
xml := New()
loc := NewLoc()
loc.SetLocation("https://somedomain.com/article")
loc.SetLastModified(Date(fixedTime))
video := NewVideo()
video.SetTitle("Some title")
video.SetThumbnailLocation("https://somedomain.com/thumb.jpg")
video.SetContentLocation("https://somedomain.com/video.mp4")
video.SetPlayerLocation("https://somedomain.com/player")
video.SetDuration(100)
video.SetExpirationDate(Date(fixedTime))
video.SetRating(2)
video.SetViewCount(100)
video.SetPublicationDate(Date(fixedTime))
loc.AddVideo(video)
xml.Add(loc)
out, _ := xml.OutputString()
assert.Equal(t, testFullVideoXML, out, "Should output video XML")
}
func TestAddVideoDescriptionLimit(t *testing.T) {
shortString := "shortstring"
longString := "a"
for i := 0; i < MaxDescriptionLength; i++ {
longString += "a"
}
video := NewVideo()
video.SetDescription(shortString)
assert.Len(t, video.Description, len(shortString), "should accept short strings")
video.SetDescription(longString)
assert.Len(t, video.Description, MaxDescriptionLength, "should truncate long strings")
}
func TestAddVideoFamilyFriendly(t *testing.T) {
xml := New()
loc := NewLoc()
video := NewVideo()
video.IsFamilyFriendly()
loc.AddVideo(video)
xml.Add(loc)
out, _ := xml.OutputString()
tag, _ := findOne(out, "//urlset/url/video:video/video:family_friendly")
assert.Equal(t, string(Yes), tag.InnerText(), "Should have a family friendly flag of 'yes'")
video.NotFamilyFriendly()
out, _ = xml.OutputString()
tag, _ = findOne(out, "//urlset/url/video:video/video:family_friendly")
assert.Equal(t, string(No), tag.InnerText(), "Should have a family friendly flag of 'no'")
}
func TestAddVideoSubRequired(t *testing.T) {
xml := New()
loc := NewLoc()
video := NewVideo()
video.SubRequired()
loc.AddVideo(video)
xml.Add(loc)
out, _ := xml.OutputString()
tag, _ := findOne(out, "//urlset/url/video:video/video:requires_subscription")
assert.Equal(t, string(Yes), tag.InnerText(), "Should have a subscription required flag of 'yes'")
video.SubNotRequired()
out, _ = xml.OutputString()
tag, _ = findOne(out, "//urlset/url/video:video/video:requires_subscription")
assert.Equal(t, string(No), tag.InnerText(), "Should have a subscription required flag of 'no'")
}
func TestAddVideoLive(t *testing.T) {
xml := New()
loc := NewLoc()
video := NewVideo()
video.IsLive()
loc.AddVideo(video)
xml.Add(loc)
out, _ := xml.OutputString()
tag, _ := findOne(out, "//urlset/url/video:video/video:live")
assert.Equal(t, string(Yes), tag.InnerText(), "Should have a live flag of 'yes'")
video.NotLive()
out, _ = xml.OutputString()
tag, _ = findOne(out, "//urlset/url/video:video/video:live")
assert.Equal(t, string(No), tag.InnerText(), "Should have a live flag of 'no'")
}
func TestAddVideoAllowCountries(t *testing.T) {
xml := New()
loc := NewLoc()
video := NewVideo()
video.AllowCountries("GB US")
loc.AddVideo(video)
xml.Add(loc)
out, _ := xml.OutputString()
tag, _ := findOne(out, "//urlset/url/video:video/video:restriction")
assert.Equal(t, "GB US", tag.InnerText(), "Should set restricted counties value")
tag, _ = findOne(out, "//urlset/url/video:video/video:restriction/@relationship")
assert.Equal(t, Allow.Value, tag.InnerText(), "Should apply a relationship of 'allow'")
}
func TestAddVideoDenyCountries(t *testing.T) {
xml := New()
loc := NewLoc()
video := NewVideo()
video.DenyCountries("GB US")
loc.AddVideo(video)
xml.Add(loc)
out, _ := xml.OutputString()
tag, _ := findOne(out, "//urlset/url/video:video/video:restriction/@relationship")
assert.Equal(t, Deny.Value, tag.InnerText(), "Should apply a relationship of 'deny'")
}
func TestAddVideoAllowPlatforms(t *testing.T) {
xml := New()
loc := NewLoc()
video := NewVideo()
video.AllowPlatforms(Web)
loc.AddVideo(video)
xml.Add(loc)
out, _ := xml.OutputString()
firstPlatform, _ := findOne(out, "//urlset/url/video:video/video:platform")
assert.Equal(t, "web", firstPlatform.InnerText(), "Should set allow platform to 'web'")
attr, _ := findOne(out, "//urlset/url/video:video/video:platform/@relationship")
assert.Equal(t, Allow.Value, attr.InnerText(), "Should apply a relationship of 'allow'")
}
func TestAddVideoDenyPlatforms(t *testing.T) {
xml := New()
loc := NewLoc()
video := NewVideo()
video.DenyPlatforms(Web)
loc.AddVideo(video)
xml.Add(loc)
out, _ := xml.OutputString()
attr, _ := findOne(out, "//urlset/url/video:video/video:platform/@relationship")
assert.Equal(t, Deny.Value, attr.InnerText(), "Should apply a relationship of 'deny'")
}
func TestAddVideoUploaderInfo(t *testing.T) {
xml := New()
loc := NewLoc()
video := NewVideo()
uploader := "http://somedomain/user/someuser"
video.SetUploaderInfo(uploader)
loc.AddVideo(video)
xml.Add(loc)
out, _ := xml.OutputString()
tag, _ := findOne(out, "//urlset/url/video:video/video:uploader/@info")
assert.NotNil(t, video.Uploader, "Should create an uploader")
assert.Equal(t, uploader, tag.InnerText(), "Should apply info attr to xml")
video.SetUploaderInfo(uploader)
assert.Equal(t, uploader, video.Uploader.Info, "Should apply value to struct")
}
func TestAddVideoUploaderVal(t *testing.T) {
xml := New()
loc := NewLoc()
video := NewVideo()
uploadVal := "SomeUser"
video.SetUploaderVal(uploadVal)
loc.AddVideo(video)
xml.Add(loc)
out, _ := xml.OutputString()
tag, _ := findOne(out, "//urlset/url/video:video/video:uploader")
assert.NotNil(t, video.Uploader, "Should create an uploader")
assert.Equal(t, uploadVal, tag.InnerText(), "Should apply value to xml")
video.SetUploaderVal(uploadVal)
assert.Equal(t, uploadVal, video.Uploader.Value, "Should apply value to struct")
}
func TestAddVideoSetTags(t *testing.T) {
xml := New()
loc := NewLoc()
video := NewVideo()
video.SetTags("tag1", "tag2", "tag3")
loc.AddVideo(video)
xml.Add(loc)
out, _ := xml.OutputString()
tags, _ := findAll(out, "//urlset/url/video:video/video:tag")
assert.Equal(t, "tag1", tags[0].InnerText(), "Should apply first tag")
assert.Equal(t, "tag2", tags[1].InnerText(), "Should apply second tag")
assert.Equal(t, "tag3", tags[2].InnerText(), "Should apply third tag")
}
func TestXMLAddEntryWithLastModified(t *testing.T) {
xml := New()
loc := NewLoc()
now := time.Now()
later := now.Add(3 * time.Hour)
loc.SetLastModified(Date(later))
xml.Add(loc)
out, _ := xml.OutputString()
tag, _ := findOne(out, "//urlset/url/lastmod")
assert.True(t, tag != nil, "Should have lastmod set")
nodeTime, _ := time.Parse(time.RFC3339, tag.InnerText())
laterFormat, _ := time.Parse(shortFormat, later.String())
assert.Equal(t, nodeTime, laterFormat, "Should match input timestamp")
}
func TestDate(t *testing.T) {
xml := New()
loc := NewLoc()
now := time.Now()
loc.SetLastModified(Date(now))
xml.Add(loc)
out, _ := xml.OutputString()
tag, _ := findOne(out, "//urlset/url/lastmod")
assert.True(t, tag != nil, "Should have lastmod set")
assert.Equal(t, tag.InnerText(), now.Format(shortFormat), "Should format a short date")
}
func TestDateFull(t *testing.T) {
xml := New()
loc := NewLoc()
now := time.Now()
loc.SetLastModified(DateFull(now))
xml.Add(loc)
out, _ := xml.OutputString()
tag, _ := findOne(out, "//urlset/url/lastmod")
assert.True(t, tag != nil, "Should have lastmod set")
assert.Equal(t, tag.InnerText(), now.Format(fullFormat), "Should format a long date")
}
func TestStrToLang(t *testing.T) {
pass := Language("zu")
fmt.Println(pass)
assert.Equal(t, pass, LanguageZU, "Should return an existing matching language")
fail := Language("notacountry")
fmt.Println(fail)
assert.Equal(t, fail, LangDefault, "Should fallback to default language")
}