-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathvalidations_test.go
More file actions
213 lines (188 loc) · 5.92 KB
/
validations_test.go
File metadata and controls
213 lines (188 loc) · 5.92 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package publiccode
import (
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
func TestGetBasicAuth(t *testing.T) {
d := Domain{BasicAuth: []string{"user:pass"}}
result := getBasicAuth(d)
if !strings.HasPrefix(result, "Basic ") {
t.Errorf("expected 'Basic ' prefix, got %q", result)
}
}
func TestGetBasicAuthEmpty(t *testing.T) {
d := Domain{}
result := getBasicAuth(d)
if result != "" {
t.Errorf("expected empty string, got %q", result)
}
}
func TestIsHostInDomainTrue(t *testing.T) {
d := Domain{UseTokenFor: []string{"example.com"}}
if !isHostInDomain(d, "https://example.com/foo") {
t.Error("expected true")
}
}
func TestIsHostInDomainFalse(t *testing.T) {
d := Domain{UseTokenFor: []string{"example.com"}}
if isHostInDomain(d, "https://other.com/foo") {
t.Error("expected false")
}
}
func TestIsHostInDomainEmptyUseTokenFor(t *testing.T) {
d := Domain{}
if isHostInDomain(d, "https://example.com") {
t.Error("expected false for empty UseTokenFor")
}
}
func TestIsHostInDomainInvalidURL(t *testing.T) {
d := Domain{UseTokenFor: []string{"example.com"}}
if isHostInDomain(d, "not-a-url") {
t.Error("expected false for relative URL with no host")
}
}
func TestIsHostInDomainMalformedURL(t *testing.T) {
d := Domain{UseTokenFor: []string{"example.com"}}
// "%" is a truly malformed URL that causes url.Parse to fail
if isHostInDomain(d, "%") {
t.Error("expected false for malformed URL")
}
}
func TestGetHeaderFromDomainNoMatch(t *testing.T) {
d := Domain{UseTokenFor: []string{"example.com"}}
headers := getHeaderFromDomain(d, "https://other.com/foo")
if headers != nil {
t.Error("expected nil headers when host not in domain")
}
}
func TestGetHeaderFromDomainMatch(t *testing.T) {
d := Domain{UseTokenFor: []string{"example.com"}, BasicAuth: []string{"user:pass"}}
headers := getHeaderFromDomain(d, "https://example.com/foo")
if headers == nil {
t.Fatal("expected non-nil headers")
}
if _, ok := headers["Authorization"]; !ok {
t.Error("expected Authorization header")
}
}
func TestIsReachableMissingScheme(t *testing.T) {
p, _ := NewParser(ParserConfig{DisableNetwork: true})
_, err := p.isReachable(url.URL{Scheme: "", Host: "example.com", Path: "/"})
if err == nil {
t.Fatal("expected error for missing scheme")
}
if !strings.Contains(err.Error(), "missing URL scheme") {
t.Errorf("unexpected error: %v", err)
}
}
func TestIsReachableNon200(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "not found", http.StatusNotFound)
}))
defer srv.Close()
p, _ := NewParser(ParserConfig{})
parsed, _ := url.Parse(srv.URL + "/path")
reachable, err := p.isReachable(*parsed)
if reachable {
t.Error("expected not reachable for 404 response")
}
if err == nil {
t.Error("expected error for non-200 response")
}
}
func TestIsImageFileInvalidExtension(t *testing.T) {
p, _ := NewParser(ParserConfig{DisableNetwork: true})
u := url.URL{Scheme: "file", Path: "/tmp/test.gif"}
ok, err := p.isImageFile(u, false)
if ok {
t.Error("expected false for .gif extension")
}
if err == nil {
t.Error("expected error for invalid extension")
}
}
func TestIsImageFileValidExtensionMissing(t *testing.T) {
p, _ := NewParser(ParserConfig{DisableNetwork: true})
u := url.URL{Scheme: "file", Path: "/nonexistent/test.png"}
ok, err := p.isImageFile(u, false)
if ok {
t.Error("expected false for nonexistent file")
}
if err == nil {
t.Error("expected error for nonexistent file")
}
}
func TestValidLogoInvalidExtension(t *testing.T) {
p, _ := NewParser(ParserConfig{DisableNetwork: true})
u := url.URL{Scheme: "file", Path: "/tmp/test.gif"}
ok, err := p.validLogo(u, false)
if ok {
t.Error("expected false for .gif extension")
}
if err == nil {
t.Error("expected error for invalid extension")
}
}
func TestValidLogoMissingFile(t *testing.T) {
p, _ := NewParser(ParserConfig{DisableNetwork: true})
u := url.URL{Scheme: "file", Path: "/nonexistent/logo.svg"}
ok, err := p.validLogo(u, false)
if ok {
t.Error("expected false for nonexistent file")
}
if err == nil {
t.Error("expected error for nonexistent file")
}
}
func TestValidLogoRemoteNoNetwork(t *testing.T) {
// Remote URL but network disabled: validLogo returns true for svg without downloading.
p, _ := NewParser(ParserConfig{DisableNetwork: true})
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
defer srv.Close()
parsed, _ := url.Parse(srv.URL + "/logo.svg")
// network=false: fileExists returns true for non-file scheme, and validLogo skips download
ok, err := p.validLogo(*parsed, false)
if !ok {
t.Errorf("expected true for remote SVG with network=false (no download): %v", err)
}
}
func TestValidLogoRemoteDownloadFailure(t *testing.T) {
// Remote PNG URL that is reachable (200) but returns non-PNG data.
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.Contains(r.URL.Path, "logo.png") {
// Return 200 but with invalid PNG content
w.Header().Set("Content-Type", "image/png")
_, _ = w.Write([]byte("not a valid PNG file at all"))
} else {
w.WriteHeader(http.StatusOK)
}
}))
defer srv.Close()
p, _ := NewParser(ParserConfig{})
parsed, _ := url.Parse(srv.URL + "/logo.png")
ok, err := p.validLogo(*parsed, true)
// The downloaded file is not a valid PNG, so DecodeConfig should fail.
if ok {
t.Error("expected false for invalid PNG content")
}
if err == nil {
t.Error("expected error for invalid PNG content")
}
}
func TestIsReachableSuccess(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
defer srv.Close()
p, _ := NewParser(ParserConfig{})
parsed, _ := url.Parse(srv.URL + "/path")
reachable, err := p.isReachable(*parsed)
if !reachable {
t.Errorf("expected reachable for 200 response, got err: %v", err)
}
}