1
0
Fork 0
mirror of https://github.com/gocsaf/csaf.git synced 2025-12-22 11:55:40 +01:00

Re-started link checker for directory listings

This commit is contained in:
Sascha L. Teichmann 2022-05-16 18:04:15 +02:00
parent d5d48c7d2e
commit 32be3602b6
5 changed files with 114 additions and 28 deletions

33
cmd/csaf_checker/links.go Normal file
View file

@ -0,0 +1,33 @@
// This file is Free Software under the MIT License
// without warranty, see README.md and LICENSES/MIT.txt for details.
//
// SPDX-License-Identifier: MIT
//
// SPDX-FileCopyrightText: 2022 German Federal Office for Information Security (BSI) <https://www.bsi.bund.de>
// Software-Engineering: 2022 Intevation GmbH <https://intevation.de>
package main
import (
"io"
"github.com/PuerkitoBio/goquery"
)
func linksOnPage(r io.Reader) ([]string, error) {
doc, err := goquery.NewDocumentFromReader(r)
if err != nil {
return nil, err
}
var links []string
doc.Find("a").Each(func(i int, s *goquery.Selection) {
if link, ok := s.Attr("href"); ok {
links = append(links, link)
}
})
return links, nil
}

View file

@ -0,0 +1,41 @@
package main
import (
"fmt"
"strings"
"testing"
)
const page0 = `<html>
<body>
<a href="link0">link0</a>
<ol>
<li><a href="link1">link1</a></li>
<li><a href="link2">link2</a></li>
</ol>
<p>
<div>
<li><a href="link3">link3</a></li>
</div>
<p>
</body>
</html>`
func TestLinksOnPage(t *testing.T) {
links, err := linksOnPage(strings.NewReader(page0))
if err != nil {
t.Fatal(err)
}
if l := len(links); l != 4 {
t.Fatalf("Expected 4 links, go %d\n", l)
}
for i, link := range links {
href := fmt.Sprintf("link%d", i)
if href != link {
t.Fatalf("Expected link '%s', got '%s'\n", href, link)
}
}
}