package starnet import ( "context" "crypto/tls" "errors" "fmt" "net" "net/url" "testing" ) type timeoutErr struct{} func (timeoutErr) Error() string { return "i/o timeout" } func (timeoutErr) Timeout() bool { return true } func (timeoutErr) Temporary() bool { return true } func TestIsTimeout(t *testing.T) { if !IsTimeout(context.DeadlineExceeded) { t.Fatal("context deadline should be timeout") } uerr := &url.Error{ Op: "Get", URL: "http://example.com", Err: timeoutErr{}, } if !IsTimeout(uerr) { t.Fatal("url timeout error should be timeout") } if !IsTimeout(fmt.Errorf("wrapped: %w", uerr)) { t.Fatal("wrapped timeout should be timeout") } if IsTimeout(errors.New("plain error")) { t.Fatal("plain error must not be timeout") } } func TestIsDNS(t *testing.T) { dnsErr := &net.DNSError{ Err: "no such host", Name: "example.invalid", IsNotFound: true, } if !IsDNS(dnsErr) { t.Fatal("dns error should be dns") } if !IsDNS(fmt.Errorf("wrapped: %w", dnsErr)) { t.Fatal("wrapped dns error should be dns") } if !IsDNS(errors.New("lookup example.invalid: no such host")) { t.Fatal("lookup no such host should be dns") } if IsDNS(errors.New("connection reset by peer")) { t.Fatal("non dns error should not be dns") } } func TestIsTLS(t *testing.T) { tlsErr := tls.RecordHeaderError{Msg: "first record does not look like a TLS handshake"} if !IsTLS(tlsErr) { t.Fatal("tls record header error should be tls") } if !IsTLS(fmt.Errorf("wrapped: %w", tlsErr)) { t.Fatal("wrapped tls error should be tls") } if !IsTLS(errors.New("x509: certificate signed by unknown authority")) { t.Fatal("x509 error text should be tls") } if !IsTLS(ErrNotTLS) { t.Fatal("ErrNotTLS should be tls related") } if IsTLS(errors.New("plain error")) { t.Fatal("plain error should not be tls") } } func TestIsProxy(t *testing.T) { raw := errors.New("proxyconnect tcp: dial tcp 127.0.0.1:8080: connect: connection refused") if !IsProxy(raw) { t.Fatal("proxyconnect error should be proxy") } uerr := &url.Error{ Op: "Get", URL: "http://example.com", Err: raw, } if !IsProxy(uerr) { t.Fatal("wrapped proxy error should be proxy") } opErr := &net.OpError{ Op: "proxyconnect", Net: "tcp", Err: errors.New("connect failed"), } if !IsProxy(opErr) { t.Fatal("net.OpError proxyconnect should be proxy") } if IsProxy(errors.New("dial tcp 127.0.0.1:8080: connect: connection refused")) { t.Fatal("non proxy dial error should not be proxy") } }