github.com/kf99916/go-sitemap-generator/v2@v2.1.0/stm/builder_url_test.go (about) 1 package stm 2 3 import ( 4 "bytes" 5 "reflect" 6 "testing" 7 "time" 8 9 "github.com/beevik/etree" 10 "github.com/clbanning/mxj" 11 ) 12 13 func TestBlank(t *testing.T) { 14 if _, err := NewSitemapURL(&Options{}, URL{}); err == nil { 15 t.Errorf(`Failed to validate blank arg ( URL{} ): %v`, err) 16 } 17 } 18 19 func TestItHasLocElement(t *testing.T) { 20 if _, err := NewSitemapURL(&Options{}, URL{}); err == nil { 21 t.Errorf(`Failed to validate about must have loc attribute in URL type ( URL{} ): %v`, err) 22 } 23 } 24 25 func TestJustSetLocElement(t *testing.T) { 26 smu, err := NewSitemapURL(&Options{}, URL{{"loc", "path"}, {"host", "http://example.com"}}) 27 28 if err != nil { 29 t.Fatalf(`Fatal to validate! This is a critical error: %v`, err) 30 } 31 32 doc := etree.NewDocument() 33 doc.ReadFromBytes(smu.XML()) 34 35 var elm *etree.Element 36 url := doc.SelectElement("url") 37 38 elm = url.SelectElement("loc") 39 if elm == nil { 40 t.Errorf(`Failed to generate xml that loc element is blank: %v`, elm) 41 } 42 if elm != nil && elm.Text() != "http://example.com/path" { 43 t.Errorf(`Failed to generate xml thats deferrent value in loc element: %v`, elm.Text()) 44 } 45 } 46 47 func TestJustSetLocElementAndThenItNeedsCompleteValues(t *testing.T) { 48 smu, err := NewSitemapURL(&Options{}, URL{{"loc", "path"}, {"host", "http://example.com"}}) 49 50 if err != nil { 51 t.Fatalf(`Fatal to validate! This is a critical error: %v`, err) 52 } 53 54 doc := etree.NewDocument() 55 doc.ReadFromBytes(smu.XML()) 56 57 var elm *etree.Element 58 url := doc.SelectElement("url") 59 60 elm = url.SelectElement("loc") 61 if elm == nil { 62 t.Errorf(`Failed to generate xml that loc element is blank: %v`, elm) 63 } 64 if elm != nil && elm.Text() != "http://example.com/path" { 65 t.Errorf(`Failed to generate xml thats deferrent value in loc element: %v`, elm.Text()) 66 } 67 68 elm = url.SelectElement("priority") 69 if elm == nil { 70 t.Errorf(`Failed to generate xml that priority element is nil: %v`, elm) 71 } 72 if elm != nil && elm.Text() != "0.5" { 73 t.Errorf(`Failed to generate xml thats deferrent value in priority element: %v`, elm.Text()) 74 } 75 76 elm = url.SelectElement("changefreq") 77 if elm == nil { 78 t.Errorf(`Failed to generate xml that changefreq element is nil: %v`, elm) 79 } 80 if elm != nil && elm.Text() != "weekly" { 81 t.Errorf(`Failed to generate xml thats deferrent value in changefreq element: %v`, elm.Text()) 82 } 83 84 elm = url.SelectElement("lastmod") 85 if elm == nil { 86 t.Errorf(`Failed to generate xml that lastmod element is nil: %v`, elm) 87 } 88 if elm != nil { 89 if _, err := time.Parse(time.RFC3339, elm.Text()); err != nil { 90 t.Errorf(`Failed to generate xml thats failed to parse datetime in lastmod element: %v`, err) 91 } 92 } 93 } 94 95 func TestSetNilValue(t *testing.T) { 96 smu, err := NewSitemapURL(&Options{}, URL{{"loc", "path"}, {"priority", nil}, {"changefreq", nil}, {"lastmod", nil}, {"host", "http://example.com"}}) 97 98 if err != nil { 99 t.Fatalf(`Fatal to validate! This is a critical error: %v`, err) 100 } 101 102 doc := etree.NewDocument() 103 doc.ReadFromBytes(smu.XML()) 104 105 var elm *etree.Element 106 url := doc.SelectElement("url") 107 108 elm = url.SelectElement("loc") 109 if elm == nil { 110 t.Errorf(`Failed to generate xml that loc element is blank: %v`, elm) 111 } 112 if elm != nil && elm.Text() != "http://example.com/path" { 113 t.Errorf(`Failed to generate xml thats deferrent value in loc element: %v`, elm.Text()) 114 } 115 116 elm = url.SelectElement("priority") 117 if elm != nil { 118 t.Errorf(`Failed to generate xml that priority element must be nil: %v`, elm) 119 } 120 121 elm = url.SelectElement("changefreq") 122 if elm != nil { 123 t.Errorf(`Failed to generate xml that changefreq element must be nil: %v`, elm) 124 } 125 126 elm = url.SelectElement("lastmod") 127 if elm != nil { 128 t.Errorf(`Failed to generate xml that lastmod element must be nil: %v`, elm) 129 } 130 } 131 132 func TestAutoGenerateSitemapHost(t *testing.T) { 133 smu, err := NewSitemapURL(&Options{}, URL{{"loc", "path"}, {"host", "http://example.com"}}) 134 135 if err != nil { 136 t.Fatalf(`Fatal to validate! This is a critical error: %v`, err) 137 } 138 139 doc := etree.NewDocument() 140 doc.ReadFromBytes(smu.XML()) 141 142 var elm *etree.Element 143 url := doc.SelectElement("url") 144 145 elm = url.SelectElement("loc") 146 if elm == nil { 147 t.Errorf(`Failed to generate xml that loc element is blank: %v`, elm) 148 } 149 if elm != nil && elm.Text() != "http://example.com/path" { 150 t.Errorf(`Failed to generate xml thats deferrent value in loc element: %v`, elm.Text()) 151 } 152 } 153 154 func TestNewsSitemaps(t *testing.T) { 155 doc := etree.NewDocument() 156 root := doc.CreateElement("root") 157 158 data := URL{{"loc", "/news"}, {"news", URL{ 159 {"publication", URL{ 160 {"name", "Example"}, 161 {"language", "en"}, 162 }}, 163 {"title", "My Article"}, 164 {"keywords", "my article, articles about myself"}, 165 {"stock_tickers", "SAO:PETR3"}, 166 {"publication_date", "2011-08-22"}, 167 {"access", "Subscription"}, 168 {"genres", "PressRelease"}, 169 }}} 170 expect := []byte(` 171 <root> 172 <news:news> 173 <news:keywords>my article, articles about myself</news:keywords> 174 <news:stock_tickers>SAO:PETR3</news:stock_tickers> 175 <news:publication_date>2011-08-22</news:publication_date> 176 <news:access>Subscription</news:access> 177 <news:genres>PressRelease</news:genres> 178 <news:publication> 179 <news:name>Example</news:name> 180 <news:language>en</news:language> 181 </news:publication> 182 <news:title>My Article</news:title> 183 </news:news> 184 </root>`) 185 186 SetBuilderElementValue(root, data, "news") 187 buf := &bytes.Buffer{} 188 doc.WriteTo(buf) 189 190 mdata, _ := mxj.NewMapXml(buf.Bytes()) 191 mexpect, _ := mxj.NewMapXml(expect) 192 193 if !reflect.DeepEqual(mdata, mexpect) { 194 t.Error(`Failed to generate sitemap xml thats deferrent output value in URL type`) 195 } 196 } 197 198 func TestImageSitemaps(t *testing.T) { 199 doc := etree.NewDocument() 200 root := doc.CreateElement("root") 201 202 data := URL{{"loc", "/images"}, {"image", []URL{ 203 {{"loc", "http://www.example.com/image.png"}, {"title", "Image"}}, 204 {{"loc", "http://www.example.com/image1.png"}, {"title", "Image1"}}, 205 }}} 206 expect := []byte(` 207 <root> 208 <image:image> 209 <image:loc>http://www.example.com/image.png</image:loc> 210 <image:title>Image</image:title> 211 </image:image> 212 <image:image> 213 <image:loc>http://www.example.com/image1.png</image:loc> 214 <image:title>Image1</image:title> 215 </image:image> 216 </root>`) 217 218 SetBuilderElementValue(root, data, "image") 219 buf := &bytes.Buffer{} 220 doc.WriteTo(buf) 221 222 mdata, _ := mxj.NewMapXml(buf.Bytes()) 223 mexpect, _ := mxj.NewMapXml(expect) 224 225 if !reflect.DeepEqual(mdata, mexpect) { 226 t.Error(`Failed to generate sitemap xml thats deferrent output value in URL type`) 227 } 228 } 229 230 func TestVideoSitemaps(t *testing.T) { 231 doc := etree.NewDocument() 232 root := doc.CreateElement("root") 233 234 data := URL{{"loc", "/videos"}, {"video", URL{ 235 {"thumbnail_loc", "http://www.example.com/video1_thumbnail.png"}, 236 {"title", "Title"}, 237 {"description", "Description"}, 238 {"content_loc", "http://www.example.com/cool_video.mpg"}, 239 {"category", "Category"}, 240 {"tag", []string{"one", "two", "three"}}, 241 }}} 242 243 expect := []byte(` 244 <root> 245 <video:video> 246 <video:thumbnail_loc>http://www.example.com/video1_thumbnail.png</video:thumbnail_loc> 247 <video:title>Title</video:title> 248 <video:description>Description</video:description> 249 <video:content_loc>http://www.example.com/cool_video.mpg</video:content_loc> 250 <video:tag>one</video:tag> 251 <video:tag>two</video:tag> 252 <video:tag>three</video:tag> 253 <video:category>Category</video:category> 254 </video:video> 255 </root>`) 256 257 SetBuilderElementValue(root, data, "video") 258 buf := &bytes.Buffer{} 259 doc.WriteTo(buf) 260 261 mdata, _ := mxj.NewMapXml(buf.Bytes()) 262 mexpect, _ := mxj.NewMapXml(expect) 263 264 if !reflect.DeepEqual(mdata, mexpect) { 265 t.Error(`Failed to generate sitemap xml thats deferrent output value in URL type`) 266 } 267 } 268 269 func TestGeoSitemaps(t *testing.T) { 270 doc := etree.NewDocument() 271 root := doc.CreateElement("root") 272 273 data := URL{{"loc", "/geos"}, {"geo", URL{{"format", "kml"}}}} 274 275 expect := []byte(` 276 <root> 277 <geo:geo> 278 <geo:format>kml</geo:format> 279 </geo:geo> 280 </root>`) 281 282 SetBuilderElementValue(root, data, "geo") 283 buf := &bytes.Buffer{} 284 doc.WriteTo(buf) 285 286 mdata, _ := mxj.NewMapXml(buf.Bytes()) 287 mexpect, _ := mxj.NewMapXml(expect) 288 289 if !reflect.DeepEqual(mdata, mexpect) { 290 t.Error(`Failed to generate sitemap xml thats deferrent output value in URL type`) 291 } 292 } 293 294 func TestMobileSitemaps(t *testing.T) { 295 doc := etree.NewDocument() 296 root := doc.CreateElement("root") 297 298 data := URL{{"loc", "/mobile"}, {"mobile", true}} 299 300 expect := []byte(` 301 <root> 302 <loc>/mobile</loc> 303 <mobile:mobile/> 304 </root>`) 305 306 SetBuilderElementValue(root, data.URLJoinBy("loc", "host", "loc"), "loc") 307 SetBuilderElementValue(root, data, "mobile") 308 309 buf := &bytes.Buffer{} 310 doc.WriteTo(buf) 311 312 mdata, _ := mxj.NewMapXml(buf.Bytes()) 313 mexpect, _ := mxj.NewMapXml(expect) 314 315 if !reflect.DeepEqual(mdata, mexpect) { 316 t.Error(`Failed to generate sitemap xml thats deferrent output value in URL type`) 317 } 318 } 319 320 func TestPageMapSitemaps(t *testing.T) {} 321 322 func TestAlternateLinks(t *testing.T) { 323 doc := etree.NewDocument() 324 root := doc.CreateElement("root") 325 326 loc := "/alternates" 327 data := URL{{"loc", loc}, {"xhtml:link", []Attr{ 328 { 329 "rel": "alternate", 330 "hreflang": "zh-tw", 331 "href": loc + "?locale=zh-tw", 332 }, 333 { 334 "rel": "alternate", 335 "hreflang": "en-us", 336 "href": loc + "?locale=en-us", 337 }, 338 }}} 339 340 expect := []byte(` 341 <root> 342 <loc>/alternates</loc> 343 <xhtml:link rel="alternate" hreflang="zh-tw" href="/alternates?locale=zh-tw"/> 344 <xhtml:link rel="alternate" hreflang="en-us" href="/alternates?locale=en-us"/> 345 </root>`) 346 347 SetBuilderElementValue(root, data.URLJoinBy("loc", "host", "loc"), "loc") 348 SetBuilderElementValue(root, data, "xhtml:link") 349 350 buf := &bytes.Buffer{} 351 doc.WriteTo(buf) 352 353 mdata, _ := mxj.NewMapXml(buf.Bytes()) 354 mexpect, _ := mxj.NewMapXml(expect) 355 356 if !reflect.DeepEqual(mdata, mexpect) { 357 t.Error(`Failed to generate sitemap xml thats deferrent output value in URL type`) 358 } 359 } 360 361 func TestAttr(t *testing.T) { 362 doc := etree.NewDocument() 363 root := doc.CreateElement("root") 364 365 data := URL{{"loc", "/videos"}, {"video", URL{ 366 {"thumbnail_loc", "http://www.example.com/video1_thumbnail.png"}, 367 {"title", "Title"}, 368 {"description", "Description"}, 369 {"content_loc", "http://www.example.com/cool_video.mpg"}, 370 {"category", "Category"}, 371 {"tag", []string{"one", "two", "three"}}, 372 {"player_loc", Attrs{"https://f.vimeocdn.com/p/flash/moogaloop/6.2.9/moogaloop.swf?clip_id=26", Attr{"allow_embed": "Yes", "autoplay": "autoplay=1"}}}, 373 }}} 374 375 expect := []byte(` 376 <root> 377 <video:video> 378 <video:thumbnail_loc>http://www.example.com/video1_thumbnail.png</video:thumbnail_loc> 379 <video:title>Title</video:title> 380 <video:description>Description</video:description> 381 <video:content_loc>http://www.example.com/cool_video.mpg</video:content_loc> 382 <video:tag>one</video:tag> 383 <video:tag>two</video:tag> 384 <video:tag>three</video:tag> 385 <video:category>Category</video:category> 386 <video:player_loc allow_embed="Yes" autoplay="autoplay=1">https://f.vimeocdn.com/p/flash/moogaloop/6.2.9/moogaloop.swf?clip_id=26</video:player_loc> 387 </video:video> 388 </root>`) 389 390 SetBuilderElementValue(root, data, "video") 391 392 buf := &bytes.Buffer{} 393 // doc.Indent(2) 394 doc.WriteTo(buf) 395 396 mdata, _ := mxj.NewMapXml(buf.Bytes()) 397 mexpect, _ := mxj.NewMapXml(expect) 398 399 // print(string(buf.Bytes())) 400 401 if !reflect.DeepEqual(mdata, mexpect) { 402 t.Error(`Failed to generate sitemap xml thats deferrent output value in URL type`) 403 } 404 } 405 406 func TestAttrWithoutTypedef(t *testing.T) { 407 doc := etree.NewDocument() 408 root := doc.CreateElement("root") 409 410 data := URL{{"loc", "/videos"}, {"video", URL{ 411 {"thumbnail_loc", "http://www.example.com/video1_thumbnail.png"}, 412 {"title", "Title"}, 413 {"description", "Description"}, 414 {"content_loc", "http://www.example.com/cool_video.mpg"}, 415 {"category", "Category"}, 416 {"tag", []string{"one", "two", "three"}}, 417 {"player_loc", Attrs{"https://f.vimeocdn.com/p/flash/moogaloop/6.2.9/moogaloop.swf?clip_id=26", map[string]string{"allow_embed": "Yes", "autoplay": "autoplay=1"}}}, 418 }}} 419 420 expect := []byte(` 421 <root> 422 <video:video> 423 <video:thumbnail_loc>http://www.example.com/video1_thumbnail.png</video:thumbnail_loc> 424 <video:title>Title</video:title> 425 <video:description>Description</video:description> 426 <video:content_loc>http://www.example.com/cool_video.mpg</video:content_loc> 427 <video:tag>one</video:tag> 428 <video:tag>two</video:tag> 429 <video:tag>three</video:tag> 430 <video:category>Category</video:category> 431 <video:player_loc allow_embed="Yes" autoplay="autoplay=1">https://f.vimeocdn.com/p/flash/moogaloop/6.2.9/moogaloop.swf?clip_id=26</video:player_loc> 432 </video:video> 433 </root>`) 434 435 SetBuilderElementValue(root, data, "video") 436 437 buf := &bytes.Buffer{} 438 // doc.Indent(2) 439 doc.WriteTo(buf) 440 441 mdata, _ := mxj.NewMapXml(buf.Bytes()) 442 mexpect, _ := mxj.NewMapXml(expect) 443 444 // print(string(buf.Bytes())) 445 446 if !reflect.DeepEqual(mdata, mexpect) { 447 t.Error(`Failed to generate sitemap xml thats deferrent output value in URL type`) 448 } 449 } 450 451 func BenchmarkGenerateXML(b *testing.B) { 452 453 b.ReportAllocs() 454 b.ResetTimer() 455 456 forPerformance := 500 457 458 for k := 0; k <= forPerformance; k++ { 459 for i := 1; i <= forPerformance; i++ { 460 461 var smu SitemapURL 462 var data URL 463 464 data = URL{{"loc", "/mobile"}, {"mobile", true}} 465 smu, _ = NewSitemapURL(&Options{}, data) 466 smu.XML() 467 468 data = URL{{"loc", "/images"}, {"image", []URL{ 469 {{"loc", "http://www.example.com/image.png"}, {"title", "Image"}}, 470 {{"loc", "http://www.example.com/image1.png"}, {"title", "Image1"}}, 471 }}} 472 smu, _ = NewSitemapURL(&Options{}, data) 473 smu.XML() 474 475 data = URL{{"loc", "/videos"}, {"video", URL{ 476 {"thumbnail_loc", "http://www.example.com/video1_thumbnail.png"}, 477 {"title", "Title"}, 478 {"description", "Description"}, 479 {"content_loc", "http://www.example.com/cool_video.mpg"}, 480 {"category", "Category"}, 481 {"tag", []string{"one", "two", "three"}}, 482 }}} 483 smu, _ = NewSitemapURL(&Options{}, data) 484 smu.XML() 485 486 data = URL{{"loc", "/news"}, {"news", URL{ 487 {"publication", URL{ 488 {"name", "Example"}, 489 {"language", "en"}, 490 }}, 491 {"title", "My Article"}, 492 {"keywords", "my article, articles about myself"}, 493 {"stock_tickers", "SAO:PETR3"}, 494 {"publication_date", "2011-08-22"}, 495 {"access", "Subscription"}, 496 {"genres", "PressRelease"}, 497 }}} 498 499 smu, _ = NewSitemapURL(&Options{}, data) 500 smu.XML() 501 } 502 } 503 }