// Copyright (c) 2026 Probo Inc . // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. package browser import ( "strings" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestParseSitemapXML(t *testing.T) { t.Parallel() t.Run( "valid urlset with multiple URLs", func(t *testing.T) { t.Parallel() xml := ` https://example.com/ https://example.com/about https://example.com/contact ` urls, err := parseSitemapXML(strings.NewReader(xml)) require.NoError(t, err) require.Len(t, urls, 3) assert.Equal(t, "https://example.com/", urls[0]) assert.Equal(t, "https://example.com/about", urls[1]) assert.Equal(t, "https://example.com/contact", urls[2]) }, ) t.Run( "valid sitemapindex with sitemap locations", func(t *testing.T) { t.Parallel() xml := ` https://example.com/sitemap-pages.xml https://example.com/sitemap-posts.xml ` urls, err := parseSitemapXML(strings.NewReader(xml)) require.NoError(t, err) require.Len(t, urls, 2) assert.Equal(t, "https://example.com/sitemap-pages.xml", urls[0]) assert.Equal(t, "https://example.com/sitemap-posts.xml", urls[1]) }, ) t.Run( "empty urlset returns empty slice", func(t *testing.T) { t.Parallel() xml := ` ` urls, err := parseSitemapXML(strings.NewReader(xml)) require.NoError(t, err) assert.Empty(t, urls) }, ) t.Run( "malformed XML returns error", func(t *testing.T) { t.Parallel() xml := `https://example.com/ https://example.com/page1 https://example.com/page2 ` urls, err := parseSitemapXML(strings.NewReader(xml)) require.NoError(t, err) require.Len(t, urls, 2) assert.Equal(t, "https://example.com/page1", urls[0]) assert.Equal(t, "https://example.com/page2", urls[1]) }, ) t.Run( "trims whitespace in loc elements", func(t *testing.T) { t.Parallel() xml := ` https://example.com/padded ` urls, err := parseSitemapXML(strings.NewReader(xml)) require.NoError(t, err) require.Len(t, urls, 1) assert.Equal(t, "https://example.com/padded", urls[0]) }, ) t.Run( "skips empty loc elements", func(t *testing.T) { t.Parallel() xml := ` https://example.com/valid ` urls, err := parseSitemapXML(strings.NewReader(xml)) require.NoError(t, err) require.Len(t, urls, 1) assert.Equal(t, "https://example.com/valid", urls[0]) }, ) t.Run( "empty reader returns empty slice", func(t *testing.T) { t.Parallel() urls, err := parseSitemapXML(strings.NewReader("")) require.NoError(t, err) assert.Empty(t, urls) }, ) t.Run( "urlset with additional elements besides loc", func(t *testing.T) { t.Parallel() xml := ` https://example.com/page 2024-01-01 weekly 0.8 ` urls, err := parseSitemapXML(strings.NewReader(xml)) require.NoError(t, err) require.Len(t, urls, 1) assert.Equal(t, "https://example.com/page", urls[0]) }, ) }