@@ -18,6 +18,7 @@ package schemaregistry
1818
1919import (
2020 "crypto/tls"
21+ "net/url"
2122 "strings"
2223 "testing"
2324)
@@ -83,3 +84,87 @@ func TestConfigureTLS(t *testing.T) {
8384 t .Errorf ("Should work with valid CA, certificate and key, got %s" , err )
8485 }
8586}
87+
88+ func TestNewAuthHeader (t * testing.T ) {
89+ url , err := url .Parse ("mock://" )
90+ if err != nil {
91+ t .Errorf ("Should work with empty config, got %s" , err )
92+ }
93+
94+ config := & Config {}
95+
96+ config .BearerAuthCredentialsSource = "STATIC_TOKEN"
97+ config .BasicAuthCredentialsSource = "URL"
98+
99+ _ , err = newAuthHeader (url , config )
100+ if err == nil {
101+ t .Errorf ("Should not work with both basic auth source and bearer auth source" )
102+ }
103+
104+ // testing bearer auth
105+ config .BasicAuthCredentialsSource = ""
106+ _ , err = newAuthHeader (url , config )
107+ if err == nil {
108+ t .Errorf ("Should not work if bearer auth token is empty" )
109+ }
110+
111+ config .BearerAuthToken = "token"
112+ config .BearerAuthLogicalCluster = "lsrc-123"
113+ config .BearerAuthIdentityPoolID = "poolID"
114+ headers , err := newAuthHeader (url , config )
115+ if err != nil {
116+ t .Errorf ("Should work with bearer auth token, got %s" , err )
117+ } else {
118+ if val , exists := headers ["Authorization" ]; ! exists || len (val ) == 0 ||
119+ ! strings .EqualFold (val [0 ], "Bearer token" ) {
120+ t .Errorf ("Should have header with key Authorization" )
121+ }
122+ if val , exists := headers [targetIdentityPoolIDKey ]; ! exists || len (val ) == 0 ||
123+ ! strings .EqualFold (val [0 ], "poolID" ) {
124+ t .Errorf ("Should have header with key Confluent-Identity-Pool-Id" )
125+ }
126+ if val , exists := headers [targetSRClusterKey ]; ! exists || len (val ) == 0 ||
127+ ! strings .EqualFold (val [0 ], "lsrc-123" ) {
128+ t .Errorf ("Should have header with key Target-Sr-Cluster" )
129+ }
130+ }
131+
132+ config .BearerAuthCredentialsSource = "other"
133+ _ , err = newAuthHeader (url , config )
134+ if err == nil {
135+ t .Errorf ("Should not work if bearer auth source is invalid" )
136+ }
137+
138+ // testing basic auth
139+ config .BearerAuthCredentialsSource = ""
140+ config .BasicAuthCredentialsSource = "USER_INFO"
141+ config .BasicAuthUserInfo = "username:password"
142+ _ , err = newAuthHeader (url , config )
143+ if err != nil {
144+ t .Errorf ("Should work with basic auth token, got %s" , err )
145+ }
146+
147+ config .BasicAuthCredentialsSource = "URL"
148+ _ , err = newAuthHeader (url , config )
149+ if err != nil {
150+ t .Errorf ("Should work with basic auth token, got %s" , err )
151+ } else if val , exists := headers ["Authorization" ]; ! exists || len (val ) == 0 {
152+ t .Errorf ("Should have header with key Authorization" )
153+ }
154+
155+ config .BasicAuthCredentialsSource = "SASL_INHERIT"
156+ config .SaslUsername = "username"
157+ config .SaslPassword = "password"
158+ _ , err = newAuthHeader (url , config )
159+ if err != nil {
160+ t .Errorf ("Should work with basic auth token, got %s" , err )
161+ } else if val , exists := headers ["Authorization" ]; ! exists || len (val ) == 0 {
162+ t .Errorf ("Should have header with key Authorization" )
163+ }
164+
165+ config .BasicAuthCredentialsSource = "other"
166+ _ , err = newAuthHeader (url , config )
167+ if err == nil {
168+ t .Errorf ("Should not work if basic auth source is invalid" )
169+ }
170+ }
0 commit comments