sitemap_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package sitemap
  2. import (
  3. "context"
  4. "io/ioutil"
  5. "net/http"
  6. "net/http/httptest"
  7. "net/url"
  8. "testing"
  9. )
  10. func TestRegisterRawString(t *testing.T) {
  11. s := SiteMap().RegisterRawString("/admin").EncodeToXml(context.TODO())
  12. expected := `<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>/admin</loc></url></urlset>`
  13. if s != expected {
  14. t.Errorf("\n\tExpected value: %s\n \tbut got: %s", expected, s)
  15. }
  16. }
  17. func TestRegisterURL(t *testing.T) {
  18. s := SiteMap().RegisterURL(URL{Loc: "/admin"}).EncodeToXml(context.TODO())
  19. expected := `<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>/admin</loc></url></urlset>`
  20. if s != expected {
  21. t.Errorf("\n\tExpected value: %s\n \tbut got: %s", expected, s)
  22. }
  23. }
  24. func TestRegisterURLAndRawString(t *testing.T) {
  25. s := SiteMap().RegisterRawString("/admin1").RegisterURL(URL{Loc: "/admin2"}).EncodeToXml(context.TODO())
  26. expected := `<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>/admin1</loc></url><url><loc>/admin2</loc></url></urlset>`
  27. if s != expected {
  28. t.Errorf("\n\tExpected value: %s\n \tbut got: %s", expected, s)
  29. }
  30. }
  31. func TestRegisterContextFunc(t *testing.T) {
  32. s := SiteMap().RegisterContextFunc(func(context.Context) []URL {
  33. return []URL{{Loc: "/admin1"}, {Loc: "/admin2"}}
  34. }).EncodeToXml(context.TODO())
  35. expected := `<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>/admin1</loc></url><url><loc>/admin2</loc></url></urlset>`
  36. if s != expected {
  37. t.Errorf("\n\tExpected value: %s\n \tbut got: %s", expected, s)
  38. }
  39. }
  40. type post struct {
  41. }
  42. func (p post) Sitemap(ctx context.Context) []URL {
  43. return []URL{
  44. {Loc: "/post1"},
  45. {Loc: "/post2"},
  46. }
  47. }
  48. func TestRegisterModel(t *testing.T) {
  49. s := SiteMap().RegisterModel(post{}).EncodeToXml(context.TODO())
  50. expected := `<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>/post1</loc></url><url><loc>/post2</loc></url></urlset>`
  51. if s != expected {
  52. t.Errorf("\n\tExpected value: %s\n \tbut got: %s", expected, s)
  53. }
  54. }
  55. func TestSiteMapIndex(t *testing.T) {
  56. s := SiteMapIndex().RegisterSiteMap(SiteMap(), SiteMap("product"), SiteMap("admin")).EncodeToXml(context.TODO())
  57. expected := `<?xml version="1.0" encoding="UTF-8"?><sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><sitemap><loc>/sitemap.xml</loc></sitemap><sitemap><loc>/product.xml</loc></sitemap><sitemap><loc>/admin.xml</loc></sitemap></sitemapindex>`
  58. if s != expected {
  59. t.Errorf("\n\tExpected value: %s\n \tbut got: %s", expected, s)
  60. }
  61. }
  62. func TestEncodeToXmlWithContext(t *testing.T) {
  63. s := SiteMap().RegisterRawString("/admin", "https://qor5-1.dev.com/product").EncodeToXml(WithHost("https://qor5.dev.com"))
  64. expected := `<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>https:/qor5.dev.com/admin</loc></url><url><loc>https://qor5-1.dev.com/product</loc></url></urlset>`
  65. if s != expected {
  66. t.Errorf("\n\tExpected value: %s\n \tbut got: %s", expected, s)
  67. }
  68. }
  69. func TestRequestHost(t *testing.T) {
  70. u, _ := url.Parse("https://qor5.dev.com/sitemap.xml")
  71. s := EncodeToXmlByRequest(&http.Request{URL: u}, SiteMap().RegisterRawString("/admin", "https://qor5-1.dev.com/product"))
  72. expected := `<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>https:/qor5.dev.com/admin</loc></url><url><loc>https://qor5-1.dev.com/product</loc></url></urlset>`
  73. if s != expected {
  74. t.Errorf("\n\tExpected value: %s\n \tbut got: %s", expected, s)
  75. }
  76. }
  77. func TestServeHTTP(t *testing.T) {
  78. site := SiteMap().RegisterRawString("/admin", "https://qor5-1.dev.com/product")
  79. serveMux := http.NewServeMux()
  80. site.MountTo(serveMux)
  81. server := httptest.NewServer(serveMux)
  82. resp, err := http.Get(server.URL + "/sitemap.xml")
  83. if err != nil {
  84. t.Error(err)
  85. }
  86. s, err := ioutil.ReadAll(resp.Body)
  87. if err != nil {
  88. t.Error(err)
  89. }
  90. expected := `<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>/admin</loc></url><url><loc>https://qor5-1.dev.com/product</loc></url></urlset>`
  91. if string(s) != expected {
  92. t.Errorf("\n\tExpected value: %s\n \tbut got: %s", expected, s)
  93. }
  94. }