@@ -6,71 +6,161 @@ package sitemap
66import (
77 "bytes"
88 "encoding/xml"
9- "fmt"
109 "strings"
1110 "testing"
1211 "time"
1312
1413 "github.com/stretchr/testify/assert"
1514)
1615
17- func TestOk (t * testing.T ) {
18- testReal := func (s * Sitemap , name string , urls []URL , expected string ) {
19- for _ , url := range urls {
20- s .Add (url )
21- }
22- buf := & bytes.Buffer {}
23- _ , err := s .WriteTo (buf )
24- assert .NoError (t , nil , err )
25- assert .Equal (t , xml .Header + "<" + name + " xmlns=\" http://www.sitemaps.org/schemas/sitemap/0.9\" >" + expected + "</" + name + ">\n " , buf .String ())
16+ func TestNewSitemap (t * testing.T ) {
17+ ts := time .Unix (1651322008 , 0 ).UTC ()
18+
19+ tests := []struct {
20+ name string
21+ urls []URL
22+ want string
23+ wantErr string
24+ }{
25+ {
26+ name : "empty" ,
27+ urls : []URL {},
28+ want : xml .Header + `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">` +
29+ "" +
30+ "</urlset>\n " ,
31+ },
32+ {
33+ name : "regular" ,
34+ urls : []URL {
35+ {URL : "https://gitea.io/test1" , LastMod : & ts },
36+ },
37+ want : xml .Header + `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">` +
38+ "<url><loc>https://gitea.io/test1</loc><lastmod>2022-04-30T12:33:28Z</lastmod></url>" +
39+ "</urlset>\n " ,
40+ },
41+ {
42+ name : "without lastmod" ,
43+ urls : []URL {
44+ {URL : "https://gitea.io/test1" },
45+ },
46+ want : xml .Header + `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">` +
47+ "<url><loc>https://gitea.io/test1</loc></url>" +
48+ "</urlset>\n " ,
49+ },
50+ {
51+ name : "multiple" ,
52+ urls : []URL {
53+ {URL : "https://gitea.io/test1" , LastMod : & ts },
54+ {URL : "https://gitea.io/test2" , LastMod : nil },
55+ },
56+ want : xml .Header + `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">` +
57+ "<url><loc>https://gitea.io/test1</loc><lastmod>2022-04-30T12:33:28Z</lastmod></url>" +
58+ "<url><loc>https://gitea.io/test2</loc></url>" +
59+ "</urlset>\n " ,
60+ },
61+ {
62+ name : "too many urls" ,
63+ urls : make ([]URL , 50001 ),
64+ wantErr : "The sitemap contains 50001 URLs, but only 50000 are allowed" ,
65+ },
66+ {
67+ name : "too big file" ,
68+ urls : []URL {
69+ {URL : strings .Repeat ("b" , 50 * 1024 * 1024 + 1 )},
70+ },
71+ wantErr : "The sitemap has 52428932 bytes, but only 52428800 are allowed" ,
72+ },
2673 }
27- test := func (urls []URL , expected string ) {
28- testReal (NewSitemap (), "urlset" , urls , expected )
29- testReal (NewSitemapIndex (), "sitemapindex" , urls , expected )
74+ for _ , tt := range tests {
75+ t .Run (tt .name , func (t * testing.T ) {
76+ s := NewSitemap ()
77+ for _ , url := range tt .urls {
78+ s .Add (url )
79+ }
80+ buf := & bytes.Buffer {}
81+ _ , err := s .WriteTo (buf )
82+ if tt .wantErr != "" {
83+ assert .EqualError (t , err , tt .wantErr )
84+ } else {
85+ assert .NoError (t , err )
86+ assert .Equalf (t , tt .want , buf .String (), "NewSitemap()" )
87+ }
88+ })
3089 }
90+ }
3191
92+ func TestNewSitemapIndex (t * testing.T ) {
3293 ts := time .Unix (1651322008 , 0 ).UTC ()
3394
34- test (
35- []URL {},
36- "" ,
37- )
38- test (
39- []URL {
40- {URL : "https://gitea.io/test1" , LastMod : & ts },
95+ tests := []struct {
96+ name string
97+ urls []URL
98+ want string
99+ wantErr string
100+ }{
101+ {
102+ name : "empty" ,
103+ urls : []URL {},
104+ want : xml .Header + `<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">` +
105+ "" +
106+ "</sitemapindex>\n " ,
41107 },
42- "<url><loc>https://gitea.io/test1</loc><lastmod>2022-04-30T12:33:28Z</lastmod></url>" ,
43- )
44- test (
45- []URL {
46- {URL : "https://gitea.io/test2" , LastMod : nil },
108+ {
109+ name : "regular" ,
110+ urls : []URL {
111+ {URL : "https://gitea.io/test1" , LastMod : & ts },
112+ },
113+ want : xml .Header + `<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">` +
114+ "<sitemap><loc>https://gitea.io/test1</loc><lastmod>2022-04-30T12:33:28Z</lastmod></sitemap>" +
115+ "</sitemapindex>\n " ,
47116 },
48- "<url><loc>https://gitea.io/test2</loc></url>" ,
49- )
50- test (
51- []URL {
52- {URL : "https://gitea.io/test1" , LastMod : & ts },
53- {URL : "https://gitea.io/test2" , LastMod : nil },
117+ {
118+ name : "without lastmod" ,
119+ urls : []URL {
120+ {URL : "https://gitea.io/test1" },
121+ },
122+ want : xml .Header + `<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">` +
123+ "<sitemap><loc>https://gitea.io/test1</loc></sitemap>" +
124+ "</sitemapindex>\n " ,
125+ },
126+ {
127+ name : "multiple" ,
128+ urls : []URL {
129+ {URL : "https://gitea.io/test1" , LastMod : & ts },
130+ {URL : "https://gitea.io/test2" , LastMod : nil },
131+ },
132+ want : xml .Header + `<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">` +
133+ "<sitemap><loc>https://gitea.io/test1</loc><lastmod>2022-04-30T12:33:28Z</lastmod></sitemap>" +
134+ "<sitemap><loc>https://gitea.io/test2</loc></sitemap>" +
135+ "</sitemapindex>\n " ,
136+ },
137+ {
138+ name : "too many sitemaps" ,
139+ urls : make ([]URL , 50001 ),
140+ wantErr : "The sitemap contains 50001 sub-sitemaps, but only 50000 are allowed" ,
141+ },
142+ {
143+ name : "too big file" ,
144+ urls : []URL {
145+ {URL : strings .Repeat ("b" , 50 * 1024 * 1024 + 1 )},
146+ },
147+ wantErr : "The sitemap has 52428952 bytes, but only 52428800 are allowed" ,
54148 },
55- "<url><loc>https://gitea.io/test1</loc><lastmod>2022-04-30T12:33:28Z</lastmod></url>" +
56- "<url><loc>https://gitea.io/test2</loc></url>" ,
57- )
58- }
59-
60- func TestTooManyURLs (t * testing.T ) {
61- s := NewSitemap ()
62- for i := 0 ; i < 50001 ; i ++ {
63- s .Add (URL {URL : fmt .Sprintf ("https://gitea.io/test%d" , i )})
64149 }
65- buf := & bytes.Buffer {}
66- _ , err := s .WriteTo (buf )
67- assert .EqualError (t , err , "The sitemap contains too many URLs: 50001" )
68- }
69-
70- func TestSitemapTooBig (t * testing.T ) {
71- s := NewSitemap ()
72- s .Add (URL {URL : strings .Repeat ("b" , sitemapFileLimit )})
73- buf := & bytes.Buffer {}
74- _ , err := s .WriteTo (buf )
75- assert .EqualError (t , err , "The sitemap is too big: 52428931" )
150+ for _ , tt := range tests {
151+ t .Run (tt .name , func (t * testing.T ) {
152+ s := NewSitemapIndex ()
153+ for _ , url := range tt .urls {
154+ s .Add (url )
155+ }
156+ buf := & bytes.Buffer {}
157+ _ , err := s .WriteTo (buf )
158+ if tt .wantErr != "" {
159+ assert .EqualError (t , err , tt .wantErr )
160+ } else {
161+ assert .NoError (t , err )
162+ assert .Equalf (t , tt .want , buf .String (), "NewSitemapIndex()" )
163+ }
164+ })
165+ }
76166}
0 commit comments