-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudflared_test.go
60 lines (58 loc) · 1.42 KB
/
cloudflared_test.go
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
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
func TestIsCloudflareAccessURL(t *testing.T) {
expectHeaders := map[string]string{}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
for k, v := range expectHeaders {
w.Header().Add(k, v)
}
fmt.Fprintf(w, "")
}))
defer ts.Close()
tsURL, err := url.Parse(ts.URL)
if err != nil {
t.Errorf("%s", err)
return
}
tests := []struct {
expectHeaders map[string]string
expectIsAccessApp bool
expectError bool
}{
{
expectHeaders: map[string]string{
"Location": "https://example.cloudflareaccess.com/cdn-cgi/access/login/example.com?kid=123&redirect_url=%2Fv1%2Fcert.pem&meta=j.w.t",
"CF-Access-Aud": "123",
"CF-Access-Domain": "example.com",
},
expectIsAccessApp: true,
expectError: false,
},
{
expectHeaders: map[string]string{
"Location": "https://example.com",
},
expectIsAccessApp: false,
expectError: false,
},
}
for _, test := range tests {
expectHeaders = test.expectHeaders
isAccessApp, _, err := getCloudflareAccessAppInfo(tsURL)
if !test.expectError && err != nil {
t.Errorf("Unexpected error: %s", err)
}
if test.expectError && err == nil {
t.Errorf("Expected error but got none")
}
if test.expectIsAccessApp && !isAccessApp {
t.Errorf("Expected isAccessApp=true but got false")
}
}
}