summaryrefslogtreecommitdiff
path: root/internal/links/find_count_test.go
diff options
context:
space:
mode:
authordemo <demo@antix1>2026-05-10 22:47:08 -0400
committerdemo <demo@antix1>2026-05-10 22:47:08 -0400
commit3bfb19098c69a6b810a2a4e478f4184420bf4200 (patch)
tree4955faa3a8f914d52b1281f1d2680d27a7de2c02 /internal/links/find_count_test.go
parent8c6e2780beb8e295c309eab503a18c9058a8cb8b (diff)
feat: resolve hrefs according to a base URL
Diffstat (limited to 'internal/links/find_count_test.go')
-rw-r--r--internal/links/find_count_test.go76
1 files changed, 76 insertions, 0 deletions
diff --git a/internal/links/find_count_test.go b/internal/links/find_count_test.go
new file mode 100644
index 0000000..2932bf9
--- /dev/null
+++ b/internal/links/find_count_test.go
@@ -0,0 +1,76 @@
+package links
+
+import (
+ "fmt"
+ "log"
+ "net/url"
+ "strings"
+ "testing"
+)
+
+type exampleType struct {
+ expectedCount int
+ rawBaseURL string
+ content string
+}
+
+var examples = []exampleType{
+ // Example 1
+ {2, "https://example.com", `
+<html>c
+ <head>
+ <title>Ex 1</title>
+ <head>
+ <body>
+ <a href="https://example.com/">Example Page</a>
+ <a href="/posts">Posts</a>
+ </body>
+</html>
+`},
+
+ // Example 2
+ {4, "https://example.com", `<html>
+ <head>
+ <title>Ex 2</title>
+ <head>
+ <body>
+ <a href="https://example.com/">Example Page</a>
+ <a href="/posts">Posts</a>
+ <a href="#">A <a href="#">rouge</a> link!</a>
+ </body>
+</html>
+`},
+
+ // Example 3
+ {2, "https://example.com", `<html>
+<a href="https://example.com">Main Page</a>
+<a href="/example1">Example 1</a>
+<a href="https://brandonirizarry.xyz">Brandon's Blog</a>
+<a href="https://brandonirizarry.xyz/post1">Post 1</a>
+<a href=":foo">Bad link</a>
+</html>
+`},
+}
+
+func TestFindCountHrefs(t *testing.T) {
+ for i, ex := range examples {
+ name := fmt.Sprintf("Example %d", i+1)
+ baseURL, err := url.Parse(ex.rawBaseURL)
+ if err != nil {
+ t.Fatalf("can't parse %s: %v", ex.rawBaseURL, err)
+ }
+
+ t.Run(name, func(t *testing.T) {
+ r := strings.NewReader(ex.content)
+ hrefs, err := Parse(r, baseURL)
+ if err != nil {
+ t.Error(err)
+ }
+
+ if actualCount := len(hrefs); actualCount != ex.expectedCount {
+ t.Errorf("got %d, want %d", actualCount, ex.expectedCount)
+ log.Print(hrefs)
+ }
+ })
+ }
+}