36 lines
1.0 KiB
Go
36 lines
1.0 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"crypto/x509"
|
|
"net"
|
|
"net/url"
|
|
"strings"
|
|
"syscall"
|
|
"testing"
|
|
)
|
|
|
|
func TestSubscriptionDownloadErrorsExplainCauseWithoutURL(t *testing.T) {
|
|
for _, test := range []struct {
|
|
err error
|
|
phrase string
|
|
}{
|
|
{x509.UnknownAuthorityError{}, "CA 证书包"},
|
|
{x509.HostnameError{}, "域名不匹配"},
|
|
{x509.CertificateInvalidError{}, "证书校验失败"},
|
|
{&net.DNSError{Err: "no such host", Name: "private.example"}, "域名解析失败"},
|
|
{context.DeadlineExceeded, "下载超时"},
|
|
{context.Canceled, "已取消"},
|
|
{syscall.ECONNREFUSED, "连接被拒绝"},
|
|
} {
|
|
wrapped := &url.Error{Op: "Get", URL: "https://private.example/sub?token=secret-token", Err: test.err}
|
|
message := subscriptionDownloadError(wrapped).Error()
|
|
if !strings.Contains(message, test.phrase) {
|
|
t.Fatalf("cause not explained: %s", message)
|
|
}
|
|
if strings.Contains(message, "secret-token") || strings.Contains(message, "private.example") {
|
|
t.Fatal("download error exposed subscription URL")
|
|
}
|
|
}
|
|
}
|