-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsitemap_test.go
134 lines (112 loc) · 3.42 KB
/
sitemap_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
package sitemap
import (
"fmt"
"log"
"os"
"path"
"testing"
"time"
)
var (
itemResult = `
<url>
<loc>http://www.google.com</loc>
<lastmod>2014-03-31T15:00:00+01:00</lastmod>
<changefreq>hourly</changefreq>
<priority>0.5</priority>
</url>`
sitemapResult = fmt.Sprintf(`<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">%s
</urlset>`, itemResult)
sitemapIndexItemResult = `
<sitemap>
<loc>http://www.google.com/sitemap.xml.gz</loc>
<lastmod>2014-03-31T15:00:00+01:00</lastmod>
</sitemap>`
sitemapIndexResult = fmt.Sprintf(`<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">%s
</sitemapindex>
`, sitemapIndexItemResult)
)
func TestFileGeneration(t *testing.T) {
testDir := os.TempDir() + "/sitemap"
err := os.Mkdir(testDir, os.ModeDir)
if err != nil {
log.Fatalf("could not create temporary test directory: %v", err)
}
defer func() {
os.RemoveAll(testDir)
}()
lastMod, _ := time.Parse(time.RFC3339, "2014-03-31T15:00:00+01:00")
// Sitemap item
item := SitemapItem{
"http://www.google.com",
lastMod,
"hourly",
0.5,
}
if item.String() != itemResult {
t.Errorf("Expected sitemap item to be %s, actual: %s", itemResult, item.String())
}
// Sitemap
sitemap := Sitemap{
[]SitemapItem{
item,
},
}
if sitemap.String() != sitemapResult {
t.Errorf("Expected sitemap to be %s, actual: %s", sitemapResult, sitemap.String())
}
// Save sitemap to test directory
err = sitemap.ToFile(testDir + "/sitemap.xml.gz")
if err != nil {
t.Errorf("Could not save the sitemap to a file: %v", err)
}
// SitemapIndexItem
sitemapIndexItem := SitemapIndexItem{
"http://www.google.com/sitemap.xml.gz",
lastMod,
}
if sitemapIndexItem.String() != sitemapIndexItemResult {
t.Errorf("Expected sitemap index item to be %s, actual: %s", sitemapIndexItemResult, sitemapIndexItem.String())
}
// SitemapIndex
sitemapIndex := SitemapIndex{
[]SitemapIndexItem{
sitemapIndexItem,
},
}
if sitemapIndex.String() != sitemapIndexResult {
t.Errorf("Expected sitemap index to be %s, actual: %s", sitemapIndexResult, sitemapIndex.String())
}
sitemapIndex2, err := NewIndexFromDir(testDir, "http://www.google.com/")
if err != nil {
log.Fatalf("could not create sitemap index from directory: %v", err)
}
file, err := os.Open(path.Join(testDir, "sitemap.xml.gz"))
if err != nil {
log.Fatalf("could not open file 'sitemap.xml.gz' in test dir: %v", err)
}
fileinfo, err := file.Stat()
if err != nil {
log.Fatalf("could not stat file 'sitemap.xml.gz' in test dir: %v", err)
}
sitemapIndexItem2 := SitemapIndexItem{
"http://www.google.com/sitemap.xml.gz",
fileinfo.ModTime(),
}
sitemapIndexResult2 := fmt.Sprintf(`<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">%s
</sitemapindex>
`, sitemapIndexItem2.String())
if sitemapIndex2.String() != sitemapIndexResult2 {
t.Errorf("Expected sitemap index created from dir '%s' to be %s, actual: %s", testDir, sitemapIndexResult2, sitemapIndex2.String())
}
// Save sitemap index to test directory
err = sitemap.ToFile(testDir + "/sitemap-index.xml.gz")
if err != nil {
t.Errorf("Could not save the sitemap index to a file: %v", err)
}
}