github.com/go-kivik/kivik/v4@v4.3.2/kiviktest/kt/connect.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 2 // use this file except in compliance with the License. You may obtain a copy of 3 // the License at 4 // 5 // http://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 // License for the specific language governing permissions and limitations under 11 // the License. 12 13 package kt 14 15 import ( 16 "net/url" 17 "os" 18 "testing" 19 20 kivik "github.com/go-kivik/kivik/v4" 21 ) 22 23 // DSN3 returns a testing DSN from the environment for CouchDB 3.x. 24 func DSN3(t *testing.T) string { 25 t.Helper() 26 for _, env := range []string{ 27 "KIVIK_TEST_DSN_COUCH33", 28 "KIVIK_TEST_DSN_COUCH32", 29 "KIVIK_TEST_DSN_COUCH31", 30 "KIVIK_TEST_DSN_COUCH30", 31 } { 32 dsn := os.Getenv(env) 33 if dsn != "" { 34 return dsn 35 } 36 } 37 t.Skip("DSN not set") 38 return "" 39 } 40 41 // NoAuthDSN returns a testing DSN with credentials stripped. 42 func NoAuthDSN(t *testing.T) string { 43 t.Helper() 44 dsn := DSN3(t) 45 parsed, err := url.Parse(dsn) 46 if err != nil { 47 t.Fatalf("invalid DSN: %s", err) 48 } 49 parsed.User = nil 50 return parsed.String() 51 } 52 53 func connect(t *testing.T, dsn string) *kivik.Client { 54 t.Helper() 55 client, err := kivik.New("couch", dsn) 56 if err != nil { 57 t.Fatalf("Failed to connect to '%s': %s", dsn, err) 58 } 59 return client 60 } 61 62 // GetClient returns a connection to a CouchDB client, for testing. 63 func GetClient(t *testing.T) *kivik.Client { 64 t.Helper() 65 return connect(t, DSN3(t)) 66 } 67 68 // GetNoAuthClient returns an unauthenticated connection to a CouchDB client, for testing. 69 func GetNoAuthClient(t *testing.T) *kivik.Client { 70 t.Helper() 71 return connect(t, NoAuthDSN(t)) 72 }