github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/internal/testhelpers/secretsapi_test/client.go (about) 1 package secretsapi_test 2 3 import ( 4 "net/http" 5 6 httptransport "github.com/go-openapi/runtime/client" 7 8 secretsapi "github.com/ActiveState/cli/pkg/platform/api/secrets" 9 "github.com/ActiveState/cli/pkg/platform/authentication" 10 ) 11 12 var testTransport http.RoundTripper 13 14 // NewTestClient creates a new secretsapi.Client with a testable Transport. Makes it possible to 15 // use httpmock. 16 func NewTestClient(scheme, host, basePath string, auth *authentication.Auth) *secretsapi.Client { 17 return withTestableTransport(secretsapi.NewClient(scheme, host, basePath, auth)) 18 } 19 20 // NewDefaultTestClient creates a testable secrets client using environment defaults for schema, host, and path. 21 func NewDefaultTestClient(bearerToken string, auth *authentication.Auth) *secretsapi.Client { 22 return withTestableTransport(secretsapi.NewDefaultClient(auth)) 23 } 24 25 // InitializeTestClient initializes a testable secrets client using environment defaults for schema, 26 // host, and path. While this function departs from the secretsapi.InitializeClient signature, it's 27 // more useful for testing. 28 func InitializeTestClient(bearerToken string, auth *authentication.Auth) *secretsapi.Client { 29 secretsapi.DefaultClient = NewDefaultTestClient(bearerToken, auth) 30 return secretsapi.DefaultClient 31 } 32 33 func withTestableTransport(client *secretsapi.Client) *secretsapi.Client { 34 rt := client.Transport.(*httptransport.Runtime) 35 rt.Transport = testTransport 36 return client 37 }