diff --git a/internal/api/v1/auth/oidc_test.go b/internal/api/v1/auth/oidc_test.go index 4688dba..f249e8f 100644 --- a/internal/api/v1/auth/oidc_test.go +++ b/internal/api/v1/auth/oidc_test.go @@ -14,7 +14,6 @@ import ( "github.com/coreos/go-oidc/v3/oidc" "github.com/golang-jwt/jwt/v5" "github.com/yusing/go-proxy/internal/common" - E "github.com/yusing/go-proxy/internal/error" "golang.org/x/oauth2" . "github.com/yusing/go-proxy/internal/utils/testing" @@ -227,7 +226,7 @@ func TestOIDCCallbackHandler(t *testing.T) { } if tt.wantStatus == http.StatusTemporaryRedirect { - setCookie := E.Must(http.ParseSetCookie(w.Header().Get("Set-Cookie"))) + setCookie := Must(http.ParseSetCookie(w.Header().Get("Set-Cookie"))) ExpectEqual(t, setCookie.Name, defaultAuth.TokenCookieName()) ExpectTrue(t, setCookie.Value != "") ExpectEqual(t, setCookie.Path, "/") diff --git a/internal/api/v1/auth/userpass_test.go b/internal/api/v1/auth/userpass_test.go index c43360e..9a9fbc4 100644 --- a/internal/api/v1/auth/userpass_test.go +++ b/internal/api/v1/auth/userpass_test.go @@ -9,7 +9,6 @@ import ( "testing" "time" - E "github.com/yusing/go-proxy/internal/error" . "github.com/yusing/go-proxy/internal/utils/testing" "golang.org/x/crypto/bcrypt" ) @@ -17,7 +16,7 @@ import ( func newMockUserPassAuth() *UserPassAuth { return &UserPassAuth{ username: "username", - pwdHash: E.Must(bcrypt.GenerateFromPassword([]byte("password"), bcrypt.DefaultCost)), + pwdHash: Must(bcrypt.GenerateFromPassword([]byte("password"), bcrypt.DefaultCost)), secret: []byte("abcdefghijklmnopqrstuvwxyz"), tokenTTL: time.Hour, } @@ -97,13 +96,13 @@ func TestUserPassLoginCallbackHandler(t *testing.T) { w := httptest.NewRecorder() req := &http.Request{ Host: "app.example.com", - Body: io.NopCloser(bytes.NewReader(E.Must(json.Marshal(tt.creds)))), + Body: io.NopCloser(bytes.NewReader(Must(json.Marshal(tt.creds)))), } auth.LoginCallbackHandler(w, req) if tt.wantErr { ExpectEqual(t, w.Code, http.StatusUnauthorized) } else { - setCookie := E.Must(http.ParseSetCookie(w.Header().Get("Set-Cookie"))) + setCookie := Must(http.ParseSetCookie(w.Header().Get("Set-Cookie"))) ExpectTrue(t, setCookie.Name == auth.TokenCookieName()) ExpectTrue(t, setCookie.Value != "") ExpectEqual(t, setCookie.Domain, "example.com") diff --git a/internal/error/utils.go b/internal/error/utils.go index 5d987fb..e4440c2 100644 --- a/internal/error/utils.go +++ b/internal/error/utils.go @@ -40,13 +40,6 @@ func From(err error) Error { return &baseError{err} } -func Must[T any](v T, err error) T { - if err != nil { - LogPanic("must failed", err) - } - return v -} - func Join(errors ...error) Error { n := 0 for _, err := range errors { diff --git a/internal/net/http/accesslog/access_logger_test.go b/internal/net/http/accesslog/access_logger_test.go index bac8db8..2398ca7 100644 --- a/internal/net/http/accesslog/access_logger_test.go +++ b/internal/net/http/accesslog/access_logger_test.go @@ -9,7 +9,6 @@ import ( "testing" "time" - E "github.com/yusing/go-proxy/internal/error" . "github.com/yusing/go-proxy/internal/net/http/accesslog" "github.com/yusing/go-proxy/internal/task" . "github.com/yusing/go-proxy/internal/utils/testing" @@ -30,7 +29,7 @@ const ( var ( testTask = task.RootTask("test", false) - testURL = E.Must(url.Parse("http://" + host + uri)) + testURL = Must(url.Parse("http://" + host + uri)) req = &http.Request{ RemoteAddr: remote, Method: method, diff --git a/internal/net/http/middleware/middleware_builder_test.go b/internal/net/http/middleware/middleware_builder_test.go index 914655a..2c9828c 100644 --- a/internal/net/http/middleware/middleware_builder_test.go +++ b/internal/net/http/middleware/middleware_builder_test.go @@ -16,7 +16,7 @@ func TestBuild(t *testing.T) { errs := E.NewBuilder("") middlewares := BuildMiddlewaresFromYAML("", testMiddlewareCompose, errs) ExpectNoError(t, errs.Error()) - E.Must(json.MarshalIndent(middlewares, "", " ")) + Must(json.MarshalIndent(middlewares, "", " ")) // t.Log(string(data)) // TODO: test } diff --git a/internal/net/http/middleware/test_utils.go b/internal/net/http/middleware/test_utils.go index 35f0873..0adb1a5 100644 --- a/internal/net/http/middleware/test_utils.go +++ b/internal/net/http/middleware/test_utils.go @@ -12,6 +12,7 @@ import ( E "github.com/yusing/go-proxy/internal/error" "github.com/yusing/go-proxy/internal/net/http/reverseproxy" "github.com/yusing/go-proxy/internal/net/types" + . "github.com/yusing/go-proxy/internal/utils/testing" ) //go:embed test_data/sample_headers.json @@ -95,13 +96,13 @@ type testArgs struct { func (args *testArgs) setDefaults() { if args.reqURL == nil { - args.reqURL = E.Must(types.ParseURL("https://example.com")) + args.reqURL = Must(types.ParseURL("https://example.com")) } if args.reqMethod == "" { args.reqMethod = http.MethodGet } if args.upstreamURL == nil { - args.upstreamURL = E.Must(types.ParseURL("https://10.0.0.1:8443")) // dummy url, no actual effect + args.upstreamURL = Must(types.ParseURL("https://10.0.0.1:8443")) // dummy url, no actual effect } if args.respHeaders == nil { args.respHeaders = http.Header{} diff --git a/internal/route/provider/docker_test.go b/internal/route/provider/docker_test.go index 6621c78..aa8a385 100644 --- a/internal/route/provider/docker_test.go +++ b/internal/route/provider/docker_test.go @@ -9,7 +9,6 @@ import ( "github.com/docker/docker/client" "github.com/yusing/go-proxy/internal/common" D "github.com/yusing/go-proxy/internal/docker" - E "github.com/yusing/go-proxy/internal/error" "github.com/yusing/go-proxy/internal/route" T "github.com/yusing/go-proxy/internal/route/types" . "github.com/yusing/go-proxy/internal/utils/testing" @@ -31,7 +30,7 @@ func makeRoutes(cont *types.Container, dockerHostIP ...string) route.Routes { host = client.DefaultDockerHost } p.name = "test" - routes := E.Must(p.routesFromContainerLabels(D.FromDocker(cont, host))) + routes := Must(p.routesFromContainerLabels(D.FromDocker(cont, host))) for _, r := range routes { r.Finalize() } @@ -252,7 +251,7 @@ func TestDisableHealthCheck(t *testing.T) { } r, ok := makeRoutes(c)["a"] ExpectTrue(t, ok) - ExpectEqual(t, r.HealthCheck, nil) + ExpectFalse(t, r.UseHealthCheck()) } func TestPublicIPLocalhost(t *testing.T) { @@ -348,7 +347,7 @@ func TestStreamDefaultValues(t *testing.T) { } func TestExplicitExclude(t *testing.T) { - _, ok := makeRoutes(&types.Container{ + r, ok := makeRoutes(&types.Container{ Names: dummyNames, Labels: map[string]string{ D.LabelAliases: "a", @@ -356,7 +355,8 @@ func TestExplicitExclude(t *testing.T) { "proxy.a.no_tls_verify": "true", }, }, "")["a"] - ExpectFalse(t, ok) + ExpectTrue(t, ok) + ExpectTrue(t, r.ShouldExclude()) } func TestImplicitExcludeDatabase(t *testing.T) {