-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknownagents_test.go
More file actions
154 lines (133 loc) · 3.2 KB
/
knownagents_test.go
File metadata and controls
154 lines (133 loc) · 3.2 KB
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
// SPDX-FileCopyrightText: 2024 polykernel
// SPDX-License-Identifier: MIT or Apache-2.0
package caddyknownagents
import (
"fmt"
"slices"
"strings"
"testing"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
)
func TestUnmarshalModule(t *testing.T) {
fmt.Println("Testing unmarshal module... ")
access_token := "aHVudGVyMg=="
config := fmt.Sprintf(`knownagents {
access_token %s
}`, access_token)
dispenser := caddyfile.NewTestDispenser(config)
m := &Knownagents{}
err := m.UnmarshalCaddyfile(dispenser)
if err != nil {
t.Errorf("UnmarshalCaddyfile failed with %v", err)
return
}
expected := access_token
got := m.AccessToken
if expected != got {
t.Errorf("Expected AccessToken to be '%s' but got '%s'", expected, got)
return
}
}
func TestUnmarshalModuleRobotsTxt(t *testing.T) {
fmt.Println("Testing unmarshal module (robots.txt block)... ")
access_token := "aHVudGVyMg=="
agent_types := []string{AIAssistant, AISearchCrawler, AIDataScraper}
var agent_types_str string
{
var b strings.Builder
for i := 0; i < len(agent_types); i++ {
fmt.Fprintf(&b, "%q", agent_types[i])
}
agent_types_str = b.String()
}
disallow := "/"
config := fmt.Sprintf(`knownagents {
access_token %s
robots_txt {
agent_types %s
disallow %s
}
}`, access_token, agent_types_str, disallow)
dispenser := caddyfile.NewTestDispenser(config)
m := &Knownagents{}
err := m.UnmarshalCaddyfile(dispenser)
if err != nil {
t.Errorf("UnmarshalCaddyfile failed with %v", err)
return
}
{
expected := access_token
got := m.AccessToken
if expected != got {
t.Errorf("Expected AccessToken to be '%s' but got '%s'", expected, got)
return
}
}
if m.RobotsTxt == nil {
t.Error("Expected RobotsTxt not to be nil")
return
}
{
expected := agent_types
got := m.RobotsTxt.AgentTypes
if !slices.Equal(expected, got) {
t.Errorf("Expected AccessToken to be '%s' but got '%s'", expected, got)
return
}
}
{
expected := disallow
got := m.RobotsTxt.Disallow
if expected != got {
t.Errorf("Expected Disallow to be '%s' but got '%s'", expected, got)
return
}
}
}
func TestUnmarshalModuleRobotsTxtWildcard(t *testing.T) {
fmt.Println("Testing unmarshal module (robots.txt block + wildcard)... ")
access_token := "aHVudGVyMg=="
disallow := "/"
config := fmt.Sprintf(`knownagents {
access_token %s
robots_txt {
agent_types *
disallow %s
}
}`, access_token, disallow)
dispenser := caddyfile.NewTestDispenser(config)
m := &Knownagents{}
err := m.UnmarshalCaddyfile(dispenser)
if err != nil {
t.Errorf("UnmarshalCaddyfile failed with %v", err)
return
}
{
expected := access_token
got := m.AccessToken
if expected != got {
t.Errorf("Expected AccessToken to be '%s' but got '%s'", expected, got)
return
}
}
if m.RobotsTxt == nil {
t.Error("Expected RobotsTxt not to be nil")
return
}
{
expected := allAgentTypes
got := m.RobotsTxt.AgentTypes
if !slices.Equal(expected, got) {
t.Errorf("Expected AccessToken to be '%s' but got '%s'", expected, got)
return
}
}
{
expected := disallow
got := m.RobotsTxt.Disallow
if expected != got {
t.Errorf("Expected Disallow to be '%s' but got '%s'", expected, got)
return
}
}
}