github.com/EconomistDigitalSolutions/go-sitemap-generator@v0.0.0-20181010142722-a820dfc2bc5e/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{} ): %s`, 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{} ): %s`, 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: %s`, 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: %s`, 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: %s`, 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: %s`, 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: %s`, 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: %s`, 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: %s`, elm) 71 } 72 if elm != nil && elm.Text() != "0.5" { 73 t.Errorf(`Failed to generate xml thats deferrent value in priority element: %s`, 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: %s`, elm) 79 } 80 if elm != nil && elm.Text() != "weekly" { 81 t.Errorf(`Failed to generate xml thats deferrent value in changefreq element: %s`, 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: %s`, 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: %s`, 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: %s`, 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: %s`, 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: %s`, 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: %s`, 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: %s`, 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: %s`, 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: %s`, 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: %s`, 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: %s`, 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 324 func TestAttr(t *testing.T) { 325 doc := etree.NewDocument() 326 root := doc.CreateElement("root") 327 328 data := URL{{"loc", "/videos"}, {"video", URL{ 329 {"thumbnail_loc", "http://www.example.com/video1_thumbnail.png"}, 330 {"title", "Title"}, 331 {"description", "Description"}, 332 {"content_loc", "http://www.example.com/cool_video.mpg"}, 333 {"category", "Category"}, 334 {"tag", []string{"one", "two", "three"}}, 335 {"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"}}}, 336 }}} 337 338 expect := []byte(` 339 <root> 340 <video:video> 341 <video:thumbnail_loc>http://www.example.com/video1_thumbnail.png</video:thumbnail_loc> 342 <video:title>Title</video:title> 343 <video:description>Description</video:description> 344 <video:content_loc>http://www.example.com/cool_video.mpg</video:content_loc> 345 <video:tag>one</video:tag> 346 <video:tag>two</video:tag> 347 <video:tag>three</video:tag> 348 <video:category>Category</video:category> 349 <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> 350 </video:video> 351 </root>`) 352 353 SetBuilderElementValue(root, data, "video") 354 355 buf := &bytes.Buffer{} 356 // doc.Indent(2) 357 doc.WriteTo(buf) 358 359 mdata, _ := mxj.NewMapXml(buf.Bytes()) 360 mexpect, _ := mxj.NewMapXml(expect) 361 362 // print(string(buf.Bytes())) 363 364 if !reflect.DeepEqual(mdata, mexpect) { 365 t.Error(`Failed to generate sitemap xml thats deferrent output value in URL type`) 366 } 367 } 368 369 func TestAttrWithoutTypedef(t *testing.T) { 370 doc := etree.NewDocument() 371 root := doc.CreateElement("root") 372 373 data := URL{{"loc", "/videos"}, {"video", URL{ 374 {"thumbnail_loc", "http://www.example.com/video1_thumbnail.png"}, 375 {"title", "Title"}, 376 {"description", "Description"}, 377 {"content_loc", "http://www.example.com/cool_video.mpg"}, 378 {"category", "Category"}, 379 {"tag", []string{"one", "two", "three"}}, 380 {"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"}}}, 381 }}} 382 383 expect := []byte(` 384 <root> 385 <video:video> 386 <video:thumbnail_loc>http://www.example.com/video1_thumbnail.png</video:thumbnail_loc> 387 <video:title>Title</video:title> 388 <video:description>Description</video:description> 389 <video:content_loc>http://www.example.com/cool_video.mpg</video:content_loc> 390 <video:tag>one</video:tag> 391 <video:tag>two</video:tag> 392 <video:tag>three</video:tag> 393 <video:category>Category</video:category> 394 <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> 395 </video:video> 396 </root>`) 397 398 SetBuilderElementValue(root, data, "video") 399 400 buf := &bytes.Buffer{} 401 // doc.Indent(2) 402 doc.WriteTo(buf) 403 404 mdata, _ := mxj.NewMapXml(buf.Bytes()) 405 mexpect, _ := mxj.NewMapXml(expect) 406 407 // print(string(buf.Bytes())) 408 409 if !reflect.DeepEqual(mdata, mexpect) { 410 t.Error(`Failed to generate sitemap xml thats deferrent output value in URL type`) 411 } 412 } 413 414 func BenchmarkGenerateXML(b *testing.B) { 415 416 b.ReportAllocs() 417 b.ResetTimer() 418 419 forPerformance := 500 420 421 for k := 0; k <= forPerformance; k++ { 422 for i := 1; i <= forPerformance; i++ { 423 424 var smu SitemapURL 425 var data URL 426 427 data = URL{{"loc", "/mobile"}, {"mobile", true}} 428 smu, _ = NewSitemapURL(&Options{}, data) 429 smu.XML() 430 431 data = URL{{"loc", "/images"}, {"image", []URL{ 432 {{"loc", "http://www.example.com/image.png"}, {"title", "Image"}}, 433 {{"loc", "http://www.example.com/image1.png"}, {"title", "Image1"}}, 434 }}} 435 smu, _ = NewSitemapURL(&Options{}, data) 436 smu.XML() 437 438 data = URL{{"loc", "/videos"}, {"video", URL{ 439 {"thumbnail_loc", "http://www.example.com/video1_thumbnail.png"}, 440 {"title", "Title"}, 441 {"description", "Description"}, 442 {"content_loc", "http://www.example.com/cool_video.mpg"}, 443 {"category", "Category"}, 444 {"tag", []string{"one", "two", "three"}}, 445 }}} 446 smu, _ = NewSitemapURL(&Options{}, data) 447 smu.XML() 448 449 data = URL{{"loc", "/news"}, {"news", URL{ 450 {"publication", URL{ 451 {"name", "Example"}, 452 {"language", "en"}, 453 }}, 454 {"title", "My Article"}, 455 {"keywords", "my article, articles about myself"}, 456 {"stock_tickers", "SAO:PETR3"}, 457 {"publication_date", "2011-08-22"}, 458 {"access", "Subscription"}, 459 {"genres", "PressRelease"}, 460 }}} 461 462 smu, _ = NewSitemapURL(&Options{}, data) 463 smu.XML() 464 } 465 } 466 }