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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
package ogtags
import (
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
)
func TestIntegrationGetOGTags(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
switch r.URL.Path {
case "/simple":
w.Write([]byte(`
<!DOCTYPE html>
<html>
<head>
<meta property="og:title" content="Simple Page" />
<meta property="og:type" content="website" />
</head>
<body><p>Simple page content</p></body>
</html>
`))
case "/complete":
w.Write([]byte(`
<!DOCTYPE html>
<html>
<head>
<meta property="og:title" content="Complete Page" />
<meta property="og:description" content="A page with many OG tags" />
<meta property="og:image" content="http://example.com/image.jpg" />
<meta property="og:url" content="http://example.com/complete" />
<meta property="og:type" content="article" />
</head>
<body><p>Complete page content</p></body>
</html>
`))
case "/no-og":
w.Write([]byte(`
<!DOCTYPE html>
<html>
<head>
<title>No OG Tags</title>
</head>
<body><p>No OG tags here</p></body>
</html>
`))
default:
w.WriteHeader(http.StatusNotFound)
}
}))
defer ts.Close()
// Test with different configurations
testCases := []struct {
name string
path string
query string
expectedTags map[string]string
expectError bool
}{
{
name: "Simple page",
path: "/simple",
query: "",
expectedTags: map[string]string{
"og:title": "Simple Page",
"og:type": "website",
},
expectError: false,
},
{
name: "Complete page",
path: "/complete",
query: "ref=test",
expectedTags: map[string]string{
"og:title": "Complete Page",
"og:description": "A page with many OG tags",
"og:image": "http://example.com/image.jpg",
"og:url": "http://example.com/complete",
"og:type": "article",
},
expectError: false,
},
{
name: "Page with no OG tags",
path: "/no-og",
query: "",
expectedTags: map[string]string{},
expectError: false,
},
{
name: "Non-existent page",
path: "/not-found",
query: "",
expectedTags: nil,
expectError: false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Create cache instance
cache := NewOGTagCache(ts.URL, true, 1*time.Minute)
// Create URL for test
testURL, _ := url.Parse(ts.URL)
testURL.Path = tc.path
testURL.RawQuery = tc.query
// Get OG tags
ogTags, err := cache.GetOGTags(testURL)
// Check error expectation
if tc.expectError {
if err == nil {
t.Error("expected error, got nil")
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Verify all expected tags are present
for key, expectedValue := range tc.expectedTags {
if value, ok := ogTags[key]; !ok || value != expectedValue {
t.Errorf("expected %s: %s, got: %s", key, expectedValue, value)
}
}
// Verify no extra tags are present
if len(ogTags) != len(tc.expectedTags) {
t.Errorf("expected %d tags, got %d", len(tc.expectedTags), len(ogTags))
}
// Test cache retrieval
cachedOGTags, err := cache.GetOGTags(testURL)
if err != nil {
t.Fatalf("failed to get OG tags from cache: %v", err)
}
// Verify cached tags match
for key, expectedValue := range tc.expectedTags {
if value, ok := cachedOGTags[key]; !ok || value != expectedValue {
t.Errorf("cached value - expected %s: %s, got: %s", key, expectedValue, value)
}
}
})
}
}
|