code.gitea.io/gitea@v1.19.3/modules/sitemap/sitemap_test.go (about)

     1  // Copyright 2022 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package sitemap
     5  
     6  import (
     7  	"bytes"
     8  	"encoding/xml"
     9  	"strings"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    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  		},
    73  	}
    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  		})
    89  	}
    90  }
    91  
    92  func TestNewSitemapIndex(t *testing.T) {
    93  	ts := time.Unix(1651322008, 0).UTC()
    94  
    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",
   107  		},
   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",
   116  		},
   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",
   148  		},
   149  	}
   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  	}
   166  }