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
|
package ogtags
import (
"net/url"
"testing"
"time"
)
func TestNewOGTagCache(t *testing.T) {
tests := []struct {
name string
target string
ogPassthrough bool
ogTimeToLive time.Duration
}{
{
name: "Basic initialization",
target: "http://example.com",
ogPassthrough: true,
ogTimeToLive: 5 * time.Minute,
},
{
name: "Empty target",
target: "",
ogPassthrough: false,
ogTimeToLive: 10 * time.Minute,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cache := NewOGTagCache(tt.target, tt.ogPassthrough, tt.ogTimeToLive)
if cache == nil {
t.Fatal("expected non-nil cache, got nil")
}
if cache.target != tt.target {
t.Errorf("expected target %s, got %s", tt.target, cache.target)
}
if cache.ogPassthrough != tt.ogPassthrough {
t.Errorf("expected ogPassthrough %v, got %v", tt.ogPassthrough, cache.ogPassthrough)
}
if cache.ogTimeToLive != tt.ogTimeToLive {
t.Errorf("expected ogTimeToLive %v, got %v", tt.ogTimeToLive, cache.ogTimeToLive)
}
})
}
}
func TestGetTarget(t *testing.T) {
tests := []struct {
name string
target string
path string
query string
expected string
}{
{
name: "No path or query",
target: "http://example.com",
path: "",
query: "",
expected: "http://example.com",
},
{
name: "With complex path",
target: "http://example.com",
path: "/pag(#*((#@)ΓΓΓΓe/Γ",
query: "id=123",
expected: "http://example.com/pag(#*((#@)ΓΓΓΓe/Γ",
},
{
name: "With query and path",
target: "http://example.com",
path: "/page",
query: "id=123",
expected: "http://example.com/page",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cache := NewOGTagCache(tt.target, false, time.Minute)
u := &url.URL{
Path: tt.path,
RawQuery: tt.query,
}
result := cache.getTarget(u)
if result != tt.expected {
t.Errorf("expected %s, got %s", tt.expected, result)
}
})
}
}
|