You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sitemap_test.go 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package sitemap
  4. import (
  5. "bytes"
  6. "encoding/xml"
  7. "strings"
  8. "testing"
  9. "time"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestNewSitemap(t *testing.T) {
  13. ts := time.Unix(1651322008, 0).UTC()
  14. tests := []struct {
  15. name string
  16. urls []URL
  17. want string
  18. wantErr string
  19. }{
  20. {
  21. name: "empty",
  22. urls: []URL{},
  23. want: xml.Header + `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">` +
  24. "" +
  25. "</urlset>\n",
  26. },
  27. {
  28. name: "regular",
  29. urls: []URL{
  30. {URL: "https://gitea.io/test1", LastMod: &ts},
  31. },
  32. want: xml.Header + `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">` +
  33. "<url><loc>https://gitea.io/test1</loc><lastmod>2022-04-30T12:33:28Z</lastmod></url>" +
  34. "</urlset>\n",
  35. },
  36. {
  37. name: "without lastmod",
  38. urls: []URL{
  39. {URL: "https://gitea.io/test1"},
  40. },
  41. want: xml.Header + `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">` +
  42. "<url><loc>https://gitea.io/test1</loc></url>" +
  43. "</urlset>\n",
  44. },
  45. {
  46. name: "multiple",
  47. urls: []URL{
  48. {URL: "https://gitea.io/test1", LastMod: &ts},
  49. {URL: "https://gitea.io/test2", LastMod: nil},
  50. },
  51. want: xml.Header + `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">` +
  52. "<url><loc>https://gitea.io/test1</loc><lastmod>2022-04-30T12:33:28Z</lastmod></url>" +
  53. "<url><loc>https://gitea.io/test2</loc></url>" +
  54. "</urlset>\n",
  55. },
  56. {
  57. name: "too many urls",
  58. urls: make([]URL, 50001),
  59. wantErr: "The sitemap contains 50001 URLs, but only 50000 are allowed",
  60. },
  61. {
  62. name: "too big file",
  63. urls: []URL{
  64. {URL: strings.Repeat("b", 50*1024*1024+1)},
  65. },
  66. wantErr: "The sitemap has 52428932 bytes, but only 52428800 are allowed",
  67. },
  68. }
  69. for _, tt := range tests {
  70. t.Run(tt.name, func(t *testing.T) {
  71. s := NewSitemap()
  72. for _, url := range tt.urls {
  73. s.Add(url)
  74. }
  75. buf := &bytes.Buffer{}
  76. _, err := s.WriteTo(buf)
  77. if tt.wantErr != "" {
  78. assert.EqualError(t, err, tt.wantErr)
  79. } else {
  80. assert.NoError(t, err)
  81. assert.Equalf(t, tt.want, buf.String(), "NewSitemap()")
  82. }
  83. })
  84. }
  85. }
  86. func TestNewSitemapIndex(t *testing.T) {
  87. ts := time.Unix(1651322008, 0).UTC()
  88. tests := []struct {
  89. name string
  90. urls []URL
  91. want string
  92. wantErr string
  93. }{
  94. {
  95. name: "empty",
  96. urls: []URL{},
  97. want: xml.Header + `<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">` +
  98. "" +
  99. "</sitemapindex>\n",
  100. },
  101. {
  102. name: "regular",
  103. urls: []URL{
  104. {URL: "https://gitea.io/test1", LastMod: &ts},
  105. },
  106. want: xml.Header + `<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">` +
  107. "<sitemap><loc>https://gitea.io/test1</loc><lastmod>2022-04-30T12:33:28Z</lastmod></sitemap>" +
  108. "</sitemapindex>\n",
  109. },
  110. {
  111. name: "without lastmod",
  112. urls: []URL{
  113. {URL: "https://gitea.io/test1"},
  114. },
  115. want: xml.Header + `<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">` +
  116. "<sitemap><loc>https://gitea.io/test1</loc></sitemap>" +
  117. "</sitemapindex>\n",
  118. },
  119. {
  120. name: "multiple",
  121. urls: []URL{
  122. {URL: "https://gitea.io/test1", LastMod: &ts},
  123. {URL: "https://gitea.io/test2", LastMod: nil},
  124. },
  125. want: xml.Header + `<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">` +
  126. "<sitemap><loc>https://gitea.io/test1</loc><lastmod>2022-04-30T12:33:28Z</lastmod></sitemap>" +
  127. "<sitemap><loc>https://gitea.io/test2</loc></sitemap>" +
  128. "</sitemapindex>\n",
  129. },
  130. {
  131. name: "too many sitemaps",
  132. urls: make([]URL, 50001),
  133. wantErr: "The sitemap contains 50001 sub-sitemaps, but only 50000 are allowed",
  134. },
  135. {
  136. name: "too big file",
  137. urls: []URL{
  138. {URL: strings.Repeat("b", 50*1024*1024+1)},
  139. },
  140. wantErr: "The sitemap has 52428952 bytes, but only 52428800 are allowed",
  141. },
  142. }
  143. for _, tt := range tests {
  144. t.Run(tt.name, func(t *testing.T) {
  145. s := NewSitemapIndex()
  146. for _, url := range tt.urls {
  147. s.Add(url)
  148. }
  149. buf := &bytes.Buffer{}
  150. _, err := s.WriteTo(buf)
  151. if tt.wantErr != "" {
  152. assert.EqualError(t, err, tt.wantErr)
  153. } else {
  154. assert.NoError(t, err)
  155. assert.Equalf(t, tt.want, buf.String(), "NewSitemapIndex()")
  156. }
  157. })
  158. }
  159. }