diff --git a/gitea/client_test.go b/gitea/client_test.go index 12cb8d7..ddd4285 100644 --- a/gitea/client_test.go +++ b/gitea/client_test.go @@ -1,3 +1,5 @@ +//go:build e2e + /* Copyright 2023 The Flux CD contributors. @@ -17,49 +19,63 @@ limitations under the License. package gitea import ( + "net/url" + "os" + "regexp" "testing" "github.com/fluxcd/go-git-providers/gitprovider" ) func Test_DomainVariations(t *testing.T) { + + giteaBaseUrl = "http://try.gitea.io" + if giteaBaseUrlVar := os.Getenv("GITEA_BASE_URL"); giteaBaseUrlVar != "" { + giteaBaseUrl = giteaBaseUrlVar + } + + u, err := url.Parse(giteaBaseUrl) + if err != nil { + t.Fatalf("failed parsing base URL %q: %s", giteaBaseUrl, err) + } + tests := []struct { - name string - opts gitprovider.ClientOption - want string - expectedErrs []error + name string + opts gitprovider.ClientOption + want string + expectedErrPattern string }{ { - name: "try.gitea.io domain", - opts: gitprovider.WithDomain("try.gitea.io"), - want: "try.gitea.io", + name: "custom domain without protocol uses HTTPS by default", + opts: gitprovider.WithDomain(u.Host), + expectedErrPattern: "http: server gave HTTP response to HTTPS client", }, { - name: "custom domain without protocol", - opts: gitprovider.WithDomain("try.gitea.io"), - want: "try.gitea.io", - }, - { - name: "custom domain with https protocol", - opts: gitprovider.WithDomain("https://try.gitea.io"), - want: "https://try.gitea.io", - }, - { - name: "custom domain with http protocol", - opts: gitprovider.WithDomain("http://try.gitea.io"), - want: "http://try.gitea.io", + name: "custom domain with scheme", + opts: gitprovider.WithDomain(giteaBaseUrl), + want: giteaBaseUrl, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { c1, err := NewClient("token", tt.opts) if err != nil { - t.Fatal(err) + if tt.expectedErrPattern == "" { + t.Fatalf("unexpected error: %s", err) + } + m, mErr := regexp.MatchString(tt.expectedErrPattern, err.Error()) + if mErr != nil { + t.Fatalf("unexpected error from matching error: %s", mErr) + } + if !m { + t.Fatalf("unexpected error %q; expected %q", err, tt.expectedErrPattern) + } + return // all assertions passed + } else if tt.expectedErrPattern != "" { + t.Fatalf("expected error %q but got none", tt.expectedErrPattern) } - assertEqual(t, tt.want, c1.SupportedDomain()) - c2, _ := NewClient("token", tt.opts) - assertEqual(t, tt.want, c2.SupportedDomain()) + assertEqual(t, tt.want, c1.SupportedDomain()) }) } }