summaryrefslogtreecommitdiff
path: root/internal/platforms/linkedin/preview_test.go
blob: f65da633d463978b22ce8d8a6a6b05ce36a3987f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package linkedin

import (
	"context"
	"net/http"
	"net/http/httptest"
	"path/filepath"
	"strings"
	"testing"

	"codeberg.org/snonux/gos/internal/config"
)

func TestPreviewExtract(t *testing.T) {
	var (
		expectedTitle    = "Baz"
		expectedImageURL = "https://free.beer:666/bar/foo.jpg"
		mockHTML         = strings.NewReader(`
<!DOCTYPE html>
<html>
<head>
    <title>Baz</title>
</head>
<body>
    <img src="./foo.jpg" alt="Foo">
</body>
</html>
`)
	)

	title, imageURL, err := extract("https://free.beer:666/bar/", mockHTML)
	if err != nil {
		t.Error(err)
	}
	if title != expectedTitle {
		t.Errorf("expected title '%s' but got '%s'", expectedTitle, title)
	}
	if imageURL != expectedImageURL {
		t.Errorf("expected imageURL '%s' but got '%s'", expectedImageURL, imageURL)
	}
}

func TestNewPreviewIgnoresForbiddenPage(t *testing.T) {
	t.Parallel()

	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		http.Error(w, "blocked", http.StatusForbidden)
	}))
	defer srv.Close()

	prev, err := NewPreview(context.Background(), config.Args{
		CacheDir: t.TempDir(),
	}, []string{srv.URL})
	if err != nil {
		t.Fatalf("expected preview fetch failure to be non-fatal, got %v", err)
	}

	title, sourceURL, ok := prev.TitleAndURL()
	if !ok {
		t.Fatal("expected preview to keep URL fallback")
	}
	if title != srv.URL {
		t.Fatalf("expected fallback title %q, got %q", srv.URL, title)
	}
	if sourceURL != srv.URL {
		t.Fatalf("expected source URL %q, got %q", srv.URL, sourceURL)
	}
	if _, ok := prev.Thumbnail(); ok {
		t.Fatal("expected no thumbnail for forbidden preview page")
	}
}

func TestNewPreviewIgnoresForbiddenImage(t *testing.T) {
	t.Parallel()

	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		switch r.URL.Path {
		case "/":
			_, _ = w.Write([]byte(`
<!DOCTYPE html>
<html>
<head>
    <title>Blocked image</title>
</head>
<body>
    <img src="/blocked.jpg" alt="blocked">
</body>
</html>
`))
		case "/blocked.jpg":
			http.Error(w, "blocked", http.StatusForbidden)
		default:
			http.NotFound(w, r)
		}
	}))
	defer srv.Close()

	cacheDir := t.TempDir()
	prev, err := NewPreview(context.Background(), config.Args{
		CacheDir: cacheDir,
	}, []string{srv.URL})
	if err != nil {
		t.Fatalf("expected image download failure to be non-fatal, got %v", err)
	}

	title, sourceURL, ok := prev.TitleAndURL()
	if !ok {
		t.Fatal("expected preview title and URL to be preserved")
	}
	if title != "Blocked image" {
		t.Fatalf("expected title %q, got %q", "Blocked image", title)
	}
	if sourceURL != srv.URL {
		t.Fatalf("expected source URL %q, got %q", srv.URL, sourceURL)
	}
	if thumbnailPath, ok := prev.Thumbnail(); ok {
		t.Fatalf("expected no thumbnail after blocked download, got %q", thumbnailPath)
	}

	matches, err := filepath.Glob(filepath.Join(cacheDir, "*"))
	if err != nil {
		t.Fatalf("glob failed: %v", err)
	}
	if len(matches) != 0 {
		t.Fatalf("expected cache dir to stay empty, got %v", matches)
	}
}