1+ //go:build e2e
2+
13/*
24Copyright 2023 The Flux CD contributors.
35
@@ -17,49 +19,63 @@ limitations under the License.
1719package gitea
1820
1921import (
22+ "net/url"
23+ "os"
24+ "regexp"
2025 "testing"
2126
2227 "github.com/fluxcd/go-git-providers/gitprovider"
2328)
2429
2530func Test_DomainVariations (t * testing.T ) {
31+
32+ giteaBaseUrl = "http://try.gitea.io"
33+ if giteaBaseUrlVar := os .Getenv ("GITEA_BASE_URL" ); giteaBaseUrlVar != "" {
34+ giteaBaseUrl = giteaBaseUrlVar
35+ }
36+
37+ u , err := url .Parse (giteaBaseUrl )
38+ if err != nil {
39+ t .Fatalf ("failed parsing base URL %q: %s" , giteaBaseUrl , err )
40+ }
41+
2642 tests := []struct {
27- name string
28- opts gitprovider.ClientOption
29- want string
30- expectedErrs [] error
43+ name string
44+ opts gitprovider.ClientOption
45+ want string
46+ expectedErrPattern string
3147 }{
3248 {
33- name : "try.gitea.io domain" ,
34- opts : gitprovider .WithDomain ("try.gitea.io" ),
35- want : "try.gitea.io " ,
49+ name : "custom domain without protocol uses HTTPS by default " ,
50+ opts : gitprovider .WithDomain (u . Host ),
51+ expectedErrPattern : "http: server gave HTTP response to HTTPS client " ,
3652 },
3753 {
38- name : "custom domain without protocol" ,
39- opts : gitprovider .WithDomain ("try.gitea.io" ),
40- want : "try.gitea.io" ,
41- },
42- {
43- name : "custom domain with https protocol" ,
44- opts : gitprovider .WithDomain ("https://try.gitea.io" ),
45- want : "https://try.gitea.io" ,
46- },
47- {
48- name : "custom domain with http protocol" ,
49- opts : gitprovider .WithDomain ("http://try.gitea.io" ),
50- want : "http://try.gitea.io" ,
54+ name : "custom domain with scheme" ,
55+ opts : gitprovider .WithDomain (giteaBaseUrl ),
56+ want : giteaBaseUrl ,
5157 },
5258 }
5359 for _ , tt := range tests {
5460 t .Run (tt .name , func (t * testing.T ) {
5561 c1 , err := NewClient ("token" , tt .opts )
5662 if err != nil {
57- t .Fatal (err )
63+ if tt .expectedErrPattern == "" {
64+ t .Fatalf ("unexpected error: %s" , err )
65+ }
66+ m , mErr := regexp .MatchString (tt .expectedErrPattern , err .Error ())
67+ if mErr != nil {
68+ t .Fatalf ("unexpected error from matching error: %s" , mErr )
69+ }
70+ if ! m {
71+ t .Fatalf ("unexpected error %q; expected %q" , err , tt .expectedErrPattern )
72+ }
73+ return // all assertions passed
74+ } else if tt .expectedErrPattern != "" {
75+ t .Fatalf ("expected error %q but got none" , tt .expectedErrPattern )
5876 }
59- assertEqual (t , tt .want , c1 .SupportedDomain ())
6077
61- c2 , _ := NewClient ("token" , tt .opts )
62- assertEqual (t , tt .want , c2 .SupportedDomain ())
78+ assertEqual (t , tt .want , c1 .SupportedDomain ())
6379 })
6480 }
6581}
0 commit comments